From 834ad78c36cec96a82507e798eda858fa286af38 Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sat, 15 Apr 2017 20:23:07 +0200 Subject: [PATCH] Allow to filter boosts and replies from home (closes #133) --- .../keylesspalace/tusky/TimelineAdapter.java | 5 +++ .../keylesspalace/tusky/TimelineFragment.java | 35 +++++++++++++++++++ app/src/main/res/layout/activity_account.xml | 1 + app/src/main/res/values/strings.xml | 4 +++ app/src/main/res/xml/preferences.xml | 20 +++++++++++ 5 files changed, 65 insertions(+) diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineAdapter.java b/app/src/main/java/com/keylesspalace/tusky/TimelineAdapter.java index 8f8fc546..9cb2f937 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineAdapter.java @@ -111,6 +111,11 @@ class TimelineAdapter extends RecyclerView.Adapter implements AdapterItemRemover notifyItemRemoved(position); } + public void clear() { + statuses.clear(); + notifyDataSetChanged(); + } + @Nullable Status getItem(int position) { if (position >= 0 && position < statuses.size()) { diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java index 4f1b6e20..b24731fd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java @@ -33,6 +33,7 @@ import android.view.ViewGroup; import com.keylesspalace.tusky.entity.Status; +import java.util.Iterator; import java.util.List; import retrofit2.Call; @@ -61,6 +62,9 @@ public class TimelineFragment extends SFragment implements private LinearLayoutManager layoutManager; private EndlessOnScrollListener scrollListener; private TabLayout.OnTabSelectedListener onTabSelectedListener; + private SharedPreferences preferences; + private boolean filterRemoveReplies; + private boolean filterRemoveReblogs; public static TimelineFragment newInstance(Kind kind) { TimelineFragment fragment = new TimelineFragment(); @@ -181,6 +185,8 @@ public class TimelineFragment extends SFragment implements }; } recyclerView.addOnScrollListener(scrollListener); + + preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); } @Override @@ -274,7 +280,36 @@ public class TimelineFragment extends SFragment implements return false; } + protected void filterStatuses(List statuses) { + Iterator it = statuses.iterator(); + while (it.hasNext()) { + Status status = it.next(); + if ((status.inReplyToId != null && filterRemoveReplies) || (status.reblog != null && filterRemoveReblogs)) { + it.remove(); + } + } + } + + protected void setFiltersFromSettings() { + boolean oldRemoveReplies = filterRemoveReplies; + boolean oldRemoveReblogs = filterRemoveReblogs; + filterRemoveReplies = (kind == Kind.HOME && !preferences.getBoolean("tabFilterHomeReplies", true)); + filterRemoveReblogs = (kind == Kind.HOME && !preferences.getBoolean("tabFilterHomeBoosts", true)); + + if (adapter.getItemCount() > 1 && (oldRemoveReblogs != filterRemoveReblogs || oldRemoveReplies != filterRemoveReplies)) { + adapter.clear(); + sendFetchTimelineRequest(); + } + } + + @Override + public void onResume() { + super.onResume(); + setFiltersFromSettings(); + } + public void onFetchTimelineSuccess(List statuses, String fromId) { + filterStatuses(statuses); if (fromId != null) { if (statuses.size() > 0 && !findStatus(statuses, fromId)) { adapter.addItems(statuses); diff --git a/app/src/main/res/layout/activity_account.xml b/app/src/main/res/layout/activity_account.xml index e3504dd6..3e442d32 100644 --- a/app/src/main/res/layout/activity_account.xml +++ b/app/src/main/res/layout/activity_account.xml @@ -20,6 +20,7 @@ android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:contentScrim="?attr/toolbar_background_color" + app:collapsedTitleTextAppearance="?attr/android:textColorPrimary" android:fitsSystemWindows="true" app:titleEnabled="false"> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7d053474..97217526 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -132,6 +132,10 @@ Browser Use Chrome Custom Tabs Hide follow button while scrolling + Toot filtering + Tabs + Show boosts + Show replies %s mentioned you %1$s, %2$s, %3$s and %4$d others diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index b65497de..8b6006e7 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -21,6 +21,26 @@ android:title="@string/pref_title_custom_tabs" android:defaultValue="true" /> + + + + + + + + + + + +