Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adding Factory for empty and no-op States that can be used in SwiftUI's previews. #67

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions Sources/Authenticator/Utilities/PreviewFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import Foundation

/// A utility class meant to provide
public enum PreviewFactory {
public enum States {
/// Returns an empty and no-op ``SignInState``.
public static func signIn() -> SignInState {
return .init(credentials: .init())
}

/// Returns an empty and no-op ``ConfirmSignInWithCodeState``.
/// - Parameter deliveryDetails: The ``AuthCodeDeliveryDetails`` associated with this state
public static func confirmSignInWithCode(deliveryDetails: AuthCodeDeliveryDetails? = nil) -> ConfirmSignInWithCodeState {
return ConfirmSignInWithCodeState(
authenticatorState: .empty(with: .confirmSignInWithMFACode(deliveryDetails: deliveryDetails))
)
}

/// Returns an empty and no-op ``ConfirmSignInWithNewPasswordState``.
public static func confirmSignInWithNewPassword() -> ConfirmSignInWithNewPasswordState {
return .init(credentials: .init())
}

/// Returns an empty and no-op ``ContinueSignInWithMFASelectionState``.
/// - Parameter allowedMFATypes: The ``AllowedMFATypes`` associated with this state
public static func continueSignInWithMFASelection(allowedMFATypes: AllowedMFATypes) -> ContinueSignInWithMFASelectionState {
return .init(
authenticatorState: .empty,
allowedMFATypes: allowedMFATypes
)
}

/// Returns an empty and no-op ``ContinueSignInWithTOTPSetupState``.
/// - Parameter issuer: The issuer associated with this state
/// - Parameter totpSetupDetails: The ``TOTPSetupDetails`` associated with this state
public static func continueSignInWithTOTPSetup(
issuer: String? = nil,
totpSetupDetails: TOTPSetupDetails
) -> ContinueSignInWithTOTPSetupState {
return .init(
authenticatorState: .empty,
issuer: issuer,
totpSetupDetails: totpSetupDetails
)
}

/// Returns an empty and no-op ``SignUpState``.
/// - Parameter signUpFields: The list of ``SignUpField``s associated with this state
public static func signUp(signUpFields: [SignUpField] = []) -> SignUpState {
let state = SignUpState(credentials: .init())
state.configure(with: signUpFields)
return state
}

/// Returns an empty and no-op ``ConfirmSignUpState``.
/// - Parameter deliveryDetails: The ``AuthCodeDeliveryDetails`` associated with this state
public static func confirmSignUp(deliveryDetails: AuthCodeDeliveryDetails? = nil) -> ConfirmSignUpState {
return .init(
authenticatorState: .empty(with: .confirmSignUp(deliveryDetails: deliveryDetails)),
credentials: .init()
)
}

/// Returns an empty and no-op ``ResetPasswordState``.
public static func resetPasword() -> ResetPasswordState {
return .init(credentials: .init())
}

/// Returns an empty and no-op ``ConfirmResetPasswordState``.
/// - Parameter deliveryDetails: The ``AuthCodeDeliveryDetails`` associated with this state
public static func confirmResetPassword(deliveryDetails: AuthCodeDeliveryDetails? = nil) -> ConfirmResetPasswordState {
return .init(
authenticatorState: .empty(with: .confirmResetPassword(deliveryDetails: deliveryDetails)),
credentials: .init()
)
}

/// Returns an empty and no-op ``VerifyUserState``.
/// - Parameter unverifiedFields: The list of ``AuthUserAttributeKey``s associated with this state
public static func verifyUser(unverifiedFields: [AuthUserAttributeKey] = []) -> VerifyUserState {
return .init(
authenticatorState: .empty(with: .verifyUser(attributes: unverifiedFields)),
credentials: .init()
)
}

/// Returns an empty and no-op ``ConfirmVerifyUserState``.
/// - Parameter userAttributeKey: The ``AuthUserAttributeKey`` associated with this state
/// - Parameter deliveryDetails: The ``AuthCodeDeliveryDetails`` associated with this state
public static func confirmVerifyUser(
userAttributeKey: AuthUserAttributeKey,
deliveryDetails: AuthCodeDeliveryDetails? = nil
) -> ConfirmVerifyUserState {
return .init(
authenticatorState: .empty(
with: .confirmVerifyUser(
attribute: userAttributeKey,
deliveryDetails: deliveryDetails
)
),
credentials: .init()
)
}
}
}

private extension AuthenticatorStateProtocol where Self == EmptyAuthenticatorState {
static func empty(with step: Step) -> AuthenticatorStateProtocol {
return EmptyAuthenticatorState(step: step)
}
}

Loading