Display the currently active restrictions in the library update preference (#5187)

* display the currently active restrictions in the library update preference

* removed first line

* use constant instead of literal string

* remove spanned string builder
This commit is contained in:
Gauthier 2021-05-26 20:57:13 +08:00 committed by GitHub
parent 8af8c57bb4
commit d9c27e7109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 8 deletions

View file

@ -8,7 +8,9 @@ import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import eu.kanade.tachiyomi.data.preference.CHARGING
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.UNMETERED_NETWORK
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -31,9 +33,9 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val interval = prefInterval ?: preferences.libraryUpdateInterval().get() val interval = prefInterval ?: preferences.libraryUpdateInterval().get()
if (interval > 0) { if (interval > 0) {
val restrictions = preferences.libraryUpdateRestriction()!! val restrictions = preferences.libraryUpdateRestriction().get()
val acRestriction = "ac" in restrictions val acRestriction = CHARGING in restrictions
val wifiRestriction = if ("wifi" in restrictions) { val wifiRestriction = if (UNMETERED_NETWORK in restrictions) {
NetworkType.UNMETERED NetworkType.UNMETERED
} else { } else {
NetworkType.CONNECTED NetworkType.CONNECTED

View file

@ -1,5 +1,8 @@
package eu.kanade.tachiyomi.data.preference package eu.kanade.tachiyomi.data.preference
const val UNMETERED_NETWORK = "wifi"
const val CHARGING = "ac"
/** /**
* This class stores the values for the preferences in the application. * This class stores the values for the preferences in the application.
*/ */

View file

@ -227,7 +227,7 @@ class PreferencesHelper(val context: Context) {
fun libraryUpdateInterval() = flowPrefs.getInt(Keys.libraryUpdateInterval, 24) fun libraryUpdateInterval() = flowPrefs.getInt(Keys.libraryUpdateInterval, 24)
fun libraryUpdateRestriction() = prefs.getStringSet(Keys.libraryUpdateRestriction, setOf("wifi")) fun libraryUpdateRestriction() = flowPrefs.getStringSet(Keys.libraryUpdateRestriction, setOf(UNMETERED_NETWORK))
fun libraryUpdateCategories() = flowPrefs.getStringSet(Keys.libraryUpdateCategories, emptySet()) fun libraryUpdateCategories() = flowPrefs.getStringSet(Keys.libraryUpdateCategories, emptySet())
fun libraryUpdateCategoriesExclude() = flowPrefs.getStringSet(Keys.libraryUpdateCategoriesExclude, emptySet()) fun libraryUpdateCategoriesExclude() = flowPrefs.getStringSet(Keys.libraryUpdateCategoriesExclude, emptySet())

View file

@ -12,7 +12,9 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.CHARGING
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.UNMETERED_NETWORK
import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction
@ -152,9 +154,8 @@ class SettingsLibraryController : SettingsController() {
key = Keys.libraryUpdateRestriction key = Keys.libraryUpdateRestriction
titleRes = R.string.pref_library_update_restriction titleRes = R.string.pref_library_update_restriction
entriesRes = arrayOf(R.string.network_unmetered, R.string.charging) entriesRes = arrayOf(R.string.network_unmetered, R.string.charging)
entryValues = arrayOf("wifi", "ac") entryValues = arrayOf(UNMETERED_NETWORK, CHARGING)
summaryRes = R.string.pref_library_update_restriction_summary defaultValue = setOf(UNMETERED_NETWORK)
defaultValue = setOf("wifi")
preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 } preferences.libraryUpdateInterval().asImmediateFlow { isVisible = it > 0 }
.launchIn(viewScope) .launchIn(viewScope)
@ -164,6 +165,29 @@ class SettingsLibraryController : SettingsController() {
Handler().post { LibraryUpdateJob.setupTask(context) } Handler().post { LibraryUpdateJob.setupTask(context) }
true true
} }
fun updateSummary() {
val restrictions = preferences.libraryUpdateRestriction().get()
.sorted()
.map {
when (it) {
UNMETERED_NETWORK -> context.getString(R.string.network_unmetered)
CHARGING -> context.getString(R.string.charging)
else -> it
}
}
val restrictionsText = if (restrictions.isEmpty()) {
context.getString(R.string.none)
} else {
restrictions.joinToString()
}
summary = context.getString(R.string.restrictions, restrictionsText)
}
preferences.libraryUpdateRestriction().asFlow()
.onEach { updateSummary() }
.launchIn(viewScope)
} }
switchPreference { switchPreference {
key = Keys.updateOnlyNonCompleted key = Keys.updateOnlyNonCompleted

View file

@ -213,9 +213,9 @@
<string name="update_weekly">Weekly</string> <string name="update_weekly">Weekly</string>
<string name="pref_library_update_prioritization">Update order</string> <string name="pref_library_update_prioritization">Update order</string>
<string name="pref_library_update_restriction">Update restrictions</string> <string name="pref_library_update_restriction">Update restrictions</string>
<string name="pref_library_update_restriction_summary">Update only when the conditions are met</string>
<string name="network_unmetered">Unmetered network</string> <string name="network_unmetered">Unmetered network</string>
<string name="charging">Charging</string> <string name="charging">Charging</string>
<string name="restrictions">Restrictions: %s</string>
<string name="pref_update_only_non_completed">Only update ongoing manga</string> <string name="pref_update_only_non_completed">Only update ongoing manga</string>
<string name="pref_library_update_refresh_metadata">Automatically refresh metadata</string> <string name="pref_library_update_refresh_metadata">Automatically refresh metadata</string>
<string name="pref_library_update_refresh_metadata_summary">Check for new cover and details when updating library</string> <string name="pref_library_update_refresh_metadata_summary">Check for new cover and details when updating library</string>