Split general settings into general and library

This commit is contained in:
arkon 2020-01-09 21:22:58 -05:00
parent 83d5e458ca
commit 044a4f7575
4 changed files with 216 additions and 192 deletions

View File

@ -1,29 +1,12 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import androidx.preference.PreferenceScreen import androidx.preference.PreferenceScreen
import android.view.View
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.util.LocaleHelper import eu.kanade.tachiyomi.util.LocaleHelper
import kotlinx.android.synthetic.main.pref_library_columns.view.*
import rx.Observable
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsGeneralController : SettingsController() { class SettingsGeneralController : SettingsController() {
private val db: DatabaseHelper = Injekt.get()
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) { override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
titleRes = R.string.pref_category_general titleRes = R.string.pref_category_general
@ -64,30 +47,6 @@ class SettingsGeneralController : SettingsController() {
true true
} }
} }
preference {
titleRes = R.string.pref_library_columns
onClick {
LibraryColumnsDialog().showDialog(router)
}
fun getColumnValue(value: Int): String {
return if (value == 0)
context.getString(R.string.default_columns)
else
value.toString()
}
Observable.combineLatest(
preferences.portraitColumns().asObservable(),
preferences.landscapeColumns().asObservable()
) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) }
.subscribeUntilDestroy { (portraitCols, landscapeCols) ->
val portrait = getColumnValue(portraitCols)
val landscape = getColumnValue(landscapeCols)
summary = "${context.getString(R.string.portrait)}: $portrait, " +
"${context.getString(R.string.landscape)}: $landscape"
}
}
intListPreference { intListPreference {
key = Keys.startScreen key = Keys.startScreen
titleRes = R.string.pref_start_screen titleRes = R.string.pref_start_screen
@ -97,149 +56,6 @@ class SettingsGeneralController : SettingsController() {
defaultValue = "1" defaultValue = "1"
summary = "%s" summary = "%s"
} }
intListPreference {
key = Keys.libraryUpdateInterval
titleRes = R.string.pref_library_update_interval
entriesRes = arrayOf(R.string.update_never, R.string.update_1hour,
R.string.update_2hour, R.string.update_3hour, R.string.update_6hour,
R.string.update_12hour, R.string.update_24hour, R.string.update_48hour)
entryValues = arrayOf("0", "1", "2", "3", "6", "12", "24", "48")
defaultValue = "0"
summary = "%s"
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated.
LibraryUpdateJob.cancelTask()
val interval = (newValue as String).toInt()
if (interval > 0) {
LibraryUpdateJob.setupTask(interval)
}
true
}
}
multiSelectListPreference {
key = Keys.libraryUpdateRestriction
titleRes = R.string.pref_library_update_restriction
entriesRes = arrayOf(R.string.wifi, R.string.charging)
entryValues = arrayOf("wifi", "ac")
summaryRes = R.string.pref_library_update_restriction_summary
preferences.libraryUpdateInterval().asObservable()
.subscribeUntilDestroy { isVisible = it > 0 }
onChange {
// Post to event looper to allow the preference to be updated.
Handler().post { LibraryUpdateJob.setupTask() }
true
}
}
switchPreference {
key = Keys.updateOnlyNonCompleted
titleRes = R.string.pref_update_only_non_completed
defaultValue = false
}
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault()) + dbCategories
multiSelectListPreference {
key = Keys.libraryUpdateCategories
titleRes = R.string.pref_library_update_categories
entries = categories.map { it.name }.toTypedArray()
entryValues = categories.map { it.id.toString() }.toTypedArray()
preferences.libraryUpdateCategories().asObservable()
.subscribeUntilDestroy {
val selectedCategories = it
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.sortedBy { it.order }
summary = if (selectedCategories.isEmpty())
context.getString(R.string.all)
else
selectedCategories.joinToString { it.name }
}
}
intListPreference{
key = Keys.libraryUpdatePrioritization
titleRes = R.string.pref_library_update_prioritization
// The following arrays are to be lined up with the list rankingScheme in:
// ../../data/library/LibraryUpdateRanker.kt
entriesRes = arrayOf(
R.string.action_sort_alpha,
R.string.action_sort_last_updated
)
entryValues = arrayOf(
"0",
"1"
)
defaultValue = "0"
summaryRes = R.string.pref_library_update_prioritization_summary
}
intListPreference {
key = Keys.defaultCategory
titleRes = R.string.default_category
val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
entries = arrayOf(context.getString(R.string.default_category_summary)) +
categories.map { it.name }.toTypedArray()
entryValues = arrayOf("-1") + categories.map { it.id.toString() }.toTypedArray()
defaultValue = "-1"
summary = selectedCategory?.name ?: context.getString(R.string.default_category_summary)
onChange { newValue ->
summary = categories.find {
it.id == (newValue as String).toInt()
}?.name ?: context.getString(R.string.default_category_summary)
true
}
}
}
class LibraryColumnsDialog : DialogController() {
private val preferences: PreferencesHelper = Injekt.get()
private var portrait = preferences.portraitColumns().getOrDefault()
private var landscape = preferences.landscapeColumns().getOrDefault()
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val dialog = MaterialDialog.Builder(activity!!)
.title(R.string.pref_library_columns)
.customView(R.layout.pref_library_columns, false)
.positiveText(android.R.string.ok)
.negativeText(android.R.string.cancel)
.onPositive { _, _ ->
preferences.portraitColumns().set(portrait)
preferences.landscapeColumns().set(landscape)
}
.build()
onViewCreated(dialog.view)
return dialog
}
fun onViewCreated(view: View) {
with(view.portrait_columns) {
displayedValues = arrayOf(context.getString(R.string.default_columns)) +
IntRange(1, 10).map(Int::toString)
value = portrait
setOnValueChangedListener { _, _, newValue ->
portrait = newValue
}
}
with(view.landscape_columns) {
displayedValues = arrayOf(context.getString(R.string.default_columns)) +
IntRange(1, 10).map(Int::toString)
value = landscape
setOnValueChangedListener { _, _, newValue ->
landscape = newValue
}
}
}
} }
} }

View File

@ -0,0 +1,199 @@
package eu.kanade.tachiyomi.ui.setting
import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import kotlinx.android.synthetic.main.pref_library_columns.view.landscape_columns
import kotlinx.android.synthetic.main.pref_library_columns.view.portrait_columns
import rx.Observable
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
class SettingsLibraryController : SettingsController() {
private val db: DatabaseHelper = Injekt.get()
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
titleRes = R.string.pref_category_library
preference {
titleRes = R.string.pref_library_columns
onClick {
LibraryColumnsDialog().showDialog(router)
}
fun getColumnValue(value: Int): String {
return if (value == 0)
context.getString(R.string.default_columns)
else
value.toString()
}
Observable.combineLatest(
preferences.portraitColumns().asObservable(),
preferences.landscapeColumns().asObservable()
) { portraitCols, landscapeCols -> Pair(portraitCols, landscapeCols) }
.subscribeUntilDestroy { (portraitCols, landscapeCols) ->
val portrait = getColumnValue(portraitCols)
val landscape = getColumnValue(landscapeCols)
summary = "${context.getString(R.string.portrait)}: $portrait, " +
"${context.getString(R.string.landscape)}: $landscape"
}
}
intListPreference {
key = Keys.libraryUpdateInterval
titleRes = R.string.pref_library_update_interval
entriesRes = arrayOf(R.string.update_never, R.string.update_1hour,
R.string.update_2hour, R.string.update_3hour, R.string.update_6hour,
R.string.update_12hour, R.string.update_24hour, R.string.update_48hour)
entryValues = arrayOf("0", "1", "2", "3", "6", "12", "24", "48")
defaultValue = "0"
summary = "%s"
onChange { newValue ->
// Always cancel the previous task, it seems that sometimes they are not updated.
LibraryUpdateJob.cancelTask()
val interval = (newValue as String).toInt()
if (interval > 0) {
LibraryUpdateJob.setupTask(interval)
}
true
}
}
multiSelectListPreference {
key = Keys.libraryUpdateRestriction
titleRes = R.string.pref_library_update_restriction
entriesRes = arrayOf(R.string.wifi, R.string.charging)
entryValues = arrayOf("wifi", "ac")
summaryRes = R.string.pref_library_update_restriction_summary
preferences.libraryUpdateInterval().asObservable()
.subscribeUntilDestroy { isVisible = it > 0 }
onChange {
// Post to event looper to allow the preference to be updated.
Handler().post { LibraryUpdateJob.setupTask() }
true
}
}
switchPreference {
key = Keys.updateOnlyNonCompleted
titleRes = R.string.pref_update_only_non_completed
defaultValue = false
}
val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault()) + dbCategories
multiSelectListPreference {
key = Keys.libraryUpdateCategories
titleRes = R.string.pref_library_update_categories
entries = categories.map { it.name }.toTypedArray()
entryValues = categories.map { it.id.toString() }.toTypedArray()
preferences.libraryUpdateCategories().asObservable()
.subscribeUntilDestroy {
val selectedCategories = it
.mapNotNull { id -> categories.find { it.id == id.toInt() } }
.sortedBy { it.order }
summary = if (selectedCategories.isEmpty())
context.getString(R.string.all)
else
selectedCategories.joinToString { it.name }
}
}
intListPreference{
key = Keys.libraryUpdatePrioritization
titleRes = R.string.pref_library_update_prioritization
// The following arrays are to be lined up with the list rankingScheme in:
// ../../data/library/LibraryUpdateRanker.kt
entriesRes = arrayOf(
R.string.action_sort_alpha,
R.string.action_sort_last_updated
)
entryValues = arrayOf(
"0",
"1"
)
defaultValue = "0"
summaryRes = R.string.pref_library_update_prioritization_summary
}
intListPreference {
key = Keys.defaultCategory
titleRes = R.string.default_category
val selectedCategory = categories.find { it.id == preferences.defaultCategory() }
entries = arrayOf(context.getString(R.string.default_category_summary)) +
categories.map { it.name }.toTypedArray()
entryValues = arrayOf("-1") + categories.map { it.id.toString() }.toTypedArray()
defaultValue = "-1"
summary = selectedCategory?.name ?: context.getString(R.string.default_category_summary)
onChange { newValue ->
summary = categories.find {
it.id == (newValue as String).toInt()
}?.name ?: context.getString(R.string.default_category_summary)
true
}
}
}
class LibraryColumnsDialog : DialogController() {
private val preferences: PreferencesHelper = Injekt.get()
private var portrait = preferences.portraitColumns().getOrDefault()
private var landscape = preferences.landscapeColumns().getOrDefault()
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val dialog = MaterialDialog.Builder(activity!!)
.title(R.string.pref_library_columns)
.customView(R.layout.pref_library_columns, false)
.positiveText(android.R.string.ok)
.negativeText(android.R.string.cancel)
.onPositive { _, _ ->
preferences.portraitColumns().set(portrait)
preferences.landscapeColumns().set(landscape)
}
.build()
onViewCreated(dialog.view)
return dialog
}
fun onViewCreated(view: View) {
with(view.portrait_columns) {
displayedValues = arrayOf(context.getString(R.string.default_columns)) +
IntRange(1, 10).map(Int::toString)
value = portrait
setOnValueChangedListener { _, _, newValue ->
portrait = newValue
}
}
with(view.landscape_columns) {
displayedValues = arrayOf(context.getString(R.string.default_columns)) +
IntRange(1, 10).map(Int::toString)
value = landscape
setOnValueChangedListener { _, _, newValue ->
landscape = newValue
}
}
}
}
}

View File

@ -17,6 +17,12 @@ class SettingsMainController : SettingsController() {
titleRes = R.string.pref_category_general titleRes = R.string.pref_category_general
onClick { navigateTo(SettingsGeneralController()) } onClick { navigateTo(SettingsGeneralController()) }
} }
preference {
iconRes = R.drawable.ic_book_black_24dp
iconTint = tintColor
titleRes = R.string.pref_category_library
onClick { navigateTo(SettingsLibraryController()) }
}
preference { preference {
iconRes = R.drawable.ic_chrome_reader_mode_black_24dp iconRes = R.drawable.ic_chrome_reader_mode_black_24dp
iconTint = tintColor iconTint = tintColor

View File

@ -109,6 +109,7 @@
<!-- Preferences --> <!-- Preferences -->
<!-- Subsections --> <!-- Subsections -->
<string name="pref_category_general">General</string> <string name="pref_category_general">General</string>
<string name="pref_category_library">Library</string>
<string name="pref_category_reader">Reader</string> <string name="pref_category_reader">Reader</string>
<string name="pref_category_downloads">Downloads</string> <string name="pref_category_downloads">Downloads</string>
<string name="pref_category_sources">Sources</string> <string name="pref_category_sources">Sources</string>
@ -117,6 +118,16 @@
<string name="pref_category_about">About</string> <string name="pref_category_about">About</string>
<!-- General section --> <!-- General section -->
<string name="pref_theme">App theme</string>
<string name="light_theme">Light</string>
<string name="dark_theme">Dark</string>
<string name="amoled_theme">AMOLED dark</string>
<string name="darkblue_theme">Dark blue</string>
<string name="pref_start_screen">Start screen</string>
<string name="pref_language">Language</string>
<string name="system_default">System default</string>
<!-- Library section -->
<string name="pref_library_columns">Library manga per row</string> <string name="pref_library_columns">Library manga per row</string>
<string name="portrait">Portrait</string> <string name="portrait">Portrait</string>
<string name="landscape">Landscape</string> <string name="landscape">Landscape</string>
@ -143,14 +154,6 @@
<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_auto_update_manga_sync">Sync chapters after reading</string> <string name="pref_auto_update_manga_sync">Sync chapters after reading</string>
<string name="pref_ask_update_manga_sync">Confirm before updating</string> <string name="pref_ask_update_manga_sync">Confirm before updating</string>
<string name="pref_theme">App theme</string>
<string name="light_theme">Light</string>
<string name="dark_theme">Dark</string>
<string name="amoled_theme">AMOLED dark</string>
<string name="darkblue_theme">Dark blue</string>
<string name="pref_start_screen">Start screen</string>
<string name="pref_language">Language</string>
<string name="system_default">System default</string>
<string name="default_category">Default category</string> <string name="default_category">Default category</string>
<string name="default_category_summary">Always ask</string> <string name="default_category_summary">Always ask</string>