diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java index 335d00914..f21f9e622 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/CacheController.java @@ -36,7 +36,7 @@ import me.grishka.appkit.utils.WorkerThread; public class CacheController{ private static final String TAG="CacheController"; - private static final int DB_VERSION=3; + private static final int DB_VERSION=4; private static final WorkerThread databaseThread=new WorkerThread("databaseThread"); private static final Handler uiHandler=new Handler(Looper.getMainLooper()); @@ -61,7 +61,7 @@ public class CacheController{ List filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.HOME)).collect(Collectors.toList()); if(!forceReload){ SQLiteDatabase db=getOrOpenDatabase(); - try(Cursor cursor=db.query("home_timeline", new String[]{"json", "flags"}, maxID==null ? null : "`id` result=new ArrayList<>(); cursor.moveToFirst(); @@ -112,7 +112,7 @@ public class CacheController{ runOnDbThread((db)->{ if(clear) db.delete("home_timeline", null, null); - ContentValues values=new ContentValues(3); + ContentValues values=new ContentValues(4); for(Status s:posts){ values.put("id", s.id); values.put("json", MastodonAPIController.gson.toJson(s)); @@ -120,6 +120,7 @@ public class CacheController{ if(s.hasGapAfter) flags|=POST_FLAG_GAP_AFTER; values.put("flags", flags); + values.put("time", s.createdAt.getEpochSecond()); db.insertWithOnConflict("home_timeline", null, values, SQLiteDatabase.CONFLICT_REPLACE); } }); @@ -134,7 +135,7 @@ public class CacheController{ if(!forceReload){ SQLiteDatabase db=getOrOpenDatabase(); String table=onlyPosts ? "notifications_posts" : onlyMentions ? "notifications_mentions" : "notifications_all"; - try(Cursor cursor=db.query(table, new String[]{"json"}, maxID==null ? null : "`id` result=new ArrayList<>(); cursor.moveToFirst(); @@ -192,7 +193,7 @@ public class CacheController{ String table=onlyPosts ? "notifications_posts" : onlyMentions ? "notifications_mentions" : "notifications_all"; if(clear) db.delete(table, null, null); - ContentValues values=new ContentValues(3); + ContentValues values=new ContentValues(4); for(Notification n:notifications){ if(n.type==null){ continue; @@ -200,6 +201,7 @@ public class CacheController{ values.put("id", n.id); values.put("json", MastodonAPIController.gson.toJson(n)); values.put("type", n.type.ordinal()); + values.put("time", n.createdAt.getEpochSecond()); db.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE); } }); @@ -296,21 +298,24 @@ public class CacheController{ CREATE TABLE `home_timeline` ( `id` VARCHAR(25) NOT NULL PRIMARY KEY, `json` TEXT NOT NULL, - `flags` INTEGER NOT NULL DEFAULT 0 + `flags` INTEGER NOT NULL DEFAULT 0, + `time` INTEGER NOT NULL )"""); db.execSQL(""" CREATE TABLE `notifications_all` ( `id` VARCHAR(25) NOT NULL PRIMARY KEY, `json` TEXT NOT NULL, `flags` INTEGER NOT NULL DEFAULT 0, - `type` INTEGER NOT NULL + `type` INTEGER NOT NULL, + `time` INTEGER NOT NULL )"""); db.execSQL(""" CREATE TABLE `notifications_mentions` ( `id` VARCHAR(25) NOT NULL PRIMARY KEY, `json` TEXT NOT NULL, `flags` INTEGER NOT NULL DEFAULT 0, - `type` INTEGER NOT NULL + `type` INTEGER NOT NULL, + `time` INTEGER NOT NULL )"""); createRecentSearchesTable(db); createPostsNotificationsTable(db); @@ -318,12 +323,16 @@ public class CacheController{ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ - if(oldVersion==1){ + if(oldVersion<2){ createRecentSearchesTable(db); } - if(oldVersion==2){ + if(oldVersion<3){ + // MEGALODON-SPECIFIC createPostsNotificationsTable(db); } + if(oldVersion<4){ + addTimeColumns(db); + } } private void createRecentSearchesTable(SQLiteDatabase db){ @@ -344,6 +353,15 @@ public class CacheController{ `type` INTEGER NOT NULL )"""); } + + private void addTimeColumns(SQLiteDatabase db){ + db.execSQL("DELETE FROM `home_timeline`"); + db.execSQL("DELETE FROM `notifications_all`"); + db.execSQL("DELETE FROM `notifications_mentions`"); + db.execSQL("ALTER TABLE `home_timeline` ADD `time` INTEGER NOT NULL DEFAULT 0"); + db.execSQL("ALTER TABLE `notifications_all` ADD `time` INTEGER NOT NULL DEFAULT 0"); + db.execSQL("ALTER TABLE `notifications_mentions` ADD `time` INTEGER NOT NULL DEFAULT 0"); + } } @FunctionalInterface