Эх сурвалжийг харах

Merge pull request #7639 from nextcloud/notificationItemViewBinding

Android view binding for notification-adapter's view holder
Andy Scherzinger 4 жил өмнө
parent
commit
e930ad31e2

+ 1 - 1
scripts/analysis/lint-results.txt

@@ -1,2 +1,2 @@
 DO NOT TOUCH; GENERATED BY DRONE
-      <span class="mdl-layout-title">Lint Report: 249 warnings</span>
+      <span class="mdl-layout-title">Lint Report: 243 warnings</span>

+ 52 - 60
src/main/java/com/owncloud/android/ui/adapter/NotificationListAdapter.java

@@ -34,11 +34,9 @@ import android.text.style.ForegroundColorSpan;
 import android.text.style.StyleSpan;
 import android.view.Gravity;
 import android.view.LayoutInflater;
-import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
-import android.widget.TextView;
 
 import com.bumptech.glide.GenericRequestBuilder;
 import com.bumptech.glide.Glide;
@@ -48,6 +46,7 @@ import com.bumptech.glide.load.resource.file.FileToStreamDecoder;
 import com.caverock.androidsvg.SVG;
 import com.google.android.material.button.MaterialButton;
 import com.owncloud.android.R;
+import com.owncloud.android.databinding.NotificationListItemBinding;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.resources.notifications.models.Action;
 import com.owncloud.android.lib.resources.notifications.models.Notification;
@@ -68,8 +67,6 @@ import java.util.List;
 
 import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.RecyclerView;
-import butterknife.BindView;
-import butterknife.ButterKnife;
 
 /**
  * This Adapter populates a RecyclerView with all notifications for an account within the app.
@@ -101,32 +98,34 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
     @NonNull
     @Override
     public NotificationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
-        View v = LayoutInflater.from(notificationsActivity).inflate(R.layout.notification_list_item, parent, false);
-        return new NotificationViewHolder(v);
+        return new NotificationViewHolder(
+            NotificationListItemBinding.inflate(LayoutInflater.from(notificationsActivity))
+        );
     }
 
     @Override
     public void onBindViewHolder(@NonNull NotificationViewHolder holder, int position) {
         Notification notification = notificationsList.get(position);
-        holder.dateTime.setText(DisplayUtils.getRelativeTimestamp(notificationsActivity,
-                notification.getDatetime().getTime()));
+        holder.binding.datetime.setText(DisplayUtils.getRelativeTimestamp(notificationsActivity,
+                                                                          notification.getDatetime().getTime()));
 
         RichObject file = notification.subjectRichParameters.get(FILE);
         String subject = notification.getSubject();
         if (file == null && !TextUtils.isEmpty(notification.getLink())) {
             subject = subject + " ↗";
-            holder.subject.setTypeface(holder.subject.getTypeface(), Typeface.BOLD);
-            holder.subject.setOnClickListener(v -> openLink(notification.getLink()));
-            holder.subject.setText(subject);
+            holder.binding.subject.setTypeface(holder.binding.subject.getTypeface(),
+                                               Typeface.BOLD);
+            holder.binding.subject.setOnClickListener(v -> openLink(notification.getLink()));
+            holder.binding.subject.setText(subject);
         } else {
             if (!TextUtils.isEmpty(notification.subjectRich)) {
-                holder.subject.setText(makeSpecialPartsBold(notification));
+                holder.binding.subject.setText(makeSpecialPartsBold(notification));
             } else {
-                holder.subject.setText(subject);
+                holder.binding.subject.setText(subject);
             }
 
             if (file != null && !TextUtils.isEmpty(file.id)) {
-                holder.subject.setOnClickListener(v -> {
+                holder.binding.subject.setOnClickListener(v -> {
                     Intent intent = new Intent(notificationsActivity, FileDisplayActivity.class);
                     intent.setAction(Intent.ACTION_VIEW);
                     intent.putExtra(FileDisplayActivity.KEY_FILE_ID, file.id);
@@ -136,28 +135,32 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
             }
         }
 
-        holder.message.setText(notification.getMessage());
+        holder.binding.message.setText(notification.getMessage());
 
         if (!TextUtils.isEmpty(notification.getIcon())) {
-            downloadIcon(notification.getIcon(), holder.icon);
+            downloadIcon(notification.getIcon(), holder.binding.icon);
         }
 
-        int nightModeFlag = notificationsActivity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
-            if (Configuration.UI_MODE_NIGHT_YES == nightModeFlag) {
-                holder.icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
-            } else {
-                holder.icon.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
-            }
+        int nightModeFlag =
+            notificationsActivity.getResources().getConfiguration().uiMode
+            & Configuration.UI_MODE_NIGHT_MASK;
+        if (Configuration.UI_MODE_NIGHT_YES == nightModeFlag) {
+            holder.binding.icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
+        } else {
+            holder.binding.icon.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
+        }
 
         setButtons(holder, notification);
 
-        holder.dismiss.setOnClickListener(v -> new DeleteNotificationTask(client, notification, holder,
-                                                                          notificationsActivity).execute());
+        holder.binding.dismiss.setOnClickListener(v -> new DeleteNotificationTask(client,
+                                                                                  notification,
+                                                                                  holder,
+                                                                                  notificationsActivity).execute());
     }
 
     public void setButtons(NotificationViewHolder holder, Notification notification) {
         // add action buttons
-        holder.buttons.removeAllViews();
+        holder.binding.buttons.removeAllViews();
         MaterialButton button;
 
         Resources resources = notificationsActivity.getResources();
@@ -168,7 +171,7 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
             0,
             resources.getDimensionPixelOffset(R.dimen.standard_half_margin),
             0
-        );
+                         );
 
         for (Action action : notification.getActions()) {
             button = new MaterialButton(notificationsActivity);
@@ -207,7 +210,7 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
                 }
             });
 
-            holder.buttons.addView(button);
+            holder.binding.buttons.addView(button);
         }
     }
 
@@ -231,7 +234,7 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
 
                 ssb.setSpan(styleSpanBold, openingBrace, closingBrace, 0);
                 ssb.setSpan(foregroundColorSpanBlack, openingBrace, closingBrace,
-                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
             }
             openingBrace = text.indexOf('{', closingBrace);
         }
@@ -256,31 +259,31 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
 
 
     public void setButtonEnabled(NotificationViewHolder holder, boolean enabled) {
-        for (int i = 0; i < holder.buttons.getChildCount(); i++) {
-            holder.buttons.getChildAt(i).setEnabled(enabled);
+        for (int i = 0; i < holder.binding.buttons.getChildCount(); i++) {
+            holder.binding.buttons.getChildAt(i).setEnabled(enabled);
         }
     }
 
     private void downloadIcon(String icon, ImageView itemViewType) {
         GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> requestBuilder = Glide.with(notificationsActivity)
-                .using(Glide.buildStreamModelLoader(Uri.class, notificationsActivity), InputStream.class)
-                .from(Uri.class)
-                .as(SVG.class)
-                .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
-                .sourceEncoder(new StreamEncoder())
-                .cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder()))
-                .decoder(new SvgDecoder())
-                .placeholder(R.drawable.ic_notification)
-                .error(R.drawable.ic_notification)
-                .animate(android.R.anim.fade_in)
-                .listener(new SvgSoftwareLayerSetter<>());
+            .using(Glide.buildStreamModelLoader(Uri.class, notificationsActivity), InputStream.class)
+            .from(Uri.class)
+            .as(SVG.class)
+            .transcode(new SvgDrawableTranscoder(), PictureDrawable.class)
+            .sourceEncoder(new StreamEncoder())
+            .cacheDecoder(new FileToStreamDecoder<>(new SvgDecoder()))
+            .decoder(new SvgDecoder())
+            .placeholder(R.drawable.ic_notification)
+            .error(R.drawable.ic_notification)
+            .animate(android.R.anim.fade_in)
+            .listener(new SvgSoftwareLayerSetter<>());
 
 
         Uri uri = Uri.parse(icon);
         requestBuilder
-                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
-                .load(uri)
-                .into(itemViewType);
+            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
+            .load(uri)
+            .into(itemViewType);
     }
 
     private void openLink(String link) {
@@ -295,22 +298,11 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
     }
 
     public static class NotificationViewHolder extends RecyclerView.ViewHolder {
-        @BindView(R.id.notification_icon)
-        public ImageView icon;
-        @BindView(R.id.notification_subject)
-        public TextView subject;
-        @BindView(R.id.notification_message)
-        public TextView message;
-        @BindView(R.id.notification_datetime)
-        public TextView dateTime;
-        @BindView(R.id.notification_buttons)
-        public LinearLayout buttons;
-        @BindView(R.id.notification_dismiss)
-        public ImageView dismiss;
-
-        private NotificationViewHolder(View itemView) {
-            super(itemView);
-            ButterKnife.bind(this, itemView);
+        NotificationListItemBinding binding;
+
+        private NotificationViewHolder(NotificationListItemBinding binding) {
+            super(binding.getRoot());
+            this.binding = binding;
         }
     }
 }

+ 8 - 8
src/main/res/layout/notification_list_item.xml

@@ -31,7 +31,7 @@
     tools:ignore="UseCompoundDrawables">
 
     <ImageView
-        android:id="@+id/notification_icon"
+        android:id="@+id/icon"
         android:layout_width="@dimen/notification_icon_width"
         android:layout_height="@dimen/notification_icon_height"
         android:layout_alignParentTop="true"
@@ -45,8 +45,8 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical"
-        android:layout_alignTop="@id/notification_icon"
-        android:layout_toEndOf="@id/notification_icon">
+        android:layout_alignTop="@id/icon"
+        android:layout_toEndOf="@id/icon">
 
         <LinearLayout
             android:layout_width="match_parent"
@@ -54,7 +54,7 @@
             android:orientation="horizontal">
 
             <TextView
-                android:id="@+id/notification_subject"
+                android:id="@+id/subject"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
@@ -63,7 +63,7 @@
                 tools:text="@string/placeholder_filename" />
 
             <ImageView
-                android:id="@+id/notification_dismiss"
+                android:id="@+id/dismiss"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:src="@drawable/ic_close"
@@ -71,7 +71,7 @@
         </LinearLayout>
 
         <TextView
-            android:id="@+id/notification_message"
+            android:id="@+id/message"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:ellipsize="end"
@@ -80,7 +80,7 @@
             android:textAppearance="?android:attr/textAppearanceListItem"/>
 
         <LinearLayout
-            android:id="@+id/notification_buttons"
+            android:id="@+id/buttons"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="@dimen/alternate_half_margin"
@@ -89,7 +89,7 @@
             android:orientation="horizontal"/>
 
         <TextView
-            android:id="@+id/notification_datetime"
+            android:id="@+id/datetime"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="end"