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 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

View File

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

View File

@ -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);
}
});

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_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();
}
}
}

View File

@ -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<Status>()); //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();

View File

@ -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