From 7205acb9d6b63e3db91810ab6bcfc6b6aefff047 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Thu, 20 Aug 2020 18:06:35 +0100 Subject: [PATCH] Sort newly added notifications. Add remote loading TTL. --- .../main/java/io/lbry/browser/MainActivity.java | 14 ++++++++++---- .../browser/adapter/NotificationListAdapter.java | 11 +++++++---- .../browser/model/lbryinc/LbryNotification.java | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 4e33a6a4..c58d386b 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -205,8 +205,10 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener private static final String CHANNEL_ID_PLAYBACK = "io.lbry.browser.LBRY_PLAYBACK_CHANNEL"; private static final int PLAYBACK_NOTIFICATION_ID = 3; private static final String SPECIAL_URL_PREFIX = "lbry://?"; + private static final int REMOTE_NOTIFICATION_REFRESH_TTL = 300000; // 5 minutes public static final String SKU_SKIP = "lbryskip"; + private Date remoteNotifcationsLastLoaded; private Map specialRouteFragmentClassMap; @Getter private boolean inPictureInPictureMode; @@ -2192,9 +2194,12 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener clearWunderbarFocus(findViewById(R.id.wunderbar)); findViewById(R.id.notifications_container).setVisibility(View.VISIBLE); ((ImageView) findViewById(R.id.notifications_toggle_icon)).setColorFilter(ContextCompat.getColor(this, R.color.lbryGreen)); - if (notificationListAdapter == null || notificationListAdapter.getItemCount() == 0) { + if (remoteNotifcationsLastLoaded == null || + (System.currentTimeMillis() - remoteNotifcationsLastLoaded.getTime() > REMOTE_NOTIFICATION_REFRESH_TTL)) { loadRemoteNotifications(true); - } else { + } + + if (notificationListAdapter != null) { markNotificationsRead(); } } @@ -3249,6 +3254,8 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener NotificationListTask task = new NotificationListTask(this, findViewById(R.id.notifications_progress), new NotificationListTask.ListNotificationsHandler() { @Override public void onSuccess(List notifications) { + remoteNotifcationsLastLoaded = new Date(); + loadLocalNotifications(); loadUnreadNotificationsCount(); if (markRead && findViewById(R.id.notifications_container).getVisibility() == View.VISIBLE) { @@ -3289,6 +3296,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener if (notificationListAdapter == null) { notificationListAdapter = new NotificationListAdapter(notifications, MainActivity.this); + ((RecyclerView) findViewById(R.id.notifications_list)).setAdapter(notificationListAdapter); } else { notificationListAdapter.addNotifications(notifications); } @@ -3322,8 +3330,6 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener hideNotifications(); } }); - - ((RecyclerView) findViewById(R.id.notifications_list)).setAdapter(notificationListAdapter); } }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } diff --git a/app/src/main/java/io/lbry/browser/adapter/NotificationListAdapter.java b/app/src/main/java/io/lbry/browser/adapter/NotificationListAdapter.java index 31839fc5..555fa10b 100644 --- a/app/src/main/java/io/lbry/browser/adapter/NotificationListAdapter.java +++ b/app/src/main/java/io/lbry/browser/adapter/NotificationListAdapter.java @@ -16,6 +16,7 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import io.lbry.browser.R; @@ -23,9 +24,13 @@ import io.lbry.browser.model.Claim; import io.lbry.browser.model.lbryinc.LbryNotification; import io.lbry.browser.ui.controls.SolidIconView; import io.lbry.browser.utils.Helper; +import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; +import lombok.ToString; +@Data public class NotificationListAdapter extends RecyclerView.Adapter { private static final String RULE_CREATOR_SUBSCRIBER = "creator_subscriber"; @@ -44,10 +49,6 @@ public class NotificationListAdapter extends RecyclerView.Adapter(notifications); } - public List getItems() { - return new ArrayList<>(items); - } - public static class ViewHolder extends RecyclerView.ViewHolder { protected View layoutView; protected TextView titleView; @@ -117,8 +118,10 @@ public class NotificationListAdapter extends RecyclerView.Adapter { private long id; private long remoteId; private String title; @@ -21,4 +22,16 @@ public class LbryNotification { // only for comment notifications private String authorUrl; private Claim commentAuthor; + + public int compare(LbryNotification a, LbryNotification b) { + long t1 = a.getTimestamp() != null ? a.getTimestamp().getTime() : 0; + long t2 = b.getTimestamp() != null ? b.getTimestamp().getTime() : 0; + if (t2 > t1) { + return 1; + } + if (t1 > t2) { + return -1; + } + return 0; + } }