Skip to content

Commit

Permalink
Revert changes to AccountSetup to make setupComplete async
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljohanneskraft committed Sep 12, 2024
1 parent 71b8a89 commit 318ed30
Showing 1 changed file with 38 additions and 46 deletions.
84 changes: 38 additions & 46 deletions Sources/SpeziAccount/AccountSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import OrderedCollections
import SpeziViews
import SwiftUI


Expand Down Expand Up @@ -67,7 +66,7 @@ public enum _AccountSetupState: EnvironmentKey, Sendable { // swiftlint:disable:
/// - ``DefaultAccountSetupHeader``
@MainActor
public struct AccountSetup<Header: View, Continue: View>: View {
private let setupCompleteClosure: (AccountDetails) async -> Void
private let setupCompleteClosure: (AccountDetails) -> Void
private let header: Header
private let continueButton: Continue

Expand All @@ -79,7 +78,6 @@ public struct AccountSetup<Header: View, Continue: View>: View {
@State private var setupState: _AccountSetupState = .generic
@State private var compliance: SignupProviderCompliance?
@State private var followUpSheet = false
@State private var isCompletingSetup = false

private var hasSetupComponents: Bool {
account.accountSetupComponents.contains { $0.configuration.isEnabled }
Expand All @@ -88,7 +86,37 @@ public struct AccountSetup<Header: View, Continue: View>: View {
public var body: some View {
GeometryReader { proxy in
ScrollView(.vertical) {
scrollableContentView
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()
}
.padding(.horizontal, ViewSizing.outerHorizontalPadding)
.frame(minHeight: proxy.size.height)
.frame(maxWidth: .infinity)
Expand All @@ -100,44 +128,10 @@ public struct AccountSetup<Header: View, Continue: View>: View {
case .setupShown = setupState else {
return
}

handleSuccessfulSetup(details)
}
}

@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:
// We allow the outer view to navigate away upon signup, before we show the existing account view
existingAccountLoading
default:
if isCompletingSetup {
ProgressView()
} else {
ExistingAccountView(details: details) {
continueButton
}
}
}
} else {
accountSetupView
.onAppear {
setupState = .setupShown
}
}
Spacer()
Spacer()
Spacer()
}
}

@ViewBuilder private var accountSetupView: some View {
if !hasSetupComponents {
Expand Down Expand Up @@ -195,7 +189,7 @@ public struct AccountSetup<Header: View, Continue: View>: View {
/// - 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) async -> Void = { _ in },
setupComplete: @escaping (AccountDetails) -> Void = { _ in },
@ViewBuilder header: () -> Header = { DefaultAccountSetupHeader() },
@ViewBuilder `continue`: () -> Continue = { EmptyView() }
) {
Expand All @@ -218,7 +212,8 @@ public struct AccountSetup<Header: View, Continue: View>: View {
}
.onChange(of: followUpSheet) {
if !followUpSheet { // follow up information was completed!
handleSetupCompleted(details)
setupState = .loadingExistingAccount
setupCompleteClosure(details)
}
}
}
Expand Down Expand Up @@ -257,11 +252,8 @@ public struct AccountSetup<Header: View, Continue: View>: View {
}

private func handleSetupCompleted(_ details: AccountDetails) {
isCompletingSetup = true
Task { @MainActor in
await setupCompleteClosure(details)
isCompletingSetup = false
}
setupState = .loadingExistingAccount
setupCompleteClosure(details)
}
}

Expand Down

0 comments on commit 318ed30

Please sign in to comment.