Fix App's preferences referencing deleted categories (#1734)
(cherry picked from commit eeb683069a3a0be7e769ac9273b5accc582e03ec) # Conflicts: # CHANGELOG.md # app/build.gradle.kts
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
package mihon.core.migration.migrations
|
||||
|
||||
import mihon.core.migration.Migration
|
||||
import mihon.core.migration.MigrationContext
|
||||
import tachiyomi.core.common.util.lang.withIOContext
|
||||
import tachiyomi.domain.category.interactor.GetCategories
|
||||
import tachiyomi.domain.download.service.DownloadPreferences
|
||||
import tachiyomi.domain.library.service.LibraryPreferences
|
||||
|
||||
class CategoryPreferencesCleanupMigration : Migration {
|
||||
override val version: Float = 72f
|
||||
|
||||
override suspend fun invoke(migrationContext: MigrationContext): Boolean = withIOContext {
|
||||
val libraryPreferences = migrationContext.get<LibraryPreferences>() ?: return@withIOContext false
|
||||
val downloadPreferences = migrationContext.get<DownloadPreferences>() ?: return@withIOContext false
|
||||
|
||||
val getCategories = migrationContext.get<GetCategories>() ?: return@withIOContext false
|
||||
val allCategories = getCategories.await().map { it.id.toString() }.toSet()
|
||||
|
||||
val defaultCategory = libraryPreferences.defaultCategory().get()
|
||||
if (defaultCategory.toString() !in allCategories) {
|
||||
libraryPreferences.defaultCategory().delete()
|
||||
}
|
||||
|
||||
val categoryPreferences = listOf(
|
||||
libraryPreferences.updateCategories(),
|
||||
libraryPreferences.updateCategoriesExclude(),
|
||||
downloadPreferences.removeExcludeCategories(),
|
||||
downloadPreferences.downloadNewChapterCategories(),
|
||||
downloadPreferences.downloadNewChapterCategoriesExclude(),
|
||||
)
|
||||
categoryPreferences.forEach { preference ->
|
||||
val ids = preference.get()
|
||||
val garbageIds = ids.minus(allCategories)
|
||||
if (garbageIds.isEmpty()) return@forEach
|
||||
preference.set(ids.minus(garbageIds))
|
||||
}
|
||||
return@withIOContext true
|
||||
}
|
||||
}
|
||||
@@ -45,4 +45,5 @@ val migrations: List<Migration>
|
||||
MoveCacheToDiskSettingMigration(),
|
||||
MoveEncryptionSettingsToAppStateMigration(),
|
||||
TrustExtensionRepositoryMigration(),
|
||||
CategoryPreferencesCleanupMigration(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user