generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
TutorialViewModel.kt
140 lines (122 loc) · 3.82 KB
/
TutorialViewModel.kt
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.bitwarden.authenticator.ui.platform.feature.tutorial
import android.os.Parcelable
import com.bitwarden.authenticator.R
import com.bitwarden.authenticator.ui.platform.base.BaseViewModel
import com.bitwarden.authenticator.ui.platform.base.util.Text
import com.bitwarden.authenticator.ui.platform.base.util.asText
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.update
import kotlinx.parcelize.Parcelize
import javax.inject.Inject
/**
* View model for the [TutorialScreen].
*/
@HiltViewModel
class TutorialViewModel @Inject constructor() :
BaseViewModel<TutorialState, TutorialEvent, TutorialAction>(
initialState = TutorialState.IntroSlide
) {
override fun handleAction(action: TutorialAction) {
when (action) {
TutorialAction.ContinueClick -> {
handleContinueClick()
}
TutorialAction.SkipClick -> {
handleSkipClick()
}
is TutorialAction.TutorialPageChange -> {
handleTutorialPageChange(action.targetPage)
}
}
}
private fun handleTutorialPageChange(page: Int) {
when (page) {
0 -> mutableStateFlow.update { TutorialState.IntroSlide }
1 -> mutableStateFlow.update { TutorialState.QrScannerSlide }
2 -> mutableStateFlow.update { TutorialState.UniqueCodesSlide }
}
}
private fun handleContinueClick() {
val currentPage = mutableStateFlow.value
val event = when (currentPage) {
TutorialState.IntroSlide -> TutorialEvent.NavigateToQrScannerSlide
TutorialState.QrScannerSlide -> TutorialEvent.NavigateToUniqueCodesSlide
TutorialState.UniqueCodesSlide -> {
TutorialEvent.NavigateToAuthenticator
}
}
sendEvent(event)
}
private fun handleSkipClick() {
sendEvent(TutorialEvent.NavigateToAuthenticator)
}
}
/**
* Models state for the Tutorial screen.
*/
@Parcelize
sealed class TutorialState(
val continueButtonText: Text,
val isLastPage: Boolean,
) : Parcelable {
/**
* Tutorial should display the introduction slide.
*/
@Parcelize
data object IntroSlide : TutorialState(
continueButtonText = R.string.continue_button.asText(),
isLastPage = false,
)
/**
* Tutorial should display the QR code scanner description slide.
*/
@Parcelize
data object QrScannerSlide : TutorialState(
continueButtonText = R.string.continue_button.asText(),
isLastPage = false
)
/**
* Tutorial should display the 2FA code description slide.
*/
@Parcelize
data object UniqueCodesSlide : TutorialState(
continueButtonText = R.string.get_started.asText(),
isLastPage = true
)
}
/**
* Represents a set of events related to the tutorial screen.
*/
sealed class TutorialEvent {
/**
* Navigate to the authenticator tutorial slide.
*/
data object NavigateToAuthenticator : TutorialEvent()
/**
* Navigate to the QR Code scanner tutorial slide.
*/
data object NavigateToQrScannerSlide : TutorialEvent()
/**
* Navigate to the unique codes tutorial slide.
*/
data object NavigateToUniqueCodesSlide : TutorialEvent()
}
/**
* Models actions that can be taken on the tutorial screen.
*/
sealed class TutorialAction {
/**
* The user has manually changed the tutorial page by swiping.
*/
data class TutorialPageChange(
val targetPage: Int,
) : TutorialAction()
/**
* The user clicked the continue button on the introduction slide.
*/
data object ContinueClick : TutorialAction()
/**
* The user clicked the skip button on one of the tutorial slides.
*/
data object SkipClick : TutorialAction()
}