From 46de365f7b77df9ef56d3b324a9b4c4bda5f183f Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 21 Dec 2018 14:25:29 +0200 Subject: [PATCH] Double board notifications bug fix --- .../bookmarks/BookmarksActivity.java | 4 +- .../bookmarks/BookmarksBoardFragment.java | 2 +- .../bookmarks/BookmarksTopicFragment.java | 2 +- .../gr/thmmy/mthmmy/base/BaseActivity.java | 10 ++--- .../java/gr/thmmy/mthmmy/model/Bookmark.java | 4 +- .../mthmmy/services/NotificationService.java | 44 ++++++++++++++++--- 6 files changed, 49 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java index 0a65da94..b42a8caf 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java @@ -47,8 +47,8 @@ public class BookmarksActivity extends BaseActivity { //Creates the adapter that will return a fragment for each section of the activity SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); - sectionsPagerAdapter.addFragment(BookmarksTopicFragment.newInstance(1, Bookmark.arrayToString(getTopicsBookmarked())), "Topics"); - sectionsPagerAdapter.addFragment(BookmarksBoardFragment.newInstance(2, Bookmark.arrayToString(getBoardsBookmarked())), "Boards"); + sectionsPagerAdapter.addFragment(BookmarksTopicFragment.newInstance(1, Bookmark.arrayListToString(getTopicsBookmarked())), "Topics"); + sectionsPagerAdapter.addFragment(BookmarksBoardFragment.newInstance(2, Bookmark.arrayListToString(getBoardsBookmarked())), "Boards"); //Sets up the ViewPager with the sections adapter. ViewPager viewPager = findViewById(R.id.bookmarks_container); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java index a73bbe6a..5b8c8d01 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java @@ -62,7 +62,7 @@ public class BookmarksBoardFragment extends Fragment { if (getArguments() != null) { String bundledBoardBookmarks = getArguments().getString(ARG_BOARD_BOOKMARKS); if (bundledBoardBookmarks != null) { - boardBookmarks = Bookmark.arrayFromString(bundledBoardBookmarks); + boardBookmarks = Bookmark.stringToArrayList(bundledBoardBookmarks); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java index 5c6232b7..4316db4b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java @@ -63,7 +63,7 @@ public class BookmarksTopicFragment extends Fragment { if (getArguments() != null) { String bundledTopicBookmarks = getArguments().getString(ARG_TOPIC_BOOKMARKS); if (bundledTopicBookmarks != null) { - topicBookmarks = Bookmark.arrayFromString(bundledTopicBookmarks); + topicBookmarks = Bookmark.stringToArrayList(bundledTopicBookmarks); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index b953c032..d8019545 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -85,7 +85,7 @@ public abstract class BaseActivity extends AppCompatActivity { //Bookmarks public static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; public static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; - private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; + public static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; protected Bookmark thisPageBookmark; private MenuItem thisPageBookmarkMenuButton; private SharedPreferences sharedPreferences; @@ -584,13 +584,13 @@ public abstract class BaseActivity extends AppCompatActivity { private void loadSavedBookmarks() { String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); if (tmpString != null) - topicsBookmarked = Bookmark.arrayFromString(tmpString); + topicsBookmarked = Bookmark.stringToArrayList(tmpString); else topicsBookmarked = new ArrayList<>(); tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); if (tmpString != null) - boardsBookmarked = Bookmark.arrayFromString(tmpString); + boardsBookmarked = Bookmark.stringToArrayList(tmpString); else { boardsBookmarked = new ArrayList<>(); } @@ -622,14 +622,14 @@ public abstract class BaseActivity extends AppCompatActivity { private void updateBoardBookmarks() { String tmpString; - tmpString = Bookmark.arrayToString(boardsBookmarked); + tmpString = Bookmark.arrayListToString(boardsBookmarked); SharedPreferences.Editor editor = bookmarksFile.edit(); editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply(); } private void updateTopicBookmarks() { String tmpString; - tmpString = Bookmark.arrayToString(topicsBookmarked); + tmpString = Bookmark.arrayListToString(topicsBookmarked); SharedPreferences.Editor editor = bookmarksFile.edit(); editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply(); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java index de35e0d0..ac622a64 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java @@ -69,7 +69,7 @@ public class Bookmark implements java.io.Serializable { } @Nullable - public static String arrayToString(@NonNull ArrayList arrayList) { + public static String arrayListToString(@NonNull ArrayList arrayList) { String returnString = ""; for (Bookmark bookmark : arrayList) { if (bookmark != null) { @@ -82,7 +82,7 @@ public class Bookmark implements java.io.Serializable { else return null; } - public static ArrayList arrayFromString(@NonNull String string) { + public static ArrayList stringToArrayList(@NonNull String string) { ArrayList returnArray = new ArrayList<>(); String[] lines = string.split("\n"); for (String line : lines) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java index 6d254021..5631b325 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java @@ -19,6 +19,10 @@ import com.google.firebase.messaging.RemoteMessage; import org.json.JSONException; import org.json.JSONObject; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; import androidx.preference.PreferenceManager; @@ -36,6 +40,7 @@ import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SELECTED_RING import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SETTINGS_SHARED_PREFS; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; +import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKED_BOARDS_KEY; import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKED_TOPICS_KEY; import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKS_SHARED_PREFS; import static gr.thmmy.mthmmy.model.Bookmark.matchExistsById; @@ -61,15 +66,27 @@ public class NotificationService extends FirebaseMessagingService { Timber.i("FCM BOARD type message detected."); SharedPreferences bookmarksFile = getSharedPreferences(BOOKMARKS_SHARED_PREFS, Context.MODE_PRIVATE); - String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); - if (tmpString != null){ - if(matchExistsById(Bookmark.arrayFromString(tmpString), topicId)){ - Timber.i("Board notification suppressed (already subscribed to topic)."); - return; - } + String bookmarkedTopicsString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); + if (bookmarkedTopicsString != null && matchExistsById(Bookmark.stringToArrayList(bookmarkedTopicsString), topicId)){ + Timber.i("Board notification suppressed (already subscribed to topic)."); + return; } boardId = Integer.parseInt(json.getString("boardId")); + + String bookmarkedBoardsString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); + if (bookmarkedBoardsString != null){ + ArrayList boardBookmarks = Bookmark.stringToArrayList(bookmarkedBoardsString); + ArrayList subBoardIds = getSubBoardIds(json.getString("boardIds"), boardId); + //TODO: Also suppress if user has chosen to be notified only for direct children of boardId && !subBoardIds.isEmpty() + for(int subId:subBoardIds){ + if(matchExistsById(boardBookmarks, subId)){ + Timber.i("Board notification suppressed (already subscribed to a subBoard)."); + return; + } + } + } + boardTitle = json.getString("boardTitle"); } else @@ -88,6 +105,21 @@ public class NotificationService extends FirebaseMessagingService { } } + private static ArrayList getSubBoardIds(String boardIdsString, int boardId){ + ArrayList subBoardIds = new ArrayList<>(); + Pattern p = Pattern.compile("(\\d+)"); + Matcher m = p.matcher(boardIdsString); + boolean boardIdfound=false; + while (m.find()){ + int subBoardId = Integer.parseInt(m.group()); + if(boardIdfound) + subBoardIds.add(subBoardId); + else if(boardId==subBoardId) + boardIdfound=true; + } + return subBoardIds; + } + private static final String CHANNEL_ID = "Posts"; private static final String CHANNEL_NAME = "New Posts"; private static final String GROUP_KEY = "PostsGroup";