From d4aee3ff4ea3eb71c84b339747d0fea468efde3e Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 7 Nov 2022 14:53:48 +0100 Subject: [PATCH] Notifications not deleted from cache --- .../client/entities/app/StatusCache.java | 4 +- .../FragmentMastodonConversation.java | 1 + .../FragmentMastodonNotification.java | 5 +-- .../timeline/FragmentMastodonTimeline.java | 2 +- .../FragmentNotificationContainer.java | 45 +++++++++++-------- .../viewmodel/mastodon/NotificationsVM.java | 8 ++-- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java index 254a8197e..b8d92dbf3 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java +++ b/app/src/main/java/app/fedilab/android/client/entities/app/StatusCache.java @@ -415,14 +415,14 @@ public class StatusCache { * @return long - db id * @throws DBException exception with database */ - public long deleteNotifications() throws DBException { + public long deleteNotifications(String user_id, String instance) throws DBException { if (db == null) { throw new DBException("db is null. Wrong initialization."); } try { return db.delete(Sqlite.TABLE_STATUS_CACHE, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_TYPE + "=?", - new String[]{MainActivity.currentUserID, MainActivity.currentInstance, Timeline.TimeLineEnum.NOTIFICATION.getValue()}); + new String[]{user_id, instance, Timeline.TimeLineEnum.NOTIFICATION.getValue()}); } catch (Exception e) { e.printStackTrace(); return -1; diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java index 708911754..3b7fdd1c1 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonConversation.java @@ -227,6 +227,7 @@ public class FragmentMastodonConversation extends Fragment implements Conversati binding.swipeContainer.setColorSchemeColors( c1, c1, c1 ); + initialConversations = null; binding.loader.setVisibility(View.VISIBLE); binding.recyclerView.setVisibility(View.GONE); timelinesVM = new ViewModelProvider(FragmentMastodonConversation.this).get(TimelinesVM.class); diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java index 80e9a9e54..b4076ffc7 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonNotification.java @@ -162,13 +162,13 @@ public class FragmentMastodonNotification extends Fragment implements Notificati binding.loader.setVisibility(View.VISIBLE); binding.recyclerView.setVisibility(View.GONE); max_id = null; + initialNotifications = null; route(null, false); } public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - flagLoading = false; isViewInitialized = Timeline.TimeLineEnum.NOTIFICATION.getValue().compareTo(Helper.getSlugOfFirstFragment(requireActivity(), currentUserID, currentInstance)) == 0; binding = FragmentPaginationBinding.inflate(inflater, container, false); @@ -350,7 +350,6 @@ public class FragmentMastodonNotification extends Fragment implements Notificati if (!isAdded()) { return; } - SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); boolean useCache = sharedpreferences.getBoolean(getString(R.string.SET_USE_CACHE), true); @@ -369,9 +368,7 @@ public class FragmentMastodonNotification extends Fragment implements Notificati timelineParams.maxId = max_id; } timelineParams.excludeType = getExcludeType(); - timelineParams.fetchingMissing = fetchingMissing; - if (useCache) { getCachedNotifications(direction, fetchingMissing, timelineParams); } else { diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java index 0e82effca..e51754bdc 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonTimeline.java @@ -232,7 +232,7 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter. timelinesVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, TimelinesVM.class); accountsVM = new ViewModelProvider(FragmentMastodonTimeline.this).get(viewModelKey, AccountsVM.class); - + initialStatuses = null; binding.loader.setVisibility(View.VISIBLE); binding.recyclerView.setVisibility(View.GONE); max_id = statusReport != null ? statusReport.id : null; diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentNotificationContainer.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentNotificationContainer.java index b243ae7d1..f74f2f756 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentNotificationContainer.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentNotificationContainer.java @@ -99,8 +99,10 @@ public class FragmentNotificationContainer extends Fragment { db.setPositiveButton(R.string.delete_all, (dialog, id) -> { changes.set(true); NotificationsVM notificationsVM = new ViewModelProvider(FragmentNotificationContainer.this).get(NotificationsVM.class); - notificationsVM.clearNotification(BaseMainActivity.currentInstance, BaseMainActivity.currentToken) - .observe(getViewLifecycleOwner(), unused -> Toasty.info(requireActivity(), R.string.delete_notification_all, Toasty.LENGTH_LONG).show()); + notificationsVM.clearNotification(BaseMainActivity.currentUserID, BaseMainActivity.currentInstance, BaseMainActivity.currentToken) + .observe(getViewLifecycleOwner(), unused -> { + Toasty.info(requireActivity(), R.string.delete_notification_all, Toasty.LENGTH_LONG).show(); + }); dialog.dismiss(); }); db.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); @@ -189,24 +191,8 @@ public class FragmentNotificationContainer extends Fragment { ((MaterialButton) v1).setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_baseline_expand_less_24, requireContext().getTheme())); } }); + dialogBuilder.setOnDismissListener(dialogInterface -> doAction(changes.get(), excludedCategoriesList)); dialogBuilder.setPositiveButton(R.string.close, (dialog, id) -> { - if (changes.get()) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - if (excludedCategoriesList.size() > 0) { - StringBuilder cat = new StringBuilder(); - for (String category : excludedCategoriesList) { - cat.append(category).append('|'); - } - if (cat.toString().endsWith("|")) { - cat.setLength(cat.length() - 1); - } - editor.putString(getString(R.string.SET_EXCLUDED_NOTIFICATIONS_TYPE) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance, cat.toString()); - } else { - editor.putString(getString(R.string.SET_EXCLUDED_NOTIFICATIONS_TYPE) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance, null); - } - editor.commit(); - ((BaseMainActivity) requireActivity()).refreshFragment(); - } dialog.dismiss(); }); AlertDialog alertDialog = dialogBuilder.create(); @@ -241,6 +227,27 @@ public class FragmentNotificationContainer extends Fragment { return binding.getRoot(); } + private void doAction(boolean changed, List excludedCategoriesList) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); + if (changed) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + if (excludedCategoriesList.size() > 0) { + StringBuilder cat = new StringBuilder(); + for (String category : excludedCategoriesList) { + cat.append(category).append('|'); + } + if (cat.toString().endsWith("|")) { + cat.setLength(cat.length() - 1); + } + editor.putString(getString(R.string.SET_EXCLUDED_NOTIFICATIONS_TYPE) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance, cat.toString()); + } else { + editor.putString(getString(R.string.SET_EXCLUDED_NOTIFICATIONS_TYPE) + BaseMainActivity.currentUserID + BaseMainActivity.currentInstance, null); + } + editor.commit(); + ((BaseMainActivity) requireActivity()).refreshFragment(); + } + } + @Override public void onResume() { super.onResume(); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java index f385ad7f7..e0788d352 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/NotificationsVM.java @@ -223,21 +223,19 @@ public class NotificationsVM extends AndroidViewModel { /** * Get a notification for the authenticated account by its id * + * @param user_id String - UserId for the api call * @param instance String - Instance for the api call * @param token String - Token of the authenticated account */ - public LiveData clearNotification(@NonNull String instance, String token) { + public LiveData clearNotification(@NonNull String user_id, @NonNull String instance, String token) { voidMutableLiveData = new MutableLiveData<>(); MastodonNotificationsService mastodonNotificationsService = init(instance); new Thread(() -> { Call voidCall = mastodonNotificationsService.clearAllNotifications(token); - try { - new StatusCache(getApplication().getApplicationContext()).deleteNotifications(); - } catch (Exception ignored) { - } if (voidCall != null) { try { voidCall.execute(); + new StatusCache(getApplication().getApplicationContext()).deleteNotifications(user_id, instance); } catch (Exception e) { e.printStackTrace(); }