Browse Source

Double board notifications bug fix

pull/61/merge
Ezerous 6 years ago
parent
commit
46de365f7b
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java
  3. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java
  4. 10
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  5. 4
      app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java
  6. 44
      app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java

4
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 //Creates the adapter that will return a fragment for each section of the activity
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
sectionsPagerAdapter.addFragment(BookmarksTopicFragment.newInstance(1, Bookmark.arrayToString(getTopicsBookmarked())), "Topics"); sectionsPagerAdapter.addFragment(BookmarksTopicFragment.newInstance(1, Bookmark.arrayListToString(getTopicsBookmarked())), "Topics");
sectionsPagerAdapter.addFragment(BookmarksBoardFragment.newInstance(2, Bookmark.arrayToString(getBoardsBookmarked())), "Boards"); sectionsPagerAdapter.addFragment(BookmarksBoardFragment.newInstance(2, Bookmark.arrayListToString(getBoardsBookmarked())), "Boards");
//Sets up the ViewPager with the sections adapter. //Sets up the ViewPager with the sections adapter.
ViewPager viewPager = findViewById(R.id.bookmarks_container); ViewPager viewPager = findViewById(R.id.bookmarks_container);

2
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksBoardFragment.java

@ -62,7 +62,7 @@ public class BookmarksBoardFragment extends Fragment {
if (getArguments() != null) { if (getArguments() != null) {
String bundledBoardBookmarks = getArguments().getString(ARG_BOARD_BOOKMARKS); String bundledBoardBookmarks = getArguments().getString(ARG_BOARD_BOOKMARKS);
if (bundledBoardBookmarks != null) { if (bundledBoardBookmarks != null) {
boardBookmarks = Bookmark.arrayFromString(bundledBoardBookmarks); boardBookmarks = Bookmark.stringToArrayList(bundledBoardBookmarks);
} }
} }

2
app/src/main/java/gr/thmmy/mthmmy/activities/bookmarks/BookmarksTopicFragment.java

@ -63,7 +63,7 @@ public class BookmarksTopicFragment extends Fragment {
if (getArguments() != null) { if (getArguments() != null) {
String bundledTopicBookmarks = getArguments().getString(ARG_TOPIC_BOOKMARKS); String bundledTopicBookmarks = getArguments().getString(ARG_TOPIC_BOOKMARKS);
if (bundledTopicBookmarks != null) { if (bundledTopicBookmarks != null) {
topicBookmarks = Bookmark.arrayFromString(bundledTopicBookmarks); topicBookmarks = Bookmark.stringToArrayList(bundledTopicBookmarks);
} }
} }

10
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -85,7 +85,7 @@ public abstract class BaseActivity extends AppCompatActivity {
//Bookmarks //Bookmarks
public static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; public static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs";
public static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; 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; protected Bookmark thisPageBookmark;
private MenuItem thisPageBookmarkMenuButton; private MenuItem thisPageBookmarkMenuButton;
private SharedPreferences sharedPreferences; private SharedPreferences sharedPreferences;
@ -584,13 +584,13 @@ public abstract class BaseActivity extends AppCompatActivity {
private void loadSavedBookmarks() { private void loadSavedBookmarks() {
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null);
if (tmpString != null) if (tmpString != null)
topicsBookmarked = Bookmark.arrayFromString(tmpString); topicsBookmarked = Bookmark.stringToArrayList(tmpString);
else else
topicsBookmarked = new ArrayList<>(); topicsBookmarked = new ArrayList<>();
tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null);
if (tmpString != null) if (tmpString != null)
boardsBookmarked = Bookmark.arrayFromString(tmpString); boardsBookmarked = Bookmark.stringToArrayList(tmpString);
else { else {
boardsBookmarked = new ArrayList<>(); boardsBookmarked = new ArrayList<>();
} }
@ -622,14 +622,14 @@ public abstract class BaseActivity extends AppCompatActivity {
private void updateBoardBookmarks() { private void updateBoardBookmarks() {
String tmpString; String tmpString;
tmpString = Bookmark.arrayToString(boardsBookmarked); tmpString = Bookmark.arrayListToString(boardsBookmarked);
SharedPreferences.Editor editor = bookmarksFile.edit(); SharedPreferences.Editor editor = bookmarksFile.edit();
editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply(); editor.putString(BOOKMARKED_BOARDS_KEY, tmpString).apply();
} }
private void updateTopicBookmarks() { private void updateTopicBookmarks() {
String tmpString; String tmpString;
tmpString = Bookmark.arrayToString(topicsBookmarked); tmpString = Bookmark.arrayListToString(topicsBookmarked);
SharedPreferences.Editor editor = bookmarksFile.edit(); SharedPreferences.Editor editor = bookmarksFile.edit();
editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply(); editor.putString(BOOKMARKED_TOPICS_KEY, tmpString).apply();
} }

4
app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java

@ -69,7 +69,7 @@ public class Bookmark implements java.io.Serializable {
} }
@Nullable @Nullable
public static String arrayToString(@NonNull ArrayList<Bookmark> arrayList) { public static String arrayListToString(@NonNull ArrayList<Bookmark> arrayList) {
String returnString = ""; String returnString = "";
for (Bookmark bookmark : arrayList) { for (Bookmark bookmark : arrayList) {
if (bookmark != null) { if (bookmark != null) {
@ -82,7 +82,7 @@ public class Bookmark implements java.io.Serializable {
else return null; else return null;
} }
public static ArrayList<Bookmark> arrayFromString(@NonNull String string) { public static ArrayList<Bookmark> stringToArrayList(@NonNull String string) {
ArrayList<Bookmark> returnArray = new ArrayList<>(); ArrayList<Bookmark> returnArray = new ArrayList<>();
String[] lines = string.split("\n"); String[] lines = string.split("\n");
for (String line : lines) { for (String line : lines) {

44
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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.preference.PreferenceManager; 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.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_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; 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.BOOKMARKED_TOPICS_KEY;
import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKS_SHARED_PREFS; import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKS_SHARED_PREFS;
import static gr.thmmy.mthmmy.model.Bookmark.matchExistsById; import static gr.thmmy.mthmmy.model.Bookmark.matchExistsById;
@ -61,15 +66,27 @@ public class NotificationService extends FirebaseMessagingService {
Timber.i("FCM BOARD type message detected."); Timber.i("FCM BOARD type message detected.");
SharedPreferences bookmarksFile = getSharedPreferences(BOOKMARKS_SHARED_PREFS, Context.MODE_PRIVATE); SharedPreferences bookmarksFile = getSharedPreferences(BOOKMARKS_SHARED_PREFS, Context.MODE_PRIVATE);
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); String bookmarkedTopicsString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null);
if (tmpString != null){ if (bookmarkedTopicsString != null && matchExistsById(Bookmark.stringToArrayList(bookmarkedTopicsString), topicId)){
if(matchExistsById(Bookmark.arrayFromString(tmpString), topicId)){ Timber.i("Board notification suppressed (already subscribed to topic).");
Timber.i("Board notification suppressed (already subscribed to topic)."); return;
return;
}
} }
boardId = Integer.parseInt(json.getString("boardId")); boardId = Integer.parseInt(json.getString("boardId"));
String bookmarkedBoardsString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null);
if (bookmarkedBoardsString != null){
ArrayList<Bookmark> boardBookmarks = Bookmark.stringToArrayList(bookmarkedBoardsString);
ArrayList<Integer> 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"); boardTitle = json.getString("boardTitle");
} }
else else
@ -88,6 +105,21 @@ public class NotificationService extends FirebaseMessagingService {
} }
} }
private static ArrayList<Integer> getSubBoardIds(String boardIdsString, int boardId){
ArrayList<Integer> 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_ID = "Posts";
private static final String CHANNEL_NAME = "New Posts"; private static final String CHANNEL_NAME = "New Posts";
private static final String GROUP_KEY = "PostsGroup"; private static final String GROUP_KEY = "PostsGroup";

Loading…
Cancel
Save