From 68d9ff9ba7e813ce71a93f21dba57c68dd5389c3 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sat, 2 Sep 2017 18:17:00 +0200 Subject: [PATCH] Improves counters --- .../activities/MainActivity.java | 27 +++--- .../DisplayNotificationsFragment.java | 1 + .../fragments/DisplayStatusFragment.java | 1 + .../gouv/etalab/mastodon/helper/Helper.java | 92 +++++++++++++++++++ .../mastodon/services/StreamingService.java | 2 + .../activities/MainActivity.java | 23 ++--- 6 files changed, 115 insertions(+), 31 deletions(-) diff --git a/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java b/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java index bda40211f..941f8602d 100644 --- a/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java +++ b/app/src/fdroid/java/fr.gouv.etalab.mastodon/activities/MainActivity.java @@ -125,7 +125,6 @@ public class MainActivity extends AppCompatActivity private DisplayStatusFragment homeFragment; private DisplayNotificationsFragment notificationsFragment; private BroadcastReceiver receive_data; - private int newNotif, newHome; private boolean display_local, display_global; public MainActivity() { @@ -134,9 +133,9 @@ public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - newNotif = 0; - newHome = 0; + + final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); receive_data = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -149,12 +148,10 @@ public class MainActivity extends AppCompatActivity if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){ notificationsFragment.updateData(notification); }else{ - newNotif++; updateNotifCounter(); notificationsFragment.refresh(notification); } }else { - newNotif++; updateNotifCounter(); } }else if(eventStreaming == StreamingService.EventStreaming.UPDATE){ @@ -163,12 +160,10 @@ public class MainActivity extends AppCompatActivity if(homeFragment.getUserVisibleHint() && isActivityVisible()){ homeFragment.updateData(status); }else{ - newHome++; updateHomeCounter(); homeFragment.refresh(status); } }else{ - newHome++; updateHomeCounter(); } }else if(eventStreaming == StreamingService.EventStreaming.DELETE){ @@ -185,7 +180,7 @@ public class MainActivity extends AppCompatActivity }; LocalBroadcastManager.getInstance(this).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA)); - final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); if( theme == Helper.THEME_LIGHT){ @@ -287,19 +282,19 @@ public class MainActivity extends AppCompatActivity if( tab.getPosition() == 0) { item = navigationView.getMenu().findItem(R.id.nav_home); fragmentTag = "HOME_TIMELINE"; - if (homeFragment != null && newHome > 0) { + if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) { homeFragment.refresh(null); } - newHome = 0; + Helper.clearUnreadToots(getApplicationContext(), null); updateHomeCounter(); }else if( tab.getPosition() == 1) { fragmentTag = "NOTIFICATIONS"; item = navigationView.getMenu().findItem(R.id.nav_notification); - if (notificationsFragment != null && newNotif > 0) { + if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) { notificationsFragment.refresh(null); } - newNotif = 0; + Helper.clearUnreadNotifications(getApplicationContext(), null); updateNotifCounter(); }else if( tab.getPosition() == 2 && display_local) { @@ -1097,8 +1092,8 @@ public class MainActivity extends AppCompatActivity if( tabHome == null) return; TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter); - tabCounterHome.setText(String.valueOf(newHome)); - if( newHome > 0){ + tabCounterHome.setText(String.valueOf(Helper.getUnreadToots(getApplicationContext(), null))); + if( Helper.getUnreadToots(getApplicationContext(), null) > 0){ //New data are available //The fragment is not displayed, so the counter is displayed if( tabLayout.getSelectedTabPosition() != 0) @@ -1118,8 +1113,8 @@ public class MainActivity extends AppCompatActivity if( tabNotif == null) return; TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter); - tabCounterNotif.setText(String.valueOf(newNotif)); - if( newNotif > 0){ + tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null))); + if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){ if( tabLayout.getSelectedTabPosition() != 1) tabCounterNotif.setVisibility(View.VISIBLE); else diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index 80df88156..a20981ccd 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -152,6 +152,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve } new_data.setVisibility(View.GONE); notificationsTmp = new ArrayList<>(); + Helper.clearUnreadNotifications(context, null); } }); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index 4475de349..9b70fc799 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -234,6 +234,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn lv_status.setAdapter(statusListAdapter); statusesTmp = new ArrayList<>(); } + Helper.clearUnreadToots(context, null); new_data.setVisibility(View.GONE); } }); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 4d9954e18..c5ff7961c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -203,6 +203,8 @@ public class Helper { public static final String SET_PREVIEW_REPLIES_PP = "set_preview_replies_pp"; public static final String SET_TRANSLATOR = "set_translator"; public static final String SET_LED_COLOUR = "set_led_colour"; + public static final String SET_UNREAD_NOTIFICATIONS = "set_unread_notifications"; + public static final String SET_UNREAD_TOOTS = "set_unread_toots"; public static final int ATTACHMENT_ALWAYS = 1; public static final int ATTACHMENT_WIFI = 2; @@ -1580,4 +1582,94 @@ public class Helper { } } } + + + public static int getUnreadNotifications(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + return sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + return sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + } + } + public static void increaseUnreadNotifications(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + int unreadNotifications = sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + SharedPreferences.Editor editor = sharedpreferences.edit(); + unreadNotifications = unreadNotifications + 1; + editor.putInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, unreadNotifications); + editor.apply(); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int unreadNotifications = sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + SharedPreferences.Editor editor = sharedpreferences.edit(); + unreadNotifications = unreadNotifications + 1; + editor.putInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, unreadNotifications); + editor.apply(); + } + } + + public static void clearUnreadNotifications(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + editor.apply(); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + editor.apply(); + } + } + + public static int getUnreadToots(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + return sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + return sharedpreferences.getInt(Helper.SET_UNREAD_NOTIFICATIONS + userId, 0); + } + } + + public static void increaseUnreadToots(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + int unreadNotifications = sharedpreferences.getInt(Helper.SET_UNREAD_TOOTS + userId, 0); + SharedPreferences.Editor editor = sharedpreferences.edit(); + unreadNotifications = unreadNotifications + 1; + editor.putInt(Helper.SET_UNREAD_TOOTS + userId, unreadNotifications); + editor.apply(); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int unreadNotifications = sharedpreferences.getInt(Helper.SET_UNREAD_TOOTS + userId, 0); + SharedPreferences.Editor editor = sharedpreferences.edit(); + unreadNotifications = unreadNotifications + 1; + editor.putInt(Helper.SET_UNREAD_TOOTS + userId, unreadNotifications); + editor.apply(); + } + } + + public static void clearUnreadToots(Context context, String userId){ + if( userId == null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putInt(Helper.SET_UNREAD_TOOTS + userId, 0); + editor.apply(); + }else { + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putInt(Helper.SET_UNREAD_TOOTS + userId, 0); + editor.apply(); + } + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java index dd41c0cb7..7ba911ce2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/services/StreamingService.java @@ -374,6 +374,7 @@ public class StreamingService extends Service { default: break; } + Helper.increaseUnreadNotifications(getApplicationContext(), userId); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId()); editor.apply(); @@ -393,6 +394,7 @@ public class StreamingService extends Service { status = API.parseStatuses(getApplicationContext(), response); status.setReplies(new ArrayList()); //Force to don't display replies. max_id_home = status.getId(); + Helper.increaseUnreadToots(getApplicationContext(), userId); if( status.getContent() != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) message = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString(); diff --git a/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java b/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java index f1bf8d21e..d6f3ff4b6 100644 --- a/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java +++ b/app/src/safetynet/java/fr.gouv.etalab.mastodon/activities/MainActivity.java @@ -128,7 +128,6 @@ public class MainActivity extends AppCompatActivity private DisplayNotificationsFragment notificationsFragment; private static final int ERROR_DIALOG_REQUEST_CODE = 97; private BroadcastReceiver receive_data; - private int newNotif, newHome; private boolean display_local, display_global; public MainActivity() { @@ -137,8 +136,6 @@ public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - newNotif = 0; - newHome = 0; receive_data = new BroadcastReceiver() { @Override @@ -151,12 +148,10 @@ public class MainActivity extends AppCompatActivity if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){ notificationsFragment.updateData(notification); }else{ - newNotif++; updateNotifCounter(); notificationsFragment.refresh(notification); } }else { - newNotif++; updateNotifCounter(); } }else if(eventStreaming == StreamingService.EventStreaming.UPDATE){ @@ -165,12 +160,10 @@ public class MainActivity extends AppCompatActivity if(homeFragment.getUserVisibleHint() && isActivityVisible()){ homeFragment.updateData(status); }else{ - newHome++; updateHomeCounter(); homeFragment.refresh(status); } }else{ - newHome++; updateHomeCounter(); } }else if(eventStreaming == StreamingService.EventStreaming.DELETE){ @@ -292,19 +285,19 @@ public class MainActivity extends AppCompatActivity if( tab.getPosition() == 0) { item = navigationView.getMenu().findItem(R.id.nav_home); fragmentTag = "HOME_TIMELINE"; - if (homeFragment != null && newHome > 0) { + if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) { homeFragment.refresh(null); } - newHome = 0; + Helper.clearUnreadToots(getApplicationContext(), null); updateHomeCounter(); }else if( tab.getPosition() == 1) { fragmentTag = "NOTIFICATIONS"; item = navigationView.getMenu().findItem(R.id.nav_notification); - if (notificationsFragment != null && newNotif > 0) { + if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) { notificationsFragment.refresh(null); } - newNotif = 0; + Helper.clearUnreadNotifications(getApplicationContext(), null); updateNotifCounter(); }else if( tab.getPosition() == 2 && display_local) { @@ -1152,8 +1145,8 @@ public class MainActivity extends AppCompatActivity if( tabHome == null) return; TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter); - tabCounterHome.setText(String.valueOf(newHome)); - if( newHome > 0){ + tabCounterHome.setText(String.valueOf(Helper.getUnreadToots(getApplicationContext(), null))); + if( Helper.getUnreadToots(getApplicationContext(), null) > 0){ //New data are available //The fragment is not displayed, so the counter is displayed if( tabLayout.getSelectedTabPosition() != 0) @@ -1173,8 +1166,8 @@ public class MainActivity extends AppCompatActivity if( tabNotif == null) return; TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter); - tabCounterNotif.setText(String.valueOf(newNotif)); - if( newNotif > 0){ + tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null))); + if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){ if( tabLayout.getSelectedTabPosition() != 1) tabCounterNotif.setVisibility(View.VISIBLE); else