Remove from cache deleted statuses

This commit is contained in:
Thomas 2022-05-09 17:14:53 +02:00
parent 84d75b6939
commit 8e33d57a19
4 changed files with 149 additions and 0 deletions

View file

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

View file

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

View file

@ -1201,6 +1201,10 @@ public class StatusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
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);

View file

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