Notifications not deleted from cache

This commit is contained in:
Thomas 2022-11-07 14:53:48 +01:00
parent ee68f4fc7d
commit d4aee3ff4e
6 changed files with 34 additions and 31 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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 {

View File

@ -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;

View File

@ -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<String> 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();

View File

@ -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<Void> clearNotification(@NonNull String instance, String token) {
public LiveData<Void> clearNotification(@NonNull String user_id, @NonNull String instance, String token) {
voidMutableLiveData = new MutableLiveData<>();
MastodonNotificationsService mastodonNotificationsService = init(instance);
new Thread(() -> {
Call<Void> 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();
}