Fixes an issue with notifications

This commit is contained in:
tom79 2017-08-26 10:00:53 +02:00
parent 067cb67e0a
commit 3ad9a81e99
4 changed files with 27 additions and 22 deletions

View File

@ -203,7 +203,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
List<Notification> notifications = apiResponse.getNotifications();
max_id = apiResponse.getMax_id();
if( refreshData ) {
manageNotifications(notifications, max_id);
manageNotifications(notifications, max_id, apiResponse.getSince_id());
if( apiResponse.getSince_id() != null) {
editor.putString(Helper.LAST_MAX_ID_BUBBLE_NOTIF + userId, apiResponse.getSince_id());
editor.apply();
@ -244,7 +244,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}
}
private void manageNotifications(List<Notification> notifications, String max_id){
private void manageNotifications(List<Notification> notifications, String max_id, String since_id){
flag_loading = (max_id == null );
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
if( !swiped && firstLoad && (notifications == null || notifications.size() == 0))
@ -271,9 +271,9 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
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){
if( currentAccount != null && firstLoad && since_id != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), max_id);
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + currentAccount.getId(), since_id);
editor.apply();
}
}

View File

@ -285,7 +285,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
if( refreshData ) {
max_id = apiResponse.getMax_id();
manageStatus(statuses, max_id);
manageStatus(statuses, max_id, apiResponse.getSince_id());
if( apiResponse.getSince_id() != null) {
editor.putString(Helper.LAST_MAX_ID_BUBBLE_HOME + userId, apiResponse.getSince_id());
editor.apply();
@ -329,14 +329,14 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
}else {
max_id = apiResponse.getMax_id();
manageStatus(statuses, max_id);
manageStatus(statuses, max_id, apiResponse.getSince_id());
}
}
private void manageStatus(List<Status> statuses, String max_id){
private void manageStatus(List<Status> statuses, String max_id, String since_id){
flag_loading = (max_id == null );
if( !swiped && firstLoad && (statuses == null || statuses.size() == 0))
textviewNoAction.setVisibility(View.VISIBLE);
@ -362,9 +362,9 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
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){
if( currentAccount != null && firstLoad && since_id != null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + currentAccount.getId(), max_id);
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + currentAccount.getId(), since_id);
editor.apply();
}
}

View File

@ -22,6 +22,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import com.evernote.android.job.Job;
@ -134,13 +135,19 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
final String max_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null);
if( max_id == null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, apiResponse.getSince_id());
editor.apply();
return;
}
//No previous notifications in cache, so no notification will be sent
String message;
for(Status status: statuses){
//The notification associated to max_id is discarded as it is supposed to have already been sent
//Also, if the toot comes from the owner, we will avoid to warn him/her...
if( (max_id != null && status.getId().equals(max_id)) || (acct != null && status.getAccount().getAcct().trim().equals(acct.trim()) ))
if( (status.getId().equals(max_id)) || (acct != null && status.getAccount().getAcct().trim().equals(acct.trim()) ))
continue;
String notificationUrl = status.getAccount().getAvatar();
@ -155,10 +162,6 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
long notif_id = Long.parseLong(userId);
final int notificationId = ((notif_id + 2) > 2147483647) ? (int) (2147483647 - notif_id - 2) : (int) (notif_id + 2);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, apiResponse.getMax_id());
editor.apply();
if( notificationUrl != null){
ImageLoader imageLoaderNoty = ImageLoader.getInstance();
File cacheDir = new File(getContext().getCacheDir(), getContext().getString(R.string.app_name));
@ -184,13 +187,11 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
if( max_id != null)
notify_user(getContext(), intent, notificationId, loadedImage, finalTitle, finalMessage);
notify_user(getContext(), intent, notificationId, loadedImage, finalTitle, finalMessage);
}
@Override
public void onLoadingFailed(java.lang.String imageUri, android.view.View view, FailReason failReason){
if( max_id != null)
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
R.drawable.mastodonlogo), finalTitle, finalMessage);
}});

View File

@ -22,6 +22,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import com.evernote.android.job.Job;
@ -145,6 +146,13 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
if( max_id == null){
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, apiResponse.getSince_id());
editor.apply();
return;
}
//No previous notifications in cache, so no notification will be sent
int newFollows = 0;
int newAdds = 0;
@ -257,10 +265,6 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
}
}
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, apiResponse.getMax_id());
editor.apply();
}
}