Skip to content

Commit 9dd7ac9

Browse files
authored
chore(authenticator): Refactor step state instantiation (#25)
1 parent 40a9408 commit 9dd7ac9

37 files changed

+723
-528
lines changed

authenticator-screenshots/src/test/java/com/amplifyframework/ui/authenticator/MockAuthenticatorData.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import com.amplifyframework.ui.authenticator.forms.MutablePasswordFieldState
2525

2626
fun mockForm(
2727
vararg fields: MutableFieldData,
28-
submitting: Boolean = false
28+
enabled: Boolean = true
2929
) = object : MutableFormState {
3030
override val fields = fields.associateBy { it.config.key }
31-
override val submitting = submitting
31+
override var enabled = enabled
3232
}
3333

3434
fun mockFieldData(
@@ -46,7 +46,7 @@ fun mockFieldState(
4646
error: FieldError? = null
4747
) = object : MutableFieldState {
4848
override var content = content
49-
override val error = error
49+
override var error = error
5050
}
5151

5252
fun mockPasswordFieldState(
@@ -56,5 +56,5 @@ fun mockPasswordFieldState(
5656
) = object : MutablePasswordFieldState {
5757
override var fieldContentVisible = visible
5858
override var content = content
59-
override val error: FieldError? = error
59+
override var error: FieldError? = error
6060
}

authenticator/src/main/java/com/amplifyframework/ui/authenticator/AuthenticatorState.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ fun rememberAuthenticatorState(
6262
@Stable
6363
interface AuthenticatorState {
6464
/**
65-
* The state holder instance for the current content visible to the user.
65+
* The state holder instance for the current [AuthenticatorStep] being shown to the user.
6666
*/
67-
val screenState: AuthenticatorScreenState
67+
val stepState: AuthenticatorStepState
6868

6969
/**
7070
* A flow of [AuthenticatorMessage] that may be presented to the user, such as messages indicating a
@@ -78,15 +78,15 @@ interface AuthenticatorState {
7878
internal class AuthenticatorStateImpl constructor(
7979
private val viewModel: AuthenticatorViewModel
8080
) : AuthenticatorState {
81-
override var screenState by mutableStateOf<AuthenticatorScreenState>(LoadingState)
81+
override var stepState by mutableStateOf<AuthenticatorStepState>(LoadingState)
8282

8383
override val messages: Flow<AuthenticatorMessage>
8484
get() = viewModel.events
8585

8686
init {
8787
viewModel.viewModelScope.launch {
88-
viewModel.screenState.collect {
89-
screenState = it
88+
viewModel.stepState.collect {
89+
stepState = it
9090
}
9191
}
9292
}
Lines changed: 155 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ import com.amplifyframework.ui.authenticator.enums.AuthenticatorStep
2727
import com.amplifyframework.ui.authenticator.forms.MutableFormState
2828

2929
/**
30-
* A Screen State is a state holder for the UI for a specific [AuthenticatorStep].
30+
* A state holder for the UI for a specific [AuthenticatorStep].
3131
*/
3232
@Stable
33-
interface AuthenticatorScreenState {
33+
interface AuthenticatorStepState {
34+
/**
35+
* The [AuthenticatorStep] that this state holder represents.
36+
*/
3437
val step: AuthenticatorStep
3538
}
3639

3740
/**
3841
* The Authenticator is loading the current state of the user's Auth session.
3942
*/
4043
@Immutable
41-
object LoadingState : AuthenticatorScreenState {
44+
object LoadingState : AuthenticatorStepState {
4245
override val step = AuthenticatorStep.Loading
4346
}
4447

@@ -47,15 +50,15 @@ object LoadingState : AuthenticatorScreenState {
4750
* @param error The error that occurred.
4851
*/
4952
@Immutable
50-
data class ErrorState(val error: AuthException) : AuthenticatorScreenState {
53+
data class ErrorState(val error: AuthException) : AuthenticatorStepState {
5154
override val step = AuthenticatorStep.Error
5255
}
5356

5457
/**
5558
* The user has completed the sign in process.
5659
*/
5760
@Immutable
58-
interface SignedInState : AuthenticatorScreenState {
61+
interface SignedInState : AuthenticatorStepState {
5962
/**
6063
* The [AuthUser] instance for the signed in user.
6164
*/
@@ -69,19 +72,23 @@ interface SignedInState : AuthenticatorScreenState {
6972
}
7073

7174
/**
72-
* An [AuthenticatorScreenState] for a part of UI that contains an input form.
75+
* The user is on the Sign In step. They can enter their Sign In information to authenticate with Amplify.
7376
*/
7477
@Stable
75-
interface FormHolderState : AuthenticatorScreenState {
78+
interface SignInState : AuthenticatorStepState {
79+
/**
80+
* The input form state holder for this step.
81+
*/
7682
val form: MutableFormState
77-
}
7883

79-
/**
80-
* The user is on the Sign In step. They can enter their Sign In information to authenticate with Amplify.
81-
*/
82-
@Stable
83-
interface SignInState : FormHolderState {
84+
/**
85+
* Move the user to a different [AuthenticatorInitialStep].
86+
*/
8487
fun moveTo(step: AuthenticatorInitialStep)
88+
89+
/**
90+
* Initiate a sign in with the information entered into the [form].
91+
*/
8592
suspend fun signIn()
8693
}
8794

@@ -90,9 +97,25 @@ interface SignInState : FormHolderState {
9097
* message to complete the sign in process.
9198
*/
9299
@Stable
93-
interface SignInConfirmMfaState : FormHolderState {
100+
interface SignInConfirmMfaState : AuthenticatorStepState {
101+
/**
102+
* The input form state holder for this step.
103+
*/
104+
val form: MutableFormState
105+
106+
/**
107+
* The [AuthCodeDeliveryDetails] for the confirmation code that was sent to the user when entering this state.
108+
*/
94109
val deliveryDetails: AuthCodeDeliveryDetails?
110+
111+
/**
112+
* Move the user to a different [AuthenticatorInitialStep].
113+
*/
95114
fun moveTo(step: AuthenticatorInitialStep)
115+
116+
/**
117+
* Confirm the user's sign in using the information entered into the [form].
118+
*/
96119
suspend fun confirmSignIn()
97120
}
98121

@@ -101,10 +124,32 @@ interface SignInConfirmMfaState : FormHolderState {
101124
* challenge to complete the sign in process.
102125
*/
103126
@Stable
104-
interface SignInConfirmCustomState : FormHolderState {
127+
interface SignInConfirmCustomState : AuthenticatorStepState {
128+
/**
129+
* The input form state holder for this step.
130+
*/
131+
val form: MutableFormState
132+
133+
/**
134+
* The additional info that is configured with your custom challenge. For more information, please see
135+
* how to [sign in with a custom flow](https://docs.amplify.aws/lib/auth/signin_with_custom_flow/q/platform/android)
136+
* in the Amplify documentation.
137+
*/
105138
val additionalInfo: Map<String, String>
139+
140+
/**
141+
* The [AuthCodeDeliveryDetails] for the confirmation code that was sent to the user when entering this state.
142+
*/
106143
val deliveryDetails: AuthCodeDeliveryDetails?
144+
145+
/**
146+
* Move the user to a different [AuthenticatorInitialStep].
147+
*/
107148
fun moveTo(step: AuthenticatorInitialStep)
149+
150+
/**
151+
* Confirm the user's sign in using the information entered into the [form].
152+
*/
108153
suspend fun confirmSignIn()
109154
}
110155

@@ -113,37 +158,93 @@ interface SignInConfirmCustomState : FormHolderState {
113158
* the sign in process.
114159
*/
115160
@Stable
116-
interface SignInConfirmNewPasswordState : FormHolderState {
161+
interface SignInConfirmNewPasswordState : AuthenticatorStepState {
162+
/**
163+
* The input form state holder for this step.
164+
*/
165+
val form: MutableFormState
166+
167+
/**
168+
* Move the user to a different [AuthenticatorInitialStep].
169+
*/
117170
fun moveTo(step: AuthenticatorInitialStep)
171+
172+
/**
173+
* Confirm the user's sign in using the information entered into the [form].
174+
*/
118175
suspend fun confirmSignIn()
119176
}
120177

121178
/**
122179
* The user is on the Sign Up step, and can fill out the account creation form to Sign Up.
123180
*/
124181
@Stable
125-
interface SignUpState : FormHolderState {
182+
interface SignUpState : AuthenticatorStepState {
183+
/**
184+
* The input form state holder for this step.
185+
*/
186+
val form: MutableFormState
187+
188+
/**
189+
* Move the user to a different [AuthenticatorInitialStep].
190+
*/
126191
fun moveTo(step: AuthenticatorInitialStep)
192+
193+
/**
194+
* Initiate the sign up using the information entered into the [form].
195+
*/
127196
suspend fun signUp()
128197
}
129198

130199
/**
131200
* The user has signed up, but needs to enter a confirmation code sent to them.
132201
*/
133202
@Stable
134-
interface SignUpConfirmState : FormHolderState {
203+
interface SignUpConfirmState : AuthenticatorStepState {
204+
/**
205+
* The input form state holder for this step.
206+
*/
207+
val form: MutableFormState
208+
209+
/**
210+
* The [AuthCodeDeliveryDetails] for the confirmation code that was sent to the user when entering this state.
211+
*/
135212
val deliveryDetails: AuthCodeDeliveryDetails?
213+
214+
/**
215+
* Move the user to a different [AuthenticatorInitialStep].
216+
*/
136217
fun moveTo(step: AuthenticatorInitialStep)
218+
219+
/**
220+
* Confirm the sign up using the information entered into the [form].
221+
*/
137222
suspend fun confirmSignUp()
223+
224+
/**
225+
* Re-send the confirmation code to the user.
226+
*/
138227
suspend fun resendCode()
139228
}
140229

141230
/**
142231
* The user is on the Password Reset step. They can enter their username to begin the password reset.
143232
*/
144233
@Stable
145-
interface PasswordResetState : FormHolderState {
234+
interface PasswordResetState : AuthenticatorStepState {
235+
/**
236+
* The input form state holder for this step.
237+
*/
238+
val form: MutableFormState
239+
240+
/**
241+
* Move the user to a different [AuthenticatorInitialStep].
242+
*/
146243
fun moveTo(step: AuthenticatorInitialStep)
244+
245+
/**
246+
* Initiate the password reset using the information entered into the [form].
247+
*/
147248
suspend fun submitPasswordReset()
148249
}
149250

@@ -152,17 +253,39 @@ interface PasswordResetState : FormHolderState {
152253
* password to complete the password reset.
153254
*/
154255
@Stable
155-
interface PasswordResetConfirmState : FormHolderState {
256+
interface PasswordResetConfirmState : AuthenticatorStepState {
257+
/**
258+
* The input form state holder for this step.
259+
*/
260+
val form: MutableFormState
261+
262+
/**
263+
* The [AuthCodeDeliveryDetails] for the confirmation code that was sent to the user when entering this state.
264+
*/
156265
val deliveryDetails: AuthCodeDeliveryDetails?
266+
267+
/**
268+
* Move the user to a different [AuthenticatorInitialStep].
269+
*/
157270
fun moveTo(step: AuthenticatorInitialStep)
271+
272+
/**
273+
* Confirm the password reset using the information entered into the [form].
274+
*/
158275
suspend fun submitPasswordResetConfirm()
159276
}
160277

161278
/**
162-
* The user has successfully signed in and their account is confirmed, however they do not have any means of account recovery (email, phone) that is confirmed.
279+
* The user has successfully signed in and their account is confirmed, however they do not have any means of account
280+
* recovery (email, phone) that is confirmed.
163281
*/
164282
@Stable
165-
interface VerifyUserState : FormHolderState {
283+
interface VerifyUserState : AuthenticatorStepState {
284+
/**
285+
* The input form state holder for this step.
286+
*/
287+
val form: MutableFormState
288+
166289
/**
167290
* The list of unverified attributes.
168291
*/
@@ -174,16 +297,22 @@ interface VerifyUserState : FormHolderState {
174297
suspend fun verifyUser()
175298

176299
/**
177-
* Skip verification and move to the Signed In state
300+
* Skip verification and move to the Signed In state.
178301
*/
179302
fun skip()
180303
}
181304

182305
/**
183-
* The user has initiated verification of an account recovery mechanism (email, phone) and needs to provide a confirmation code.
306+
* The user has initiated verification of an account recovery mechanism (email, phone) and needs to provide a
307+
* confirmation code.
184308
*/
185309
@Stable
186-
interface VerifyUserConfirmState : FormHolderState {
310+
interface VerifyUserConfirmState : AuthenticatorStepState {
311+
/**
312+
* The input form state holder for this step.
313+
*/
314+
val form: MutableFormState
315+
187316
/**
188317
* The details of where the verification code was sent.
189318
*/
@@ -200,7 +329,7 @@ interface VerifyUserConfirmState : FormHolderState {
200329
suspend fun resendCode()
201330

202331
/**
203-
* Skip verification and move to the Signed In state
332+
* Skip verification and move to the Signed In state.
204333
*/
205334
fun skip()
206335
}

0 commit comments

Comments
 (0)