diff --git a/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java b/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java index ccc8a0d3..64389e91 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java +++ b/app/src/main/java/app/fedilab/android/client/entities/QuickLoad.java @@ -159,6 +159,73 @@ public class QuickLoad { } } + + /** + * Delete a status in quickload + * + * @param account {@link Account} + * @param id - String id of the status + * @throws DBException exception with database + */ + public void deleteStatus(Account account, String id) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + + QuickLoad homeQuickLoad = getSavedValue(account, Timeline.TimeLineEnum.HOME, null); + QuickLoad localQuickLoad = getSavedValue(account, Timeline.TimeLineEnum.LOCAL, null); + QuickLoad publicQuickLoad = getSavedValue(account, Timeline.TimeLineEnum.PUBLIC, null); + + for (Status status : homeQuickLoad.statuses) { + if (status.id.equals(id)) { + homeQuickLoad.statuses.remove(status); + break; + } + } + for (Status status : localQuickLoad.statuses) { + if (status.id.equals(id)) { + localQuickLoad.statuses.remove(status); + break; + } + } + for (Status status : publicQuickLoad.statuses) { + if (status.id.equals(id)) { + publicQuickLoad.statuses.remove(status); + break; + } + } + ContentValues valuesHome = new ContentValues(); + valuesHome.put(Sqlite.COL_STATUSES, StatusDraft.mastodonStatusListToStringStorage(homeQuickLoad.statuses)); + //Inserts token + try { + db.update(Sqlite.TABLE_QUICK_LOAD, + valuesHome, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_SLUG + "=?", + new String[]{homeQuickLoad.user_id, homeQuickLoad.instance, homeQuickLoad.slug}); + } catch (Exception e) { + e.printStackTrace(); + } + ContentValues valuesLocal = new ContentValues(); + valuesLocal.put(Sqlite.COL_STATUSES, StatusDraft.mastodonStatusListToStringStorage(homeQuickLoad.statuses)); + //Inserts token + try { + db.update(Sqlite.TABLE_QUICK_LOAD, + valuesLocal, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_SLUG + "=?", + new String[]{homeQuickLoad.user_id, homeQuickLoad.instance, homeQuickLoad.slug}); + } catch (Exception e) { + e.printStackTrace(); + } + ContentValues valuesPublic = new ContentValues(); + valuesPublic.put(Sqlite.COL_STATUSES, StatusDraft.mastodonStatusListToStringStorage(homeQuickLoad.statuses)); + //Inserts token + try { + db.update(Sqlite.TABLE_QUICK_LOAD, + valuesPublic, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_SLUG + "=?", + new String[]{homeQuickLoad.user_id, homeQuickLoad.instance, homeQuickLoad.slug}); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * Retrieves saved values * @@ -182,6 +249,29 @@ public class QuickLoad { return null; } + /** + * Retrieves saved values + * + * @param timeLineType - Timeline.TimeLineEnum + * @param ident - the name for pinned timeline + * @return SavedValues + */ + public QuickLoad getSavedValue(Account account, Timeline.TimeLineEnum timeLineType, String ident) { + if (cannotBeStored(timeLineType)) { + return null; + } + String key = timeLineType.getValue(); + if (ident != null) { + key += "|" + ident; + } + try { + return get(key, account); + } catch (DBException e) { + e.printStackTrace(); + } + return null; + } + /** * Purge the list to avoid long list of Statuses in db * @@ -240,6 +330,25 @@ public class QuickLoad { } } + /** + * Get paginated statuses from db + * + * @return Statuses + * @throws DBException - throws a db exception + */ + private QuickLoad get(String slug, Account account) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + Cursor c = db.query(Sqlite.TABLE_QUICK_LOAD, null, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =? AND " + Sqlite.COL_SLUG + "=?", + new String[]{account.user_id, account.instance, slug}, null, null, null, "1"); + return cursorToQuickLoad(c); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } /** * Convert a cursor to QuickLoad diff --git a/app/src/main/java/app/fedilab/android/client/entities/StatusCache.java b/app/src/main/java/app/fedilab/android/client/entities/StatusCache.java index e6d951c3..c24531ee 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/StatusCache.java +++ b/app/src/main/java/app/fedilab/android/client/entities/StatusCache.java @@ -193,6 +193,29 @@ public class StatusCache { } } + + /** + * delete a status in db + * + * @param instance - String instance + * @param id - String status id + * @return long - db id + * @throws DBException exception with database + */ + public long deleteStatus(String instance, String id) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + return db.delete(Sqlite.TABLE_STATUS_CACHE, + Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?", + new String[]{id, instance}); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + /** * Get paginated statuses from db * diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java index 73f5bf0e..58072ec0 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/StatusAdapter.java @@ -1201,6 +1201,10 @@ public class StatusAdapter extends RecyclerView.Adapter StatusDraft statusDraft = new StatusDraft(); statusDraft.statusDraftList = new ArrayList<>(); statusDraft.statusReplyList = new ArrayList<>(); + if (statusDeleted == null) { + Toasty.error(context, context.getString(R.string.toast_error), Toasty.LENGTH_SHORT).show(); + return; + } statusDeleted.id = null; statusDraft.statusDraftList.add(statusDeleted); intent.putExtra(Helper.ARG_STATUS_DRAFT, statusDraft); diff --git a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java index 8e708d35..63b8992c 100644 --- a/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java +++ b/app/src/main/java/app/fedilab/android/viewmodel/mastodon/StatusesVM.java @@ -31,6 +31,8 @@ import java.util.List; import java.util.concurrent.TimeUnit; import app.fedilab.android.R; +import app.fedilab.android.client.entities.QuickLoad; +import app.fedilab.android.client.entities.StatusCache; import app.fedilab.android.client.mastodon.MastodonStatusesService; import app.fedilab.android.client.mastodon.entities.Account; import app.fedilab.android.client.mastodon.entities.Accounts; @@ -41,6 +43,7 @@ import app.fedilab.android.client.mastodon.entities.Poll; import app.fedilab.android.client.mastodon.entities.ScheduledStatus; import app.fedilab.android.client.mastodon.entities.ScheduledStatuses; import app.fedilab.android.client.mastodon.entities.Status; +import app.fedilab.android.exception.DBException; import app.fedilab.android.helper.Helper; import app.fedilab.android.helper.MastodonHelper; import app.fedilab.android.helper.SpannableHelper; @@ -312,6 +315,16 @@ public class StatusesVM extends AndroidViewModel { e.printStackTrace(); } } + //The status must also be deleted in cache + try { + app.fedilab.android.client.entities.Account account = new app.fedilab.android.client.entities.Account(getApplication().getApplicationContext()).getAccountByToken(token); + new StatusCache(getApplication().getApplicationContext()).deleteStatus(id, account.instance); + new QuickLoad(getApplication().getApplicationContext()).deleteStatus(account, id); + } catch (DBException e) { + e.printStackTrace(); + } + + Handler mainHandler = new Handler(Looper.getMainLooper()); Status finalStatus = status; Runnable myRunnable = () -> statusMutableLiveData.setValue(finalStatus);