From d57ed18f7ce41ba5fcaef44119f7f29420bd3df3 Mon Sep 17 00:00:00 2001 From: nuclearfog Date: Tue, 6 Feb 2024 21:55:54 +0100 Subject: [PATCH] database fix --- .../twidda/backend/async/StatusLoader.java | 23 ++++++++-- .../twidda/database/AppDatabase.java | 46 ++++++++++++++++++- .../twidda/database/DatabaseAdapter.java | 5 ++ .../twidda/ui/fragments/StatusFragment.java | 2 +- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/async/StatusLoader.java b/app/src/main/java/org/nuclearfog/twidda/backend/async/StatusLoader.java index b55eabdda..12ccddd90 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/async/StatusLoader.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/async/StatusLoader.java @@ -52,22 +52,35 @@ protected Result doInBackground(@NonNull Param param) { return new Result(statuses, param.pos, null); case Param.USER: - case Param.USER_ALL: - boolean withReplies = param.type == Param.USER_ALL; if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) { statuses = db.getUserTimeline(param.id); if (statuses.isEmpty()) { - statuses = connection.getUserTimeline(param.id, 0L, 0L, withReplies); + statuses = connection.getUserTimeline(param.id, 0L, 0L, false); db.saveUserTimeline(statuses); } } else { - statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, withReplies); + statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, false); if (param.maxId == Param.NO_ID) { db.saveUserTimeline(statuses); } } return new Result(statuses, param.pos, null); + case Param.USER_REPLIES: + if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) { + statuses = db.getUserReplies(param.id); + if (statuses.isEmpty()) { + statuses = connection.getUserTimeline(param.id, 0L, 0L, true); + db.saveUserReplies(statuses); + } + } else { + statuses = connection.getUserTimeline(param.id, param.minId, param.maxId, true); + if (param.maxId == Param.NO_ID) { + db.saveUserReplies(statuses); + } + } + return new Result(statuses, param.pos, null); + case Param.FAVORIT: if (param.minId == Param.NO_ID && param.maxId == Param.NO_ID) { statuses = db.getUserFavorites(param.id); @@ -150,7 +163,7 @@ public static class Param { public static final int HOME = 1; public static final int USER = 2; - public static final int USER_ALL = 3; + public static final int USER_REPLIES = 3; public static final int FAVORIT = 4; public static final int REPLIES = 5; public static final int REPLIES_LOCAL = 6; diff --git a/app/src/main/java/org/nuclearfog/twidda/database/AppDatabase.java b/app/src/main/java/org/nuclearfog/twidda/database/AppDatabase.java index 4c878ad6a..749131c2e 100644 --- a/app/src/main/java/org/nuclearfog/twidda/database/AppDatabase.java +++ b/app/src/main/java/org/nuclearfog/twidda/database/AppDatabase.java @@ -107,7 +107,7 @@ public class AppDatabase { + " LIMIT ?;"; /** - * SQL query to get status of an user + * SQL query to get timeline of an user */ private static final String USER_STATUS_QUERY = "SELECT * FROM(" + STATUS_SUBQUERY + ")" + " WHERE " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.FLAGS + "&" + StatusPropertiesTable.MASK_STATUS_USER_TIMELINE + " IS NOT 0" @@ -117,6 +117,17 @@ public class AppDatabase { + " ORDER BY " + StatusTable.ID + " DESC" + " LIMIT ?;"; + /** + * SQL query to get timeline of an user with replies + */ + private static final String USER_REPLY_QUERY = "SELECT * FROM(" + STATUS_SUBQUERY + ")" + + " WHERE " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.FLAGS + "&" + StatusPropertiesTable.MASK_STATUS_USER_REPLY + " IS NOT 0" + + " AND " + StatusPropertiesTable.TABLE + "." + StatusPropertiesTable.OWNER + "=?" + + " AND " + UserPropertiesTable.TABLE + "." + UserPropertiesTable.OWNER + "=?" + + " AND " + StatusTable.TABLE + "." + StatusTable.USER + "=?" + + " ORDER BY " + StatusTable.ID + " DESC" + + " LIMIT ?;"; + /** * SQL query to get status favored by an user */ @@ -359,6 +370,22 @@ public void saveUserTimeline(Statuses statuses) { } } + /** + * save user reply timeline + * + * @param statuses user timeline + */ + public void saveUserReplies(Statuses statuses) { + synchronized (adapter) { + if (!statuses.isEmpty()) { + SQLiteDatabase db = adapter.getDbWrite(); + for (Status status : statuses) + saveStatus(status, db, StatusPropertiesTable.MASK_STATUS_USER_REPLY); + adapter.commit(); + } + } + } + /** * save user favorite timeline * @@ -693,6 +720,23 @@ public Statuses getUserTimeline(long userID) { } } + /** + * load user reply timeline + * + * @param userId user ID + * @return user timeline + */ + public Statuses getUserReplies(long userId) { + synchronized (adapter) { + String homeStr = Long.toString(settings.getLogin().getId()); + String[] args = {homeStr, homeStr, Long.toString(userId), Integer.toString(settings.getListSize())}; + + SQLiteDatabase db = adapter.getDbRead(); + Cursor cursor = db.rawQuery(USER_REPLY_QUERY, args); + return getStatuses(cursor, db); + } + } + /** * load favorite timeline * diff --git a/app/src/main/java/org/nuclearfog/twidda/database/DatabaseAdapter.java b/app/src/main/java/org/nuclearfog/twidda/database/DatabaseAdapter.java index 18e9e35db..e0d381da9 100644 --- a/app/src/main/java/org/nuclearfog/twidda/database/DatabaseAdapter.java +++ b/app/src/main/java/org/nuclearfog/twidda/database/DatabaseAdapter.java @@ -954,6 +954,11 @@ public interface StatusPropertiesTable { * status is pinned to profile */ int MASK_STATUS_PINNED = 1 << 13; + + /** + * status is from an user timeline (replies included) + */ + int MASK_STATUS_USER_REPLY = 1 << 14; } /** diff --git a/app/src/main/java/org/nuclearfog/twidda/ui/fragments/StatusFragment.java b/app/src/main/java/org/nuclearfog/twidda/ui/fragments/StatusFragment.java index c835d9616..ec6f0aa65 100644 --- a/app/src/main/java/org/nuclearfog/twidda/ui/fragments/StatusFragment.java +++ b/app/src/main/java/org/nuclearfog/twidda/ui/fragments/StatusFragment.java @@ -276,7 +276,7 @@ private void load(long sinceId, long maxId, int index) { break; case MODE_USER_ALL: - request = new StatusLoader.Param(StatusLoader.Param.USER_ALL, id, sinceId, maxId, index, search); + request = new StatusLoader.Param(StatusLoader.Param.USER_REPLIES, id, sinceId, maxId, index, search); break; case MODE_FAVORIT: