@@ -7,10 +7,21 @@ import io.reactivex.rxjava3.core.Single
77import io.reactivex.rxjava3.schedulers.Schedulers
88import kotlinx.coroutines.CoroutineScope
99import kotlinx.coroutines.Dispatchers
10+ import kotlinx.coroutines.flow.Flow
11+ import kotlinx.coroutines.flow.MutableStateFlow
12+ import kotlinx.coroutines.flow.distinctUntilChanged
13+ import kotlinx.coroutines.flow.flowOf
14+ import kotlinx.coroutines.flow.flowOn
15+ import kotlinx.coroutines.flow.map
16+ import kotlinx.coroutines.flow.onEach
1017import kotlinx.coroutines.launch
18+ import timber.log.Timber
1119import javax.inject.Inject
1220
13- class PrefRepository @Inject constructor() {
21+
22+
23+ class PrefRepository @Inject constructor(): CoroutineScope by CoroutineScope(Dispatchers .IO ) {
24+
1425 fun get (key : PrefsString ): Flowable <String > {
1526 val db = Database .getInstance() ? : return Flowable .empty()
1627 return db.prefStringDao().get(key.value)
@@ -27,6 +38,38 @@ class PrefRepository @Inject constructor() {
2738 .distinctUntilChanged()
2839 }
2940
41+ fun observeOrDefault (key : PrefsBool , default : Boolean ): Flow <Boolean > {
42+ val db = Database .getInstance() ? : return flowOf(default)
43+ return db.prefBoolDao().observe(key.value)
44+ .flowOn(Dispatchers .IO )
45+ .map { it?.value ? : default }
46+ .distinctUntilChanged()
47+ }
48+
49+ fun observeOrDefault (key : PrefsString , default : String ): Flow <String > {
50+ val db = Database .getInstance() ? : return flowOf(default)
51+ return db.prefStringDao().observe(key.value)
52+ .flowOn(Dispatchers .IO )
53+ .map { it?.value ? : default }
54+ .distinctUntilChanged()
55+ }
56+
57+ fun observeOrDefault (key : PrefDouble , default : Double ): Flow <Double > {
58+ val db = Database .getInstance() ? : return flowOf(default)
59+ return db.prefDoubleDao().observe(key.key)
60+ .flowOn(Dispatchers .IO )
61+ .map { it?.value ? : default }
62+ .distinctUntilChanged()
63+ }
64+
65+ fun observeOrDefault (key : PrefInt , default : Long ): Flow <Long > {
66+ val db = Database .getInstance() ? : return flowOf(default)
67+ return db.prefIntDao().observe(key.key)
68+ .flowOn(Dispatchers .IO )
69+ .map { it?.value ? : default }
70+ .distinctUntilChanged()
71+ }
72+
3073 fun get (key : String ): Flowable <Long > {
3174 val db = Database .getInstance() ? : return Flowable .empty()
3275 return db.prefIntDao().get(key)
@@ -60,7 +103,7 @@ class PrefRepository @Inject constructor() {
60103 }
61104
62105 fun set (vararg list : Pair <PrefsString , String >) {
63- CoroutineScope ( Dispatchers . IO ). launch {
106+ launch {
64107 list.forEach { pair ->
65108 Database .getInstance()?.prefStringDao()?.insert(PrefString (pair.first.value, pair.second))
66109 }
@@ -74,13 +117,13 @@ class PrefRepository @Inject constructor() {
74117 fun set (key : String , value : Int ) = set(key, value.toLong())
75118
76119 fun set (key : String , value : Long ) {
77- CoroutineScope ( Dispatchers . IO ). launch {
120+ launch {
78121 Database .getInstance()?.prefIntDao()?.insert(PrefInt (key, value))
79122 }
80123 }
81124
82125 fun set (key : PrefsBool , value : Boolean ) {
83- CoroutineScope ( Dispatchers . IO ). launch {
126+ launch {
84127 Database .getInstance()?.prefBoolDao()?.insert(PrefBool (key.value, value))
85128 }
86129 }
0 commit comments