ContextExtensions

pull/32/head
Invinciblelee 6 years ago
parent ad6c347b30
commit 9e2124513b
  1. 4
      app/src/main/java/io/legado/app/App.kt
  2. 2
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  3. 2
      app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt
  4. 9
      app/src/main/java/io/legado/app/ui/config/WebDavConfigFragment.kt
  5. 3
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt
  6. 12
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -53,7 +53,7 @@ class App : Application() {
} }
fun initNightTheme() { fun initNightTheme() {
if (getPrefBoolean("isNightTheme", false)) { if (getPrefBoolean("isNightTheme")) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else { } else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
@ -64,7 +64,7 @@ class App : Application() {
* 更新主题 * 更新主题
*/ */
fun upThemeStore() { fun upThemeStore() {
if (getPrefBoolean("isNightTheme", false)) { if (getPrefBoolean("isNightTheme")) {
ThemeStore.editTheme(this) ThemeStore.editTheme(this)
.primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.colorPrimary))) .primaryColor(getPrefInt("colorPrimaryNight", getCompatColor(R.color.colorPrimary)))
.accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.colorAccent))) .accentColor(getPrefInt("colorAccentNight", getCompatColor(R.color.colorAccent)))

@ -83,7 +83,7 @@ abstract class BaseActivity<VM : ViewModel> : AppCompatActivity(), CoroutineScop
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) 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.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
if (getPrefBoolean("transparentStatusBar", false)) { if (getPrefBoolean("transparentStatusBar")) {
window.statusBarColor = Color.TRANSPARENT window.statusBarColor = Color.TRANSPARENT
} else { } else {
window.statusBarColor = getCompatColor(R.color.status_bar_bag) window.statusBarColor = getCompatColor(R.color.status_bar_bag)

@ -131,7 +131,7 @@ class ThemeConfigFragment : PreferenceFragmentCompat(), SharedPreferences.OnShar
} }
private fun upTheme(isNightTheme: Boolean) { private fun upTheme(isNightTheme: Boolean) {
if (App.INSTANCE.getPrefBoolean("isNightTheme", false) == isNightTheme) { if (App.INSTANCE.getPrefBoolean("isNightTheme") == isNightTheme) {
App.INSTANCE.upThemeStore() App.INSTANCE.upThemeStore()
LiveEventBus.get().with(Bus.recreate).post("") LiveEventBus.get().with(Bus.recreate).post("")
Handler().postDelayed({ activity?.recreate() }, 100) Handler().postDelayed({ activity?.recreate() }, 100)

@ -30,14 +30,13 @@ class WebDavConfigFragment : PreferenceFragmentCompat(), Preference.OnPreference
} }
private fun bindPreferenceSummaryToValue(preference: Preference?) { private fun bindPreferenceSummaryToValue(preference: Preference?) {
preference?.let { preference?.apply {
preference.onPreferenceChangeListener = this onPreferenceChangeListener = this@WebDavConfigFragment
onPreferenceChange( onPreferenceChange(
preference, this,
preference.context.getPrefString(preference.key, "") context.getPrefString(key)
) )
} }
} }
} }

@ -15,8 +15,7 @@ class SourceEditViewModel(application: Application) : BaseViewModel(application)
fun setBookSource(key: String) { fun setBookSource(key: String) {
launch(IO) { launch(IO) {
val source = App.db.bookSourceDao().findByKey(key) App.db.bookSourceDao().findByKey(key)?.let { sourceLiveData.postValue(it) }
sourceLiveData.postValue(source)
} }
} }

@ -12,32 +12,32 @@ import org.jetbrains.anko.defaultSharedPreferences
fun Context.isOnline() = connectivityManager.activeNetworkInfo?.isConnected == true 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) defaultSharedPreferences.getBoolean(key, defValue)
fun Context.putPrefBoolean(key: String, value: Boolean) = fun Context.putPrefBoolean(key: String, value: Boolean = false) =
defaultSharedPreferences.edit { putBoolean(key, value) } defaultSharedPreferences.edit { putBoolean(key, value) }
fun Context.getPrefInt(key: String, defValue: Int) = fun Context.getPrefInt(key: String, defValue: Int = 0) =
defaultSharedPreferences.getInt(key, defValue) defaultSharedPreferences.getInt(key, defValue)
fun Context.putPrefInt(key: String, value: Int) = fun Context.putPrefInt(key: String, value: Int) =
defaultSharedPreferences.edit { putInt(key, value) } defaultSharedPreferences.edit { putInt(key, value) }
fun Context.getPrefLong(key: String, defValue: Long) = fun Context.getPrefLong(key: String, defValue: Long = 0L) =
defaultSharedPreferences.getLong(key, defValue) defaultSharedPreferences.getLong(key, defValue)
fun Context.putPrefLong(key: String, value: Long) = fun Context.putPrefLong(key: String, value: Long) =
defaultSharedPreferences.edit { putLong(key, value) } defaultSharedPreferences.edit { putLong(key, value) }
fun Context.getPrefString(key: String, defValue: String) = fun Context.getPrefString(key: String, defValue: String? = null) =
defaultSharedPreferences.getString(key, defValue) defaultSharedPreferences.getString(key, defValue)
fun Context.putPrefString(key: String, value: String) = fun Context.putPrefString(key: String, value: String) =
defaultSharedPreferences.edit { putString(key, value) } defaultSharedPreferences.edit { putString(key, value) }
fun Context.getPrefStringSet(key: String, defValue: MutableSet<String>) = fun Context.getPrefStringSet(key: String, defValue: MutableSet<String>? = null) =
defaultSharedPreferences.getStringSet(key, defValue) defaultSharedPreferences.getStringSet(key, defValue)
fun Context.putPrefStringSet(key: String, value: MutableSet<String>) = fun Context.putPrefStringSet(key: String, value: MutableSet<String>) =

Loading…
Cancel
Save