Skip to content

Commit

Permalink
download’s cache renew interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong-tran committed Mar 18, 2024
1 parent 7c11ddf commit f6dcb24
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ object SettingsDownloadScreen : SearchableSettings {
allCategories = allCategories,
),
getDownloadAheadGroup(downloadPreferences = downloadPreferences),
// KMK -->
getDownloadCacheRenewInterval(downloadPreferences = downloadPreferences),
// KMK <--
)
}

Expand Down Expand Up @@ -190,4 +193,30 @@ object SettingsDownloadScreen : SearchableSettings {
),
)
}

// KMK -->
@Composable
private fun getDownloadCacheRenewInterval(
downloadPreferences: DownloadPreferences,
): Preference.PreferenceGroup {
return Preference.PreferenceGroup(
title = stringResource(MR.strings.download_cache_renew_interval),
preferenceItems = persistentListOf(
Preference.PreferenceItem.ListPreference(
pref = downloadPreferences.downloadCacheRenewInterval(),
title = stringResource(MR.strings.download_cache_renew_interval),
entries = persistentMapOf(
-1 to stringResource(MR.strings.download_cache_renew_interval_manual),
1 to stringResource(MR.strings.download_cache_renew_interval_1hour),
2 to stringResource(MR.strings.download_cache_renew_interval_2hour),
6 to stringResource(MR.strings.download_cache_renew_interval_6hour),
12 to stringResource(MR.strings.download_cache_renew_interval_12hour),
24 to stringResource(MR.strings.download_cache_renew_interval_24hour),
),
),
Preference.PreferenceItem.InfoPreference(stringResource(MR.strings.download_cache_renew_interval_info)),
),
)
}
// KMK <--
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.core.common.util.lang.launchNonCancellable
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
import tachiyomi.domain.source.service.SourceManager
import tachiyomi.domain.storage.service.StorageManager
Expand All @@ -69,6 +70,9 @@ class DownloadCache(
) {

private val scope = CoroutineScope(Dispatchers.IO)
// KMK -->
private val downloadPreferences: DownloadPreferences = Injekt.get()
// KMK <--

private val _changes: Channel<Unit> = Channel(Channel.UNLIMITED)
val changes = _changes.receiveAsFlow()
Expand All @@ -79,7 +83,11 @@ class DownloadCache(
* The interval after which this cache should be invalidated. 1 hour shouldn't cause major
* issues, as the cache is only used for UI feedback.
*/
private val renewInterval = 1.hours.inWholeMilliseconds
private val renewInterval //= 1.hours.inWholeMilliseconds
// KMK -->
get() = downloadPreferences.downloadCacheRenewInterval().get()
.hours.inWholeMilliseconds
// KMK <--

/**
* The last time the cache was refreshed.
Expand Down Expand Up @@ -320,7 +328,7 @@ class DownloadCache(
lastRenew = 0L
renewalJob?.cancel()
diskCacheFile.delete()
renewCache()
renewCache(/* KMK --> */ 0L /* KMK <-- */)
}

// KMK -->
Expand All @@ -335,9 +343,9 @@ class DownloadCache(
/**
* Renews the downloads cache.
*/
private fun renewCache() {
private fun renewCache(/* KMK --> */ renewInterval: Long = this.renewInterval /* KMK <-- */) {
// Avoid renewing cache if in the process nor too often
if (lastRenew + renewInterval >= System.currentTimeMillis() || renewalJob?.isActive == true) {
if (/* KMK --> */ renewInterval < 0L || /* KMK <-- */ lastRenew + renewInterval >= System.currentTimeMillis() || renewalJob?.isActive == true) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ class DownloadPreferences(
"download_new_categories_exclude",
emptySet(),
)

// KMK -->
fun downloadCacheRenewInterval() = preferenceStore.getInt("download_cache_renew_interval", 1)
// KMK <--
}
8 changes: 8 additions & 0 deletions i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@
<string name="save_chapter_as_cbz">Save as CBZ archive</string>
<string name="split_tall_images">Split tall images</string>
<string name="split_tall_images_summary">Improves reader performance</string>
<string name="download_cache_renew_interval">Download cache renew interval</string>
<string name="download_cache_renew_interval_manual">Manual</string>
<string name="download_cache_renew_interval_1hour">1 hour</string>
<string name="download_cache_renew_interval_2hour">2 hours</string>
<string name="download_cache_renew_interval_6hour">6 hours</string>
<string name="download_cache_renew_interval_12hour">12 hours</string>
<string name="download_cache_renew_interval_24hour">1 day</string>
<string name="download_cache_renew_interval_info">If set to "manual", either refresh each manga or using the "Invalidate downloads index" action in "Advanced"</string>

<!-- Tracking section -->
<string name="tracking_guide">Tracking guide</string>
Expand Down

0 comments on commit f6dcb24

Please sign in to comment.