From 6221de2d3c56ce3d1fe44e56a1b9229af1a982a5 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Wed, 9 Dec 2020 11:31:13 +0100 Subject: [PATCH] sync notificationsDisabled states for followed channels --- .../tasks/wallet/LoadSharedUserStateTask.java | 24 ++++++++++++- .../tasks/wallet/SaveSharedUserStateTask.java | 36 +++++++++++++++++-- .../browser/ui/channel/ChannelFragment.java | 13 +++---- .../ui/findcontent/FileViewFragment.java | 4 +++ 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/tasks/wallet/LoadSharedUserStateTask.java b/app/src/main/java/io/lbry/browser/tasks/wallet/LoadSharedUserStateTask.java index 371c1e5f..0bf93baa 100644 --- a/app/src/main/java/io/lbry/browser/tasks/wallet/LoadSharedUserStateTask.java +++ b/app/src/main/java/io/lbry/browser/tasks/wallet/LoadSharedUserStateTask.java @@ -18,6 +18,7 @@ import io.lbry.browser.exceptions.ApiCallException; import io.lbry.browser.exceptions.LbryUriException; import io.lbry.browser.model.Tag; import io.lbry.browser.model.lbryinc.Subscription; +import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.Lbry; import io.lbry.browser.utils.LbryUri; @@ -69,6 +70,8 @@ public class LoadSharedUserStateTask extends AsyncTask { value.has("subscriptions") && !value.isNull("subscriptions") ? value.getJSONArray("subscriptions") : null; JSONArray tags = value.has("tags") && !value.isNull("tags") ? value.getJSONArray("tags") : null; + JSONArray following = + value.has("following") && !value.isNull("following") ? value.getJSONArray("following") : null; if (subscriptionUrls != null) { subscriptions = new ArrayList<>(); @@ -78,7 +81,8 @@ public class LoadSharedUserStateTask extends AsyncTask { LbryUri uri = LbryUri.parse(LbryUri.normalize(url)); Subscription subscription = new Subscription(); subscription.setChannelName(uri.getChannelName()); - subscription.setUrl(url); + subscription.setUrl(uri.toString()); + subscription.setNotificationsDisabled(isNotificationsDisabledForSubUrl(uri.toString(), following)); subscriptions.add(subscription); if (db != null) { DatabaseHelper.createOrUpdateSubscription(subscription, db); @@ -125,6 +129,24 @@ public class LoadSharedUserStateTask extends AsyncTask { return false; } + protected boolean isNotificationsDisabledForSubUrl(String url, JSONArray following) { + try { + for (int i = 0; i < following.length(); i++) { + JSONObject item = following.getJSONObject(i); + String itemUrl = Helper.getJSONString("url", null, item); + boolean notificationsDisabled = Helper.getJSONBoolean("notificationsDisabled", true, item); + if (url.equalsIgnoreCase(itemUrl)) { + return notificationsDisabled; + } + } + } catch (JSONException ex) { + // pass + } + + // always default notifications disabled to true + return true; + } + protected void onPostExecute(Boolean result) { if (handler != null) { if (result) { diff --git a/app/src/main/java/io/lbry/browser/tasks/wallet/SaveSharedUserStateTask.java b/app/src/main/java/io/lbry/browser/tasks/wallet/SaveSharedUserStateTask.java index 9a0559af..2b962061 100644 --- a/app/src/main/java/io/lbry/browser/tasks/wallet/SaveSharedUserStateTask.java +++ b/app/src/main/java/io/lbry/browser/tasks/wallet/SaveSharedUserStateTask.java @@ -2,6 +2,7 @@ package io.lbry.browser.tasks.wallet; import android.os.AsyncTask; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -11,9 +12,11 @@ import java.util.List; import java.util.Map; import io.lbry.browser.exceptions.ApiCallException; +import io.lbry.browser.exceptions.LbryUriException; import io.lbry.browser.model.lbryinc.Subscription; import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.Lbry; +import io.lbry.browser.utils.LbryUri; import io.lbry.browser.utils.Lbryio; /* @@ -40,9 +43,16 @@ public class SaveSharedUserStateTask extends AsyncTask { protected Boolean doInBackground(Void... params) { // data to save // current subscriptions + List subs = new ArrayList<>(Lbryio.subscriptions); List subscriptionUrls = new ArrayList<>(); - for (Subscription subscription : Lbryio.subscriptions) { - subscriptionUrls.add(subscription.getUrl()); + try { + for (Subscription subscription : subs) { + LbryUri uri = LbryUri.parse(LbryUri.normalize(subscription.getUrl())); + subscriptionUrls.add(uri.toString()); + } + } catch (LbryUriException ex) { + error = ex; + return false; } // followed tags @@ -62,6 +72,7 @@ public class SaveSharedUserStateTask extends AsyncTask { JSONObject value = shared.getJSONObject("value"); value.put("subscriptions", Helper.jsonArrayFromList(subscriptionUrls)); value.put("tags", Helper.jsonArrayFromList(followedTags)); + value.put("following", buildUpdatedNotificationsDisabledStates(subs)); sharedObject = shared; } } @@ -71,6 +82,7 @@ public class SaveSharedUserStateTask extends AsyncTask { JSONObject value = new JSONObject(); value.put("subscriptions", Helper.jsonArrayFromList(subscriptionUrls)); value.put("tags", Helper.jsonArrayFromList(followedTags)); + value.put("following", buildUpdatedNotificationsDisabledStates(subs)); sharedObject = new JSONObject(); sharedObject.put("type", "object"); @@ -91,6 +103,26 @@ public class SaveSharedUserStateTask extends AsyncTask { return false; } + private static JSONArray buildUpdatedNotificationsDisabledStates(List subscriptions) { + JSONArray states = new JSONArray(); + for (Subscription subscription : subscriptions) { + if (!Helper.isNullOrEmpty(subscription.getUrl())) { + try { + JSONObject item = new JSONObject(); + LbryUri uri = LbryUri.parse(LbryUri.normalize(subscription.getUrl())); + item.put("uri", uri.toString()); + item.put("notificationsDisabled", subscription.isNotificationsDisabled()); + states.put(item); + } catch (JSONException | LbryUriException ex) { + // pass + + } + } + } + + return states; + } + protected void onPostExecute(Boolean result) { if (handler != null) { if (result) { diff --git a/app/src/main/java/io/lbry/browser/ui/channel/ChannelFragment.java b/app/src/main/java/io/lbry/browser/ui/channel/ChannelFragment.java index aa18ff43..75750775 100644 --- a/app/src/main/java/io/lbry/browser/ui/channel/ChannelFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/channel/ChannelFragment.java @@ -228,8 +228,11 @@ public class ChannelFragment extends BaseFragment implements FetchChannelsListen ((MainActivity) context).showMessage(subscription.isNotificationsDisabled() ? R.string.receive_no_notifications : R.string.receive_all_notifications); } - checkIsFollowing(); + + if (context != null) { + context.sendBroadcast(new Intent(MainActivity.ACTION_SAVE_SHARED_USER_STATE)); + } } @Override @@ -268,11 +271,9 @@ public class ChannelFragment extends BaseFragment implements FetchChannelsListen checkIsFollowing(); FollowingFragment.resetClaimSearchContent = true; - if (Lbry.SDK_READY) { - Context context = getContext(); - if (context instanceof MainActivity) { - ((MainActivity) context).saveSharedUserState(); - } + Context context = getContext(); + if (context != null) { + context.sendBroadcast(new Intent(MainActivity.ACTION_SAVE_SHARED_USER_STATE)); } } diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index e45e0c9a..8f63d446 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -807,6 +807,10 @@ public class FileViewFragment extends BaseFragment implements R.string.receive_no_notifications : R.string.receive_all_notifications); } checkIsFollowing(); + + if (context != null) { + context.sendBroadcast(new Intent(MainActivity.ACTION_SAVE_SHARED_USER_STATE)); + } } @Override