-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrezorStore.kt
More file actions
43 lines (35 loc) · 1.18 KB
/
TrezorStore.kt
File metadata and controls
43 lines (35 loc) · 1.18 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
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.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.serialization.Serializable
import to.bitkit.data.serializers.TrezorDataSerializer
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,
) {
private val store = context.trezorDataStore
val data: Flow<TrezorData> = store.data
suspend fun loadKnownDevices(): List<KnownDevice> =
store.data.first().knownDevices
suspend fun saveKnownDevices(devices: List<KnownDevice>) {
store.updateData { it.copy(knownDevices = devices) }
}
suspend fun reset() {
store.updateData { TrezorData() }
}
}
@Serializable
data class TrezorData(
val knownDevices: List<KnownDevice> = emptyList(),
)