2020-10-08 22:20:20 +02:00
package org.mosad.teapod.preferences
2020-10-23 11:28:47 +02:00
import android.content.Context
2020-11-23 20:11:10 +01:00
import android.content.SharedPreferences
2020-10-23 11:28:47 +02:00
import org.mosad.teapod.R
2020-11-23 20:11:10 +01:00
import org.mosad.teapod.util.DataTypes
2020-10-23 11:28:47 +02:00
2020-10-08 22:20:20 +02:00
object Preferences {
2020-10-23 11:28:47 +02:00
var preferSecondary = false
2020-10-08 22:20:20 +02:00
internal set
2020-11-15 13:39:33 +01:00
var autoplay = true
internal set
2021-01-16 00:16:47 +01:00
var theme = DataTypes . Theme . DARK
2020-11-23 20:11:10 +01:00
internal set
2020-10-08 22:20:20 +02:00
2020-11-23 20:11:10 +01:00
private fun getSharedPref ( context : Context ) : SharedPreferences {
return context . getSharedPreferences (
2020-10-23 11:28:47 +02:00
context . getString ( R . string . preference _file _key ) ,
Context . MODE _PRIVATE
)
2020-11-23 20:11:10 +01:00
}
2020-10-08 22:20:20 +02:00
2020-11-23 20:11:10 +01:00
fun savePreferSecondary ( context : Context , preferSecondary : Boolean ) {
with ( getSharedPref ( context ) . edit ( ) ) {
2020-10-23 11:28:47 +02:00
putBoolean ( context . getString ( R . string . save _key _prefer _secondary ) , preferSecondary )
apply ( )
}
2020-10-08 22:20:20 +02:00
2020-10-23 11:28:47 +02:00
this . preferSecondary = preferSecondary
2020-10-08 22:20:20 +02:00
}
2020-11-15 13:39:33 +01:00
fun saveAutoplay ( context : Context , autoplay : Boolean ) {
2020-11-23 20:11:10 +01:00
with ( getSharedPref ( context ) . edit ( ) ) {
2020-11-15 13:39:33 +01:00
putBoolean ( context . getString ( R . string . save _key _autoplay ) , autoplay )
apply ( )
}
this . autoplay = autoplay
}
2020-11-23 20:11:10 +01:00
fun saveTheme ( context : Context , theme : DataTypes . Theme ) {
with ( getSharedPref ( context ) . edit ( ) ) {
putString ( context . getString ( R . string . save _key _theme ) , theme . toString ( ) )
apply ( )
}
this . theme = theme
}
2020-10-23 11:28:47 +02:00
/ * *
* initially load the stored values
* /
fun load ( context : Context ) {
2020-11-23 20:11:10 +01:00
val sharedPref = getSharedPref ( context )
2020-10-08 22:20:20 +02:00
2020-10-23 11:28:47 +02:00
preferSecondary = sharedPref . getBoolean (
context . getString ( R . string . save _key _prefer _secondary ) , false
)
2020-11-15 13:39:33 +01:00
autoplay = sharedPref . getBoolean (
context . getString ( R . string . save _key _autoplay ) , true
)
2020-11-23 20:11:10 +01:00
theme = DataTypes . Theme . valueOf (
sharedPref . getString (
2021-01-16 00:16:47 +01:00
context . getString ( R . string . save _key _theme ) , DataTypes . Theme . DARK . toString ( )
) ?: DataTypes . Theme . DARK . toString ( )
2020-11-23 20:11:10 +01:00
)
2020-10-08 22:20:20 +02:00
}
2020-10-23 11:28:47 +02:00
2020-10-08 22:20:20 +02:00
}