Prevent query SharedPreference in adapters.
This commit is contained in:
		
					parent
					
						
							
								6d1ec78984
							
						
					
				
			
			
				commit
				
					
						219eafe6fc
					
				
			
		
					 11 changed files with 60 additions and 27 deletions
				
			
		|  | @ -16,12 +16,10 @@ | |||
| package com.keylesspalace.tusky.adapter; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.graphics.Color; | ||||
| import android.graphics.PorterDuff; | ||||
| import android.graphics.Typeface; | ||||
| import android.graphics.drawable.Drawable; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.support.v4.content.ContextCompat; | ||||
|  | @ -70,6 +68,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|     private NotificationActionListener notificationActionListener; | ||||
|     private FooterViewHolder.State footerState; | ||||
|     private boolean mediaPreviewEnabled; | ||||
|     private boolean useAbsoluteTime; | ||||
|     private BidiFormatter bidiFormatter; | ||||
| 
 | ||||
|     public NotificationsAdapter(StatusActionListener statusListener, | ||||
|  | @ -80,6 +79,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|         this.notificationActionListener = notificationActionListener; | ||||
|         footerState = FooterViewHolder.State.END; | ||||
|         mediaPreviewEnabled = true; | ||||
|         useAbsoluteTime = false; | ||||
|         bidiFormatter = BidiFormatter.getInstance(); | ||||
|     } | ||||
| 
 | ||||
|  | @ -91,7 +91,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|             case VIEW_TYPE_MENTION: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|                         .inflate(R.layout.item_status, parent, false); | ||||
|                 return new StatusViewHolder(view); | ||||
|                 return new StatusViewHolder(view, useAbsoluteTime); | ||||
|             } | ||||
|             case VIEW_TYPE_FOOTER: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|  | @ -101,7 +101,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|             case VIEW_TYPE_STATUS_NOTIFICATION: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|                         .inflate(R.layout.item_status_notification, parent, false); | ||||
|                 return new StatusNotificationViewHolder(view); | ||||
|                 return new StatusNotificationViewHolder(view, useAbsoluteTime); | ||||
|             } | ||||
|             case VIEW_TYPE_FOLLOW: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|  | @ -142,7 +142,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|                     StatusNotificationViewHolder holder = (StatusNotificationViewHolder) viewHolder; | ||||
|                     StatusViewData.Concrete statusViewData = concreteNotificaton.getStatusViewData(); | ||||
| 
 | ||||
|                     if(statusViewData == null) { | ||||
|                     if (statusViewData == null) { | ||||
|                         holder.showNotificationContent(false); | ||||
|                     } else { | ||||
|                         holder.showNotificationContent(true); | ||||
|  | @ -245,6 +245,10 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|         mediaPreviewEnabled = enabled; | ||||
|     } | ||||
| 
 | ||||
|     public void setUseAbsoluteTime(boolean useAbsoluteTime) { | ||||
|         this.useAbsoluteTime = useAbsoluteTime; | ||||
|     } | ||||
| 
 | ||||
|     public interface NotificationActionListener { | ||||
|         void onViewAccount(String id); | ||||
| 
 | ||||
|  | @ -323,7 +327,9 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|         private NotificationActionListener notificationActionListener; | ||||
|         private StatusViewData.Concrete statusViewData; | ||||
| 
 | ||||
|         StatusNotificationViewHolder(View itemView) { | ||||
|         private boolean useAbsoluteTime; | ||||
| 
 | ||||
|         StatusNotificationViewHolder(View itemView, boolean useAbsoluteTime) { | ||||
|             super(itemView); | ||||
|             message = itemView.findViewById(R.id.notification_top_text); | ||||
|             statusNameBar = itemView.findViewById(R.id.status_name_bar); | ||||
|  | @ -345,6 +351,8 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|             message.setOnClickListener(this); | ||||
|             statusContent.setOnClickListener(this); | ||||
|             contentWarningButton.setOnCheckedChangeListener(this); | ||||
| 
 | ||||
|             this.useAbsoluteTime = useAbsoluteTime; | ||||
|         } | ||||
| 
 | ||||
|         private void showNotificationContent(boolean show) { | ||||
|  | @ -370,8 +378,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|         } | ||||
| 
 | ||||
|         protected void setCreatedAt(@Nullable Date createdAt) { | ||||
|             SharedPreferences defPrefs = PreferenceManager.getDefaultSharedPreferences(timestampInfo.getContext()); | ||||
|             if (defPrefs.getBoolean("absoluteTimeView", true)) { | ||||
|             if (useAbsoluteTime) { | ||||
|                 String time = "ERROR!"; | ||||
|                 if (createdAt != null) { | ||||
|                     SimpleDateFormat sdf; | ||||
|  | @ -429,7 +436,7 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|                 } | ||||
|                 case REBLOG: { | ||||
|                     icon = ContextCompat.getDrawable(context, R.drawable.ic_repeat_24dp); | ||||
|                     if(icon != null) { | ||||
|                     if (icon != null) { | ||||
|                         icon.setColorFilter(ContextCompat.getColor(context, | ||||
|                                 R.color.color_accent_dark), PorterDuff.Mode.SRC_ATOP); | ||||
|                     } | ||||
|  | @ -490,10 +497,12 @@ public class NotificationsAdapter extends RecyclerView.Adapter { | |||
|             switch (v.getId()) { | ||||
|                 case R.id.notification_container: | ||||
|                 case R.id.notification_content: | ||||
|                     if (notificationActionListener != null) notificationActionListener.onViewStatusForNotificationId(notificationId); | ||||
|                     if (notificationActionListener != null) | ||||
|                         notificationActionListener.onViewStatusForNotificationId(notificationId); | ||||
|                     break; | ||||
|                 case R.id.notification_top_text: | ||||
|                     if (notificationActionListener != null) notificationActionListener.onViewAccount(accountId); | ||||
|                     if (notificationActionListener != null) | ||||
|                         notificationActionListener.onViewAccount(accountId); | ||||
|                     break; | ||||
|             } | ||||
|         } | ||||
|  |  | |||
|  | @ -48,12 +48,14 @@ public class SearchResultsAdapter extends RecyclerView.Adapter { | |||
| 
 | ||||
|     private boolean mediaPreviewsEnabled; | ||||
|     private boolean alwaysShowSensitiveMedia; | ||||
|     private boolean useAbsoluteTime; | ||||
| 
 | ||||
|     private LinkListener linkListener; | ||||
|     private StatusActionListener statusListener; | ||||
| 
 | ||||
|     public SearchResultsAdapter(boolean mediaPreviewsEnabled, boolean alwaysShowSensitiveMedia, | ||||
|                                 LinkListener linkListener, StatusActionListener statusListener) { | ||||
|                                 LinkListener linkListener, StatusActionListener statusListener, | ||||
|                                 boolean useAbsoluteTime) { | ||||
| 
 | ||||
|         this.accountList = Collections.emptyList(); | ||||
|         this.statusList = Collections.emptyList(); | ||||
|  | @ -62,6 +64,7 @@ public class SearchResultsAdapter extends RecyclerView.Adapter { | |||
| 
 | ||||
|         this.mediaPreviewsEnabled = mediaPreviewsEnabled; | ||||
|         this.alwaysShowSensitiveMedia = alwaysShowSensitiveMedia; | ||||
|         this.useAbsoluteTime = useAbsoluteTime; | ||||
| 
 | ||||
|         this.linkListener = linkListener; | ||||
|         this.statusListener = statusListener; | ||||
|  | @ -86,7 +89,7 @@ public class SearchResultsAdapter extends RecyclerView.Adapter { | |||
|             case VIEW_TYPE_STATUS: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|                         .inflate(R.layout.item_status, parent, false); | ||||
|                 return new StatusViewHolder(view); | ||||
|                 return new StatusViewHolder(view, useAbsoluteTime); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -1,9 +1,7 @@ | |||
| package com.keylesspalace.tusky.adapter; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.content.SharedPreferences; | ||||
| import android.graphics.drawable.Drawable; | ||||
| import android.preference.PreferenceManager; | ||||
| import android.support.annotation.DrawableRes; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
|  | @ -68,7 +66,9 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { | |||
|     TextView content; | ||||
|     TextView contentWarningDescription; | ||||
| 
 | ||||
|     StatusBaseViewHolder(View itemView) { | ||||
|     private boolean useAbsoluteTime; | ||||
| 
 | ||||
|     StatusBaseViewHolder(View itemView, boolean useAbsoluteTime) { | ||||
|         super(itemView); | ||||
|         container = itemView.findViewById(R.id.status_container); | ||||
|         displayName = itemView.findViewById(R.id.status_display_name); | ||||
|  | @ -95,6 +95,8 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { | |||
|         mediaLabel = itemView.findViewById(R.id.status_media_label); | ||||
|         contentWarningDescription = itemView.findViewById(R.id.status_content_warning_description); | ||||
|         contentWarningButton = itemView.findViewById(R.id.status_content_warning_button); | ||||
| 
 | ||||
|         this.useAbsoluteTime = useAbsoluteTime; | ||||
|     } | ||||
| 
 | ||||
|     protected abstract int getMediaPreviewHeight(Context context); | ||||
|  | @ -130,8 +132,7 @@ abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder { | |||
|     } | ||||
| 
 | ||||
|     protected void setCreatedAt(@Nullable Date createdAt) { | ||||
|         SharedPreferences defPrefs = PreferenceManager.getDefaultSharedPreferences(timestampInfo.getContext()); | ||||
|         if (defPrefs.getBoolean("absoluteTimeView", true)) { | ||||
|         if (useAbsoluteTime) { | ||||
|             String time = "ERROR!"; | ||||
|             if (createdAt != null) { | ||||
|                 SimpleDateFormat sdf; | ||||
|  |  | |||
|  | @ -41,7 +41,7 @@ class StatusDetailedViewHolder extends StatusBaseViewHolder { | |||
|     private TextView cardUrl; | ||||
| 
 | ||||
|     StatusDetailedViewHolder(View view) { | ||||
|         super(view); | ||||
|         super(view, false); | ||||
|         reblogs = view.findViewById(R.id.status_reblogs); | ||||
|         favourites = view.findViewById(R.id.status_favourites); | ||||
|         cardView = view.findViewById(R.id.card_view); | ||||
|  |  | |||
|  | @ -34,8 +34,8 @@ public class StatusViewHolder extends StatusBaseViewHolder { | |||
|     private ImageView avatarReblog; | ||||
|     private TextView rebloggedBar; | ||||
| 
 | ||||
|     StatusViewHolder(View itemView) { | ||||
|         super(itemView); | ||||
|     StatusViewHolder(View itemView, boolean useAbsoluteTime) { | ||||
|         super(itemView, useAbsoluteTime); | ||||
|         avatarReblog = itemView.findViewById(R.id.status_avatar_reblog); | ||||
|         rebloggedBar = itemView.findViewById(R.id.status_reblogged); | ||||
|         //workaround because Android < API 21 does not support setting drawableLeft from xml when it is a vector image | ||||
|  |  | |||
|  | @ -36,12 +36,14 @@ public class ThreadAdapter extends RecyclerView.Adapter { | |||
|     private List<StatusViewData.Concrete> statuses; | ||||
|     private StatusActionListener statusActionListener; | ||||
|     private boolean mediaPreviewEnabled; | ||||
|     private boolean useAbsoluteTime; | ||||
|     private int detailedStatusPosition; | ||||
| 
 | ||||
|     public ThreadAdapter(StatusActionListener listener) { | ||||
|         this.statusActionListener = listener; | ||||
|         this.statuses = new ArrayList<>(); | ||||
|         mediaPreviewEnabled = true; | ||||
|         useAbsoluteTime = false; | ||||
|         detailedStatusPosition = RecyclerView.NO_POSITION; | ||||
|     } | ||||
| 
 | ||||
|  | @ -53,7 +55,7 @@ public class ThreadAdapter extends RecyclerView.Adapter { | |||
|             case VIEW_TYPE_STATUS: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|                         .inflate(R.layout.item_status, parent, false); | ||||
|                 return new StatusViewHolder(view); | ||||
|                 return new StatusViewHolder(view, useAbsoluteTime); | ||||
|             } | ||||
|             case VIEW_TYPE_STATUS_DETAILED: { | ||||
|                 View view = LayoutInflater.from(parent.getContext()) | ||||
|  | @ -149,6 +151,10 @@ public class ThreadAdapter extends RecyclerView.Adapter { | |||
|         mediaPreviewEnabled = enabled; | ||||
|     } | ||||
| 
 | ||||
|     public void setUseAbsoluteTime(boolean useAbsoluteTime) { | ||||
|         this.useAbsoluteTime = useAbsoluteTime; | ||||
|     } | ||||
| 
 | ||||
|     public void setDetailedStatusPosition(int position) { | ||||
|         if (position != detailedStatusPosition | ||||
|                 && detailedStatusPosition != RecyclerView.NO_POSITION) { | ||||
|  |  | |||
|  | @ -39,6 +39,7 @@ public final class TimelineAdapter extends RecyclerView.Adapter { | |||
|     private final AdapterDataSource<StatusViewData> dataSource; | ||||
|     private final StatusActionListener statusListener; | ||||
|     private boolean mediaPreviewEnabled; | ||||
|     private boolean useAbsoluteTime; | ||||
| 
 | ||||
|     public TimelineAdapter(AdapterDataSource<StatusViewData> dataSource, | ||||
|                            StatusActionListener statusListener) { | ||||
|  | @ -46,6 +47,7 @@ public final class TimelineAdapter extends RecyclerView.Adapter { | |||
|         this.dataSource = dataSource; | ||||
|         this.statusListener = statusListener; | ||||
|         mediaPreviewEnabled = true; | ||||
|         useAbsoluteTime = false; | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|  | @ -56,7 +58,7 @@ public final class TimelineAdapter extends RecyclerView.Adapter { | |||
|             case VIEW_TYPE_STATUS: { | ||||
|                 View view = LayoutInflater.from(viewGroup.getContext()) | ||||
|                         .inflate(R.layout.item_status, viewGroup, false); | ||||
|                 return new StatusViewHolder(view); | ||||
|                 return new StatusViewHolder(view, useAbsoluteTime); | ||||
|             } | ||||
|             case VIEW_TYPE_PLACEHOLDER: { | ||||
|                 View view = LayoutInflater.from(viewGroup.getContext()) | ||||
|  | @ -98,6 +100,10 @@ public final class TimelineAdapter extends RecyclerView.Adapter { | |||
|         mediaPreviewEnabled = enabled; | ||||
|     } | ||||
| 
 | ||||
|     public void setUseAbsoluteTime(boolean useAbsoluteTime){ | ||||
|         this.useAbsoluteTime=useAbsoluteTime; | ||||
|     } | ||||
| 
 | ||||
|     public boolean getMediaPreviewEnabled() { | ||||
|         return mediaPreviewEnabled; | ||||
|     } | ||||
|  |  | |||
|  | @ -198,6 +198,8 @@ public class NotificationsFragment extends SFragment implements | |||
|         alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false); | ||||
|         boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true); | ||||
|         adapter.setMediaPreviewEnabled(mediaPreviewEnabled); | ||||
|         boolean useAbsoluteTime = preferences.getBoolean("absoluteTimeView", false); | ||||
|         adapter.setUseAbsoluteTime(useAbsoluteTime); | ||||
|         recyclerView.setAdapter(adapter); | ||||
| 
 | ||||
|         notifications.clear(); | ||||
|  |  | |||
|  | @ -50,6 +50,7 @@ class SearchFragment : SFragment(), StatusActionListener, Injectable { | |||
| 
 | ||||
|     private var alwaysShowSensitiveMedia = false | ||||
|     private var mediaPreviewEnabled = true | ||||
|     private var useAbsoluteTime = false | ||||
| 
 | ||||
| 
 | ||||
|     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||||
|  | @ -60,10 +61,11 @@ class SearchFragment : SFragment(), StatusActionListener, Injectable { | |||
|         val preferences = PreferenceManager.getDefaultSharedPreferences(view.context) | ||||
|         alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false) | ||||
|         mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true) | ||||
|         useAbsoluteTime = preferences.getBoolean("absoluteTimeView", false) | ||||
| 
 | ||||
|         searchRecyclerView.addItemDecoration(DividerItemDecoration(view.context, DividerItemDecoration.VERTICAL)) | ||||
|         searchRecyclerView.layoutManager = LinearLayoutManager(view.context) | ||||
|         searchAdapter = SearchResultsAdapter(mediaPreviewEnabled, alwaysShowSensitiveMedia, this, this) | ||||
|         searchAdapter = SearchResultsAdapter(mediaPreviewEnabled, alwaysShowSensitiveMedia, this, this, useAbsoluteTime) | ||||
|         searchRecyclerView.adapter = searchAdapter | ||||
| 
 | ||||
|     } | ||||
|  |  | |||
|  | @ -248,6 +248,8 @@ public class TimelineFragment extends SFragment implements | |||
|         alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false); | ||||
|         boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true); | ||||
|         adapter.setMediaPreviewEnabled(mediaPreviewEnabled); | ||||
|         boolean useAbsoluteTime = preferences.getBoolean("absoluteTimeView", false); | ||||
|         adapter.setUseAbsoluteTime(useAbsoluteTime); | ||||
| 
 | ||||
|         boolean filter = preferences.getBoolean("tabFilterHomeReplies", true); | ||||
|         filterRemoveReplies = kind == Kind.HOME && !filter; | ||||
|  | @ -605,7 +607,7 @@ public class TimelineFragment extends SFragment implements | |||
|             case "mediaPreviewEnabled": { | ||||
|                 boolean enabled = sharedPreferences.getBoolean("mediaPreviewEnabled", true); | ||||
|                 boolean oldMediaPreviewEnabled = adapter.getMediaPreviewEnabled(); | ||||
|                 if(enabled != oldMediaPreviewEnabled) { | ||||
|                 if (enabled != oldMediaPreviewEnabled) { | ||||
|                     adapter.setMediaPreviewEnabled(enabled); | ||||
|                     fullyRefresh(); | ||||
|                 } | ||||
|  | @ -827,7 +829,7 @@ public class TimelineFragment extends SFragment implements | |||
|     } | ||||
| 
 | ||||
|     private void onFetchTimelineFailure(Exception exception, FetchEnd fetchEnd, int position) { | ||||
|         if(isAdded()) { | ||||
|         if (isAdded()) { | ||||
|             swipeRefreshLayout.setRefreshing(false); | ||||
| 
 | ||||
|             if (fetchEnd == FetchEnd.MIDDLE && !statuses.get(position).isRight()) { | ||||
|  | @ -1049,7 +1051,7 @@ public class TimelineFragment extends SFragment implements | |||
|     private final ListUpdateCallback listUpdateCallback = new ListUpdateCallback() { | ||||
|         @Override | ||||
|         public void onInserted(int position, int count) { | ||||
|             if(isAdded()) { | ||||
|             if (isAdded()) { | ||||
|                 adapter.notifyItemRangeInserted(position, count); | ||||
|                 Context context = getContext(); | ||||
|                 if (position == 0 && context != null) { | ||||
|  |  | |||
|  | @ -156,6 +156,8 @@ public final class ViewThreadFragment extends SFragment implements | |||
|         alwaysShowSensitiveMedia = preferences.getBoolean("alwaysShowSensitiveMedia", false); | ||||
|         boolean mediaPreviewEnabled = preferences.getBoolean("mediaPreviewEnabled", true); | ||||
|         adapter.setMediaPreviewEnabled(mediaPreviewEnabled); | ||||
|         boolean useAbsoluteTime = preferences.getBoolean("absoluteTimeView", false); | ||||
|         adapter.setUseAbsoluteTime(useAbsoluteTime); | ||||
|         recyclerView.setAdapter(adapter); | ||||
| 
 | ||||
|         statuses.clear(); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue