-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHwWalletViewModel.kt
More file actions
55 lines (47 loc) · 1.94 KB
/
Copy pathHwWalletViewModel.kt
File metadata and controls
55 lines (47 loc) · 1.94 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
51
52
53
54
55
package to.bitkit.ui.screens.wallets
import android.content.Context
import androidx.compose.runtime.Immutable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import to.bitkit.R
import to.bitkit.models.HwWallet
import to.bitkit.models.Toast
import to.bitkit.repositories.HwWalletRepo
import to.bitkit.ui.shared.toast.ToastEventBus
import javax.inject.Inject
@HiltViewModel
class HwWalletViewModel @Inject constructor(
@ApplicationContext private val context: Context,
private val hwWalletRepo: HwWalletRepo,
) : ViewModel() {
val wallets: StateFlow<ImmutableList<HwWallet>> = hwWalletRepo.wallets
val walletsLoaded: StateFlow<Boolean> = hwWalletRepo.walletsLoaded
private val _uiState = MutableStateFlow(HwWalletDetailUiState())
val uiState: StateFlow<HwWalletDetailUiState> = _uiState.asStateFlow()
fun onRemoveClick(wallet: HwWallet) = _uiState.update { it.copy(isPendingRemoval = wallet) }
fun onDismissRemoveDialog() = _uiState.update { it.copy(isPendingRemoval = null) }
fun removeDevice(deviceId: String) {
viewModelScope.launch {
_uiState.update { it.copy(isPendingRemoval = null) }
hwWalletRepo.removeDevice(deviceId).onFailure {
ToastEventBus.send(
type = Toast.ToastType.ERROR,
title = context.getString(R.string.common__error),
description = context.getString(R.string.hardware__remove_error),
)
}
}
}
}
@Immutable
data class HwWalletDetailUiState(
val isPendingRemoval: HwWallet? = null,
)