Skip to content

Commit 7477ec9

Browse files
committed
Add primitive types settings
1 parent 91c015b commit 7477ec9

File tree

4 files changed

+228
-7
lines changed

4 files changed

+228
-7
lines changed

app/src/main/java/com/omega_r/base/simple/Settings.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package com.omega_r.base.simple
22

33
import com.omega_r.base.settings.BaseSettings
44
import com.omega_r.base.settings.SettingStorage
5+
import com.squareup.moshi.Moshi
6+
import com.squareup.moshi.adapter
57

6-
class Settings(setting: SettingStorage): BaseSettings(setting) {
8+
class Settings(setting: SettingStorage, moshi: Moshi): BaseSettings(setting) {
79

810
var firstLaunch by provideBoolean(key = "jklh", defaultValue = false, label = "possim")
911

12+
@OptIn(ExperimentalStdlibApi::class)
13+
var session: Session? by provideAnyJson(key = "", defaultValue = null, label = "sdf", moshi.adapter())
14+
15+
class Session(val token: String, val refreshToken: String)
1016

1117
}

core/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ dependencies {
8181
api 'com.github.Omega-R.OmegaIntentBuilder:core:1.3.4'
8282
api 'com.github.Omega-R.OmegaLaunchers:omegalauncherslib:1.0.6'
8383
api 'com.github.Omega-R:OmegaExtensions:1.0.6'
84-
api 'com.github.Omega-R.OmegaIntentBuilder:annotations:1.0.5'
84+
api 'com.github.Omega-R.OmegaIntentBuilder:annotations:1.3.4'
8585
api 'com.github.Omega-R:OmegaBind:1.0.5'
8686
api 'com.github.Omega-R:OmegaClicks:1.0.0'
8787
api 'com.github.Omega-R:OmegaAdapters:1.0.3'
@@ -90,7 +90,7 @@ dependencies {
9090
api project(':annotations')
9191

9292
api "com.squareup.retrofit2:retrofit:2.9.0"
93-
api "com.squareup.moshi:moshi-kotlin:1.11.0"
93+
api "com.squareup.moshi:moshi-kotlin:1.14.0"
9494

9595
// define any required OkHttp artifacts without version
9696
api "com.squareup.okhttp3:okhttp:3.14.9"

core/src/main/java/com/omega_r/base/settings/BaseSettings.kt

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.omega_r.base.settings
22

33
import com.omega_r.base.enitity.Percent
4-
import kotlin.properties.ReadWriteProperty
4+
import com.squareup.moshi.JsonAdapter
5+
import java.util.Date
6+
import kotlin.time.Duration
57

68
open class BaseSettings(private val setting: SettingStorage) {
79

@@ -37,6 +39,102 @@ open class BaseSettings(private val setting: SettingStorage) {
3739
)
3840
}
3941

42+
protected fun provideInt(
43+
key: String,
44+
defaultValue: Int,
45+
label: String,
46+
description: String = ""
47+
) = run {
48+
setting.provide(
49+
Setting.IntSetting(
50+
key = key,
51+
defaultValue = defaultValue,
52+
label = label,
53+
description = description
54+
)
55+
)
56+
}
57+
58+
protected fun provideFloat(
59+
key: String,
60+
defaultValue: Float,
61+
label: String,
62+
description: String = ""
63+
) = run {
64+
setting.provide(
65+
Setting.FloatSetting(
66+
key = key,
67+
defaultValue = defaultValue,
68+
label = label,
69+
description = description
70+
)
71+
)
72+
}
73+
74+
protected fun provideLong(
75+
key: String,
76+
defaultValue: Long,
77+
label: String,
78+
description: String = ""
79+
) = run {
80+
setting.provide(
81+
Setting.LongSetting(
82+
key = key,
83+
defaultValue = defaultValue,
84+
label = label,
85+
description = description
86+
)
87+
)
88+
}
89+
90+
protected fun provideDouble(
91+
key: String,
92+
defaultValue: Double,
93+
label: String,
94+
description: String = ""
95+
) = run {
96+
setting.provide(
97+
Setting.DoubleSetting(
98+
key = key,
99+
defaultValue = defaultValue,
100+
label = label,
101+
description = description
102+
)
103+
)
104+
}
105+
106+
protected fun provideDate(
107+
key: String,
108+
defaultValue: Date,
109+
label: String,
110+
description: String = ""
111+
) = run {
112+
setting.provide(
113+
Setting.DateSetting(
114+
key = key,
115+
defaultValue = defaultValue,
116+
label = label,
117+
description = description
118+
)
119+
)
120+
}
121+
122+
protected fun provideDuration(
123+
key: String,
124+
defaultValue: Duration,
125+
label: String,
126+
description: String = ""
127+
) = run {
128+
setting.provide(
129+
Setting.DurationSetting(
130+
key = key,
131+
defaultValue = defaultValue,
132+
label = label,
133+
description = description
134+
)
135+
)
136+
}
137+
40138
protected fun providePercent(
41139
key: String,
42140
defaultValue: Percent,
@@ -53,4 +151,23 @@ open class BaseSettings(private val setting: SettingStorage) {
53151
)
54152
}
55153

154+
155+
protected fun <T: Any?> provideAnyJson(
156+
key: String,
157+
defaultValue: T,
158+
label: String,
159+
jsonAdapter: JsonAdapter<T>,
160+
description: String = "",
161+
) = run {
162+
setting.provide(
163+
Setting.AnyJsonSetting(
164+
key = key,
165+
defaultValue = defaultValue,
166+
label = label,
167+
description = description,
168+
jsonAdapter = jsonAdapter
169+
)
170+
)
171+
}
172+
56173
}

core/src/main/java/com/omega_r/base/settings/Setting.kt

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import android.content.SharedPreferences
44
import com.omega_r.base.enitity.Percent
55
import com.omega_r.base.enitity.PercentModel
66
import com.omega_r.base.enitity.toPercent
7+
import com.squareup.moshi.JsonAdapter
8+
import java.util.Date
9+
import kotlin.time.Duration
10+
import kotlin.time.Duration.Companion.milliseconds
711

812
sealed class Setting<T>(
913
val key: String,
@@ -29,6 +33,77 @@ sealed class Setting<T>(
2933

3034
}
3135

36+
open class IntSetting(key: String, defaultValue: Int, label: String, description: String) :
37+
Setting<Int>(key = key, defaultValue = defaultValue, label = label, description = description) {
38+
override fun getValue(sharedPreferences: SharedPreferences): Int {
39+
return sharedPreferences.getInt(key, defaultValue)
40+
}
41+
42+
override fun setValue(editor: SharedPreferences.Editor, value: Int) {
43+
editor.putInt(key, value)
44+
}
45+
}
46+
47+
open class FloatSetting(key: String, defaultValue: Float, label: String, description: String) :
48+
Setting<Float>(key = key, defaultValue = defaultValue, label = label, description = description) {
49+
override fun getValue(sharedPreferences: SharedPreferences): Float {
50+
return sharedPreferences.getFloat(key, defaultValue)
51+
}
52+
53+
override fun setValue(editor: SharedPreferences.Editor, value: Float) {
54+
editor.putFloat(key, value)
55+
}
56+
}
57+
58+
open class LongSetting(key: String, defaultValue: Long, label: String, description: String) :
59+
Setting<Long>(key = key, defaultValue = defaultValue, label = label, description = description) {
60+
override fun getValue(sharedPreferences: SharedPreferences): Long {
61+
return sharedPreferences.getLong(key, defaultValue)
62+
}
63+
64+
override fun setValue(editor: SharedPreferences.Editor, value: Long) {
65+
editor.putLong(key, value)
66+
}
67+
}
68+
69+
open class DoubleSetting(key: String, defaultValue: Double, label: String, description: String) :
70+
Setting<Double>(key = key, defaultValue = defaultValue, label = label, description = description) {
71+
override fun getValue(sharedPreferences: SharedPreferences): Double {
72+
return java.lang.Double.longBitsToDouble(
73+
sharedPreferences.getLong(
74+
key,
75+
java.lang.Double.doubleToLongBits(defaultValue)
76+
)
77+
)
78+
}
79+
80+
override fun setValue(editor: SharedPreferences.Editor, value: Double) {
81+
editor.putLong(key, java.lang.Double.doubleToLongBits(value))
82+
}
83+
}
84+
85+
open class DateSetting(key: String, defaultValue: Date, label: String, description: String) :
86+
Setting<Date>(key = key, defaultValue = defaultValue, label = label, description = description) {
87+
override fun getValue(sharedPreferences: SharedPreferences): Date {
88+
return Date(sharedPreferences.getLong(key, defaultValue.time))
89+
}
90+
91+
override fun setValue(editor: SharedPreferences.Editor, value: Date) {
92+
editor.putLong(key, value.time)
93+
}
94+
}
95+
96+
open class DurationSetting(key: String, defaultValue: Duration, label: String, description: String) :
97+
Setting<Duration>(key = key, defaultValue = defaultValue, label = label, description = description) {
98+
override fun getValue(sharedPreferences: SharedPreferences): Duration {
99+
return sharedPreferences.getLong(key, defaultValue.inWholeMilliseconds).milliseconds
100+
}
101+
102+
override fun setValue(editor: SharedPreferences.Editor, value: Duration) {
103+
editor.putLong(key, value.inWholeMilliseconds)
104+
}
105+
}
106+
32107
open class StringSetting(key: String, defaultValue: String, label: String, description: String) :
33108
Setting<String>(key = key, defaultValue = defaultValue, label = label, description = description) {
34109
override fun getValue(sharedPreferences: SharedPreferences): String {
@@ -43,9 +118,9 @@ sealed class Setting<T>(
43118
open class PercentSetting(key: String, defaultValue: Percent, label: String, description: String) :
44119
Setting<Percent>(key = key, defaultValue = defaultValue, label = label, description = description) {
45120

46-
private companion object {
47-
private val percentModel = PercentModel(0f, 100f)
48-
}
121+
private companion object {
122+
private val percentModel = PercentModel(0f, 100f)
123+
}
49124

50125
override fun getValue(sharedPreferences: SharedPreferences): Percent {
51126
return sharedPreferences.getFloat(key, defaultValue.toFloat(percentModel)).toPercent(percentModel)
@@ -56,4 +131,27 @@ sealed class Setting<T>(
56131
}
57132
}
58133

134+
135+
open class AnyJsonSetting<T : Any?>(
136+
key: String,
137+
defaultValue: T,
138+
label: String,
139+
description: String,
140+
private val jsonAdapter: JsonAdapter<T>
141+
) : Setting<T>(key = key, defaultValue = defaultValue, label = label, description = description) {
142+
143+
private companion object {
144+
private const val DEFAULT_VALUE = "default_value"
145+
}
146+
147+
override fun getValue(sharedPreferences: SharedPreferences): T {
148+
val json = sharedPreferences.getString(key, DEFAULT_VALUE)
149+
return if (json == DEFAULT_VALUE) defaultValue else json?.let { jsonAdapter.fromJson(json) } as T
150+
}
151+
152+
override fun setValue(editor: SharedPreferences.Editor, value: T) {
153+
editor.putString(key, jsonAdapter.toJson(value))
154+
}
155+
}
156+
59157
}

0 commit comments

Comments
 (0)