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:
Cuong-Tran
2025-02-25 01:44:28 +07:00
committed by Jobobby04
parent 9c22e7fcb7
commit ce6b847c8b
5 changed files with 66 additions and 2 deletions
@@ -110,7 +110,7 @@ class DomainModule : InjektModule {
addFactory { RenameCategory(get()) }
addFactory { ReorderCategory(get()) }
addFactory { UpdateCategory(get()) }
addFactory { DeleteCategory(get()) }
addFactory { DeleteCategory(get(), get(), get()) }
addSingletonFactory<MangaRepository> { MangaRepositoryImpl(get()) }
addFactory { GetDuplicateLibraryManga(get()) }
@@ -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(),
)