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

Allow AccountSetup to be created with async function to handle completeness of the account setup #73

Merged
merged 24 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
144 changes: 67 additions & 77 deletions Sources/SpeziAccount/AccountSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@
//

import OrderedCollections
import SpeziViews
import SwiftUI


public enum _AccountSetupState: EnvironmentKey, Sendable { // swiftlint:disable:this type_name
case generic
case setupShown
case requiringAdditionalInfo(_ keys: [any AccountKey.Type])
case loadingExistingAccount

public static let defaultValue: _AccountSetupState = .generic
}


/// Login or signup for a user account.
///
/// This view handles account setup for a user. It will show all enabled ``IdentityProvider``s from the configured ``AccountService``.
Expand Down Expand Up @@ -64,9 +55,13 @@
///
/// ### Header
/// - ``DefaultAccountSetupHeader``
///
/// ### Setup State
/// - ``SwiftUICore/EnvironmentValues/accountSetupState``
/// - ``AccountSetupState``
@MainActor
public struct AccountSetup<Header: View, Continue: View>: View {
private let setupCompleteClosure: (AccountDetails) -> Void
private let setupCompleteClosure: @MainActor (AccountDetails) async -> Void
private let header: Header
private let continueButton: Continue

Expand All @@ -75,9 +70,10 @@
@Environment(\.followUpBehavior)
private var followUpBehavior

@State private var setupState: _AccountSetupState = .generic
@State private var setupState: AccountSetupState = .presentingExistingAccount
@State private var compliance: SignupProviderCompliance?
@State private var followUpSheet = false
@State private var presentFollowUpSheet = false
@State private var accountSetupTask: Task<Void, Never>?

private var hasSetupComponents: Bool {
account.accountSetupComponents.contains { $0.configuration.isEnabled }
Expand All @@ -86,51 +82,54 @@
public var body: some View {
GeometryReader { proxy in
ScrollView(.vertical) {
VStack {
if hasSetupComponents {
header
.environment(\._accountSetupState, setupState)
}

Spacer()

if let details = account.details, !details.isAnonymous {
switch setupState {
case let .requiringAdditionalInfo(keys):
followUpInformationSheet(details, requiredKeys: keys)
case .loadingExistingAccount:
// We allow the outer view to navigate away upon signup, before we show the existing account view
existingAccountLoading
default:
ExistingAccountView(details: details) {
continueButton
}
}
} else {
accountSetupView
.onAppear {
setupState = .setupShown
}
}

Spacer()
Spacer()
Spacer()
}
scrollableContentView
.padding(.horizontal, ViewSizing.outerHorizontalPadding)
.frame(minHeight: proxy.size.height)
.frame(maxWidth: .infinity)
}
}
.onChange(of: [account.signedIn, account.details?.isAnonymous]) {
guard let details = account.details,
!details.isAnonymous,
case .setupShown = setupState else {
guard case .presentingSignup = setupState,
let details = account.details,
!details.isAnonymous else {
return
}

handleSuccessfulSetup(details)
}
.onDisappear {
accountSetupTask?.cancel()
}
}

@ViewBuilder private var scrollableContentView: some View {
VStack {
if hasSetupComponents {
header
.environment(\.accountSetupState, setupState)
}
Spacer()
if let details = account.details, !details.isAnonymous {
switch setupState {
case let .requiringAdditionalInfo(keys):
followUpInformationSheet(details, requiredKeys: keys)
case .loadingExistingAccount, .presentingSignup:
ProgressView()
case .presentingExistingAccount:
ExistingAccountView(details: details) {
continueButton
}
}
} else {
accountSetupView
.onAppear {
setupState = .presentingSignup
}
}
Spacer()
Spacer()
Spacer()
}
}

@ViewBuilder private var accountSetupView: some View {
Expand Down Expand Up @@ -163,17 +162,10 @@
}
}
}


@ViewBuilder private var existingAccountLoading: some View {
ProgressView()
.task {
try? await Task.sleep(for: .seconds(2))
setupState = .generic
}
}


fileprivate init(state: _AccountSetupState) where Header == DefaultAccountSetupHeader, Continue == EmptyView {
// for preview purposes
fileprivate init(state: AccountSetupState) where Header == DefaultAccountSetupHeader, Continue == EmptyView {

Check warning on line 168 in Sources/SpeziAccount/AccountSetup.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziAccount/AccountSetup.swift#L168

Added line #L168 was not covered by tests
self.setupCompleteClosure = { _ in }
self.header = DefaultAccountSetupHeader()
self.continueButton = EmptyView()
Expand All @@ -189,7 +181,7 @@
/// - continue: A custom continue button you can place. This view will be rendered if the AccountSetup view is
/// displayed with an already associated account.
public init(
setupComplete: @escaping (AccountDetails) -> Void = { _ in },
setupComplete: @MainActor @escaping (AccountDetails) async -> Void = { _ in },

Check warning on line 184 in Sources/SpeziAccount/AccountSetup.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziAccount/AccountSetup.swift#L184

Added line #L184 was not covered by tests
@ViewBuilder header: () -> Header = { DefaultAccountSetupHeader() },
@ViewBuilder `continue`: () -> Continue = { EmptyView() }
) {
Expand All @@ -202,19 +194,17 @@
@ViewBuilder
private func followUpInformationSheet(_ details: AccountDetails, requiredKeys: [any AccountKey.Type]) -> some View {
ProgressView()
.sheet(isPresented: $followUpSheet) {
.sheet(isPresented: $presentFollowUpSheet) {
// follow up information was completed!
handleSetupCompleted(details)
} content: {
NavigationStack {
FollowUpInfoSheet(keys: requiredKeys)
}
}
.onAppear {
followUpSheet = true // we want full control through the setupState property
}
.onChange(of: followUpSheet) {
if !followUpSheet { // follow up information was completed!
setupState = .loadingExistingAccount
setupCompleteClosure(details)
}
// setupState made this view show up, therefore, automatically present the sheet
presentFollowUpSheet = true
}
}

Expand Down Expand Up @@ -252,19 +242,19 @@
}

private func handleSetupCompleted(_ details: AccountDetails) {
setupState = .loadingExistingAccount
setupCompleteClosure(details)
}
}
guard accountSetupTask == nil else {
return // already in progress

Check warning on line 246 in Sources/SpeziAccount/AccountSetup.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziAccount/AccountSetup.swift#L246

Added line #L246 was not covered by tests
}

setupState = .loadingExistingAccount
accountSetupTask = Task { @MainActor in
defer {
accountSetupTask = nil
}

extension EnvironmentValues {
public var _accountSetupState: _AccountSetupState { // swiftlint:disable:this identifier_name missing_docs
get {
self[_AccountSetupState.self]
}
set {
self[_AccountSetupState.self] = newValue
await setupCompleteClosure(details)
try? await Task.sleep(for: .seconds(2))
setupState = .presentingExistingAccount
Supereg marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions Sources/SpeziAccount/Environment/AccountRequiredKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,9 @@
import SwiftUI


struct AccountRequiredKey: EnvironmentKey {
static let defaultValue = false
}


extension EnvironmentValues {
/// An environment variable that indicates if an account was configured to be required for the app.
///
/// Fore more information have a look at ``SwiftUICore/View/accountRequired(_:considerAnonymousAccounts:setupSheet:)``.
public var accountRequired: Bool {
get {
self[AccountRequiredKey.self]
}
set {
self[AccountRequiredKey.self] = newValue
}
}
/// Fore more information have a look at ``SwiftUICore/View/accountRequired(_:accountSetupIsComplete:setupSheet:)``.
@Entry public var accountRequired: Bool = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,10 @@
import SwiftUI


// swiftlint:disable:next type_name
private struct AccountServiceConfigurationEnvironmentKey: EnvironmentKey {
static var defaultValue: AccountServiceConfiguration {
.init(supportedKeys: .arbitrary)
}
}


extension EnvironmentValues {
/// Access the ``AccountServiceConfiguration`` within the context of a ``DataDisplayView`` or ``DataEntryView``.
///
/// - Note: Accessing this environment value outside the view body of such view types, you will receive a unusable
/// mock value.
public var accountServiceConfiguration: AccountServiceConfiguration {
get {
self[AccountServiceConfigurationEnvironmentKey.self]
}
set {
self[AccountServiceConfigurationEnvironmentKey.self] = newValue
}
}
@Entry public var accountServiceConfiguration: AccountServiceConfiguration = .init(supportedKeys: .arbitrary)
}
13 changes: 1 addition & 12 deletions Sources/SpeziAccount/Environment/AccountViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,9 @@ extension AccountViewType: Sendable, Hashable {}


extension EnvironmentValues {
private struct AccountViewTypeKey: EnvironmentKey {
static let defaultValue: AccountViewType? = nil
}

/// The type of `SpeziAccount` view a ``DataEntryView`` or ``DataDisplayView`` is placed in.
///
/// ## Topics
/// - ``AccountViewType``
public var accountViewType: AccountViewType? {
get {
self[AccountViewTypeKey.self]
}
set {
self[AccountViewTypeKey.self] = newValue
}
}
@Entry public var accountViewType: AccountViewType?
}
13 changes: 1 addition & 12 deletions Sources/SpeziAccount/Environment/FollowUpBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,7 @@ extension FollowUpBehavior: Sendable, Hashable {}


extension EnvironmentValues {
private struct FollowUpBehaviorKey: EnvironmentKey {
static let defaultValue: FollowUpBehavior = .automatic
}

var followUpBehavior: FollowUpBehavior {
get {
self[FollowUpBehaviorKey.self]
}
set {
self[FollowUpBehaviorKey.self] = newValue
}
}
@Entry var followUpBehavior: FollowUpBehavior = .automatic
}


Expand Down
9 changes: 1 addition & 8 deletions Sources/SpeziAccount/Environment/PasswordFieldType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,5 @@ extension EnvironmentValues {
/// ## Topics
///
/// - ``PasswordFieldType``
public var passwordFieldType: PasswordFieldType {
get {
self[PasswordFieldTypeKey.self]
}
set {
self[PasswordFieldTypeKey.self] = newValue
}
}
@Entry public var passwordFieldType: PasswordFieldType = .password
}
13 changes: 1 addition & 12 deletions Sources/SpeziAccount/Environment/PreferredSetupStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,8 @@ public enum PreferredSetupStyle {


extension EnvironmentValues {
private struct PreferredSetupStyleKey: EnvironmentKey {
static let defaultValue: PreferredSetupStyle = .automatic
}

/// The preferred style of presenting account setup views.
var preferredSetupStyle: PreferredSetupStyle {
get {
self[PreferredSetupStyleKey.self]
}
set {
self[PreferredSetupStyleKey.self] = newValue
}
}
@Entry var preferredSetupStyle: PreferredSetupStyle = .automatic
}


Expand Down
3 changes: 2 additions & 1 deletion Sources/SpeziAccount/SpeziAccount.docc/SpeziAccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ Refer to the <doc:Creating-your-own-Account-Service> article if you plan on impl
- ``AccountOverview``
- ``AccountHeader``
- ``FollowUpInfoSheet``
- ``SwiftUICore/View/accountRequired(_:considerAnonymousAccounts:setupSheet:)``
- ``SwiftUICore/View/accountRequired(_:accountSetupIsComplete:setupSheet:)``
- ``SwiftUICore/EnvironmentValues/accountRequired``
>>>>>>> main

### Environment & Preferences

Expand Down
Loading
Loading