Retrieves count of unread toots/notifications

This commit is contained in:
tom79 2017-08-24 15:22:04 +02:00
parent 48cf5732c0
commit f21881f697
11 changed files with 151 additions and 26 deletions

View File

@ -158,7 +158,7 @@ public class HashTagActivity extends AppCompatActivity implements OnRetrieveFeed
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
public void onRetrieveFeeds(APIResponse apiResponse, boolean refreshData) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);

View File

@ -23,12 +23,10 @@ import android.database.sqlite.SQLiteDatabase;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
@ -46,6 +44,7 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -118,7 +117,8 @@ public class MainActivity extends AppCompatActivity
private ViewPager viewPager;
private RelativeLayout main_app_container;
private Stack<Integer> stackBack = new Stack<>();
private DisplayStatusFragment homeFragment;
private DisplayNotificationsFragment notificationsFragment;
public MainActivity() {
@ -943,12 +943,13 @@ public class MainActivity extends AppCompatActivity
Bundle bundle = new Bundle();
switch (position) {
case 0:
statusFragment = new DisplayStatusFragment();
homeFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.HOME);
statusFragment.setArguments(bundle);
return statusFragment;
homeFragment.setArguments(bundle);
return homeFragment;
case 1:
return new DisplayNotificationsFragment();
notificationsFragment = new DisplayNotificationsFragment();
return notificationsFragment;
case 2:
statusFragment = new DisplayStatusFragment();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.LOCAL);
@ -969,7 +970,7 @@ public class MainActivity extends AppCompatActivity
}
}
private void displayUnreadCounter(){
private void refreshData(){
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
@ -979,11 +980,48 @@ public class MainActivity extends AppCompatActivity
String last_refresh = sharedpreferences.getString(Helper.LAST_BUBBLE_REFRESH + account.getId(), null);
Date last_refresh_date = Helper.stringToDate(getApplicationContext(), last_refresh);
if (last_refresh_date == null || new Date().getTime() - last_refresh_date.getTime() >= TimeUnit.MINUTES.toMillis(5)) {
//Last read id for home
String since_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + account.getId(), null);
homeFragment.update();
notificationsFragment.update();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_BUBBLE_REFRESH+ account.getId(),Helper.dateToString(getApplicationContext(), new Date()));
editor.apply();
}
}
}
public void updateHomeCounter(int newHomeCount){
if( tabLayout.getTabAt(0) == null )
return;
View tabHome = tabLayout.getTabAt(0).getCustomView();
TextView tabCounterHome = (TextView) tabHome.findViewById(R.id.tab_counter);
tabCounterHome.setText(newHomeCount);
if( newHomeCount > 0){
//New data are available
//The fragment is not displayed, so the counter is displayed
if( tabLayout.getSelectedTabPosition() != 0)
tabCounterHome.setVisibility(View.VISIBLE);
else
tabCounterHome.setVisibility(View.GONE);
}else {
tabCounterHome.setVisibility(View.GONE);
}
}
public void updateNotifCounter(int newNotifCount){
if(tabLayout.getTabAt(1) == null)
return;
View tabNotif = tabLayout.getTabAt(1).getCustomView();
TextView tabCounterNotif = (TextView) tabNotif.findViewById(R.id.tab_counter);
tabCounterNotif.setText(newNotifCount);
if( newNotifCount > 0){
if( tabLayout.getSelectedTabPosition() != 1)
tabCounterNotif.setVisibility(View.VISIBLE);
else
tabCounterNotif.setVisibility(View.GONE);
}else {
tabCounterNotif.setVisibility(View.GONE);
}
}
}

View File

@ -216,7 +216,7 @@ public class ShowConversationActivity extends AppCompatActivity implements OnRet
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
public void onRetrieveFeeds(APIResponse apiResponse, boolean refreshData) {
if( apiResponse.getError() != null){
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true);

View File

@ -36,6 +36,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
private String targetedID;
private String tag;
private boolean showMediaOnly = false;
private boolean refreshData;
public enum Type{
HOME,
@ -54,6 +55,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.refreshData = true;
}
public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
@ -63,6 +65,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.listener = onRetrieveFeedsInterface;
this.targetedID = targetedID;
this.showMediaOnly = showMediaOnly;
this.refreshData = true;
}
public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.context = context;
@ -71,7 +74,17 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
this.listener = onRetrieveFeedsInterface;
this.targetedID = targetedID;
this.tag = tag;
this.refreshData = true;
}
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, boolean refreshData, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.context = context;
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.refreshData = refreshData;
}
@Override
protected Void doInBackground(Void... params) {
@ -109,7 +122,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveFeeds(apiResponse);
listener.onRetrieveFeeds(apiResponse, refreshData);
}
}

View File

@ -35,7 +35,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
private OnRetrieveNotificationsInterface listener;
private String instance;
private String token;
private boolean refreshData;
public RetrieveNotificationsAsyncTask(Context context, String instance, String token, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
this.context = context;
@ -45,6 +45,18 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
this.instance = instance;
this.userId = userId;
this.token = token;
this.refreshData = true;
}
public RetrieveNotificationsAsyncTask(Context context, String instance, String token, String max_id, String acct, String userId, boolean refreshData, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
this.context = context;
this.max_id = max_id;
this.listener = onRetrieveNotificationsInterface;
this.acct = acct;
this.instance = instance;
this.userId = userId;
this.token = token;
this.refreshData = refreshData;
}
@Override
@ -60,7 +72,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveNotifications(apiResponse, acct, userId);
listener.onRetrieveNotifications(apiResponse, acct, userId, refreshData);
}
}

View File

@ -31,8 +31,10 @@ import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -63,6 +65,10 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
private int notificationPerPage;
private boolean swiped;
private ListView lv_notifications;
private int newElements;
private DisplayNotificationsFragment displayNotificationsFragment;
private List<Notification> notificationsTemp;
private String new_max_id;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -74,7 +80,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
flag_loading = true;
notifications = new ArrayList<>();
swiped = false;
newElements = 0;
displayNotificationsFragment = this;
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15);
@ -153,7 +160,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
@Override
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId) {
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
@ -166,7 +173,28 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
swiped = false;
return;
}
String old_max_id = max_id;
new_max_id = apiResponse.getMax_id();
List<Notification> notifications = apiResponse.getNotifications();
if( refreshData || !displayNotificationsFragment.isVisible()) {
manageNotifications(notifications, new_max_id);
if( !displayNotificationsFragment.isVisible()){
int countData = 0;
for(Notification nt : notifications){
if( nt.getId().equals(old_max_id))
break;
countData++;
}
((MainActivity)getActivity()).updateNotifCounter(countData);
}
}else {
notificationsTemp = notifications;
}
}
private void manageNotifications(List<Notification> notifications, String max_id){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( !swiped && firstLoad && (notifications == null || notifications.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
@ -178,7 +206,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
lv_notifications.setAdapter(notificationsListAdapter);
swiped = false;
}
max_id = apiResponse.getMax_id();
if( notifications != null && notifications.size() > 0) {
for(Notification tmpNotification: notifications){
@ -191,7 +219,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
//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);
String 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){
@ -207,4 +235,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if( lv_notifications != null)
lv_notifications.setAdapter(notificationsListAdapter);
}
public void update(){
asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, !displayNotificationsFragment.isVisible(), DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}

View File

@ -33,6 +33,7 @@ import android.widget.Toast;
import java.util.ArrayList;
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;
@ -73,6 +74,10 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
private int behaviorWithAttachments;
private String instanceValue;
private boolean showMediaOnly;
private int newElements;
private DisplayStatusFragment displayStatusFragment;
private List<Status> statusesTemp;
private String new_max_id;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -104,6 +109,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = true;
firstLoad = true;
swiped = false;
newElements = 0;
displayStatusFragment = this;
isOnWifi = Helper.isOnWIFI(context);
swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
@ -215,7 +222,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
public void onRetrieveFeeds(APIResponse apiResponse, boolean refreshData) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
@ -229,8 +236,29 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false;
return;
}
flag_loading = (apiResponse.getMax_id() == null );
List<Status> statuses = apiResponse.getStatuses();
String old_max_id = max_id;
new_max_id = apiResponse.getMax_id();
if( refreshData || !displayStatusFragment.isVisible()) {
manageStatus(statuses, max_id);
if( !displayStatusFragment.isVisible()){
int countData = 0;
for(Status st : statuses){
if( st.getId().equals(old_max_id))
break;
countData++;
}
((MainActivity)getActivity()).updateNotifCounter(countData);
}
}else {
statusesTemp = statuses;
}
}
private void manageStatus(List<Status> statuses, String max_id){
flag_loading = (max_id == null );
if( !swiped && firstLoad && (statuses == null || statuses.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
else
@ -240,7 +268,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
lv_status.setAdapter(statusListAdapter);
swiped = false;
}
max_id = apiResponse.getMax_id();
if( statuses != null && statuses.size() > 0) {
for(Status tmpStatus: statuses){
this.statuses.add(tmpStatus);
@ -273,7 +300,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
new RetrieveRepliesAsyncTask(context, statuses, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}
public void scrollToTop(){
@ -295,4 +321,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
statusListAdapter.notifyDataSetChanged();
}
public void update() {
asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, !displayStatusFragment.isVisible(), DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}

View File

@ -21,5 +21,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse;
* Interface when status have been retrieved
*/
public interface OnRetrieveFeedsInterface {
void onRetrieveFeeds(APIResponse apiResponse);
void onRetrieveFeeds(APIResponse apiResponse, boolean refreshData);
}

View File

@ -21,5 +21,5 @@ import fr.gouv.etalab.mastodon.client.APIResponse;
* Interface when notifications have been retrieved
*/
public interface OnRetrieveNotificationsInterface {
void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId);
void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData);
}

View File

@ -133,7 +133,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
@Override
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId) {
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
List<Notification> notifications = apiResponse.getNotifications();
if( apiResponse.getError() != null || notifications == null || notifications.size() == 0)

View File

@ -25,6 +25,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tab_counter"
android:visibility="gone"
android:textColor="@color/mastodonC1"
android:background="@drawable/shape_counter"