mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-24 18:08:24 +01:00
Remove some logic around old legacy backup versions + minor optimizations
This commit is contained in:
parent
e8c35ae4e1
commit
bbee093c63
@ -53,30 +53,14 @@ import kotlin.math.max
|
|||||||
|
|
||||||
class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : AbstractBackupManager(context) {
|
class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : AbstractBackupManager(context) {
|
||||||
|
|
||||||
var parserVersion: Int = version
|
val parser: Gson = when (version) {
|
||||||
private set
|
2 -> GsonBuilder()
|
||||||
|
.registerTypeAdapter<MangaImpl>(MangaTypeAdapter.build())
|
||||||
var parser: Gson = initParser()
|
.registerTypeHierarchyAdapter<ChapterImpl>(ChapterTypeAdapter.build())
|
||||||
|
.registerTypeAdapter<CategoryImpl>(CategoryTypeAdapter.build())
|
||||||
/**
|
.registerTypeAdapter<DHistory>(HistoryTypeAdapter.build())
|
||||||
* Set version of parser
|
.registerTypeHierarchyAdapter<TrackImpl>(TrackTypeAdapter.build())
|
||||||
*
|
.create()
|
||||||
* @param version version of parser
|
|
||||||
*/
|
|
||||||
internal fun setVersion(version: Int) {
|
|
||||||
this.parserVersion = version
|
|
||||||
parser = initParser()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun initParser(): Gson = when (parserVersion) {
|
|
||||||
2 ->
|
|
||||||
GsonBuilder()
|
|
||||||
.registerTypeAdapter<MangaImpl>(MangaTypeAdapter.build())
|
|
||||||
.registerTypeHierarchyAdapter<ChapterImpl>(ChapterTypeAdapter.build())
|
|
||||||
.registerTypeAdapter<CategoryImpl>(CategoryTypeAdapter.build())
|
|
||||||
.registerTypeAdapter<DHistory>(HistoryTypeAdapter.build())
|
|
||||||
.registerTypeHierarchyAdapter<TrackImpl>(TrackTypeAdapter.build())
|
|
||||||
.create()
|
|
||||||
else -> throw Exception("Unknown backup version")
|
else -> throw Exception("Unknown backup version")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,7 +292,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
|
|||||||
*/
|
*/
|
||||||
internal fun restoreCategoriesForManga(manga: Manga, categories: List<String>) {
|
internal fun restoreCategoriesForManga(manga: Manga, categories: List<String>) {
|
||||||
val dbCategories = databaseHelper.getCategories().executeAsBlocking()
|
val dbCategories = databaseHelper.getCategories().executeAsBlocking()
|
||||||
val mangaCategoriesToUpdate = mutableListOf<MangaCategory>()
|
val mangaCategoriesToUpdate = ArrayList<MangaCategory>(categories.size)
|
||||||
for (backupCategoryStr in categories) {
|
for (backupCategoryStr in categories) {
|
||||||
for (dbCategory in dbCategories) {
|
for (dbCategory in dbCategories) {
|
||||||
if (backupCategoryStr == dbCategory.name) {
|
if (backupCategoryStr == dbCategory.name) {
|
||||||
@ -332,7 +316,7 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
|
|||||||
*/
|
*/
|
||||||
internal fun restoreHistoryForManga(history: List<DHistory>) {
|
internal fun restoreHistoryForManga(history: List<DHistory>) {
|
||||||
// List containing history to be updated
|
// List containing history to be updated
|
||||||
val historyToBeUpdated = mutableListOf<History>()
|
val historyToBeUpdated = ArrayList<History>(history.size)
|
||||||
for ((url, lastRead) in history) {
|
for ((url, lastRead) in history) {
|
||||||
val dbHistory = databaseHelper.getHistoryByChapterUrl(url).executeAsBlocking()
|
val dbHistory = databaseHelper.getHistoryByChapterUrl(url).executeAsBlocking()
|
||||||
// Check if history already in database and update
|
// Check if history already in database and update
|
||||||
@ -361,14 +345,14 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
|
|||||||
* @param tracks the track list to restore.
|
* @param tracks the track list to restore.
|
||||||
*/
|
*/
|
||||||
internal fun restoreTrackForManga(manga: Manga, tracks: List<Track>) {
|
internal fun restoreTrackForManga(manga: Manga, tracks: List<Track>) {
|
||||||
// Fix foreign keys with the current manga id
|
|
||||||
tracks.map { it.manga_id = manga.id!! }
|
|
||||||
|
|
||||||
// Get tracks from database
|
// Get tracks from database
|
||||||
val dbTracks = databaseHelper.getTracks(manga).executeAsBlocking()
|
val dbTracks = databaseHelper.getTracks(manga).executeAsBlocking()
|
||||||
val trackToUpdate = mutableListOf<Track>()
|
val trackToUpdate = ArrayList<Track>(tracks.size)
|
||||||
|
|
||||||
tracks.forEach { track ->
|
tracks.forEach { track ->
|
||||||
|
// Fix foreign keys with the current manga id
|
||||||
|
track.manga_id = manga.id!!
|
||||||
|
|
||||||
val service = trackManager.getService(track.sync_id)
|
val service = trackManager.getService(track.sync_id)
|
||||||
if (service != null && service.isLogged) {
|
if (service != null && service.isLogged) {
|
||||||
var isInDatabase = false
|
var isInDatabase = false
|
||||||
@ -423,12 +407,13 @@ class LegacyBackupManager(context: Context, version: Int = CURRENT_VERSION) : Ab
|
|||||||
chapter.copyFrom(dbChapter)
|
chapter.copyFrom(dbChapter)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Filter the chapters that couldn't be found.
|
|
||||||
chapters.filter { it.id != null }
|
|
||||||
chapters.map { it.manga_id = manga.id }
|
|
||||||
|
|
||||||
updateChapters(chapters)
|
chapter.manga_id = manga.id
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter the chapters that couldn't be found.
|
||||||
|
updateChapters(chapters.filter { it.id != null })
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ class BackupTest {
|
|||||||
fun setup() {
|
fun setup() {
|
||||||
app = RuntimeEnvironment.application
|
app = RuntimeEnvironment.application
|
||||||
context = app.applicationContext
|
context = app.applicationContext
|
||||||
legacyBackupManager = LegacyBackupManager(context)
|
legacyBackupManager = LegacyBackupManager(context, 2)
|
||||||
db = legacyBackupManager.databaseHelper
|
db = legacyBackupManager.databaseHelper
|
||||||
|
|
||||||
// Mock the source manager
|
// Mock the source manager
|
||||||
@ -84,6 +84,8 @@ class BackupTest {
|
|||||||
|
|
||||||
root.add(Backup.MANGAS, mangaEntries)
|
root.add(Backup.MANGAS, mangaEntries)
|
||||||
root.add(Backup.CATEGORIES, categoryEntries)
|
root.add(Backup.CATEGORIES, categoryEntries)
|
||||||
|
|
||||||
|
clearJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,9 +93,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testRestoreEmptyCategory() {
|
fun testRestoreEmptyCategory() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Create backup of empty database
|
// Create backup of empty database
|
||||||
legacyBackupManager.backupCategories(categoryEntries)
|
legacyBackupManager.backupCategories(categoryEntries)
|
||||||
|
|
||||||
@ -110,9 +109,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testRestoreSingleCategory() {
|
fun testRestoreSingleCategory() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Create category and add to json
|
// Create category and add to json
|
||||||
val category = addSingleCategory("category")
|
val category = addSingleCategory("category")
|
||||||
|
|
||||||
@ -130,9 +126,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testRestoreMultipleCategories() {
|
fun testRestoreMultipleCategories() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Create category and add to json
|
// Create category and add to json
|
||||||
val category = addSingleCategory("category")
|
val category = addSingleCategory("category")
|
||||||
val category2 = addSingleCategory("category2")
|
val category2 = addSingleCategory("category2")
|
||||||
@ -161,9 +154,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testRestoreManga() {
|
fun testRestoreManga() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Add manga to database
|
// Add manga to database
|
||||||
val manga = getSingleManga("One Piece")
|
val manga = getSingleManga("One Piece")
|
||||||
manga.viewer = 3
|
manga.viewer = 3
|
||||||
@ -227,9 +217,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun testRestoreChapters() {
|
fun testRestoreChapters() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Insert manga
|
// Insert manga
|
||||||
val manga = getSingleManga("One Piece")
|
val manga = getSingleManga("One Piece")
|
||||||
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
|
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
|
||||||
@ -266,9 +253,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun restoreHistoryForManga() {
|
fun restoreHistoryForManga() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
val manga = getSingleManga("One Piece")
|
val manga = getSingleManga("One Piece")
|
||||||
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
|
manga.id = legacyBackupManager.databaseHelper.insertManga(manga).executeAsBlocking().insertedId()
|
||||||
|
|
||||||
@ -300,9 +284,6 @@ class BackupTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
fun restoreTrackForManga() {
|
fun restoreTrackForManga() {
|
||||||
// Initialize json with version 2
|
|
||||||
initializeJsonTest(2)
|
|
||||||
|
|
||||||
// Create mangas
|
// Create mangas
|
||||||
val manga = getSingleManga("One Piece")
|
val manga = getSingleManga("One Piece")
|
||||||
val manga2 = getSingleManga("Bleach")
|
val manga2 = getSingleManga("Bleach")
|
||||||
@ -359,31 +340,26 @@ class BackupTest {
|
|||||||
assertThat(trackDB[0].last_chapter_read).isEqualTo(10)
|
assertThat(trackDB[0].last_chapter_read).isEqualTo(10)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearJson() {
|
private fun clearJson() {
|
||||||
root = JsonObject()
|
root = JsonObject()
|
||||||
information = JsonObject()
|
information = JsonObject()
|
||||||
mangaEntries = JsonArray()
|
mangaEntries = JsonArray()
|
||||||
categoryEntries = JsonArray()
|
categoryEntries = JsonArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun initializeJsonTest(version: Int) {
|
private fun addSingleCategory(name: String): Category {
|
||||||
clearJson()
|
|
||||||
legacyBackupManager.setVersion(version)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addSingleCategory(name: String): Category {
|
|
||||||
val category = Category.create(name)
|
val category = Category.create(name)
|
||||||
val catJson = legacyBackupManager.parser.toJsonTree(category)
|
val catJson = legacyBackupManager.parser.toJsonTree(category)
|
||||||
categoryEntries.add(catJson)
|
categoryEntries.add(catJson)
|
||||||
return category
|
return category
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearDatabase() {
|
private fun clearDatabase() {
|
||||||
db.deleteMangas().executeAsBlocking()
|
db.deleteMangas().executeAsBlocking()
|
||||||
db.deleteHistory().executeAsBlocking()
|
db.deleteHistory().executeAsBlocking()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getSingleHistory(chapter: Chapter): DHistory {
|
private fun getSingleHistory(chapter: Chapter): DHistory {
|
||||||
return DHistory(chapter.url, 1000)
|
return DHistory(chapter.url, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user