Skip to content

Commit

Permalink
Add advanced Library setting to allow library manga's titles to updat…
Browse files Browse the repository at this point in the history
…e from source
  • Loading branch information
FlaminSarge committed Sep 1, 2024
1 parent 8f9a325 commit f665338
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 8 deletions.
20 changes: 17 additions & 3 deletions app/src/main/java/eu/kanade/domain/manga/interactor/UpdateManga.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package eu.kanade.domain.manga.interactor

import eu.kanade.domain.manga.model.hasCustomCover
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.SManga
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.manga.interactor.FetchInterval
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.manga.model.MangaUpdate
Expand All @@ -27,19 +30,26 @@ class UpdateManga(
}

suspend fun awaitUpdateFromSource(
source: Source,
localManga: Manga,
remoteManga: SManga,
manualFetch: Boolean,
coverCache: CoverCache = Injekt.get(),
libraryPreferences: LibraryPreferences = Injekt.get(),
downloadManager: DownloadManager = Injekt.get()
): Boolean {
val remoteTitle = try {
remoteManga.title
} catch (_: UninitializedPropertyAccessException) {
""
}

// if the manga isn't a favorite, set its title from source and update in db
val title = if (remoteTitle.isEmpty() || localManga.favorite) null else remoteTitle
// if the manga isn't a favorite (or 'update titles' preference is enabled), set its title from source and update in db
val title =
if (remoteTitle.isNotEmpty() && (!localManga.favorite || libraryPreferences.updateMangaTitles().get()))
remoteTitle
else
null

val coverLastModified =
when {
Expand All @@ -59,7 +69,7 @@ class UpdateManga(

val thumbnailUrl = remoteManga.thumbnail_url?.takeIf { it.isNotEmpty() }

return mangaRepository.update(
val success = mangaRepository.update(
MangaUpdate(
id = localManga.id,
title = title,
Expand All @@ -74,6 +84,10 @@ class UpdateManga(
initialized = true,
),
)
if (success && title != null) {
downloadManager.renameManga(source, localManga, title)
}
return success;
}

suspend fun awaitUpdateFetchInterval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import okhttp3.Headers
import tachiyomi.core.common.util.lang.launchNonCancellable
import tachiyomi.core.common.util.lang.withUIContext
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.library.service.LibraryPreferences
import tachiyomi.domain.manga.interactor.ResetViewerFlags
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.i18n.stringResource
Expand All @@ -84,6 +85,7 @@ object SettingsAdvancedScreen : SearchableSettings {

val basePreferences = remember { Injekt.get<BasePreferences>() }
val networkPreferences = remember { Injekt.get<NetworkPreferences>() }
val libraryPreferences = remember { Injekt.get<LibraryPreferences>() }

return listOf(
Preference.PreferenceItem.TextPreference(
Expand Down Expand Up @@ -124,7 +126,7 @@ object SettingsAdvancedScreen : SearchableSettings {
getBackgroundActivityGroup(),
getDataGroup(),
getNetworkGroup(networkPreferences = networkPreferences),
getLibraryGroup(),
getLibraryGroup(libraryPreferences = libraryPreferences),
getReaderGroup(basePreferences = basePreferences),
getExtensionsGroup(basePreferences = basePreferences),
)
Expand Down Expand Up @@ -284,7 +286,9 @@ object SettingsAdvancedScreen : SearchableSettings {
}

@Composable
private fun getLibraryGroup(): Preference.PreferenceGroup {
private fun getLibraryGroup(
libraryPreferences: LibraryPreferences
): Preference.PreferenceGroup {
val scope = rememberCoroutineScope()
val context = LocalContext.current

Expand Down Expand Up @@ -312,6 +316,11 @@ object SettingsAdvancedScreen : SearchableSettings {
}
},
),
Preference.PreferenceItem.SwitchPreference(
pref = libraryPreferences.updateMangaTitles(),
title = stringResource(MR.strings.pref_update_library_manga_titles),
subtitle = stringResource(MR.strings.pref_update_library_manga_titles_summary),
),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,33 @@ class DownloadManager(
}
}

/**
* Renames manga download folder
*
* @param source the source of the manga.
* @param manga the manga to rename
* @param newTitle the new manga title.
*/
fun renameManga(source: Source, manga: Manga, newTitle: String) {
val oldFolder = provider.findMangaDir(manga.title, source) ?: return
val newName = provider.getMangaDirName(newTitle)

if (oldFolder.name == newName) return

val capitalizationChanged = oldFolder.name.equals(newName, ignoreCase = true)
if (capitalizationChanged) {
val tempName = newName + Downloader.TMP_DIR_SUFFIX
if (!oldFolder.renameTo(tempName)) {
logcat(LogPriority.ERROR) { "Failed to rename manga download folder: ${oldFolder.name}" }
return
}
}

if (!oldFolder.renameTo(newName)) {
logcat(LogPriority.ERROR) { "Failed to rename manga download folder: ${oldFolder.name}" }
}
}

/**
* Renames an already downloaded chapter
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
// Update manga metadata if needed
if (libraryPreferences.autoUpdateMetadata().get()) {
val networkManga = source.getMangaDetails(manga.toSManga())
updateManga.awaitUpdateFromSource(manga, networkManga, manualFetch = false, coverCache)
updateManga.awaitUpdateFromSource(source, manga, networkManga, manualFetch = false, coverCache)
}

val chapters = source.getChapterList(manga.toSManga())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class MangaScreenModel(
try {
withIOContext {
val networkManga = state.source.getMangaDetails(state.manga.toSManga())
updateManga.awaitUpdateFromSource(state.manga, networkManga, manualFetch)
updateManga.awaitUpdateFromSource(state.source, state.manga, networkManga, manualFetch)
}
} catch (e: Throwable) {
// Ignore early hints "errors" that aren't handled by OkHttp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class LibraryPreferences(
ChapterSwipeAction.ToggleRead,
)

fun updateMangaTitles() = preferenceStore.getBoolean("pref_update_library_manga_titles", false)

// endregion

enum class ChapterSwipeAction {
Expand Down
2 changes: 1 addition & 1 deletion i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This module houses the string resources and translations.

Original English strings are manged in `src/commonMain/resources/MR/base/`. Translations are done externally via Weblate. See [our website](https://mihon.app/docs/contribute#translation) for more details.
Original English strings are managed in `src/commonMain/moko-resources/base/`. Translations are done externally via Weblate. See [our website](https://mihon.app/docs/contribute#translation) for more details.
2 changes: 2 additions & 0 deletions i18n/src/commonMain/moko-resources/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@
<string name="pref_verbose_logging">Verbose logging</string>
<string name="pref_verbose_logging_summary">Print verbose logs to system log (reduces app performance)</string>
<string name="pref_debug_info">Debug info</string>
<string name="pref_update_library_manga_titles">Allow title changes for library entries</string>
<string name="pref_update_library_manga_titles_summary">If a source has a different title for an entry than the one stored in the library, update it to match the source\'s title.</string>

<!-- About section -->
<string name="website">Website</string>
Expand Down

0 comments on commit f665338

Please sign in to comment.