Skip to content

Commit

Permalink
Fix preference change listener going away
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbanes committed Jun 7, 2019
1 parent 5e2b35b commit 36865a8
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 75 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/app/tivi/inject/AppModuleBinds.kt
Expand Up @@ -27,6 +27,8 @@ import app.tivi.appinitializers.PreferencesInitializer
import app.tivi.appinitializers.RxAndroidInitializer
import app.tivi.appinitializers.ThreeTenBpInitializer
import app.tivi.appinitializers.TimberInitializer
import app.tivi.settings.TiviPreferences
import app.tivi.settings.TiviPreferencesImpl
import app.tivi.util.Logger
import app.tivi.util.TiviLogger
import dagger.Binds
Expand All @@ -49,6 +51,10 @@ abstract class AppModuleBinds {
@Binds
abstract fun provideLogger(bind: TiviLogger): Logger

@Singleton
@Binds
abstract fun providePreferences(bind: TiviPreferencesImpl): TiviPreferences

@Binds
@IntoSet
abstract fun provideEmojiInitializer(bind: EmojiInitializer): AppInitializer
Expand Down
62 changes: 0 additions & 62 deletions app/src/main/java/app/tivi/settings/TiviPreferences.kt

This file was deleted.

76 changes: 76 additions & 0 deletions app/src/main/java/app/tivi/settings/TiviPreferencesImpl.kt
@@ -0,0 +1,76 @@
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.settings

import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.edit
import app.tivi.R
import app.tivi.settings.TiviPreferences.Theme
import javax.inject.Inject
import javax.inject.Named

class TiviPreferencesImpl @Inject constructor(
private val context: Context,
@Named("app") private val sharedPreferences: SharedPreferences
) : TiviPreferences {
private val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
when (key) {
KEY_THEME -> updateUsingThemePreference()
}
}

private val defaultThemeValue = context.getString(R.string.pref_theme_default_value)

companion object {
const val KEY_THEME = "pref_theme"
}

override fun setup() {
updateUsingThemePreference()
sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
}

override var themePreference: Theme
get() = getThemeForStorageValue(sharedPreferences.getString(KEY_THEME, defaultThemeValue)!!)
set(value) = sharedPreferences.edit {
putString(KEY_THEME, getStorageKeyForTheme(value))
}

private fun getStorageKeyForTheme(theme: Theme) = when (theme) {
Theme.LIGHT -> context.getString(R.string.pref_theme_light_value)
Theme.DARK -> context.getString(R.string.pref_theme_dark_value)
Theme.BATTERY_SAVER_ONLY -> context.getString(R.string.pref_theme_battery_value)
Theme.SYSTEM -> context.getString(R.string.pref_theme_system_value)
}

private fun getThemeForStorageValue(value: String) = when (value) {
context.getString(R.string.pref_theme_system_value) -> Theme.SYSTEM
context.getString(R.string.pref_theme_light_value) -> Theme.LIGHT
context.getString(R.string.pref_theme_dark_value) -> Theme.DARK
context.getString(R.string.pref_theme_battery_value) -> Theme.BATTERY_SAVER_ONLY
else -> throw IllegalArgumentException("Invalid preference value for theme")
}

private fun updateUsingThemePreference() = when (themePreference) {
Theme.DARK -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Theme.LIGHT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Theme.SYSTEM -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
Theme.BATTERY_SAVER_ONLY -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
}
}
9 changes: 6 additions & 3 deletions app/src/main/res/values-v29/theme_prefs.xml
Expand Up @@ -16,15 +16,18 @@
-->

<resources>
<!-- On Q+ we default using the system default -->
<string name="pref_theme_default_value" translatable="false">@string/pref_theme_system_value</string>

<string-array name="prefs_theme_titles">
<item>@string/pref_theme_light</item>
<item>@string/pref_theme_dark</item>
<item>@string/pref_theme_system</item>
</string-array>

<string-array name="prefs_theme_values">
<item>@string/pref_theme_light_key</item>
<item>@string/pref_theme_dark_key</item>
<item>@string/pref_theme_system_key</item>
<item>@string/pref_theme_light_value</item>
<item>@string/pref_theme_dark_value</item>
<item>@string/pref_theme_system_value</item>
</string-array>
</resources>
17 changes: 10 additions & 7 deletions app/src/main/res/values/theme_prefs.xml
Expand Up @@ -22,10 +22,13 @@
<string name="pref_theme_battery">Set by Battery Saver</string>
<string name="pref_theme_system">Use system default</string>

<string name="pref_theme_light_key" translatable="false">light</string>
<string name="pref_theme_dark_key" translatable="false">dark</string>
<string name="pref_theme_battery_key" translatable="false">battery</string>
<string name="pref_theme_system_key" translatable="false">system</string>
<string name="pref_theme_light_value" translatable="false">light</string>
<string name="pref_theme_dark_value" translatable="false">dark</string>
<string name="pref_theme_battery_value" translatable="false">battery</string>
<string name="pref_theme_system_value" translatable="false">system</string>

<!-- Pre-Q we default to using Battery Saver -->
<string name="pref_theme_default_value" translatable="false">@string/pref_theme_battery_value</string>

<string-array name="prefs_theme_titles">
<item>@string/pref_theme_light</item>
Expand All @@ -34,8 +37,8 @@
</string-array>

<string-array name="prefs_theme_values">
<item>@string/pref_theme_light_key</item>
<item>@string/pref_theme_dark_key</item>
<item>@string/pref_theme_battery_key</item>
<item>@string/pref_theme_light_value</item>
<item>@string/pref_theme_dark_value</item>
<item>@string/pref_theme_battery_value</item>
</string-array>
</resources>
7 changes: 4 additions & 3 deletions app/src/main/res/xml/preferences.xml
Expand Up @@ -16,7 +16,8 @@
~
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory android:title="@string/settings_ui_category_title">

Expand All @@ -25,7 +26,8 @@
android:entries="@array/prefs_theme_titles"
android:entryValues="@array/prefs_theme_values"
android:key="pref_theme"
android:title="@string/settings_theme_title" />
android:title="@string/settings_theme_title"
app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

Expand All @@ -38,7 +40,6 @@
android:title="@string/view_privacy_policy" />

<Preference
android:enabled="false"
android:key="version"
android:title="@string/settings_app_version" />

Expand Down
31 changes: 31 additions & 0 deletions base/src/main/java/app/tivi/settings/TiviPreferences.kt
@@ -0,0 +1,31 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.settings

interface TiviPreferences {

fun setup()

val themePreference: Theme

enum class Theme {
LIGHT,
DARK,
BATTERY_SAVER_ONLY,
SYSTEM
}
}

0 comments on commit 36865a8

Please sign in to comment.