Improves counters

This commit is contained in:
tom79 2017-09-02 18:17:00 +02:00
parent 25271e1a1e
commit 68d9ff9ba7
6 changed files with 115 additions and 31 deletions

View File

@ -125,7 +125,6 @@ public class MainActivity extends AppCompatActivity
private DisplayStatusFragment homeFragment; private DisplayStatusFragment homeFragment;
private DisplayNotificationsFragment notificationsFragment; private DisplayNotificationsFragment notificationsFragment;
private BroadcastReceiver receive_data; private BroadcastReceiver receive_data;
private int newNotif, newHome;
private boolean display_local, display_global; private boolean display_local, display_global;
public MainActivity() { public MainActivity() {
@ -134,9 +133,9 @@ public class MainActivity extends AppCompatActivity
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
newNotif = 0;
newHome = 0;
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
receive_data = new BroadcastReceiver() { receive_data = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@ -149,12 +148,10 @@ public class MainActivity extends AppCompatActivity
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){ if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification); notificationsFragment.updateData(notification);
}else{ }else{
newNotif++;
updateNotifCounter(); updateNotifCounter();
notificationsFragment.refresh(notification); notificationsFragment.refresh(notification);
} }
}else { }else {
newNotif++;
updateNotifCounter(); updateNotifCounter();
} }
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){ }else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
@ -163,12 +160,10 @@ public class MainActivity extends AppCompatActivity
if(homeFragment.getUserVisibleHint() && isActivityVisible()){ if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status); homeFragment.updateData(status);
}else{ }else{
newHome++;
updateHomeCounter(); updateHomeCounter();
homeFragment.refresh(status); homeFragment.refresh(status);
} }
}else{ }else{
newHome++;
updateHomeCounter(); updateHomeCounter();
} }
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){ }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)); 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); final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == Helper.THEME_LIGHT){ if( theme == Helper.THEME_LIGHT){
@ -287,19 +282,19 @@ public class MainActivity extends AppCompatActivity
if( tab.getPosition() == 0) { if( tab.getPosition() == 0) {
item = navigationView.getMenu().findItem(R.id.nav_home); item = navigationView.getMenu().findItem(R.id.nav_home);
fragmentTag = "HOME_TIMELINE"; fragmentTag = "HOME_TIMELINE";
if (homeFragment != null && newHome > 0) { if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) {
homeFragment.refresh(null); homeFragment.refresh(null);
} }
newHome = 0; Helper.clearUnreadToots(getApplicationContext(), null);
updateHomeCounter(); updateHomeCounter();
}else if( tab.getPosition() == 1) { }else if( tab.getPosition() == 1) {
fragmentTag = "NOTIFICATIONS"; fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification); item = navigationView.getMenu().findItem(R.id.nav_notification);
if (notificationsFragment != null && newNotif > 0) { if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) {
notificationsFragment.refresh(null); notificationsFragment.refresh(null);
} }
newNotif = 0; Helper.clearUnreadNotifications(getApplicationContext(), null);
updateNotifCounter(); updateNotifCounter();
}else if( tab.getPosition() == 2 && display_local) { }else if( tab.getPosition() == 2 && display_local) {
@ -1097,8 +1092,8 @@ public class MainActivity extends AppCompatActivity
if( tabHome == null) if( tabHome == null)
return; return;
TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter); TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter);
tabCounterHome.setText(String.valueOf(newHome)); tabCounterHome.setText(String.valueOf(Helper.getUnreadToots(getApplicationContext(), null)));
if( newHome > 0){ if( Helper.getUnreadToots(getApplicationContext(), null) > 0){
//New data are available //New data are available
//The fragment is not displayed, so the counter is displayed //The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0) if( tabLayout.getSelectedTabPosition() != 0)
@ -1118,8 +1113,8 @@ public class MainActivity extends AppCompatActivity
if( tabNotif == null) if( tabNotif == null)
return; return;
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter); TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(String.valueOf(newNotif)); tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null)));
if( newNotif > 0){ if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){
if( tabLayout.getSelectedTabPosition() != 1) if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE); tabCounterNotif.setVisibility(View.VISIBLE);
else else

View File

@ -152,6 +152,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
} }
new_data.setVisibility(View.GONE); new_data.setVisibility(View.GONE);
notificationsTmp = new ArrayList<>(); notificationsTmp = new ArrayList<>();
Helper.clearUnreadNotifications(context, null);
} }
}); });

View File

@ -234,6 +234,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
lv_status.setAdapter(statusListAdapter); lv_status.setAdapter(statusListAdapter);
statusesTmp = new ArrayList<>(); statusesTmp = new ArrayList<>();
} }
Helper.clearUnreadToots(context, null);
new_data.setVisibility(View.GONE); new_data.setVisibility(View.GONE);
} }
}); });

View File

@ -203,6 +203,8 @@ public class Helper {
public static final String SET_PREVIEW_REPLIES_PP = "set_preview_replies_pp"; 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_TRANSLATOR = "set_translator";
public static final String SET_LED_COLOUR = "set_led_colour"; 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_ALWAYS = 1;
public static final int ATTACHMENT_WIFI = 2; 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();
}
}
} }

View File

@ -374,6 +374,7 @@ public class StreamingService extends Service {
default: default:
break; break;
} }
Helper.increaseUnreadNotifications(getApplicationContext(), userId);
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId()); editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId());
editor.apply(); editor.apply();
@ -393,6 +394,7 @@ public class StreamingService extends Service {
status = API.parseStatuses(getApplicationContext(), response); status = API.parseStatuses(getApplicationContext(), response);
status.setReplies(new ArrayList<Status>()); //Force to don't display replies. status.setReplies(new ArrayList<Status>()); //Force to don't display replies.
max_id_home = status.getId(); max_id_home = status.getId();
Helper.increaseUnreadToots(getApplicationContext(), userId);
if( status.getContent() != null) { if( status.getContent() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString(); message = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString();

View File

@ -128,7 +128,6 @@ public class MainActivity extends AppCompatActivity
private DisplayNotificationsFragment notificationsFragment; private DisplayNotificationsFragment notificationsFragment;
private static final int ERROR_DIALOG_REQUEST_CODE = 97; private static final int ERROR_DIALOG_REQUEST_CODE = 97;
private BroadcastReceiver receive_data; private BroadcastReceiver receive_data;
private int newNotif, newHome;
private boolean display_local, display_global; private boolean display_local, display_global;
public MainActivity() { public MainActivity() {
@ -137,8 +136,6 @@ public class MainActivity extends AppCompatActivity
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
newNotif = 0;
newHome = 0;
receive_data = new BroadcastReceiver() { receive_data = new BroadcastReceiver() {
@Override @Override
@ -151,12 +148,10 @@ public class MainActivity extends AppCompatActivity
if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){ if(notificationsFragment.getUserVisibleHint() && isActivityVisible()){
notificationsFragment.updateData(notification); notificationsFragment.updateData(notification);
}else{ }else{
newNotif++;
updateNotifCounter(); updateNotifCounter();
notificationsFragment.refresh(notification); notificationsFragment.refresh(notification);
} }
}else { }else {
newNotif++;
updateNotifCounter(); updateNotifCounter();
} }
}else if(eventStreaming == StreamingService.EventStreaming.UPDATE){ }else if(eventStreaming == StreamingService.EventStreaming.UPDATE){
@ -165,12 +160,10 @@ public class MainActivity extends AppCompatActivity
if(homeFragment.getUserVisibleHint() && isActivityVisible()){ if(homeFragment.getUserVisibleHint() && isActivityVisible()){
homeFragment.updateData(status); homeFragment.updateData(status);
}else{ }else{
newHome++;
updateHomeCounter(); updateHomeCounter();
homeFragment.refresh(status); homeFragment.refresh(status);
} }
}else{ }else{
newHome++;
updateHomeCounter(); updateHomeCounter();
} }
}else if(eventStreaming == StreamingService.EventStreaming.DELETE){ }else if(eventStreaming == StreamingService.EventStreaming.DELETE){
@ -292,19 +285,19 @@ public class MainActivity extends AppCompatActivity
if( tab.getPosition() == 0) { if( tab.getPosition() == 0) {
item = navigationView.getMenu().findItem(R.id.nav_home); item = navigationView.getMenu().findItem(R.id.nav_home);
fragmentTag = "HOME_TIMELINE"; fragmentTag = "HOME_TIMELINE";
if (homeFragment != null && newHome > 0) { if (homeFragment != null && Helper.getUnreadToots(getApplicationContext(), null) > 0) {
homeFragment.refresh(null); homeFragment.refresh(null);
} }
newHome = 0; Helper.clearUnreadToots(getApplicationContext(), null);
updateHomeCounter(); updateHomeCounter();
}else if( tab.getPosition() == 1) { }else if( tab.getPosition() == 1) {
fragmentTag = "NOTIFICATIONS"; fragmentTag = "NOTIFICATIONS";
item = navigationView.getMenu().findItem(R.id.nav_notification); item = navigationView.getMenu().findItem(R.id.nav_notification);
if (notificationsFragment != null && newNotif > 0) { if (notificationsFragment != null && Helper.getUnreadNotifications(getApplicationContext(), null) > 0) {
notificationsFragment.refresh(null); notificationsFragment.refresh(null);
} }
newNotif = 0; Helper.clearUnreadNotifications(getApplicationContext(), null);
updateNotifCounter(); updateNotifCounter();
}else if( tab.getPosition() == 2 && display_local) { }else if( tab.getPosition() == 2 && display_local) {
@ -1152,8 +1145,8 @@ public class MainActivity extends AppCompatActivity
if( tabHome == null) if( tabHome == null)
return; return;
TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter); TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter);
tabCounterHome.setText(String.valueOf(newHome)); tabCounterHome.setText(String.valueOf(Helper.getUnreadToots(getApplicationContext(), null)));
if( newHome > 0){ if( Helper.getUnreadToots(getApplicationContext(), null) > 0){
//New data are available //New data are available
//The fragment is not displayed, so the counter is displayed //The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0) if( tabLayout.getSelectedTabPosition() != 0)
@ -1173,8 +1166,8 @@ public class MainActivity extends AppCompatActivity
if( tabNotif == null) if( tabNotif == null)
return; return;
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter); TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(String.valueOf(newNotif)); tabCounterNotif.setText(String.valueOf(Helper.getUnreadNotifications(getApplicationContext(), null)));
if( newNotif > 0){ if( Helper.getUnreadNotifications(getApplicationContext(), null) > 0){
if( tabLayout.getSelectedTabPosition() != 1) if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE); tabCounterNotif.setVisibility(View.VISIBLE);
else else