diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f75b00c8..8418cb33b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ The format is a modified version of [Keep a Changelog](https://keepachangelog.co - Fix MAL `main_picture` nullability breaking search if a result doesn't have a cover set ([@MajorTanya](https://github.com/MajorTanya)) ([#1618](https://github.com/mihonapp/mihon/pull/1618)) - Fix Bangumi and MAL tracking 401 errors due to Mihon sending expired credentials ([@MajorTanya](https://github.com/MajorTanya)) ([#1681](https://github.com/mihonapp/mihon/pull/1681), [#1682](https://github.com/mihonapp/mihon/pull/1682)) - Fix certain Infinix devices being unable to use any "Open link in browser" actions, including tracker setup ([@MajorTanya](https://github.com/MajorTanya)) ([#1684](https://github.com/mihonapp/mihon/pull/1684)) +- Fix App's preferences referencing deleted categories ([@cuong-tran](https://github.com/cuong-tran)) ([#1734](https://github.com/mihonapp/mihon/pull/1734)) ### Other - Add zoned "Current time" to debug info and include year & timezone in logcat output ([@MajorTanya](https://github.com/MajorTanya)) ([#1672](https://github.com/mihonapp/mihon/pull/1672)) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index cd269aeae..29836fc8c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -29,7 +29,7 @@ android { defaultConfig { applicationId = "app.mihon" - versionCode = 9 + versionCode = 10 versionName = "0.17.1" buildConfigField("String", "COMMIT_COUNT", "\"${getCommitCount()}\"") diff --git a/app/src/main/java/eu/kanade/domain/DomainModule.kt b/app/src/main/java/eu/kanade/domain/DomainModule.kt index 266ff82db..51161ffdf 100644 --- a/app/src/main/java/eu/kanade/domain/DomainModule.kt +++ b/app/src/main/java/eu/kanade/domain/DomainModule.kt @@ -111,7 +111,7 @@ class DomainModule : InjektModule { addFactory { RenameCategory(get()) } addFactory { ReorderCategory(get()) } addFactory { UpdateCategory(get()) } - addFactory { DeleteCategory(get()) } + addFactory { DeleteCategory(get(), get(), get()) } addSingletonFactory { MangaRepositoryImpl(get()) } addFactory { GetDuplicateLibraryManga(get()) } diff --git a/app/src/main/java/mihon/core/migration/migrations/CategoryPreferencesCleanupMigration.kt b/app/src/main/java/mihon/core/migration/migrations/CategoryPreferencesCleanupMigration.kt new file mode 100644 index 000000000..9958e1ac6 --- /dev/null +++ b/app/src/main/java/mihon/core/migration/migrations/CategoryPreferencesCleanupMigration.kt @@ -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 = 10f + + override suspend fun invoke(migrationContext: MigrationContext): Boolean = withIOContext { + val libraryPreferences = migrationContext.get() ?: return@withIOContext false + val downloadPreferences = migrationContext.get() ?: return@withIOContext false + + val getCategories = migrationContext.get() ?: 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 + } +} diff --git a/app/src/main/java/mihon/core/migration/migrations/Migrations.kt b/app/src/main/java/mihon/core/migration/migrations/Migrations.kt index 821434e2e..92d1956e5 100644 --- a/app/src/main/java/mihon/core/migration/migrations/Migrations.kt +++ b/app/src/main/java/mihon/core/migration/migrations/Migrations.kt @@ -7,4 +7,5 @@ val migrations: List SetupBackupCreateMigration(), SetupLibraryUpdateMigration(), TrustExtensionRepositoryMigration(), + CategoryPreferencesCleanupMigration(), ) diff --git a/domain/src/main/java/tachiyomi/domain/category/interactor/DeleteCategory.kt b/domain/src/main/java/tachiyomi/domain/category/interactor/DeleteCategory.kt index bf26d959f..279b1c9af 100644 --- a/domain/src/main/java/tachiyomi/domain/category/interactor/DeleteCategory.kt +++ b/domain/src/main/java/tachiyomi/domain/category/interactor/DeleteCategory.kt @@ -5,9 +5,13 @@ import tachiyomi.core.common.util.lang.withNonCancellableContext import tachiyomi.core.common.util.system.logcat import tachiyomi.domain.category.model.CategoryUpdate import tachiyomi.domain.category.repository.CategoryRepository +import tachiyomi.domain.download.service.DownloadPreferences +import tachiyomi.domain.library.service.LibraryPreferences class DeleteCategory( private val categoryRepository: CategoryRepository, + private val libraryPreferences: LibraryPreferences, + private val downloadPreferences: DownloadPreferences, ) { suspend fun await(categoryId: Long) = withNonCancellableContext { @@ -26,6 +30,25 @@ class DeleteCategory( ) } + val defaultCategory = libraryPreferences.defaultCategory().get() + if (defaultCategory == categoryId.toInt()) { + libraryPreferences.defaultCategory().delete() + } + + val categoryPreferences = listOf( + libraryPreferences.updateCategories(), + libraryPreferences.updateCategoriesExclude(), + downloadPreferences.removeExcludeCategories(), + downloadPreferences.downloadNewChapterCategories(), + downloadPreferences.downloadNewChapterCategoriesExclude(), + ) + val categoryIdString = categoryId.toString() + categoryPreferences.forEach { preference -> + val ids = preference.get() + if (categoryIdString !in ids) return@forEach + preference.set(ids.minus(categoryIdString)) + } + try { categoryRepository.updatePartial(updates) Result.Success