ImportKeys: Use data binding to set CardView's content

This commit is contained in:
Andrea Torlaschi 2016-08-04 11:20:37 +02:00
parent 9bf06e216b
commit 0e66999ecf
4 changed files with 86 additions and 39 deletions

View file

@ -17,16 +17,12 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.res.Resources;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
@ -44,8 +40,6 @@ import org.sufficientlysecure.keychain.operations.results.ImportKeyResult;
import org.sufficientlysecure.keychain.pgp.CanonicalizedKeyRing;
import org.sufficientlysecure.keychain.service.ImportKeyringParcel;
import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper;
import org.sufficientlysecure.keychain.ui.util.FormattingUtils;
import org.sufficientlysecure.keychain.ui.util.Highlighter;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils.State;
import org.sufficientlysecure.keychain.ui.util.Notify;
@ -120,13 +114,7 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
public ViewHolder(View view) {
super(view);
b = DataBindingUtil.bind(view);
b.setNonInteractive(mNonInteractive);
Resources resources = mActivity.getResources();
b.setStandardColor(FormattingUtils.getColorFromAttr(mActivity, R.attr.colorText));
b.setRevokedExpiredColor(resources.getColor(R.color.key_flag_gray));
b.setSecretColor(Color.RED);
}
}
@ -140,10 +128,6 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ImportKeysListItemBinding b = holder.b;
final ImportKeysListEntry entry = mData.get(position);
Highlighter highlighter = new Highlighter(mActivity, entry.getQuery());
b.setHighlighter(highlighter);
b.setEntry(entry);
if (entry.isRevoked()) {
@ -198,11 +182,6 @@ public class ImportKeysAdapter extends RecyclerView.Adapter<ImportKeysAdapter.Vi
b.extraContainer.setVisibility(showed ? View.VISIBLE : View.GONE);
}
@BindingAdapter("app:keyId")
public static void setKeyId(TextView textView, String keyId) {
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
}
@Override
public int getItemCount() {
return mData != null ? mData.size() : 0;

View file

@ -0,0 +1,72 @@
package org.sufficientlysecure.keychain.ui.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.databinding.BindingAdapter;
import android.graphics.Color;
import android.widget.TextView;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.ui.util.Highlighter;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
public class ImportKeysAdapterBinding {
@BindingAdapter({"app:userId", "app:secret", "app:revokedOrExpired", "app:query"})
public static void setUserId(TextView textView, CharSequence userId, boolean secret,
boolean revokedOrExpired, String query) {
Context context = textView.getContext();
Resources resources = context.getResources();
if (userId == null)
userId = resources.getString(R.string.user_id_no_name);
if (secret) {
userId = resources.getString(R.string.secret_key) + " " + userId;
} else {
Highlighter highlighter = new Highlighter(context, query);
userId = highlighter.highlight(userId);
}
textView.setText(userId);
if (revokedOrExpired) {
textView.setTextColor(resources.getColor(R.color.key_flag_gray));
} else if (secret) {
textView.setTextColor(Color.RED);
}
}
@BindingAdapter({"app:userEmail", "app:revokedOrExpired", "app:query"})
public static void setUserEmail(TextView textView, CharSequence userEmail,
boolean revokedOrExpired, String query) {
Context context = textView.getContext();
if (userEmail == null)
userEmail = "";
Highlighter highlighter = new Highlighter(context, query);
textView.setText(highlighter.highlight(userEmail));
if (revokedOrExpired) {
Resources resources = context.getResources();
textView.setTextColor(resources.getColor(R.color.key_flag_gray));
}
}
@BindingAdapter({"app:keyId", "app:revokedOrExpired"})
public static void setKeyId(TextView textView, String keyId, boolean revokedOrExpired) {
Context context = textView.getContext();
if (keyId == null)
keyId = "";
if (revokedOrExpired) {
Resources resources = context.getResources();
textView.setTextColor(resources.getColor(R.color.key_flag_gray));
}
textView.setText(KeyFormattingUtils.beautifyKeyIdWithPrefix(keyId));
}
}

View file

@ -18,10 +18,7 @@ public class Highlighter {
mQuery = query;
}
public Spannable highlight(String text) {
if (text == null)
return null;
public Spannable highlight(CharSequence text) {
Spannable highlight = Spannable.Factory.getInstance().newSpannable(text);
if (mQuery == null) {

View file

@ -3,17 +3,10 @@
<data>
<import type="android.view.View" />
<import type="org.sufficientlysecure.keychain.ui.util.Highlighter" />
<import type="org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry" />
<variable name="nonInteractive" type="boolean" />
<variable name="standardColor" type="int" />
<variable name="revokedExpiredColor" type="int" />
<variable name="secretColor" type="int" />
<variable name="highlighter" type="Highlighter" />
<variable name="entry" type="ImportKeysListEntry" />
</data>
@ -59,24 +52,30 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='@{entry.secretKey ? @string/secret_key + " " + (entry.primaryUserId.name ?? @string/user_id_no_name) : highlighter.highlight(entry.primaryUserId.name ?? @string/user_id_no_name)}'
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@{entry.revokedOrExpired ? revokedExpiredColor : (entry.secretKey ? secretColor : standardColor)}" />
android:textColor="?attr/colorText"
app:query="@{entry.query}"
app:revokedOrExpired="@{entry.revokedOrExpired}"
app:secret="@{entry.secretKey}"
app:userId="@{entry.primaryUserId.name}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@{highlighter.highlight(entry.primaryUserId.email)}"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@{entry.revokedOrExpired ? revokedExpiredColor : standardColor}" />
android:textColor="?attr/colorText"
app:query="@{entry.query}"
app:revokedOrExpired="@{entry.revokedOrExpired}"
app:userEmail="@{entry.primaryUserId.email}" />
<TextView
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@{entry.revokedOrExpired ? revokedExpiredColor : standardColor}"
app:keyId='@{entry.keyIdHex ?? ""}' />
android:textColor="?attr/colorText"
app:keyId="@{entry.keyIdHex}"
app:revokedOrExpired="@{entry.revokedOrExpired}" />
</LinearLayout>