Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 37 additions & 11 deletions heatguard-iOS/App/HeatGuardApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct HeatGuardApp: App {
@State private var hasFinishedIntro = false
@State private var isAuthenticated = false
@State private var showsTeamFlow = false
@State private var teamFlowHasPreviousScreen = false
@State private var startsAccountRegistration = false
@State private var pendingInvitationToken: String?
@State private var restoredProfile: HGTeamProfile?
Expand All @@ -31,37 +32,55 @@ struct HeatGuardApp: App {

@ViewBuilder
private var rootView: some View {
NavigationStack {
rootContent
.navigationDestination(isPresented: $showsTeamFlow) {
teamFlowDestination
}
}
}

@ViewBuilder
private var rootContent: some View {
if !hasFinishedIntro {
IntroView()
} else if isRestoringSession {
ProgressView()
} else if showsTeamFlow {
NavigationStack {
TeamMembershipFlowView(
linkToken: pendingInvitationToken,
startsWithRegistration: startsAccountRegistration,
onClose: closeTeamFlow,
onAuthenticated: { _ in isAuthenticated = true },
onTeamSelected: finishTeamSelection
)
}
} else if showsTeamFlow && !teamFlowHasPreviousScreen {
HGColor.appBackground.ignoresSafeArea()
} else if isAuthenticated {
HomeView(
initialProfile: restoredProfile,
onSessionEnded: handleSessionEnded,
onTeamSelectionRequested: { showsTeamFlow = true }
onTeamSelectionRequested: {
startsAccountRegistration = false
teamFlowHasPreviousScreen = true
showsTeamFlow = true
}
)
} else {
LoginView(
onAuthenticated: { _ in Task { await routeAuthenticatedSession() } },
onSignUpTap: {
startsAccountRegistration = true
teamFlowHasPreviousScreen = true
showsTeamFlow = true
}
)
}
}

private var teamFlowDestination: some View {
TeamMembershipFlowView(
linkToken: pendingInvitationToken,
startsWithRegistration: startsAccountRegistration,
onClose: closeTeamFlow,
onAuthenticated: { _ in isAuthenticated = true },
onTeamSelected: finishTeamSelection
)
.navigationBarBackButtonHidden(!teamFlowHasPreviousScreen)
}

@MainActor
private func startApp() async {
async let introDuration: Void = waitForIntroDuration()
Expand Down Expand Up @@ -102,17 +121,21 @@ struct HeatGuardApp: App {
let memberships = try await HGAuthenticationService().memberships()
if let selectedTeamID = memberships.selectedTeamID,
memberships.items.contains(where: { $0.teamID == selectedTeamID }) {
teamFlowHasPreviousScreen = false
showsTeamFlow = false
return
}
if memberships.items.count == 1, let onlyMembership = memberships.items.first {
try await HGAuthenticationService().selectMembership(teamID: onlyMembership.teamID)
teamFlowHasPreviousScreen = false
showsTeamFlow = false
return
}
teamFlowHasPreviousScreen = false
showsTeamFlow = true
} catch {
// Keep the authenticated worker in the team-selection flow so the API error is visible.
teamFlowHasPreviousScreen = false
showsTeamFlow = true
}
}
Expand All @@ -121,13 +144,15 @@ struct HeatGuardApp: App {
private func finishTeamSelection() {
isAuthenticated = true
showsTeamFlow = false
teamFlowHasPreviousScreen = false
startsAccountRegistration = false
pendingInvitationToken = nil
}

@MainActor
private func closeTeamFlow() {
showsTeamFlow = false
teamFlowHasPreviousScreen = false
startsAccountRegistration = false
pendingInvitationToken = nil
if isAuthenticated {
Expand Down Expand Up @@ -170,6 +195,7 @@ struct HeatGuardApp: App {
guard let token, !token.isEmpty else { return }
pendingInvitationToken = token
startsAccountRegistration = false
teamFlowHasPreviousScreen = true
showsTeamFlow = true
}
}
Loading
Loading