From 9e2124513bf0fd5b6b1364752d0ab352524aec2f Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Wed, 19 Jun 2019 16:19:31 +0800 Subject: [PATCH] ContextExtensions --- app/src/main/java/io/legado/app/App.kt | 4 ++-- app/src/main/java/io/legado/app/base/BaseActivity.kt | 2 +- .../io/legado/app/ui/config/ThemeConfigFragment.kt | 2 +- .../io/legado/app/ui/config/WebDavConfigFragment.kt | 9 ++++----- .../legado/app/ui/sourceedit/SourceEditViewModel.kt | 3 +-- .../java/io/legado/app/utils/ContextExtensions.kt | 12 ++++++------ 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 563183807..98caa5b44 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -53,7 +53,7 @@ class App : Application() { } fun initNightTheme() { - if (getPrefBoolean("isNightTheme", false)) { + if (getPrefBoolean("isNightTheme")) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) @@ -64,7 +64,7 @@ class App : Application() { * 更新主题 */ fun upThemeStore() { - if (getPrefBoolean("isNightTheme", false)) { + if (getPrefBoolean("isNightTheme")) { ThemeStore.editTheme(this) .primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.colorPrimary))) .accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.colorAccent))) diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index aa4f49d99..4d43f9ab1 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -83,7 +83,7 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScop window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) - if (getPrefBoolean("transparentStatusBar", false)) { + if (getPrefBoolean("transparentStatusBar")) { window.statusBarColor = Color.TRANSPARENT } else { window.statusBarColor = getCompatColor(R.color.status_bar_bag) diff --git a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt index 62d20203a..bd3a1f6ae 100644 --- a/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt @@ -131,7 +131,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar } private fun upTheme(isNightTheme: Boolean) { - if (App.INSTANCE.getPrefBoolean("isNightTheme", false) == isNightTheme) { + if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) { App.INSTANCE.upThemeStore() LiveEventBus.get().with(Bus.recreate).post("") Handler().postDelayed({ activity?.recreate() }, 100) diff --git a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt index d83e8b519..b6e8f778c 100644 --- a/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt @@ -30,14 +30,13 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference } private fun bindPreferenceSummaryToValue(preference: Preference?) { - preference?.let { - preference.onPreferenceChangeListener = this + preference?.apply { + onPreferenceChangeListener = this@WebDavConfigFragment onPreferenceChange( - preference, - preference.context.getPrefString(preference.key, "") + this, + context.getPrefString(key) ) } - } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt index 89138d790..c6e4c0ed6 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt @@ -15,8 +15,7 @@ class SourceEditViewModel(application: Application) : BaseViewModel(application) fun setBookSource(key: String) { launch(IO) { - val source = App.db.bookSourceDao().findByKey(key) - sourceLiveData.postValue(source) + App.db.bookSourceDao().findByKey(key)?.let { sourceLiveData.postValue(it) } } } diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 6a80cd841..94eb8a122 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -12,32 +12,32 @@ import org.jetbrains.anko.defaultSharedPreferences fun Context.isOnline() = connectivityManager.activeNetworkInfo?.isConnected == true -fun Context.getPrefBoolean(key: String, defValue: Boolean) = +fun Context.getPrefBoolean(key: String, defValue: Boolean = false) = defaultSharedPreferences.getBoolean(key, defValue) -fun Context.putPrefBoolean(key: String, value: Boolean) = +fun Context.putPrefBoolean(key: String, value: Boolean = false) = defaultSharedPreferences.edit { putBoolean(key, value) } -fun Context.getPrefInt(key: String, defValue: Int) = +fun Context.getPrefInt(key: String, defValue: Int = 0) = defaultSharedPreferences.getInt(key, defValue) fun Context.putPrefInt(key: String, value: Int) = defaultSharedPreferences.edit { putInt(key, value) } -fun Context.getPrefLong(key: String, defValue: Long) = +fun Context.getPrefLong(key: String, defValue: Long = 0L) = defaultSharedPreferences.getLong(key, defValue) fun Context.putPrefLong(key: String, value: Long) = defaultSharedPreferences.edit { putLong(key, value) } -fun Context.getPrefString(key: String, defValue: String) = +fun Context.getPrefString(key: String, defValue: String? = null) = defaultSharedPreferences.getString(key, defValue) fun Context.putPrefString(key: String, value: String) = defaultSharedPreferences.edit { putString(key, value) } -fun Context.getPrefStringSet(key: String, defValue: MutableSet) = +fun Context.getPrefStringSet(key: String, defValue: MutableSet? = null) = defaultSharedPreferences.getStringSet(key, defValue) fun Context.putPrefStringSet(key: String, value: MutableSet) =