This commit is contained in:
Grishka 2023-05-23 12:56:49 +03:00
parent 36f1a557d7
commit 1dcb5717ea
1 changed files with 26 additions and 9 deletions

View File

@ -35,7 +35,7 @@ import me.grishka.appkit.utils.WorkerThread;
public class CacheController{ public class CacheController{
private static final String TAG="CacheController"; private static final String TAG="CacheController";
private static final int DB_VERSION=2; private static final int DB_VERSION=3;
private static final WorkerThread databaseThread=new WorkerThread("databaseThread"); private static final WorkerThread databaseThread=new WorkerThread("databaseThread");
private static final Handler uiHandler=new Handler(Looper.getMainLooper()); private static final Handler uiHandler=new Handler(Looper.getMainLooper());
@ -60,7 +60,7 @@ public class CacheController{
List<Filter> filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.HOME)).collect(Collectors.toList()); List<Filter> filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.HOME)).collect(Collectors.toList());
if(!forceReload){ if(!forceReload){
SQLiteDatabase db=getOrOpenDatabase(); SQLiteDatabase db=getOrOpenDatabase();
try(Cursor cursor=db.query("home_timeline", new String[]{"json", "flags"}, maxID==null ? null : "`id`<?", maxID==null ? null : new String[]{maxID}, null, null, "`id` DESC", count+"")){ try(Cursor cursor=db.query("home_timeline", new String[]{"json", "flags"}, maxID==null ? null : "`id`<?", maxID==null ? null : new String[]{maxID}, null, null, "`time` DESC", count+"")){
if(cursor.getCount()==count){ if(cursor.getCount()==count){
ArrayList<Status> result=new ArrayList<>(); ArrayList<Status> result=new ArrayList<>();
cursor.moveToFirst(); cursor.moveToFirst();
@ -113,7 +113,7 @@ public class CacheController{
runOnDbThread((db)->{ runOnDbThread((db)->{
if(clear) if(clear)
db.delete("home_timeline", null, null); db.delete("home_timeline", null, null);
ContentValues values=new ContentValues(3); ContentValues values=new ContentValues(4);
for(Status s:posts){ for(Status s:posts){
values.put("id", s.id); values.put("id", s.id);
values.put("json", MastodonAPIController.gson.toJson(s)); values.put("json", MastodonAPIController.gson.toJson(s));
@ -121,6 +121,7 @@ public class CacheController{
if(s.hasGapAfter) if(s.hasGapAfter)
flags|=POST_FLAG_GAP_AFTER; flags|=POST_FLAG_GAP_AFTER;
values.put("flags", flags); values.put("flags", flags);
values.put("time", s.createdAt.getEpochSecond());
db.insertWithOnConflict("home_timeline", null, values, SQLiteDatabase.CONFLICT_REPLACE); db.insertWithOnConflict("home_timeline", null, values, SQLiteDatabase.CONFLICT_REPLACE);
} }
}); });
@ -133,7 +134,7 @@ public class CacheController{
List<Filter> filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.NOTIFICATIONS)).collect(Collectors.toList()); List<Filter> filters=AccountSessionManager.getInstance().getAccount(accountID).wordFilters.stream().filter(f->f.context.contains(Filter.FilterContext.NOTIFICATIONS)).collect(Collectors.toList());
if(!forceReload){ if(!forceReload){
SQLiteDatabase db=getOrOpenDatabase(); SQLiteDatabase db=getOrOpenDatabase();
try(Cursor cursor=db.query(onlyMentions ? "notifications_mentions" : "notifications_all", new String[]{"json"}, maxID==null ? null : "`id`<?", maxID==null ? null : new String[]{maxID}, null, null, "`id` DESC", count+"")){ try(Cursor cursor=db.query(onlyMentions ? "notifications_mentions" : "notifications_all", new String[]{"json"}, maxID==null ? null : "`id`<?", maxID==null ? null : new String[]{maxID}, null, null, "`time` DESC", count+"")){
if(cursor.getCount()==count){ if(cursor.getCount()==count){
ArrayList<Notification> result=new ArrayList<>(); ArrayList<Notification> result=new ArrayList<>();
cursor.moveToFirst(); cursor.moveToFirst();
@ -196,7 +197,7 @@ public class CacheController{
String table=onlyMentions ? "notifications_mentions" : "notifications_all"; String table=onlyMentions ? "notifications_mentions" : "notifications_all";
if(clear) if(clear)
db.delete(table, null, null); db.delete(table, null, null);
ContentValues values=new ContentValues(3); ContentValues values=new ContentValues(4);
for(Notification n:notifications){ for(Notification n:notifications){
if(n.type==null){ if(n.type==null){
continue; continue;
@ -204,6 +205,7 @@ public class CacheController{
values.put("id", n.id); values.put("id", n.id);
values.put("json", MastodonAPIController.gson.toJson(n)); values.put("json", MastodonAPIController.gson.toJson(n));
values.put("type", n.type.ordinal()); values.put("type", n.type.ordinal());
values.put("time", n.createdAt.getEpochSecond());
db.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE); db.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE);
} }
}); });
@ -300,30 +302,36 @@ public class CacheController{
CREATE TABLE `home_timeline` ( CREATE TABLE `home_timeline` (
`id` VARCHAR(25) NOT NULL PRIMARY KEY, `id` VARCHAR(25) NOT NULL PRIMARY KEY,
`json` TEXT NOT NULL, `json` TEXT NOT NULL,
`flags` INTEGER NOT NULL DEFAULT 0 `flags` INTEGER NOT NULL DEFAULT 0,
`time` INTEGER NOT NULL
)"""); )""");
db.execSQL(""" db.execSQL("""
CREATE TABLE `notifications_all` ( CREATE TABLE `notifications_all` (
`id` VARCHAR(25) NOT NULL PRIMARY KEY, `id` VARCHAR(25) NOT NULL PRIMARY KEY,
`json` TEXT NOT NULL, `json` TEXT NOT NULL,
`flags` INTEGER NOT NULL DEFAULT 0, `flags` INTEGER NOT NULL DEFAULT 0,
`type` INTEGER NOT NULL `type` INTEGER NOT NULL,
`time` INTEGER NOT NULL
)"""); )""");
db.execSQL(""" db.execSQL("""
CREATE TABLE `notifications_mentions` ( CREATE TABLE `notifications_mentions` (
`id` VARCHAR(25) NOT NULL PRIMARY KEY, `id` VARCHAR(25) NOT NULL PRIMARY KEY,
`json` TEXT NOT NULL, `json` TEXT NOT NULL,
`flags` INTEGER NOT NULL DEFAULT 0, `flags` INTEGER NOT NULL DEFAULT 0,
`type` INTEGER NOT NULL `type` INTEGER NOT NULL,
`time` INTEGER NOT NULL
)"""); )""");
createRecentSearchesTable(db); createRecentSearchesTable(db);
} }
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
if(oldVersion==1){ if(oldVersion<2){
createRecentSearchesTable(db); createRecentSearchesTable(db);
} }
if(oldVersion<3){
addTimeColumns(db);
}
} }
private void createRecentSearchesTable(SQLiteDatabase db){ private void createRecentSearchesTable(SQLiteDatabase db){
@ -334,6 +342,15 @@ public class CacheController{
`time` INTEGER NOT NULL `time` 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 @FunctionalInterface