-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrezorStore.kt
More file actions
50 lines (42 loc) · 1.47 KB
/
Copy pathTrezorStore.kt
File metadata and controls
50 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package to.bitkit.data
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import to.bitkit.data.serializers.TrezorDataSerializer
import to.bitkit.di.IoDispatcher
import to.bitkit.repositories.KnownDevice
import javax.inject.Inject
import javax.inject.Singleton
private val Context.trezorDataStore: DataStore<TrezorData> by dataStore(
fileName = "trezor_device.json",
serializer = TrezorDataSerializer
)
@Singleton
class TrezorStore @Inject constructor(
@ApplicationContext private val context: Context,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) {
private val store = context.trezorDataStore
val data: Flow<TrezorData> = store.data
suspend fun loadKnownDevices(): List<KnownDevice> = withContext(ioDispatcher) {
store.data.first().knownDevices
}
suspend fun saveKnownDevices(devices: List<KnownDevice>) = withContext(ioDispatcher) {
store.updateData { it.copy(knownDevices = devices) }
Unit
}
suspend fun reset() = withContext(ioDispatcher) {
store.updateData { TrezorData() }
Unit
}
}
@Serializable
data class TrezorData(
val knownDevices: List<KnownDevice> = emptyList(),
)