Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix preference change listener going away
- Loading branch information
1 parent
5e2b35b
commit 36865a8
Showing
7 changed files
with
133 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
app/src/main/java/app/tivi/settings/TiviPreferencesImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |