record cache work

This commit is contained in:
Thomas 2023-02-03 17:23:33 +01:00
parent 6e4bb95dda
commit 2b300ceae4
10 changed files with 166 additions and 11 deletions

View file

@ -233,6 +233,10 @@ public class HashTagActivity extends BaseActivity {
tagTimeline.any.add(stripTag.trim());
pinnedTimeline.tagTimeline = tagTimeline;
pinned.pinnedTimelines.add(pinnedTimeline);
if (pinned.instance == null || pinned.user_id == null) {
pinned.instance = MainActivity.currentInstance;
pinned.user_id = MainActivity.currentUserID;
}
if (update) {
new Pinned(HashTagActivity.this).updatePinned(pinned);
} else {

View file

@ -836,6 +836,10 @@ public class ProfileActivity extends BaseActivity {
pinnedTimeline.type = Timeline.TimeLineEnum.REMOTE;
pinnedTimeline.position = pinned.pinnedTimelines.size();
pinned.pinnedTimelines.add(pinnedTimeline);
if (pinned.instance == null || pinned.user_id == null) {
pinned.instance = MainActivity.currentInstance;
pinned.user_id = MainActivity.currentUserID;
}
Pinned finalPinned = pinned;
boolean finalPresent = present;
new Thread(() -> {

View file

@ -44,6 +44,7 @@ import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.databinding.ActivityReorderTabsBinding;
import app.fedilab.android.databinding.PopupSearchInstanceBinding;
import app.fedilab.android.mastodon.client.entities.app.BottomMenu;
@ -261,7 +262,10 @@ public class ReorderTimelinesActivity extends BaseBarActivity implements OnStart
pinnedTimeline.type = Timeline.TimeLineEnum.REMOTE;
pinnedTimeline.position = pinned.pinnedTimelines.size();
pinned.pinnedTimelines.add(pinnedTimeline);
if (pinned.user_id == null || pinned.instance == null) {
pinned.user_id = MainActivity.currentUserID;
pinned.instance = MainActivity.currentInstance;
}
if (update) {
try {
new Pinned(ReorderTimelinesActivity.this).updatePinned(pinned);

View file

@ -166,8 +166,8 @@ public class BottomMenu implements Serializable {
throw new DBException("db is null. Wrong initialization.");
}
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, BaseMainActivity.currentInstance);
values.put(Sqlite.COL_USER_ID, BaseMainActivity.currentUserID);
values.put(Sqlite.COL_INSTANCE, bottomMenu.instance);
values.put(Sqlite.COL_USER_ID, bottomMenu.user_id);
values.put(Sqlite.COL_BOTTOM_MENU, menuItemListToStringStorage(bottomMenu.bottom_menu));
//Inserts bottom
try {

View file

@ -0,0 +1,86 @@
package app.fedilab.android.mastodon.client.entities.app;
/* Copyright 2023 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.Date;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.helper.Helper;
import app.fedilab.android.sqlite.Sqlite;
public class HomeFetchLog implements Serializable {
private final SQLiteDatabase db;
@SerializedName("id")
public long id = -1;
@SerializedName("instance")
public String instance;
@SerializedName("user_id")
public String user_id;
@SerializedName("fetched_count")
public int fetched_count;
@SerializedName("inserted")
public int inserted;
@SerializedName("updated")
public int updated;
@SerializedName("failed")
public int failed;
@SerializedName("frequency")
public int frequency;
@SerializedName("created_at")
public Date created_ad;
private Context context;
public HomeFetchLog() {
db = null;
}
public HomeFetchLog(Context context) {
//Creation of the DB with tables
this.context = context;
this.db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
}
public long insert(HomeFetchLog homeFetchLog) throws DBException {
if (db == null) {
throw new DBException("db is null. Wrong initialization.");
}
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, homeFetchLog.instance);
values.put(Sqlite.COL_USER_ID, homeFetchLog.user_id);
values.put(Sqlite.COL_FETCHED_COUNT, homeFetchLog.fetched_count);
values.put(Sqlite.COL_FAILED, homeFetchLog.failed);
values.put(Sqlite.COL_INSERTED, homeFetchLog.inserted);
values.put(Sqlite.COL_UPDATED, homeFetchLog.updated);
values.put(Sqlite.COL_FREQUENCY, homeFetchLog.frequency);
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(new Date()));
//Inserts logs
try {
return db.insertOrThrow(Sqlite.TABLE_HOME_FETCH_LOGS, null, values);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}

View file

@ -28,7 +28,6 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.mastodon.exception.DBException;
import app.fedilab.android.mastodon.helper.Helper;
import app.fedilab.android.sqlite.Sqlite;
@ -104,8 +103,8 @@ public class Pinned implements Serializable {
throw new DBException("db is null. Wrong initialization.");
}
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, BaseMainActivity.currentInstance);
values.put(Sqlite.COL_USER_ID, BaseMainActivity.currentUserID);
values.put(Sqlite.COL_INSTANCE, pinned.instance);
values.put(Sqlite.COL_USER_ID, pinned.user_id);
values.put(Sqlite.COL_PINNED_TIMELINES, mastodonPinnedTimelinesToStringStorage(pinned.pinnedTimelines));
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(new Date()));
//Inserts pinned

View file

@ -277,6 +277,7 @@ public class PinnedTimelineHelper {
pinnedTimeline.position = pinnedAll.pinnedTimelines.size();
pinnedTimeline.mastodonList = mastodonList;
pinnedAll.pinnedTimelines.add(pinnedTimeline);
try {
boolean exist = new Pinned(activity).pinnedExist(pinnedAll);
if (exist) {

View file

@ -46,6 +46,7 @@ import app.fedilab.android.mastodon.client.entities.api.Pagination;
import app.fedilab.android.mastodon.client.entities.api.Status;
import app.fedilab.android.mastodon.client.entities.app.Account;
import app.fedilab.android.mastodon.client.entities.app.BaseAccount;
import app.fedilab.android.mastodon.client.entities.app.HomeFetchLog;
import app.fedilab.android.mastodon.client.entities.app.StatusCache;
import app.fedilab.android.mastodon.client.entities.app.Timeline;
import app.fedilab.android.mastodon.exception.DBException;
@ -163,6 +164,10 @@ public class FetchHomeWorker extends Worker {
int call = 0;
String max_id = null;
MastodonTimelinesService mastodonTimelinesService = init(account.instance);
int inserted = 0;
int updated = 0;
int failed = 0;
int count = 0;
while (canContinue && call < max_calls) {
Call<List<Status>> homeCall = mastodonTimelinesService.getHome(account.token, account.instance, max_id, null, status_per_page, null);
if (homeCall != null) {
@ -170,6 +175,7 @@ public class FetchHomeWorker extends Worker {
if (homeResponse.isSuccessful()) {
List<Status> statusList = homeResponse.body();
if (statusList != null && statusList.size() > 0) {
count += statusList.size();
for (Status status : statusList) {
StatusCache statusCacheDAO = new StatusCache(getApplicationContext());
StatusCache statusCache = new StatusCache();
@ -179,9 +185,16 @@ public class FetchHomeWorker extends Worker {
statusCache.type = Timeline.TimeLineEnum.HOME;
statusCache.status_id = status.id;
try {
statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue());
int val = statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue());
if (val == 1) {
inserted++;
}
if (val == 0) {
updated++;
}
} catch (DBException e) {
e.printStackTrace();
failed++;
}
}
Pagination pagination = MastodonHelper.getPagination(homeResponse.headers());
@ -205,7 +218,20 @@ public class FetchHomeWorker extends Worker {
}
call++;
}
HomeFetchLog homeFetchLog = new HomeFetchLog();
homeFetchLog.user_id = account.user_id;
homeFetchLog.instance = account.instance;
String frequency = prefs.getString(context.getString(R.string.SET_FETCH_HOME_DELAY_VALUE) + account.user_id + account.instance, "60");
homeFetchLog.frequency = Integer.parseInt(frequency);
homeFetchLog.fetched_count = count;
homeFetchLog.inserted = inserted;
homeFetchLog.updated = updated;
homeFetchLog.failed = failed;
try {
new HomeFetchLog(context).insert(homeFetchLog);
} catch (DBException e) {
e.printStackTrace();
}
}
}

View file

@ -28,6 +28,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.databinding.DrawerReorderBinding;
import app.fedilab.android.mastodon.activities.ReorderTimelinesActivity;
import app.fedilab.android.mastodon.client.entities.app.BottomMenu;
@ -100,6 +101,8 @@ public class ReorderBottomMenuAdapter extends RecyclerView.Adapter<RecyclerView.
holder.binding.hide.setOnClickListener(v -> {
bottomMenu.bottom_menu.get(position).visible = !bottomMenu.bottom_menu.get(position).visible;
bottomMenu.user_id = MainActivity.currentUserID;
bottomMenu.instance = MainActivity.currentInstance;
if (bottomMenu.bottom_menu.get(position).visible) {
holder.binding.hide.setImageResource(R.drawable.ic_baseline_visibility_24);
} else {

View file

@ -23,7 +23,7 @@ import android.database.sqlite.SQLiteOpenHelper;
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 9;
public static final int DB_VERSION = 10;
public static final String DB_NAME = "fedilab_db";
//Table of owned accounts
@ -93,6 +93,15 @@ public class Sqlite extends SQLiteOpenHelper {
public static final String COL_ABOUT = "ABOUT";
public static final String COL_USER_INSTANCE = "USER_INSTANCE";
//Home fetch logs
public static final String TABLE_HOME_FETCH_LOGS = "TABLE_HOME_FETCH_LOGS";
public static final String COL_INSERTED = "INSERTED";
public static final String COL_UPDATED = "UPDATED";
public static final String COL_FAILED = "FAILED";
public static final String COL_FREQUENCY = "FREQUENCY";
public static final String COL_FETCHED_COUNT = "FETCHED_COUNT";
private static final String CREATE_TABLE_USER_ACCOUNT = "CREATE TABLE " + TABLE_USER_ACCOUNT + " ("
+ COL_USER_ID + " TEXT NOT NULL, "
+ COL_INSTANCE + " TEXT NOT NULL, "
@ -192,8 +201,8 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_USER_ID + " TEXT NOT NULL, "
+ COL_TYPE + " TEXT NOT NULL, "
+ COL_MUTED_ACCOUNTS + " TEXT)";
public static SQLiteDatabase db;
private static Sqlite sInstance;
private final String CREATE_TABLE_STORED_INSTANCES = "CREATE TABLE "
+ TABLE_BOOKMARKED_INSTANCES + "("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
@ -202,6 +211,22 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_ABOUT + " TEXT NOT NULL, "
+ COL_USER_INSTANCE + " TEXT NOT NULL)";
private static final String CREATE_TABLE_HOME_FETCH_LOGS = "CREATE TABLE IF NOT EXISTS " + TABLE_HOME_FETCH_LOGS + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_INSTANCE + " TEXT NOT NULL, "
+ COL_USER_ID + " TEXT NOT NULL, "
+ COL_FETCHED_COUNT + " INTEGER NOT NULL DEFAULT 0, "
+ COL_INSERTED + " INTEGER NOT NULL DEFAULT 0, "
+ COL_UPDATED + " INTEGER NOT NULL DEFAULT 0, "
+ COL_FAILED + " INTEGER NOT NULL DEFAULT 0, "
+ COL_FREQUENCY + " INTEGER NOT NULL DEFAULT 0, "
+ COL_CREATED_AT + " TEXT NOT NULL)";
public static SQLiteDatabase db;
private static Sqlite sInstance;
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@ -229,6 +254,7 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL(CREATE_DOMAINS_TRACKING);
db.execSQL(CREATE_TABLE_MUTED);
db.execSQL(CREATE_TABLE_STORED_INSTANCES);
db.execSQL(CREATE_TABLE_HOME_FETCH_LOGS);
}
@Override
@ -257,6 +283,8 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL(CREATE_TABLE_MUTED);
case 8:
db.execSQL(CREATE_TABLE_STORED_INSTANCES);
case 9:
db.execSQL(CREATE_TABLE_HOME_FETCH_LOGS);
default:
break;
}