Skip to content

Commit

Permalink
Add setting to show/hide notification filter bar (#1314)
Browse files Browse the repository at this point in the history
* Add setting to show/hide notification filter bar #1306

* Remove not required requestLayout

* Fix notifications reload issue
  • Loading branch information
pandasoft0 authored and connyduck committed Jun 11, 2019
1 parent a6819ce commit ce501f2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@
import android.widget.PopupWindow;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.arch.core.util.Function;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.util.Pair;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.keylesspalace.tusky.R;
Expand Down Expand Up @@ -75,21 +92,6 @@

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.arch.core.util.Function;
import androidx.core.util.Pair;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import at.connyduck.sparkbutton.helpers.Utils;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
Expand Down Expand Up @@ -160,6 +162,7 @@ private Placeholder(long id) {
private boolean bottomLoading;
private String bottomId;
private boolean alwaysShowSensitiveMedia;
private boolean showNotificationsFilter;

// Each element is either a Notification for loading data or a Placeholder
private final PairedList<Either<Placeholder, Notification>, NotificationViewData> notifications
Expand Down Expand Up @@ -192,6 +195,14 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
View rootView = inflater.inflate(R.layout.fragment_timeline_notifications, container, false);

@NonNull Context context = inflater.getContext(); // from inflater to silence warning
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());

boolean showNotificationsFilterSetting = preferences.getBoolean("showNotificationsFilter", true);
//Clear notifications on filter visibility change to force refresh
if (showNotificationsFilterSetting != showNotificationsFilter)
notifications.clear();
showNotificationsFilter = showNotificationsFilterSetting;

// Setup the SwipeRefreshLayout.
swipeRefreshLayout = rootView.findViewById(R.id.swipeRefreshLayout);
recyclerView = rootView.findViewById(R.id.recyclerView);
Expand Down Expand Up @@ -224,7 +235,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

adapter = new NotificationsAdapter(accountManager.getActiveAccount().getAccountId(),
dataSource, this, this);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
alwaysShowSensitiveMedia = accountManager.getActiveAccount().getAlwaysShowSensitiveMedia();
boolean mediaPreviewEnabled = accountManager.getActiveAccount().getMediaPreviewEnabled();
adapter.setMediaPreviewEnabled(mediaPreviewEnabled);
Expand Down Expand Up @@ -255,10 +265,28 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

updateFilterVisibility();

return rootView;
}

private void confirmClearNotifications(){
private void updateFilterVisibility() {
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) swipeRefreshLayout.getLayoutParams();
if (showNotificationsFilter) {
appBarOptions.setExpanded(true, false);
appBarOptions.setVisibility(View.VISIBLE);
//Set content behaviour to hide filter on scroll
params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
} else {
appBarOptions.setExpanded(false, false);
appBarOptions.setVisibility(View.GONE);
//Clear behaviour to hide app bar
params.setBehavior(null);
}
}

private void confirmClearNotifications() {
new AlertDialog.Builder(getContext())
.setMessage(R.string.notification_clear_text)
.setPositiveButton(android.R.string.yes, (DialogInterface dia, int which) -> clearNotifications())
Expand Down Expand Up @@ -583,18 +611,7 @@ public void onNotificationContentCollapsedChange(boolean isCollapsed, int positi
private void clearNotifications() {
//Cancel all ongoing requests
swipeRefreshLayout.setRefreshing(false);
for (Call callItem : callList) {
callItem.cancel();
}
callList.clear();
bottomLoading = false;
topLoading = false;

//Disable load more
bottomId = null;

//Clear exists notifications
notifications.clear();
resetNotificationsLoad();

//Show friend elephant
this.statusView.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -625,6 +642,21 @@ public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
callList.add(call);
}

private void resetNotificationsLoad() {
for (Call callItem : callList) {
callItem.cancel();
}
callList.clear();
bottomLoading = false;
topLoading = false;

//Disable load more
bottomId = null;

//Clear exists notifications
notifications.clear();
}


private void showFilterMenu() {
List<Notification.Type> notificationsList = Notification.Type.Companion.getAsList();
Expand Down Expand Up @@ -751,6 +783,13 @@ private void onPreferenceChanged(String key) {
fullyRefresh();
}
}
case "showNotificationsFilter": {
if (isAdded()) {
showNotificationsFilter = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("showNotificationsFilter", true);
updateFilterVisibility();
fullyRefreshWithProgressBar(true);
}
}
}
}

Expand Down Expand Up @@ -805,7 +844,7 @@ private Placeholder newPlaceholder() {

private void jumpToTop() {
if (isAdded()) {
appBarOptions.setExpanded(true,false);
appBarOptions.setExpanded(true, false);
layoutManager.scrollToPosition(0);
scrollListener.reset();
}
Expand All @@ -828,7 +867,7 @@ private void sendFetchNotificationsRequest(String fromId, String uptoId,
bottomLoading = true;
}

Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE, notificationFilter);
Call<List<Notification>> call = mastodonApi.notifications(fromId, uptoId, LOAD_AT_ONCE, showNotificationsFilter ? notificationFilter : null);

call.enqueue(new Callback<List<Notification>>() {
@Override
Expand All @@ -844,7 +883,8 @@ public void onResponse(@NonNull Call<List<Notification>> call,

@Override
public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {
onFetchNotificationsFailure((Exception) t, fetchEnd, pos);
if (!call.isCanceled())
onFetchNotificationsFailure((Exception) t, fetchEnd, pos);
}
});
callList.add(call);
Expand All @@ -853,21 +893,22 @@ public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable
private void onFetchNotificationsSuccess(List<Notification> notifications, String linkHeader,
FetchEnd fetchEnd, int pos) {
List<HttpHeaderLink> links = HttpHeaderLink.parse(linkHeader);
HttpHeaderLink next = HttpHeaderLink.findByRelationType(links, "next");
String fromId = null;
if (next != null) {
fromId = next.uri.getQueryParameter("max_id");
}

switch (fetchEnd) {
case TOP: {
update(notifications, null);
update(notifications, this.notifications.isEmpty() ? fromId : null);
break;
}
case MIDDLE: {
replacePlaceholderWithNotifications(notifications, pos);
break;
}
case BOTTOM: {
HttpHeaderLink next = HttpHeaderLink.findByRelationType(links, "next");
String fromId = null;
if (next != null) {
fromId = next.uri.getQueryParameter("max_id");
}

if (!this.notifications.isEmpty()
&& !this.notifications.get(this.notifications.size() - 1).isRight()) {
Expand Down Expand Up @@ -1026,8 +1067,8 @@ private List<Either<Placeholder, Notification>> liftNotificationList(List<Notifi
}

private void fullyRefreshWithProgressBar(boolean isShow) {
notifications.clear();
if (isShow && notifications.isEmpty()) {
resetNotificationsLoad();
if (isShow) {
progressBar.setVisibility(View.VISIBLE);
statusView.setVisibility(View.GONE);
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,5 @@
<string name="report_description_1">The report will be sent to your server moderator. You can provide an explanation of why you are reporting this account below:</string>
<string name="report_description_remote_instance">The account is from another server. Send an anonymized copy of the report there as well?</string>

<string name="pref_title_show_notifications_filter">Show Notifications filter</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
android:defaultValue="false"
android:key="animateGifAvatars"
android:title="@string/pref_title_animate_gif_avatars" />

<SwitchPreferenceCompat
android:defaultValue="true"
android:key="showNotificationsFilter"
android:title="@string/pref_title_show_notifications_filter" />

</PreferenceCategory>

<PreferenceCategory android:title="@string/pref_title_browser_settings">
Expand Down

0 comments on commit ce501f2

Please sign in to comment.