瀏覽代碼

refactor and optimize binder code

AndyScherzinger 7 年之前
父節點
當前提交
9b1520c0f2
共有 1 個文件被更改,包括 46 次插入43 次删除
  1. 46 43
      src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactListFragment.java

+ 46 - 43
src/main/java/com/owncloud/android/ui/fragment/contactsbackup/ContactListFragment.java

@@ -613,65 +613,27 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
     }
 
     @Override
-    public ContactListFragment.ContactItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+    @NonNull
+    public ContactListFragment.ContactItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View view = LayoutInflater.from(context).inflate(R.layout.contactlist_list_item, parent, false);
 
         return new ContactListFragment.ContactItemViewHolder(view);
     }
 
     @Override
-    public void onBindViewHolder(final ContactListFragment.ContactItemViewHolder holder, final int position) {
+    public void onBindViewHolder(@NonNull final ContactListFragment.ContactItemViewHolder holder, final int position) {
         final int verifiedPosition = holder.getAdapterPosition();
         final VCard vcard = vCards.get(verifiedPosition);
 
         if (vcard != null) {
 
-            if (checkedVCards.contains(position)) {
-                holder.getName().setChecked(true);
-
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-                    holder.getName().getCheckMarkDrawable()
-                            .setColorFilter(ThemeUtils.primaryAccentColor(context), PorterDuff.Mode.SRC_ATOP);
-                }
-            } else {
-                holder.getName().setChecked(false);
-
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
-                    holder.getName().getCheckMarkDrawable().clearColorFilter();
-                }
-            }
+            setChecked(checkedVCards.contains(position), holder.getName());
 
             holder.getName().setText(getDisplayName(vcard));
 
             // photo
             if (vcard.getPhotos().size() > 0) {
-                Photo firstPhoto = vcard.getPhotos().get(0);
-                String url = firstPhoto.getUrl();
-                byte[] data = firstPhoto.getData();
-
-                if (data != null && data.length > 0) {
-                    Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
-                    RoundedBitmapDrawable drawable = BitmapUtils.bitmapToCircularBitmapDrawable(context.getResources(),
-                            thumbnail);
-
-                    holder.getBadge().setImageDrawable(drawable);
-                } else if (url != null) {
-                    ImageView badge = holder.getBadge();
-                    SimpleTarget target = new SimpleTarget<Drawable>() {
-                        @Override
-                        public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
-                            holder.getBadge().setImageDrawable(resource);
-                        }
-
-                        @Override
-                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
-                            super.onLoadFailed(e, errorDrawable);
-                            holder.getBadge().setImageDrawable(errorDrawable);
-                        }
-                    };
-                    DisplayUtils.downloadIcon(context, url, target, R.drawable.ic_user,
-                            badge.getWidth(), badge.getHeight());
-                }
+                setPhoto(holder.getBadge(), vcard.getPhotos().get(0));
             } else {
                 try {
                     holder.getBadge().setImageDrawable(
@@ -689,6 +651,47 @@ class ContactListAdapter extends RecyclerView.Adapter<ContactListFragment.Contac
         }
     }
 
+    private void setPhoto(ImageView imageView, Photo firstPhoto) {
+        String url = firstPhoto.getUrl();
+        byte[] data = firstPhoto.getData();
+
+        if (data != null && data.length > 0) {
+            Bitmap thumbnail = BitmapFactory.decodeByteArray(data, 0, data.length);
+            RoundedBitmapDrawable drawable = BitmapUtils.bitmapToCircularBitmapDrawable(context.getResources(),
+                    thumbnail);
+
+            imageView.setImageDrawable(drawable);
+        } else if (url != null) {
+            SimpleTarget target = new SimpleTarget<Drawable>() {
+                @Override
+                public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
+                    imageView.setImageDrawable(resource);
+                }
+
+                @Override
+                public void onLoadFailed(Exception e, Drawable errorDrawable) {
+                    super.onLoadFailed(e, errorDrawable);
+                    imageView.setImageDrawable(errorDrawable);
+                }
+            };
+            DisplayUtils.downloadIcon(context, url, target, R.drawable.ic_user, imageView.getWidth(),
+                    imageView.getHeight());
+        }
+    }
+
+    private void setChecked(boolean checked, CheckedTextView checkedTextView) {
+        checkedTextView.setChecked(checked);
+
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+            if (checked) {
+                checkedTextView.getCheckMarkDrawable()
+                        .setColorFilter(ThemeUtils.primaryAccentColor(context), PorterDuff.Mode.SRC_ATOP);
+            } else {
+                checkedTextView.getCheckMarkDrawable().clearColorFilter();
+            }
+        }
+    }
+
     private void toggleVCard(ContactListFragment.ContactItemViewHolder holder, int verifiedPosition) {
         holder.getName().setChecked(!holder.getName().isChecked());