Adds instance to sharedpref for notifications
This commit is contained in:
parent
0707fb7570
commit
842afbbe8a
|
@ -21,6 +21,7 @@ import java.lang.ref.WeakReference;
|
|||
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
||||
|
||||
|
||||
|
@ -35,22 +36,17 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
|
||||
private APIResponse apiResponse;
|
||||
private String max_id;
|
||||
private String acct, userId;
|
||||
private Account account;
|
||||
private OnRetrieveNotificationsInterface listener;
|
||||
private String instance;
|
||||
private String token;
|
||||
private boolean refreshData;
|
||||
private WeakReference<Context> contextReference;
|
||||
private boolean display;
|
||||
|
||||
public RetrieveNotificationsAsyncTask(Context context, boolean display, String instance, String token, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
|
||||
public RetrieveNotificationsAsyncTask(Context context, boolean display, Account account, String max_id, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
|
||||
this.contextReference = new WeakReference<>(context);
|
||||
this.max_id = max_id;
|
||||
this.listener = onRetrieveNotificationsInterface;
|
||||
this.acct = acct;
|
||||
this.instance = instance;
|
||||
this.userId = userId;
|
||||
this.token = token;
|
||||
this.account = account;
|
||||
this.refreshData = true;
|
||||
this.display = display;
|
||||
}
|
||||
|
@ -58,8 +54,8 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
API api = new API(this.contextReference.get(), instance, token);
|
||||
if( acct == null)
|
||||
API api = new API(this.contextReference.get(), account.getInstance(), account.getToken());
|
||||
if( account == null)
|
||||
apiResponse = api.getNotifications(max_id, display);
|
||||
else
|
||||
apiResponse = api.getNotificationsSince(max_id, display);
|
||||
|
@ -68,7 +64,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
listener.onRetrieveNotifications(apiResponse, acct, userId, refreshData);
|
||||
listener.onRetrieveNotifications(apiResponse, account, refreshData);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ import fr.gouv.etalab.mastodon.R;
|
|||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
|
||||
|
@ -112,7 +113,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
|||
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
||||
if (!flag_loading) {
|
||||
flag_loading = true;
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
nextElementLoader.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else {
|
||||
|
@ -131,20 +132,20 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
|||
swiped = true;
|
||||
MainActivity.countNewNotifications = 0;
|
||||
if( context != null)
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
});
|
||||
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
|
||||
R.color.mastodonC2,
|
||||
R.color.mastodonC3);
|
||||
if( context != null)
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
else
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if( context != null)
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
}, 500);
|
||||
return rootView;
|
||||
|
@ -173,7 +174,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData) {
|
||||
mainLoader.setVisibility(View.GONE);
|
||||
nextElementLoader.setVisibility(View.GONE);
|
||||
if( apiResponse.getError() != null){
|
||||
|
@ -266,7 +267,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
|||
flag_loading = true;
|
||||
swiped = true;
|
||||
MainActivity.countNewNotifications = 0;
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,11 +15,12 @@
|
|||
package fr.gouv.etalab.mastodon.interfaces;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 28/04/2017.
|
||||
* Interface when notifications have been retrieved
|
||||
*/
|
||||
public interface OnRetrieveNotificationsInterface {
|
||||
void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData);
|
||||
void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData);
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
|||
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_INSTANCE;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.canNotify;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
||||
|
@ -124,7 +125,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
return;
|
||||
//Retrieve users in db that owner has.
|
||||
for (Account account: accounts) {
|
||||
new RetrieveNotificationsAsyncTask(getContext(), false, account.getInstance(), account.getToken(), null, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
new RetrieveNotificationsAsyncTask(getContext(), false, account, null, NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -132,7 +133,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
|
||||
|
||||
@Override
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, String acct, final String userId, boolean refreshData) {
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, final Account account, boolean refreshData) {
|
||||
List<Notification> notificationsReceived = apiResponse.getNotifications();
|
||||
if( apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0)
|
||||
return;
|
||||
|
@ -141,7 +142,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
||||
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);
|
||||
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||
final List<Notification> notifications = new ArrayList<>();
|
||||
int pos = 0;
|
||||
for(Notification notif: notificationsReceived){
|
||||
|
@ -226,8 +227,9 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
final Intent intent = new Intent(getContext(), MainActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
|
||||
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
||||
intent.putExtra(PREF_KEY_ID, userId);
|
||||
long notif_id = Long.parseLong(userId);
|
||||
intent.putExtra(PREF_KEY_ID, account.getId());
|
||||
intent.putExtra(PREF_INSTANCE, account.getInstance());
|
||||
long notif_id = Long.parseLong(account.getId());
|
||||
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
|
||||
if( notificationUrl != null ){
|
||||
|
||||
|
@ -247,10 +249,10 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
|
||||
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
|
||||
R.drawable.mastodonlogo), finalTitle, message);
|
||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId());
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
||||
editor.apply();
|
||||
}
|
||||
return false;
|
||||
|
@ -260,10 +262,10 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
@Override
|
||||
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
|
||||
notify_user(getContext(), intent, notificationId, resource, finalTitle, message);
|
||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId());
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue