From 9cc5170a7059d186498be2856f97255f2218b559 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 22 May 2022 11:51:32 +0200 Subject: [PATCH] Fix issue #82 - Wrong position when storing in cache --- .../android/client/entities/QuickLoad.java | 43 ++++++++++++++++++- .../android/client/entities/StatusCache.java | 39 +++++++++++++++++ .../app/fedilab/android/helper/Helper.java | 8 +++- .../timeline/FragmentMastodonTimeline.java | 6 +-- 4 files changed, 89 insertions(+), 7 deletions(-) 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 d33b68a03..3829c2b2f 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 @@ -124,8 +124,8 @@ public class QuickLoad { quickLoad.position = position; quickLoad.statuses = statusList; quickLoad.slug = key; - quickLoad.instance = user_id; - quickLoad.user_id = instance; + quickLoad.instance = instance; + quickLoad.user_id = user_id; purge(quickLoad); try { insertOrUpdate(quickLoad); @@ -159,6 +159,45 @@ public class QuickLoad { } + /** + * delete all cache for all accounts + * + * @return long - db id + * @throws DBException exception with database + */ + public long deleteForAllAccount() throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + return db.delete(Sqlite.TABLE_QUICK_LOAD, null, null); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + /** + * delete all cache for an account + * + * @param account - Account + * @return long - db id + * @throws DBException exception with database + */ + public long deleteForAccount(Account account) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + return db.delete(Sqlite.TABLE_QUICK_LOAD, + Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?", + new String[]{account.user_id, account.instance}); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + /** * Delete a status in 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 c24531ee4..83cc135f2 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 @@ -194,6 +194,45 @@ public class StatusCache { } + /** + * delete all cache for all account + * + * @return long - db id + * @throws DBException exception with database + */ + public long deleteForAllAccount() throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + return db.delete(Sqlite.TABLE_STATUS_CACHE, null, null); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + + /** + * delete all cache for an account + * + * @param account - Account + * @return long - db id + * @throws DBException exception with database + */ + public long deleteForAccount(Account account) 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 + " =?", + new String[]{account.user_id, account.instance}); + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + /** * delete a status in db * diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 173999036..170882206 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -125,6 +125,8 @@ import app.fedilab.android.activities.MainActivity; import app.fedilab.android.activities.WebviewActivity; import app.fedilab.android.broadcastreceiver.ToastMessage; import app.fedilab.android.client.entities.Account; +import app.fedilab.android.client.entities.QuickLoad; +import app.fedilab.android.client.entities.StatusCache; import app.fedilab.android.client.mastodon.entities.Attachment; import app.fedilab.android.exception.DBException; import app.fedilab.android.sqlite.Sqlite; @@ -1518,9 +1520,11 @@ public class Helper { deleteDir(dir); } if (clean_all.isChecked()) { - + new QuickLoad(contextReference.get()).deleteForAllAccount(); + new StatusCache(contextReference.get()).deleteForAllAccount(); } else { - + new QuickLoad(contextReference.get()).deleteForAccount(MainActivity.accountWeakReference.get()); + new StatusCache(contextReference.get()).deleteForAccount(MainActivity.accountWeakReference.get()); } Handler mainHandler2 = new Handler(Looper.getMainLooper()); Runnable myRunnable2 = () -> { 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 8b96b1dcd..67d34c0fa 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 @@ -407,15 +407,15 @@ public class FragmentMastodonTimeline extends Fragment { @Override public void onPause() { - /* if (mLayoutManager != null) { + if (mLayoutManager != null) { int position = mLayoutManager.findFirstVisibleItemPosition(); new Thread(() -> { try { - new QuickLoad(requireActivity()).storeTimeline(position, timelineType, statuses, ident); + new QuickLoad(requireActivity()).storeTimeline(position, user_id, instance, timelineType, statuses, ident); } catch (Exception ignored) { } }).start(); - }*/ + } storeMarker(); super.onPause(); }