From c6305baa7c5aad1c1192835a8d57eab99c7d9f5f Mon Sep 17 00:00:00 2001 From: tom79 Date: Wed, 13 Sep 2017 15:21:03 +0200 Subject: [PATCH] Fixes some behaviors --- .../activities/MainActivity.java | 11 +++-- .../DisplayNotificationsFragment.java | 40 +++++++++--------- .../fragments/DisplayStatusFragment.java | 41 +++++++++---------- 3 files changed, 46 insertions(+), 46 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 e324217ba..d0ba3d213 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 @@ -41,6 +41,7 @@ import android.support.v4.view.ViewPager; import android.support.v7.app.AlertDialog; import android.support.v7.widget.SearchView; import android.support.v7.widget.SwitchCompat; +import android.util.Log; import android.util.Patterns; import android.view.LayoutInflater; import android.view.View; @@ -344,10 +345,10 @@ public class MainActivity extends AppCompatActivity switch (tab.getPosition()){ case 0: DisplayStatusFragment displayStatusFragment = ((DisplayStatusFragment) fragment); - if( displayStatusFragment != null ) - displayStatusFragment.scrollToTop(); countNewStatus = 0; updateHomeCounter(); + if( displayStatusFragment != null ) + displayStatusFragment.scrollToTop(); break; case 2: case 3: @@ -357,10 +358,10 @@ public class MainActivity extends AppCompatActivity break; case 1: DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment); - if( displayNotificationsFragment != null ) - displayNotificationsFragment.scrollToTop(); countNewNotifications = 0; updateNotifCounter(); + if( displayNotificationsFragment != null ) + displayNotificationsFragment.scrollToTop(); break; } } @@ -1158,6 +1159,7 @@ public class MainActivity extends AppCompatActivity if( tabHome == null) return; TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter); + tabCounterHome.setText(String.valueOf(countNewStatus)); if( countNewStatus> 0){ //New data are available //The fragment is not displayed, so the counter is displayed @@ -1175,6 +1177,7 @@ public class MainActivity extends AppCompatActivity if( tabNotif == null) return; TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter); + tabCounterNotif.setText(String.valueOf(countNewNotifications)); if( countNewNotifications > 0){ 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 fb39c26b3..e36a118c4 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 @@ -20,6 +20,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -62,8 +63,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve private SwipeRefreshLayout swipeRefreshLayout; private boolean swiped; private ListView lv_notifications; - private String userId; private String lastReadNotifications; + private String userId; public DisplayNotificationsFragment(){ } @@ -175,10 +176,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve swiped = false; return; } - SharedPreferences.Editor editor = sharedpreferences.edit(); - List notifications = apiResponse.getNotifications(); - String since_id = apiResponse.getSince_id(); max_id = apiResponse.getMax_id(); + List notifications = apiResponse.getNotifications(); //The initial call comes from a classic tab refresh flag_loading = (max_id == null ); if( !swiped && firstLoad && (notifications == null || notifications.size() == 0)) @@ -192,30 +191,35 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve lv_notifications.setAdapter(notificationsListAdapter); swiped = false; } + if( firstLoad) + MainActivity.countNewNotifications = 0; if( notifications != null && notifications.size() > 0) { for(Notification tmpNotification: notifications){ if( Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) MainActivity.countNewNotifications++; this.notifications.add(tmpNotification); } - ((MainActivity)context).updateNotifCounter(); notificationsListAdapter.notifyDataSetChanged(); } + if( firstLoad ) + ((MainActivity)context).updateNotifCounter(); swipeRefreshLayout.setRefreshing(false); - //Store last notification id to avoid to notify for those that have been already seen - if( notifications != null && notifications.size() > 0) { - //acct is null as userId when used in Fragment, data need to be retrieved via shared preferences and db - userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - Account currentAccount = new AccountDAO(context, db).getAccountByID(userId); - if( currentAccount != null && firstLoad && since_id != null){ - editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), notifications.get(0).getId()); - editor.apply(); - } - } firstLoad = false; } + @Override + public void setMenuVisibility(final boolean visible) { + super.setMenuVisibility(visible); + if( context == null) + return; + //Store last notification id to avoid to notify for those that have been already seen + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + if (visible && notifications != null && notifications.size() > 0) { + editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + this.userId, notifications.get(0).getId()); + editor.apply(); + } + } public void scrollToTop(){ if( lv_notifications != null) @@ -231,10 +235,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve boolean isOnWifi = Helper.isOnWIFI(context); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); - SharedPreferences.Editor editor = sharedpreferences.edit(); - String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId()); - editor.apply(); notificationsListAdapter = new NotificationsListAdapter(context,isOnWifi, behaviorWithAttachments, notifications); lv_notifications.setAdapter(notificationsListAdapter); if( textviewNoAction.getVisibility() == View.VISIBLE) 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 66bb428b4..f97f53632 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 @@ -38,12 +38,9 @@ import java.util.List; import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.asynctasks.RetrieveRepliesAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; -import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.drawers.StatusListAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveRepliesInterface; -import fr.gouv.etalab.mastodon.sqlite.AccountDAO; -import fr.gouv.etalab.mastodon.sqlite.Sqlite; import mastodon.etalab.gouv.fr.mastodon.R; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.client.Entities.Status; @@ -261,7 +258,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn return; } List statuses = apiResponse.getStatuses(); - String since_id = apiResponse.getSince_id(); max_id = apiResponse.getMax_id(); flag_loading = (max_id == null ); @@ -274,6 +270,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn lv_status.setAdapter(statusListAdapter); swiped = false; } + if( firstLoad) + MainActivity.countNewStatus = 0; if( statuses != null && statuses.size() > 0) { for(Status tmpStatus: statuses){ if( type == RetrieveFeedsAsyncTask.Type.HOME && firstLoad && Long.parseLong(tmpStatus.getId()) > Long.parseLong(lastReadStatus)){ @@ -285,22 +283,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn this.statuses.add(tmpStatus); } statusListAdapter.notifyDataSetChanged(); + } + if( firstLoad) ((MainActivity)context).updateHomeCounter(); - } swipeRefreshLayout.setRefreshing(false); - - //Store last toot id for home timeline to avoid to notify for those that have been already seen - if(statuses != null && statuses.size() > 0 && type == RetrieveFeedsAsyncTask.Type.HOME ){ - //acct is null when used in Fragment, data need to be retrieved via shared preferences and db - - SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - Account currentAccount = new AccountDAO(context, db).getAccountByID(userId); - if( currentAccount != null && firstLoad && since_id != null){ - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + currentAccount.getId(), statuses.get(0).getId()); - editor.apply(); - } - } firstLoad = false; //Retrieves replies @@ -323,10 +309,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn boolean isOnWifi = Helper.isOnWIFI(context); final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); - SharedPreferences.Editor editor = sharedpreferences.edit(); - String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, status.getId()); - editor.apply(); statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, statuses); lv_status.setAdapter(statusListAdapter); if (textviewNoAction.getVisibility() == View.VISIBLE) @@ -335,6 +317,21 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn } } + @Override + public void setMenuVisibility(final boolean visible) { + super.setMenuVisibility(visible); + if( context == null) + return; + //Store last toot id for home timeline to avoid to notify for those that have been already seen + if (visible && statuses != null && statuses.size() > 0) { + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId()); + editor.apply(); + } + } + public void scrollToTop(){ if( lv_status != null) lv_status.setAdapter(statusListAdapter);