Support manga sources in backups

This commit is contained in:
Koitharu
2023-12-13 12:16:06 +02:00
parent bb2294f248
commit db3db4637c
8 changed files with 59 additions and 8 deletions

View File

@@ -15,5 +15,6 @@ class BackupEntry(
const val FAVOURITES = "favourites"
const val SETTINGS = "settings"
const val BOOKMARKS = "bookmarks"
const val SOURCES = "sources"
}
}

View File

@@ -101,6 +101,16 @@ class BackupRepository @Inject constructor(
return entry
}
suspend fun dumpSources(): BackupEntry {
val entry = BackupEntry(BackupEntry.SOURCES, JSONArray())
val all = db.getSourcesDao().findAll()
for (source in all) {
val json = JsonSerializer(source).toJson()
entry.data.put(json)
}
return entry
}
fun createIndex(): BackupEntry {
val entry = BackupEntry(BackupEntry.INDEX, JSONArray())
val json = JSONObject()
@@ -184,6 +194,17 @@ class BackupRepository @Inject constructor(
return result
}
suspend fun restoreSources(entry: BackupEntry): CompositeResult {
val result = CompositeResult()
for (item in entry.data.JSONIterator()) {
val source = JsonDeserializer(item).toMangaSourceEntity()
result += runCatchingCancellable {
db.getSourcesDao().upsert(source)
}
}
return result
}
fun restoreSettings(entry: BackupEntry): CompositeResult {
val result = CompositeResult()
for (item in entry.data.JSONIterator()) {

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.backup
import org.json.JSONObject
import org.koitharu.kotatsu.bookmarks.data.BookmarkEntity
import org.koitharu.kotatsu.core.db.entity.MangaEntity
import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity
import org.koitharu.kotatsu.core.db.entity.TagEntity
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
@@ -78,6 +79,12 @@ class JsonDeserializer(private val json: JSONObject) {
percent = json.getDouble("percent").toFloat(),
)
fun toMangaSourceEntity() = MangaSourceEntity(
source = json.getString("source"),
isEnabled = json.getBoolean("enabled"),
sortKey = json.getInt("sort_key"),
)
fun toMap(): Map<String, Any?> {
val map = mutableMapOf<String, Any?>()
val keys = json.keys()

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.backup
import org.json.JSONObject
import org.koitharu.kotatsu.bookmarks.data.BookmarkEntity
import org.koitharu.kotatsu.core.db.entity.MangaEntity
import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity
import org.koitharu.kotatsu.core.db.entity.TagEntity
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.FavouriteEntity
@@ -82,6 +83,14 @@ class JsonSerializer private constructor(private val json: JSONObject) {
},
)
constructor(e: MangaSourceEntity) : this(
JSONObject().apply {
put("source", e.source)
put("enabled", e.isEnabled)
put("sort_key", e.sortKey)
},
)
constructor(m: Map<String, *>) : this(
JSONObject(m),
)

View File

@@ -69,6 +69,7 @@ class AppBackupAgent : BackupAgent() {
backup.put(repository.dumpCategories())
backup.put(repository.dumpFavourites())
backup.put(repository.dumpBookmarks())
backup.put(repository.dumpSources())
backup.put(repository.dumpSettings())
backup.finish()
backup.file
@@ -90,6 +91,7 @@ class AppBackupAgent : BackupAgent() {
backup.getEntry(BackupEntry.CATEGORIES)?.let { repository.restoreCategories(it) }
backup.getEntry(BackupEntry.FAVOURITES)?.let { repository.restoreFavourites(it) }
backup.getEntry(BackupEntry.BOOKMARKS)?.let { repository.restoreBookmarks(it) }
backup.getEntry(BackupEntry.SOURCES)?.let { repository.restoreSources(it) }
backup.getEntry(BackupEntry.SETTINGS)?.let { repository.restoreSettings(it) }
}
} finally {

View File

@@ -24,21 +24,25 @@ class BackupViewModel @Inject constructor(
init {
launchLoadingJob {
val file = BackupZipOutput(context).use { backup ->
val step = 1f / 6f
backup.put(repository.createIndex())
progress.value = 0f
backup.put(repository.dumpHistory())
progress.value = 0.2f
progress.value += step
backup.put(repository.dumpCategories())
progress.value = 0.4f
progress.value += step
backup.put(repository.dumpFavourites())
progress.value = 0.6f
progress.value += step
backup.put(repository.dumpBookmarks())
progress.value = 0.8f
progress.value += step
backup.put(repository.dumpSources())
progress.value += step
backup.put(repository.dumpSettings())
backup.finish()

View File

@@ -42,6 +42,7 @@ class PeriodicalBackupWorker @AssistedInject constructor(
backup.put(repository.dumpCategories())
backup.put(repository.dumpFavourites())
backup.put(repository.dumpBookmarks())
backup.put(repository.dumpSources())
backup.put(repository.dumpSettings())
backup.finish()
backup.file

View File

@@ -46,28 +46,34 @@ class RestoreViewModel @Inject constructor(
}
try {
val result = CompositeResult()
val step = 1f/6f
progress.value = 0f
backup.getEntry(BackupEntry.HISTORY)?.let {
result += repository.restoreHistory(it)
}
progress.value = 0.2f
progress.value += step
backup.getEntry(BackupEntry.CATEGORIES)?.let {
result += repository.restoreCategories(it)
}
progress.value = 0.4f
progress.value += step
backup.getEntry(BackupEntry.FAVOURITES)?.let {
result += repository.restoreFavourites(it)
}
progress.value = 0.6f
progress.value += step
backup.getEntry(BackupEntry.BOOKMARKS)?.let {
result += repository.restoreBookmarks(it)
}
progress.value = 0.8f
progress.value += step
backup.getEntry(BackupEntry.SOURCES)?.let {
result += repository.restoreSources(it)
}
progress.value += step
backup.getEntry(BackupEntry.SETTINGS)?.let {
result += repository.restoreSettings(it)
}