@@ -8,12 +8,15 @@ import io.reactivex.rxjava3.schedulers.Schedulers
88import kotlinx.coroutines.CoroutineScope
99import kotlinx.coroutines.Dispatchers
1010import kotlinx.coroutines.flow.Flow
11- import kotlinx.coroutines.flow.MutableStateFlow
1211import kotlinx.coroutines.flow.distinctUntilChanged
12+ import kotlinx.coroutines.flow.emptyFlow
13+ import kotlinx.coroutines.flow.firstOrNull
14+ import kotlinx.coroutines.flow.flatMapLatest
15+ import kotlinx.coroutines.flow.flow
1316import kotlinx.coroutines.flow.flowOf
1417import kotlinx.coroutines.flow.flowOn
1518import kotlinx.coroutines.flow.map
16- import kotlinx.coroutines.flow.onEach
19+ import kotlinx.coroutines.flow.onEmpty
1720import kotlinx.coroutines.launch
1821import timber.log.Timber
1922import javax.inject.Inject
@@ -22,15 +25,31 @@ import javax.inject.Inject
2225
2326class PrefRepository @Inject constructor(): CoroutineScope by CoroutineScope(Dispatchers .IO ) {
2427
25- fun get (key : PrefsString ): Flowable <String > {
28+ suspend fun get (key : PrefsString , default : String ): String {
29+ return observeOrDefault(key, default).firstOrNull() ? : default
30+ }
31+
32+ suspend fun get (key : PrefsBool , default : Boolean ): Boolean {
33+ return observeOrDefault(key, default).firstOrNull() ? : default
34+ }
35+
36+ suspend fun get (key : PrefDouble , default : Double ): Double {
37+ return observeOrDefault(key, default).firstOrNull() ? : default
38+ }
39+
40+ suspend fun get (key : PrefInt , default : Long ): Long {
41+ return observeOrDefault(key, default).firstOrNull() ? : default
42+ }
43+
44+ fun getFlowable (key : PrefsString ): Flowable <String > {
2645 val db = Database .getInstance() ? : return Flowable .empty()
2746 return db.prefStringDao().get(key.value)
2847 .subscribeOn(Schedulers .computation())
2948 .map { it.value }
3049 .distinctUntilChanged()
3150 }
3251
33- fun get (key : PrefsBool ): Flowable <Boolean > {
52+ fun getFlowable (key : PrefsBool ): Flowable <Boolean > {
3453 val db = Database .getInstance() ? : return Flowable .empty()
3554 return db.prefBoolDao().get(key.value)
3655 .subscribeOn(Schedulers .computation())
@@ -39,11 +58,10 @@ class PrefRepository @Inject constructor(): CoroutineScope by CoroutineScope(Dis
3958 }
4059
4160 fun observeOrDefault (key : PrefsBool , default : Boolean ): Flow <Boolean > {
42- val db = Database .getInstance() ? : return flowOf(default)
61+ val db = Database .getInstance() ? : return flowOf(default). also { Timber .e( " observe bool ; DB not available " ) }
4362 return db.prefBoolDao().observe(key.value)
4463 .flowOn(Dispatchers .IO )
4564 .map { it?.value ? : default }
46- .distinctUntilChanged()
4765 }
4866
4967 fun observeOrDefault (key : PrefsString , default : String ): Flow <String > {
@@ -70,7 +88,7 @@ class PrefRepository @Inject constructor(): CoroutineScope by CoroutineScope(Dis
7088 .distinctUntilChanged()
7189 }
7290
73- fun get (key : String ): Flowable <Long > {
91+ fun getFlowable (key : String ): Flowable <Long > {
7492 val db = Database .getInstance() ? : return Flowable .empty()
7593 return db.prefIntDao().get(key)
7694 .subscribeOn(Schedulers .computation())
@@ -124,7 +142,10 @@ class PrefRepository @Inject constructor(): CoroutineScope by CoroutineScope(Dis
124142
125143 fun set (key : PrefsBool , value : Boolean ) {
126144 launch {
127- Database .getInstance()?.prefBoolDao()?.insert(PrefBool (key.value, value))
145+ runCatching {
146+ val db = Database .getInstance() ? : throw IllegalStateException (" No DB" )
147+ db.prefBoolDao().insert(PrefBool (key.value, value))
148+ }.onFailure { Timber .d(it.message) }.onSuccess { Timber .d(" saved ${key.value} => $value " ) }
128149 }
129150 }
130151
0 commit comments