Use 1.x preference abstraction (#8020)

* Use 1.x preference abstraction

- Uses SharedPreferences compared to 1.x impl which uses DataStore but it breaks all settings screens currently
- Move PreferencesHelper to new PreferenceStore
  - PreferencesHelper should be split into smaller preference stores and be in core or domain
- Remove flow preferences as new PreferenceStore handles changes for us

Co-authored-by: inorichi <3521738+inorichi@users.noreply.github.com>

* Fix PreferenceMutableState not updating

* Fix changes not emitting on first subscription

Co-authored-by: inorichi <3521738+inorichi@users.noreply.github.com>
This commit is contained in:
Andreas
2022-09-17 17:48:24 +02:00
committed by GitHub
parent bc8c45832e
commit 0086743a53
64 changed files with 698 additions and 340 deletions
@@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Target
import eu.kanade.tachiyomi.data.preference.PreferenceValues
import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.NetworkPreferences
import eu.kanade.tachiyomi.network.PREF_DOH_360
import eu.kanade.tachiyomi.network.PREF_DOH_ADGUARD
import eu.kanade.tachiyomi.network.PREF_DOH_ALIDNS
@@ -69,6 +70,7 @@ class SettingsAdvancedController(
private val network: NetworkHelper by injectLazy()
private val chapterCache: ChapterCache by injectLazy()
private val trackManager: TrackManager by injectLazy()
private val networkPreferences: NetworkPreferences by injectLazy()
@SuppressLint("BatteryLife")
override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply {
@@ -96,7 +98,7 @@ class SettingsAdvancedController(
}
switchPreference {
key = Keys.verboseLogging
key = networkPreferences.verboseLogging().key()
titleRes = R.string.pref_verbose_logging
summaryRes = R.string.pref_verbose_logging_summary
defaultValue = isDevFlavor
@@ -189,7 +191,7 @@ class SettingsAdvancedController(
onClick { clearWebViewData() }
}
intListPreference {
key = Keys.dohProvider
key = networkPreferences.dohProvider().key()
titleRes = R.string.pref_dns_over_https
entries = arrayOf(
context.getString(R.string.disabled),
@@ -227,10 +229,11 @@ class SettingsAdvancedController(
true
}
}
val defaultUserAgent = networkPreferences.defaultUserAgent()
editTextPreference {
key = Keys.defaultUserAgent
key = defaultUserAgent.key()
titleRes = R.string.pref_user_agent_string
text = preferences.defaultUserAgent().get()
text = defaultUserAgent.get()
summary = network.defaultUserAgent
onChange {
@@ -247,10 +250,10 @@ class SettingsAdvancedController(
key = "pref_reset_user_agent"
titleRes = R.string.pref_reset_user_agent_string
visibleIf(preferences.defaultUserAgent()) { it != preferences.defaultUserAgent().defaultValue }
visibleIf(defaultUserAgent) { it != defaultUserAgent.defaultValue() }
onClick {
preferences.defaultUserAgent().delete()
defaultUserAgent.delete()
activity?.toast(R.string.requires_app_restart)
}
}
@@ -136,7 +136,7 @@ class SettingsBackupController : SettingsController() {
}
}
preferences.backupsDirectory().asFlow()
preferences.backupsDirectory().changes()
.onEach { path ->
val dir = UniFile.fromUri(context, path.toUri())
summary = dir.filePath + "/automatic"
@@ -130,7 +130,7 @@ abstract class SettingsController : PreferenceController() {
(activity as? AppCompatActivity)?.supportActionBar?.title = getTitle()
}
inline fun <T> Preference.visibleIf(preference: com.fredporciuncula.flow.preferences.Preference<T>, crossinline block: (T) -> Boolean) {
inline fun <T> Preference.visibleIf(preference: eu.kanade.tachiyomi.core.preference.Preference<T>, crossinline block: (T) -> Boolean) {
preference.asHotFlow { isVisible = block(it) }
.launchIn(viewScope)
}
@@ -58,7 +58,7 @@ class SettingsDownloadController : SettingsController() {
ctrl.showDialog(router)
}
preferences.downloadsDirectory().asFlow()
preferences.downloadsDirectory().changes()
.onEach { path ->
val dir = UniFile.fromUri(context, path.toUri())
summary = dir.filePath ?: path
@@ -114,7 +114,7 @@ class SettingsDownloadController : SettingsController() {
entries = categories.map { it.visualName(context) }.toTypedArray()
entryValues = categories.map { it.id.toString() }.toTypedArray()
preferences.removeExcludeCategories().asFlow()
preferences.removeExcludeCategories().changes()
.onEach { mutable ->
val selected = mutable
.mapNotNull { id -> categories.find { it.id == id.toLong() } }
@@ -171,10 +171,10 @@ class SettingsDownloadController : SettingsController() {
}
}
preferences.downloadNewChapterCategories().asFlow()
preferences.downloadNewChapterCategories().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
preferences.downloadNewChapterCategoriesExclude().asFlow()
preferences.downloadNewChapterCategoriesExclude().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
}
@@ -79,7 +79,7 @@ class SettingsLibraryController : SettingsController() {
}
}
combine(preferences.portraitColumns().asFlow(), preferences.landscapeColumns().asFlow()) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) }
combine(preferences.portraitColumns().changes(), preferences.landscapeColumns().changes()) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) }
.onEach { (portraitCols, landscapeCols) ->
val portrait = getColumnValue(portraitCols)
val landscape = getColumnValue(landscapeCols)
@@ -114,7 +114,7 @@ class SettingsLibraryController : SettingsController() {
entryValues = arrayOf("-1") + allCategories.map { it.id.toString() }.toTypedArray()
defaultValue = "-1"
val selectedCategory = allCategories.find { it.id == preferences.defaultCategory().toLong() }
val selectedCategory = allCategories.find { it.id == preferences.defaultCategory().get().toLong() }
summary = selectedCategory?.visualName(context)
?: context.getString(R.string.default_category_summary)
onChange { newValue ->
@@ -129,7 +129,7 @@ class SettingsLibraryController : SettingsController() {
bindTo(preferences.categorizedDisplaySettings())
titleRes = R.string.categorized_display_settings
preferences.categorizedDisplaySettings().asFlow()
preferences.categorizedDisplaySettings().changes()
.onEach {
if (it.not()) {
resetCategoryFlags.await()
@@ -197,7 +197,7 @@ class SettingsLibraryController : SettingsController() {
summary = context.getString(R.string.restrictions, restrictionsText)
}
preferences.libraryUpdateDeviceRestriction().asFlow()
preferences.libraryUpdateDeviceRestriction().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
}
@@ -226,7 +226,7 @@ class SettingsLibraryController : SettingsController() {
summary = restrictionsText
}
preferences.libraryUpdateMangaRestriction().asFlow()
preferences.libraryUpdateMangaRestriction().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
}
@@ -269,10 +269,10 @@ class SettingsLibraryController : SettingsController() {
}
}
preferences.libraryUpdateCategories().asFlow()
preferences.libraryUpdateCategories().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
preferences.libraryUpdateCategoriesExclude().asFlow()
preferences.libraryUpdateCategoriesExclude().changes()
.onEach { updateSummary() }
.launchIn(viewScope)
}