From 4524c705dab44bb51bbdda38fe4c5257e361c503 Mon Sep 17 00:00:00 2001 From: len Date: Thu, 6 Oct 2016 19:39:59 +0200 Subject: [PATCH] Add simple method for preference bindings --- .../ui/setting/SettingsAboutFragment.kt | 21 +++++++++---------- .../ui/setting/SettingsAdvancedFragment.kt | 9 ++++---- .../ui/setting/SettingsDownloadsFragment.kt | 3 ++- .../tachiyomi/ui/setting/SettingsFragment.kt | 5 +++++ .../ui/setting/SettingsGeneralFragment.kt | 21 +++++-------------- .../ui/setting/SettingsSyncFragment.kt | 3 ++- app/src/main/res/values/keys.xml | 1 + app/src/main/res/xml/pref_sync.xml | 2 +- 8 files changed, 31 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutFragment.kt index aa8b97b94f..22e309fd77 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutFragment.kt @@ -22,6 +22,15 @@ import java.text.SimpleDateFormat import java.util.* class SettingsAboutFragment : SettingsFragment() { + + companion object { + fun newInstance(rootKey: String): SettingsAboutFragment { + val args = Bundle() + args.putString(XpPreferenceFragment.ARG_PREFERENCE_ROOT, rootKey) + return SettingsAboutFragment().apply { arguments = args } + } + } + /** * Checks for new releases */ @@ -32,17 +41,7 @@ class SettingsAboutFragment : SettingsFragment() { */ private var releaseSubscription: Subscription? = null - val automaticUpdates by lazy { - findPreference(getString(R.string.pref_enable_automatic_updates_key)) as SwitchPreference - } - - companion object { - fun newInstance(rootKey: String): SettingsAboutFragment { - val args = Bundle() - args.putString(XpPreferenceFragment.ARG_PREFERENCE_ROOT, rootKey) - return SettingsAboutFragment().apply { arguments = args } - } - } + val automaticUpdates: SwitchPreference by bindPref(R.string.pref_enable_automatic_updates_key) override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt index 42f8e34fce..ad3c918fa3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedFragment.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.ui.setting import android.os.Bundle +import android.support.v7.preference.Preference import android.support.v7.preference.XpPreferenceFragment import android.view.View import com.afollestad.materialdialogs.MaterialDialog @@ -33,13 +34,13 @@ class SettingsAdvancedFragment : SettingsFragment() { private val db: DatabaseHelper by injectLazy() - private val clearCache by lazy { findPreference(getString(R.string.pref_clear_chapter_cache_key)) } + private val clearCache: Preference by bindPref(R.string.pref_clear_chapter_cache_key) - private val clearDatabase by lazy { findPreference(getString(R.string.pref_clear_database_key)) } + private val clearDatabase: Preference by bindPref(R.string.pref_clear_database_key) - private val clearCookies by lazy { findPreference(getString(R.string.pref_clear_cookies_key)) } + private val clearCookies: Preference by bindPref(R.string.pref_clear_cookies_key) - private val refreshMetadata by lazy { findPreference(getString(R.string.pref_refresh_library_metadata_key)) } + private val refreshMetadata: Preference by bindPref(R.string.pref_refresh_library_metadata_key) override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt index 9ff03c7f67..3168cb1baa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import android.os.Environment import android.support.v4.content.ContextCompat +import android.support.v7.preference.Preference import android.support.v7.preference.XpPreferenceFragment import android.support.v7.widget.RecyclerView import android.view.View @@ -36,7 +37,7 @@ class SettingsDownloadsFragment : SettingsFragment() { private val preferences: PreferencesHelper by injectLazy() - val downloadDirPref by lazy { findPreference(getString(R.string.pref_download_directory_key)) } + val downloadDirPref: Preference by bindPref(R.string.pref_download_directory_key) override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsFragment.kt index 9fc116b765..6431ad249b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.support.annotation.CallSuper import android.support.graphics.drawable.VectorDrawableCompat import android.support.v4.content.ContextCompat +import android.support.v7.preference.Preference import android.support.v7.preference.XpPreferenceFragment import android.view.View import eu.kanade.tachiyomi.R @@ -83,4 +84,8 @@ open class SettingsFragment : XpPreferenceFragment() { "about_screen" to R.drawable.ic_help_black_24dp ) + protected inline fun bindPref(resId: Int): Lazy { + return lazy { findPreference(getString(resId)) as T } + } + } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt index 06b5806893..3de6714ee9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralFragment.kt @@ -34,26 +34,15 @@ class SettingsGeneralFragment : SettingsFragment(), private val db: DatabaseHelper by injectLazy() + val columnsPreference: SimpleDialogPreference by bindPref(R.string.pref_library_columns_dialog_key) - val columnsPreference by lazy { - findPreference(getString(R.string.pref_library_columns_dialog_key)) as SimpleDialogPreference - } + val updateInterval: IntListPreference by bindPref(R.string.pref_library_update_interval_key) - val updateInterval by lazy { - findPreference(getString(R.string.pref_library_update_interval_key)) as IntListPreference - } + val updateRestriction: MultiSelectListPreference by bindPref(R.string.pref_library_update_restriction_key) - val updateRestriction by lazy { - findPreference(getString(R.string.pref_library_update_restriction_key)) as MultiSelectListPreference - } + val themePreference: IntListPreference by bindPref(R.string.pref_theme_key) - val themePreference by lazy { - findPreference(getString(R.string.pref_theme_key)) as IntListPreference - } - - val categoryUpdate by lazy { - findPreference(getString(R.string.pref_library_update_categories_key)) as MultiSelectListPreference - } + val categoryUpdate: MultiSelectListPreference by bindPref(R.string.pref_library_update_categories_key) override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSyncFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSyncFragment.kt index 8cf1fcdd12..e701056fac 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSyncFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsSyncFragment.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.support.v7.preference.PreferenceCategory import android.support.v7.preference.XpPreferenceFragment import android.view.View +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.mangasync.MangaSyncManager import eu.kanade.tachiyomi.data.mangasync.MangaSyncService import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -28,7 +29,7 @@ class SettingsSyncFragment : SettingsFragment() { private val preferences: PreferencesHelper by injectLazy() - val syncCategory by lazy { findPreference("pref_category_manga_sync_accounts") as PreferenceCategory } + val syncCategory: PreferenceCategory by bindPref(R.string.pref_category_manga_sync_accounts_key) override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 04ef15297b..b7f135600d 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -53,6 +53,7 @@ last_used_category pref_source_languages + category_manga_sync_accounts pref_clear_chapter_cache_key pref_clear_database_key diff --git a/app/src/main/res/xml/pref_sync.xml b/app/src/main/res/xml/pref_sync.xml index d9c24c54cc..fc7a8bd78d 100644 --- a/app/src/main/res/xml/pref_sync.xml +++ b/app/src/main/res/xml/pref_sync.xml @@ -18,7 +18,7 @@ android:dependency="@string/pref_auto_update_manga_sync_key"/>