Remove SavedFilterBackup class
This commit is contained in:
@@ -26,7 +26,6 @@ import org.koitharu.kotatsu.backups.data.model.CategoryBackup
|
|||||||
import org.koitharu.kotatsu.backups.data.model.FavouriteBackup
|
import org.koitharu.kotatsu.backups.data.model.FavouriteBackup
|
||||||
import org.koitharu.kotatsu.backups.data.model.HistoryBackup
|
import org.koitharu.kotatsu.backups.data.model.HistoryBackup
|
||||||
import org.koitharu.kotatsu.backups.data.model.MangaBackup
|
import org.koitharu.kotatsu.backups.data.model.MangaBackup
|
||||||
import org.koitharu.kotatsu.backups.data.model.SavedFilterBackup
|
|
||||||
import org.koitharu.kotatsu.backups.data.model.ScrobblingBackup
|
import org.koitharu.kotatsu.backups.data.model.ScrobblingBackup
|
||||||
import org.koitharu.kotatsu.backups.data.model.SourceBackup
|
import org.koitharu.kotatsu.backups.data.model.SourceBackup
|
||||||
import org.koitharu.kotatsu.backups.data.model.StatisticBackup
|
import org.koitharu.kotatsu.backups.data.model.StatisticBackup
|
||||||
@@ -36,6 +35,7 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
|
|||||||
import org.koitharu.kotatsu.core.util.CompositeResult
|
import org.koitharu.kotatsu.core.util.CompositeResult
|
||||||
import org.koitharu.kotatsu.core.util.progress.Progress
|
import org.koitharu.kotatsu.core.util.progress.Progress
|
||||||
import org.koitharu.kotatsu.explore.data.MangaSourcesRepository
|
import org.koitharu.kotatsu.explore.data.MangaSourcesRepository
|
||||||
|
import org.koitharu.kotatsu.filter.data.PersistableFilter
|
||||||
import org.koitharu.kotatsu.filter.data.SavedFiltersRepository
|
import org.koitharu.kotatsu.filter.data.SavedFiltersRepository
|
||||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
import org.koitharu.kotatsu.reader.data.TapGridSettings
|
import org.koitharu.kotatsu.reader.data.TapGridSettings
|
||||||
@@ -48,271 +48,267 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
@Reusable
|
@Reusable
|
||||||
class BackupRepository @Inject constructor(
|
class BackupRepository @Inject constructor(
|
||||||
private val database: MangaDatabase,
|
private val database: MangaDatabase,
|
||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
private val tapGridSettings: TapGridSettings,
|
private val tapGridSettings: TapGridSettings,
|
||||||
private val mangaSourcesRepository: MangaSourcesRepository,
|
private val mangaSourcesRepository: MangaSourcesRepository,
|
||||||
private val savedFiltersRepository: SavedFiltersRepository,
|
private val savedFiltersRepository: SavedFiltersRepository,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val json = Json {
|
private val json = Json {
|
||||||
allowSpecialFloatingPointValues = true
|
allowSpecialFloatingPointValues = true
|
||||||
coerceInputValues = true
|
coerceInputValues = true
|
||||||
encodeDefaults = true
|
encodeDefaults = true
|
||||||
ignoreUnknownKeys = true
|
ignoreUnknownKeys = true
|
||||||
useAlternativeNames = false
|
useAlternativeNames = false
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun createBackup(
|
suspend fun createBackup(
|
||||||
output: ZipOutputStream,
|
output: ZipOutputStream,
|
||||||
progress: FlowCollector<Progress>?,
|
progress: FlowCollector<Progress>?,
|
||||||
) {
|
) {
|
||||||
progress?.emit(Progress.INDETERMINATE)
|
progress?.emit(Progress.INDETERMINATE)
|
||||||
var commonProgress = Progress(0, BackupSection.entries.size)
|
var commonProgress = Progress(0, BackupSection.entries.size)
|
||||||
for (section in BackupSection.entries) {
|
for (section in BackupSection.entries) {
|
||||||
when (section) {
|
when (section) {
|
||||||
BackupSection.INDEX -> output.writeJsonArray(
|
BackupSection.INDEX -> output.writeJsonArray(
|
||||||
section = BackupSection.INDEX,
|
section = BackupSection.INDEX,
|
||||||
data = flowOf(BackupIndex()),
|
data = flowOf(BackupIndex()),
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.HISTORY -> output.writeJsonArray(
|
BackupSection.HISTORY -> output.writeJsonArray(
|
||||||
section = BackupSection.HISTORY,
|
section = BackupSection.HISTORY,
|
||||||
data = database.getHistoryDao().dump().map { HistoryBackup(it) },
|
data = database.getHistoryDao().dump().map { HistoryBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.CATEGORIES -> output.writeJsonArray(
|
BackupSection.CATEGORIES -> output.writeJsonArray(
|
||||||
section = BackupSection.CATEGORIES,
|
section = BackupSection.CATEGORIES,
|
||||||
data = database.getFavouriteCategoriesDao().findAll().asFlow().map { CategoryBackup(it) },
|
data = database.getFavouriteCategoriesDao().findAll().asFlow().map { CategoryBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.FAVOURITES -> output.writeJsonArray(
|
BackupSection.FAVOURITES -> output.writeJsonArray(
|
||||||
section = BackupSection.FAVOURITES,
|
section = BackupSection.FAVOURITES,
|
||||||
data = database.getFavouritesDao().dump().map { FavouriteBackup(it) },
|
data = database.getFavouritesDao().dump().map { FavouriteBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.SETTINGS -> output.writeString(
|
BackupSection.SETTINGS -> output.writeString(
|
||||||
section = BackupSection.SETTINGS,
|
section = BackupSection.SETTINGS,
|
||||||
data = dumpSettings(),
|
data = dumpSettings(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.SETTINGS_READER_GRID -> output.writeString(
|
BackupSection.SETTINGS_READER_GRID -> output.writeString(
|
||||||
section = BackupSection.SETTINGS_READER_GRID,
|
section = BackupSection.SETTINGS_READER_GRID,
|
||||||
data = dumpReaderGridSettings(),
|
data = dumpReaderGridSettings(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.BOOKMARKS -> output.writeJsonArray(
|
BackupSection.BOOKMARKS -> output.writeJsonArray(
|
||||||
section = BackupSection.BOOKMARKS,
|
section = BackupSection.BOOKMARKS,
|
||||||
data = database.getBookmarksDao().dump().map { BookmarkBackup(it.first, it.second) },
|
data = database.getBookmarksDao().dump().map { BookmarkBackup(it.first, it.second) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.SOURCES -> output.writeJsonArray(
|
BackupSection.SOURCES -> output.writeJsonArray(
|
||||||
section = BackupSection.SOURCES,
|
section = BackupSection.SOURCES,
|
||||||
data = database.getSourcesDao().dumpEnabled().map { SourceBackup(it) },
|
data = database.getSourcesDao().dumpEnabled().map { SourceBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.SCROBBLING -> output.writeJsonArray(
|
BackupSection.SCROBBLING -> output.writeJsonArray(
|
||||||
section = BackupSection.SCROBBLING,
|
section = BackupSection.SCROBBLING,
|
||||||
data = database.getScrobblingDao().dumpEnabled().map { ScrobblingBackup(it) },
|
data = database.getScrobblingDao().dumpEnabled().map { ScrobblingBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.STATS -> output.writeJsonArray(
|
BackupSection.STATS -> output.writeJsonArray(
|
||||||
section = BackupSection.STATS,
|
section = BackupSection.STATS,
|
||||||
data = database.getStatsDao().dumpEnabled().map { StatisticBackup(it) },
|
data = database.getStatsDao().dumpEnabled().map { StatisticBackup(it) },
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
|
|
||||||
BackupSection.SAVED_FILTERS -> {
|
BackupSection.SAVED_FILTERS -> {
|
||||||
val sources = mangaSourcesRepository.getEnabledSources()
|
val sources = mangaSourcesRepository.getEnabledSources()
|
||||||
val filters = sources.flatMap { source ->
|
val filters = sources.flatMap { source ->
|
||||||
savedFiltersRepository.getAll(source)
|
savedFiltersRepository.getAll(source)
|
||||||
}
|
}
|
||||||
output.writeJsonArray(
|
output.writeJsonArray(
|
||||||
section = BackupSection.SAVED_FILTERS,
|
section = BackupSection.SAVED_FILTERS,
|
||||||
data = filters.asFlow().map { SavedFilterBackup(it) },
|
data = filters.asFlow(),
|
||||||
serializer = serializer(),
|
serializer = serializer(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
progress?.emit(commonProgress)
|
progress?.emit(commonProgress)
|
||||||
commonProgress++
|
commonProgress++
|
||||||
}
|
}
|
||||||
progress?.emit(commonProgress)
|
progress?.emit(commonProgress)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun restoreBackup(
|
suspend fun restoreBackup(
|
||||||
input: ZipInputStream,
|
input: ZipInputStream,
|
||||||
sections: Set<BackupSection>,
|
sections: Set<BackupSection>,
|
||||||
progress: FlowCollector<Progress>?,
|
progress: FlowCollector<Progress>?,
|
||||||
): CompositeResult {
|
): CompositeResult {
|
||||||
progress?.emit(Progress.INDETERMINATE)
|
progress?.emit(Progress.INDETERMINATE)
|
||||||
var commonProgress = Progress(0, sections.size)
|
var commonProgress = Progress(0, sections.size)
|
||||||
var entry = input.nextEntry
|
var entry = input.nextEntry
|
||||||
var result = CompositeResult.EMPTY
|
var result = CompositeResult.EMPTY
|
||||||
while (entry != null) {
|
while (entry != null) {
|
||||||
val section = BackupSection.of(entry)
|
val section = BackupSection.of(entry)
|
||||||
if (section in sections) {
|
if (section in sections) {
|
||||||
result = result + when (section) {
|
result += when (section) {
|
||||||
BackupSection.INDEX -> CompositeResult.EMPTY // useless in our case
|
BackupSection.INDEX -> CompositeResult.EMPTY // useless in our case
|
||||||
BackupSection.HISTORY -> input.readJsonArray<HistoryBackup>(serializer()).restoreToDb {
|
BackupSection.HISTORY -> input.readJsonArray<HistoryBackup>(serializer()).restoreToDb {
|
||||||
upsertManga(it.manga)
|
upsertManga(it.manga)
|
||||||
getHistoryDao().upsert(it.toEntity())
|
getHistoryDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.CATEGORIES -> input.readJsonArray<CategoryBackup>(serializer()).restoreToDb {
|
BackupSection.CATEGORIES -> input.readJsonArray<CategoryBackup>(serializer()).restoreToDb {
|
||||||
getFavouriteCategoriesDao().upsert(it.toEntity())
|
getFavouriteCategoriesDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.FAVOURITES -> input.readJsonArray<FavouriteBackup>(serializer()).restoreToDb {
|
BackupSection.FAVOURITES -> input.readJsonArray<FavouriteBackup>(serializer()).restoreToDb {
|
||||||
upsertManga(it.manga)
|
upsertManga(it.manga)
|
||||||
getFavouritesDao().upsert(it.toEntity())
|
getFavouritesDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.SETTINGS -> input.readMap().let {
|
BackupSection.SETTINGS -> input.readMap().let {
|
||||||
settings.upsertAll(it)
|
settings.upsertAll(it)
|
||||||
CompositeResult.success()
|
CompositeResult.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.SETTINGS_READER_GRID -> input.readMap().let {
|
BackupSection.SETTINGS_READER_GRID -> input.readMap().let {
|
||||||
tapGridSettings.upsertAll(it)
|
tapGridSettings.upsertAll(it)
|
||||||
CompositeResult.success()
|
CompositeResult.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.BOOKMARKS -> input.readJsonArray<BookmarkBackup>(serializer()).restoreToDb {
|
BackupSection.BOOKMARKS -> input.readJsonArray<BookmarkBackup>(serializer()).restoreToDb {
|
||||||
upsertManga(it.manga)
|
upsertManga(it.manga)
|
||||||
getBookmarksDao().upsert(it.bookmarks.map { b -> b.toEntity() })
|
getBookmarksDao().upsert(it.bookmarks.map { b -> b.toEntity() })
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.SOURCES -> input.readJsonArray<SourceBackup>(serializer()).restoreToDb {
|
BackupSection.SOURCES -> input.readJsonArray<SourceBackup>(serializer()).restoreToDb {
|
||||||
getSourcesDao().upsert(it.toEntity())
|
getSourcesDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.SCROBBLING -> input.readJsonArray<ScrobblingBackup>(serializer()).restoreToDb {
|
BackupSection.SCROBBLING -> input.readJsonArray<ScrobblingBackup>(serializer()).restoreToDb {
|
||||||
getScrobblingDao().upsert(it.toEntity())
|
getScrobblingDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.STATS -> input.readJsonArray<StatisticBackup>(serializer()).restoreToDb {
|
BackupSection.STATS -> input.readJsonArray<StatisticBackup>(serializer()).restoreToDb {
|
||||||
getStatsDao().upsert(it.toEntity())
|
getStatsDao().upsert(it.toEntity())
|
||||||
}
|
}
|
||||||
|
|
||||||
BackupSection.SAVED_FILTERS -> input.readJsonArray<SavedFilterBackup>(serializer())
|
BackupSection.SAVED_FILTERS -> input.readJsonArray<PersistableFilter>(serializer())
|
||||||
.restoreWithoutTransaction {
|
.restoreWithoutTransaction {
|
||||||
savedFiltersRepository.save(
|
savedFiltersRepository.save(it)
|
||||||
source = it.source,
|
}
|
||||||
name = it.name,
|
|
||||||
filter = it.filter,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
null -> CompositeResult.EMPTY // skip unknown entries
|
null -> CompositeResult.EMPTY // skip unknown entries
|
||||||
}
|
}
|
||||||
progress?.emit(commonProgress)
|
progress?.emit(commonProgress)
|
||||||
commonProgress++
|
commonProgress++
|
||||||
}
|
}
|
||||||
input.closeEntry()
|
input.closeEntry()
|
||||||
entry = input.nextEntry
|
entry = input.nextEntry
|
||||||
}
|
}
|
||||||
progress?.emit(commonProgress)
|
progress?.emit(commonProgress)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun <T> ZipOutputStream.writeJsonArray(
|
private suspend fun <T> ZipOutputStream.writeJsonArray(
|
||||||
section: BackupSection,
|
section: BackupSection,
|
||||||
data: Flow<T>,
|
data: Flow<T>,
|
||||||
serializer: SerializationStrategy<T>,
|
serializer: SerializationStrategy<T>,
|
||||||
) {
|
) {
|
||||||
data.onStart {
|
data.onStart {
|
||||||
putNextEntry(ZipEntry(section.entryName))
|
putNextEntry(ZipEntry(section.entryName))
|
||||||
write("[")
|
write("[")
|
||||||
}.onCompletion { error ->
|
}.onCompletion { error ->
|
||||||
if (error == null) {
|
if (error == null) {
|
||||||
write("]")
|
write("]")
|
||||||
}
|
}
|
||||||
closeEntry()
|
closeEntry()
|
||||||
flush()
|
flush()
|
||||||
}.collectIndexed { index, value ->
|
}.collectIndexed { index, value ->
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
write(",")
|
write(",")
|
||||||
}
|
}
|
||||||
json.encodeToStream(serializer, value, this)
|
json.encodeToStream(serializer, value, this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T> InputStream.readJsonArray(
|
private fun <T> InputStream.readJsonArray(
|
||||||
serializer: DeserializationStrategy<T>,
|
serializer: DeserializationStrategy<T>,
|
||||||
): Sequence<T> = json.decodeToSequence(this, serializer, DecodeSequenceMode.ARRAY_WRAPPED)
|
): Sequence<T> = json.decodeToSequence(this, serializer, DecodeSequenceMode.ARRAY_WRAPPED)
|
||||||
|
|
||||||
private fun InputStream.readMap(): Map<String, Any?> {
|
private fun InputStream.readMap(): Map<String, Any?> {
|
||||||
val jo = JSONArray(readString()).getJSONObject(0)
|
val jo = JSONArray(readString()).getJSONObject(0)
|
||||||
val map = ArrayMap<String, Any?>(jo.length())
|
val map = ArrayMap<String, Any?>(jo.length())
|
||||||
val keys = jo.keys()
|
val keys = jo.keys()
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
val key = keys.next()
|
val key = keys.next()
|
||||||
map[key] = jo.get(key)
|
map[key] = jo.get(key)
|
||||||
}
|
}
|
||||||
return map
|
return map
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ZipOutputStream.writeString(
|
private fun ZipOutputStream.writeString(
|
||||||
section: BackupSection,
|
section: BackupSection,
|
||||||
data: String,
|
data: String,
|
||||||
) {
|
) {
|
||||||
putNextEntry(ZipEntry(section.entryName))
|
putNextEntry(ZipEntry(section.entryName))
|
||||||
try {
|
try {
|
||||||
write("[")
|
write("[")
|
||||||
write(data)
|
write(data)
|
||||||
write("]")
|
write("]")
|
||||||
} finally {
|
} finally {
|
||||||
closeEntry()
|
closeEntry()
|
||||||
flush()
|
flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun OutputStream.write(str: String) = write(str.toByteArray())
|
private fun OutputStream.write(str: String) = write(str.toByteArray())
|
||||||
|
|
||||||
private fun InputStream.readString(): String = readBytes().decodeToString()
|
private fun InputStream.readString(): String = readBytes().decodeToString()
|
||||||
|
|
||||||
private fun dumpSettings(): String {
|
private fun dumpSettings(): String {
|
||||||
val map = settings.getAllValues().toMutableMap()
|
val map = settings.getAllValues().toMutableMap()
|
||||||
map.remove(AppSettings.KEY_APP_PASSWORD)
|
map.remove(AppSettings.KEY_APP_PASSWORD)
|
||||||
map.remove(AppSettings.KEY_PROXY_PASSWORD)
|
map.remove(AppSettings.KEY_PROXY_PASSWORD)
|
||||||
map.remove(AppSettings.KEY_PROXY_LOGIN)
|
map.remove(AppSettings.KEY_PROXY_LOGIN)
|
||||||
map.remove(AppSettings.KEY_INCOGNITO_MODE)
|
map.remove(AppSettings.KEY_INCOGNITO_MODE)
|
||||||
return JSONObject(map).toString()
|
return JSONObject(map).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun dumpReaderGridSettings(): String {
|
private fun dumpReaderGridSettings(): String {
|
||||||
return JSONObject(tapGridSettings.getAllValues()).toString()
|
return JSONObject(tapGridSettings.getAllValues()).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun MangaDatabase.upsertManga(manga: MangaBackup) {
|
private suspend fun MangaDatabase.upsertManga(manga: MangaBackup) {
|
||||||
val tags = manga.tags.map { it.toEntity() }
|
val tags = manga.tags.map { it.toEntity() }
|
||||||
getTagsDao().upsert(tags)
|
getTagsDao().upsert(tags)
|
||||||
getMangaDao().upsert(manga.toEntity(), tags)
|
getMangaDao().upsert(manga.toEntity(), tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend inline fun <T> Sequence<T>.restoreToDb(crossinline block: suspend MangaDatabase.(T) -> Unit): CompositeResult {
|
private suspend inline fun <T> Sequence<T>.restoreToDb(crossinline block: suspend MangaDatabase.(T) -> Unit): CompositeResult {
|
||||||
return fold(CompositeResult.EMPTY) { result, item ->
|
return fold(CompositeResult.EMPTY) { result, item ->
|
||||||
result + runCatchingCancellable {
|
result + runCatchingCancellable {
|
||||||
database.withTransaction {
|
database.withTransaction {
|
||||||
database.block(item)
|
database.block(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend inline fun <T> Sequence<T>.restoreWithoutTransaction(crossinline block: suspend (T) -> Unit): CompositeResult {
|
private suspend inline fun <T> Sequence<T>.restoreWithoutTransaction(crossinline block: suspend (T) -> Unit): CompositeResult {
|
||||||
return fold(CompositeResult.EMPTY) { result, item ->
|
return fold(CompositeResult.EMPTY) { result, item ->
|
||||||
result + runCatchingCancellable {
|
result + runCatchingCancellable {
|
||||||
block(item)
|
block(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package org.koitharu.kotatsu.backups.data.model
|
|
||||||
|
|
||||||
import kotlinx.serialization.SerialName
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import org.koitharu.kotatsu.core.model.MangaSourceSerializer
|
|
||||||
import org.koitharu.kotatsu.filter.data.MangaListFilterSerializer
|
|
||||||
import org.koitharu.kotatsu.filter.data.PersistableFilter
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaListFilter
|
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class SavedFilterBackup(
|
|
||||||
@SerialName("name")
|
|
||||||
val name: String,
|
|
||||||
@Serializable(with = MangaSourceSerializer::class)
|
|
||||||
@SerialName("source")
|
|
||||||
val source: MangaSource,
|
|
||||||
@Serializable(with = MangaListFilterSerializer::class)
|
|
||||||
@SerialName("filter")
|
|
||||||
val filter: MangaListFilter,
|
|
||||||
) {
|
|
||||||
|
|
||||||
constructor(persistableFilter: PersistableFilter) : this(
|
|
||||||
name = persistableFilter.name,
|
|
||||||
source = persistableFilter.source,
|
|
||||||
filter = persistableFilter.filter,
|
|
||||||
)
|
|
||||||
|
|
||||||
fun toPersistableFilter() = PersistableFilter(
|
|
||||||
name = name,
|
|
||||||
source = source,
|
|
||||||
filter = filter,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -57,10 +57,16 @@ class SavedFiltersRepository @Inject constructor(
|
|||||||
source = source,
|
source = source,
|
||||||
filter = filter,
|
filter = filter,
|
||||||
)
|
)
|
||||||
persist(source, persistableFilter)
|
persist(persistableFilter)
|
||||||
persistableFilter
|
persistableFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun save(
|
||||||
|
filter: PersistableFilter,
|
||||||
|
) = withContext(Dispatchers.Default) {
|
||||||
|
persist(filter)
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun rename(source: MangaSource, id: Int, newName: String) = withContext(Dispatchers.Default) {
|
suspend fun rename(source: MangaSource, id: Int, newName: String) = withContext(Dispatchers.Default) {
|
||||||
val filter = load(source, id) ?: return@withContext
|
val filter = load(source, id) ?: return@withContext
|
||||||
val newFilter = filter.copy(name = newName)
|
val newFilter = filter.copy(name = newName)
|
||||||
@@ -79,8 +85,8 @@ class SavedFiltersRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun persist(source: MangaSource, persistableFilter: PersistableFilter) {
|
private fun persist(persistableFilter: PersistableFilter) {
|
||||||
val prefs = getPrefs(source)
|
val prefs = getPrefs(persistableFilter.source)
|
||||||
val json = Json.encodeToString(persistableFilter)
|
val json = Json.encodeToString(persistableFilter)
|
||||||
prefs.edit(commit = true) {
|
prefs.edit(commit = true) {
|
||||||
putString(key(persistableFilter.id), json)
|
putString(key(persistableFilter.id), json)
|
||||||
|
|||||||
Reference in New Issue
Block a user