Pixelcat-App-Android/app/src/main/kotlin/at/connyduck/pixelcat/components/settings/AppSettings.kt

65 lines
2.3 KiB
Kotlin
Raw Normal View History

2020-06-12 15:44:45 +02:00
package at.connyduck.pixelcat.components.settings
import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.StyleRes
import androidx.appcompat.app.AppCompatDelegate
2020-06-12 19:58:15 +02:00
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
2020-06-12 15:44:45 +02:00
import at.connyduck.pixelcat.R
import javax.inject.Inject
class AppSettings @Inject constructor (
2020-06-12 19:58:15 +02:00
private val sharedPrefs: SharedPreferences,
private val context: Context
2020-06-12 15:44:45 +02:00
) {
@StyleRes
fun getAppColorStyle(): Int {
val appColorPref = sharedPrefs.getString(
2020-06-12 19:58:15 +02:00
context.getString(R.string.key_pref_app_color),
context.getString(R.string.key_pref_app_color_default)
2020-06-12 15:44:45 +02:00
)
return when (appColorPref) {
context.getString(R.string.key_pref_app_color_warm) -> R.style.Warm
context.getString(R.string.key_pref_app_color_cold) -> R.style.Cold
else -> throw IllegalStateException()
}
}
@AppCompatDelegate.NightMode
fun getNightMode(): Int {
val nightModePref = sharedPrefs.getString(
2020-06-12 19:58:15 +02:00
context.getString(R.string.key_pref_night_mode),
context.getString(R.string.key_pref_night_mode_default)
2020-06-12 15:44:45 +02:00
)
return when (nightModePref) {
context.getString(R.string.key_pref_night_mode_off) -> MODE_NIGHT_NO
context.getString(R.string.key_pref_night_mode_on) -> MODE_NIGHT_YES
context.getString(R.string.key_pref_night_mode_follow_system) -> MODE_NIGHT_FOLLOW_SYSTEM
else -> throw IllegalStateException()
}
}
fun isBlackNightMode(): Boolean {
return sharedPrefs.getBoolean(
2020-06-12 19:58:15 +02:00
context.getString(R.string.key_pref_black_night_mode),
context.resources.getBoolean(R.bool.pref_title_black_night_mode_default)
2020-06-12 15:44:45 +02:00
)
}
fun useSystemFont(): Boolean {
2020-06-12 19:58:15 +02:00
return sharedPrefs.getBoolean(
2020-06-12 15:44:45 +02:00
context.getString(R.string.key_pref_system_font),
context.resources.getBoolean(R.bool.pref_title_system_font_default)
)
}
}
private fun SharedPreferences.getNonNullString(key: String, default: String): String {
return getString(key, default) ?: default
2020-06-12 19:58:15 +02:00
}