From 4882896f4d65acabb82771699906b797b3de7662 Mon Sep 17 00:00:00 2001 From: Semen <55978818+semenvav@users.noreply.github.com> Date: Fri, 7 Jul 2023 16:57:02 +0300 Subject: [PATCH] Add function to delete downloaded chapters when migrating manga (#9621) add function to delete downloaded chapters when migrating manga and getFlagsFromPositions fix --- .../ui/browse/migration/MigrationFlags.kt | 31 ++++++++++++++----- .../browse/migration/search/MigrateDialog.kt | 10 ++++++ i18n/src/main/res/values/strings.xml | 1 + 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/MigrationFlags.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/MigrationFlags.kt index 79e342030..f0643d81c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/MigrationFlags.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/MigrationFlags.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.browse.migration import eu.kanade.domain.manga.model.hasCustomCover import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.CoverCache +import eu.kanade.tachiyomi.data.download.DownloadCache import kotlinx.coroutines.runBlocking import tachiyomi.domain.manga.model.Manga import tachiyomi.domain.track.interactor.GetTracks @@ -12,15 +13,18 @@ import uy.kohesive.injekt.injectLazy object MigrationFlags { - private const val CHAPTERS = 0b0001 - private const val CATEGORIES = 0b0010 - private const val TRACK = 0b0100 - private const val CUSTOM_COVER = 0b1000 + private const val CHAPTERS = 0b00001 + private const val CATEGORIES = 0b00010 + private const val TRACK = 0b00100 + private const val CUSTOM_COVER = 0b01000 + private const val DELETE_DOWNLOADED = 0b10000 private val coverCache: CoverCache by injectLazy() private val getTracks: GetTracks = Injekt.get() + private val downloadCache: DownloadCache by injectLazy() - val flags get() = arrayOf(CHAPTERS, CATEGORIES, TRACK, CUSTOM_COVER) + val flags get() = arrayOf(CHAPTERS, CATEGORIES, TRACK, CUSTOM_COVER, DELETE_DOWNLOADED) + private var enableFlags = emptyList().toMutableList() fun hasChapters(value: Int): Boolean { return value and CHAPTERS != 0 @@ -38,23 +42,36 @@ object MigrationFlags { return value and CUSTOM_COVER != 0 } + fun hasDeleteDownloaded(value: Int): Boolean { + return value and DELETE_DOWNLOADED != 0 + } + fun getEnabledFlagsPositions(value: Int): List { return flags.mapIndexedNotNull { index, flag -> if (value and flag != 0) index else null } } fun getFlagsFromPositions(positions: Array): Int { - return positions.fold(0) { accumulated, position -> accumulated or (1 shl position) } + val fold = positions.fold(0) { accumulated, position -> accumulated or enableFlags[position] } + enableFlags.clear() + return fold } fun titles(manga: Manga?): Array { + enableFlags.add(CHAPTERS) + enableFlags.add(CATEGORIES) val titles = arrayOf(R.string.chapters, R.string.categories).toMutableList() if (manga != null) { if (runBlocking { getTracks.await(manga.id) }.isNotEmpty()) { titles.add(R.string.track) + enableFlags.add(TRACK) } - if (manga.hasCustomCover(coverCache)) { titles.add(R.string.custom_cover) + enableFlags.add(CUSTOM_COVER) + } + if (downloadCache.getDownloadCount(manga) > 0) { + titles.add(R.string.delete_downloaded) + enableFlags.add(DELETE_DOWNLOADED) } } return titles.toTypedArray() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateDialog.kt index c3358a88b..496a40c58 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateDialog.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/MigrateDialog.kt @@ -34,6 +34,7 @@ import eu.kanade.domain.manga.model.hasCustomCover import eu.kanade.domain.manga.model.toSManga import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.cache.CoverCache +import eu.kanade.tachiyomi.data.download.DownloadManager import eu.kanade.tachiyomi.data.track.EnhancedTrackService import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.source.Source @@ -161,6 +162,7 @@ internal fun MigrateDialog( internal class MigrateDialogScreenModel( private val sourceManager: SourceManager = Injekt.get(), + private val downloadManager: DownloadManager = Injekt.get(), private val updateManga: UpdateManga = Injekt.get(), private val getChapterByMangaId: GetChapterByMangaId = Injekt.get(), private val syncChaptersWithSource: SyncChaptersWithSource = Injekt.get(), @@ -219,6 +221,7 @@ internal class MigrateDialogScreenModel( val migrateCategories = MigrationFlags.hasCategories(flags) val migrateTracks = MigrationFlags.hasTracks(flags) val migrateCustomCover = MigrationFlags.hasCustomCover(flags) + val deleteDownloaded = MigrationFlags.hasDeleteDownloaded(flags) try { syncChaptersWithSource.await(sourceChapters, newManga, newSource) @@ -283,6 +286,13 @@ internal class MigrateDialogScreenModel( insertTrack.awaitAll(tracks) } + // Delete downloaded + if (deleteDownloaded) { + if (oldSource != null) { + downloadManager.deleteManga(oldManga, oldSource) + } + } + if (replace) { updateManga.await(MangaUpdate(oldManga.id, favorite = false, dateAdded = 0)) } diff --git a/i18n/src/main/res/values/strings.xml b/i18n/src/main/res/values/strings.xml index 03783484e..e03781151 100644 --- a/i18n/src/main/res/values/strings.xml +++ b/i18n/src/main/res/values/strings.xml @@ -12,6 +12,7 @@ Library entries Chapters Tracking + Delete downloaded History