Add favorite entry alternative handling, allowing parennt versions to take priority for favorites sync

This commit is contained in:
Jobobby04
2023-05-12 22:50:23 -04:00
parent e9a3463455
commit 282a0c4e16
12 changed files with 333 additions and 34 deletions
@@ -2,13 +2,14 @@ package tachiyomi.data.manga
import tachiyomi.domain.manga.model.FavoriteEntry
val favoriteEntryMapper: (Long, String, String, String, Long) -> FavoriteEntry =
{ id, title, gid, token, category ->
val favoriteEntryMapper: (String, String, String, Long, String?, String?) -> FavoriteEntry =
{ gid, token, title, category, otherGid, otherToken ->
FavoriteEntry(
id = id,
title = title,
gid = gid,
token = token,
title = title,
category = category.toInt(),
otherGid = otherGid,
otherToken = otherToken,
)
}
@@ -2,6 +2,7 @@ package tachiyomi.data.manga
import tachiyomi.data.DatabaseHandler
import tachiyomi.domain.manga.model.FavoriteEntry
import tachiyomi.domain.manga.model.FavoriteEntryAlternative
import tachiyomi.domain.manga.repository.FavoritesEntryRepository
class FavoritesEntryRepositoryImpl(
@@ -14,7 +15,12 @@ class FavoritesEntryRepositoryImpl(
override suspend fun insertAll(favoriteEntries: List<FavoriteEntry>) {
handler.await(true) {
favoriteEntries.forEach {
eh_favoritesQueries.insertEhFavorites(it.id, it.title, it.gid, it.token, it.category.toLong())
eh_favoritesQueries.insertEhFavorites(
title = it.title,
gid = it.gid,
token = it.token,
category = it.category.toLong(),
)
}
}
}
@@ -22,4 +28,15 @@ class FavoritesEntryRepositoryImpl(
override suspend fun selectAll(): List<FavoriteEntry> {
return handler.awaitList { eh_favoritesQueries.selectAll(favoriteEntryMapper) }
}
override suspend fun addAlternative(favoriteEntryAlternative: FavoriteEntryAlternative) {
handler.await {
eh_favoritesQueries.addAlternative(
otherGid = favoriteEntryAlternative.otherGid,
otherToken = favoriteEntryAlternative.otherToken,
gid = favoriteEntryAlternative.gid,
token = favoriteEntryAlternative.token,
)
}
}
}
@@ -1,16 +1,34 @@
CREATE TABLE eh_favorites (
_id INTEGER NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
gid TEXT NOT NULL,
token TEXT NOT NULL,
category INTEGER NOT NULL
title TEXT NOT NULL,
category INTEGER NOT NULL,
PRIMARY KEY (gid, token)
);
CREATE TABLE eh_favorites_alternatives (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gid TEXT NOT NULL,
token TEXT NOT NULL,
otherGid TEXT NOT NULL,
otherToken TEXT NOT NULL,
FOREIGN KEY (gid, token) REFERENCES eh_favorites(gid, token)
);
CREATE INDEX eh_favorites_alternatives_gid_token_index ON eh_favorites_alternatives(gid, token);
CREATE INDEX eh_favorites_alternatives_other_gid_token_index ON eh_favorites_alternatives(otherGid, otherToken);
selectAll:
SELECT * FROM eh_favorites;
SELECT f.gid, f.token, f.title, f.category, a.otherGid, a.otherToken
FROM eh_favorites AS f
LEFT JOIN eh_favorites_alternatives AS a ON f.gid = a.gid AND f.token = a.token;
insertEhFavorites:
INSERT INTO eh_favorites (_id, title, gid, token, category) VALUES (?, ?, ?, ?, ?);
INSERT INTO eh_favorites (title, gid, token, category) VALUES (?, ?, ?, ?);
deleteAll:
DELETE FROM eh_favorites;
DELETE FROM eh_favorites;
addAlternative:
INSERT INTO eh_favorites_alternatives (gid, token, otherGid, otherToken)
VALUES (:gid, :token, :otherGid, :otherToken);
@@ -0,0 +1,25 @@
ALTER TABLE eh_favorites RENAME TO eh_favorites_temp;
CREATE TABLE eh_favorites (
gid TEXT NOT NULL,
token TEXT NOT NULL,
title TEXT NOT NULL,
category INTEGER NOT NULL,
PRIMARY KEY (gid, token)
);
INSERT INTO eh_favorites
SELECT gid, token, title, category
FROM eh_favorites_temp;
DROP TABLE IF EXISTS eh_favorites_temp;
CREATE TABLE eh_favorites_alternatives (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gid TEXT NOT NULL,
token TEXT NOT NULL,
otherGid TEXT NOT NULL,
otherToken TEXT NOT NULL,
FOREIGN KEY (gid, token) REFERENCES eh_favorites(gid, token)
);
CREATE INDEX eh_favorites_alternatives_gid_token_index ON eh_favorites_alternatives(gid, token);
CREATE INDEX eh_favorites_alternatives_other_gid_token_index ON eh_favorites_alternatives(otherGid, otherToken);