Repository navigation
feat(auth): add pickerContent and pickerDestination slots to AuthPickerView #1403
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||||||||||
| // Copyright 2025 Google LLC | ||||||||||||||
| // | ||||||||||||||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
| // you may not use this file except in compliance with the License. | ||||||||||||||
| // You may obtain a copy of the License at | ||||||||||||||
| // | ||||||||||||||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
| // | ||||||||||||||
| // Unless required by applicable law or agreed to in writing, software | ||||||||||||||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
| // See the License for the specific language governing permissions and | ||||||||||||||
| // limitations under the License. | ||||||||||||||
|
|
||||||||||||||
| import FirebaseAuth | ||||||||||||||
| import FirebaseAuthUIComponents | ||||||||||||||
| import SwiftUI | ||||||||||||||
|
|
||||||||||||||
| /// An ``AuthPickerView`` with custom sheet content, returned by | ||||||||||||||
| /// ``AuthPickerView/pickerContent(_:)`` and ``AuthPickerView/pickerDestination(_:)``. | ||||||||||||||
| /// | ||||||||||||||
| /// It owns the authentication sheet, its `NavigationStack`, error alerts, and MFA and | ||||||||||||||
| /// account-conflict handling; only the first screen and the pushed screens come from the slots. | ||||||||||||||
| /// You don't create it directly. | ||||||||||||||
| @MainActor | ||||||||||||||
| public struct AuthPickerContent< | ||||||||||||||
| Content: View, | ||||||||||||||
| PickerContent: View, | ||||||||||||||
| DestinationContent: View | ||||||||||||||
| >: View { | ||||||||||||||
| init(content: @escaping () -> Content, | ||||||||||||||
| pickerContent: @escaping () -> PickerContent, | ||||||||||||||
| destination: @escaping (AuthView) -> DestinationContent) { | ||||||||||||||
| self.content = content | ||||||||||||||
| self.pickerContent = pickerContent | ||||||||||||||
| self.destination = destination | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Environment(AuthService.self) private var authService | ||||||||||||||
| private let content: () -> Content | ||||||||||||||
| private let pickerContent: () -> PickerContent | ||||||||||||||
| private let destination: (AuthView) -> DestinationContent | ||||||||||||||
|
|
||||||||||||||
| // View-layer error state | ||||||||||||||
| @State private var error: AlertError? | ||||||||||||||
|
|
||||||||||||||
| public var body: some View { | ||||||||||||||
| @Bindable var authService = authService | ||||||||||||||
| content() | ||||||||||||||
| .sheet(isPresented: $authService.isPresented) { | ||||||||||||||
| @Bindable var navigator = authService.navigator | ||||||||||||||
| NavigationStack(path: $navigator.routes) { | ||||||||||||||
| root | ||||||||||||||
| .navigationTitle(authService.authenticationState == .unauthenticated ? authService | ||||||||||||||
| .string.authPickerTitle : "") | ||||||||||||||
| .navigationBarTitleDisplayMode(.large) | ||||||||||||||
| .toolbar { | ||||||||||||||
| toolbar | ||||||||||||||
| } | ||||||||||||||
| .navigationDestination(for: AuthView.self) { view in | ||||||||||||||
| destination(view) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| .environment(\.reportError, reportError) | ||||||||||||||
| .errorAlert( | ||||||||||||||
| error: $error, | ||||||||||||||
| okButtonLabel: authService.string.okButtonLabel | ||||||||||||||
| ) | ||||||||||||||
| .sheet(item: $authService.legacySignInRecovery) { _ in | ||||||||||||||
| LegacySignInRecoveryView() | ||||||||||||||
| .environment(authService) | ||||||||||||||
| } | ||||||||||||||
| .interactiveDismissDisabled(authService.configuration.interactiveDismissEnabled) | ||||||||||||||
| // Apply account conflict handling at NavigationStack level | ||||||||||||||
| .accountConflictHandler() | ||||||||||||||
| // Apply MFA handling at NavigationStack level | ||||||||||||||
| .mfaHandler() | ||||||||||||||
| .environment(authService) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// The sheet's first screen. The slot only replaces the signed-out screen, so signed-in users | ||||||||||||||
| /// always reach ``SignedInView`` and the progress overlay always shows while signing in. | ||||||||||||||
| @ViewBuilder | ||||||||||||||
| private var root: some View { | ||||||||||||||
| VStack { | ||||||||||||||
| if authService.authenticationState == .authenticated { | ||||||||||||||
| SignedInView() | ||||||||||||||
| } else { | ||||||||||||||
| pickerContent() | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| .overlay { | ||||||||||||||
| if authService.authenticationState == .authenticating { | ||||||||||||||
| VStack(spacing: 24) { | ||||||||||||||
| ProgressView() | ||||||||||||||
| .scaleEffect(1.25) | ||||||||||||||
| .tint(.white) | ||||||||||||||
| Text("Authenticating...") | ||||||||||||||
| .authFont(.body) | ||||||||||||||
| .foregroundStyle(.white) | ||||||||||||||
| } | ||||||||||||||
| .frame(maxWidth: .infinity, maxHeight: .infinity) | ||||||||||||||
| .background(.black.opacity(0.7)) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Replaces the signed-out first screen of the authentication sheet. See | ||||||||||||||
| /// ``AuthPickerView/pickerContent(_:)``. | ||||||||||||||
| public func pickerContent<NewPickerContent: View>( | ||||||||||||||
| @ViewBuilder _ pickerContent: @escaping () -> NewPickerContent | ||||||||||||||
| ) -> AuthPickerContent<Content, NewPickerContent, DestinationContent> { | ||||||||||||||
| AuthPickerContent<Content, NewPickerContent, DestinationContent>( | ||||||||||||||
| content: content, | ||||||||||||||
| pickerContent: pickerContent, | ||||||||||||||
| destination: destination | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Replaces the screens pushed inside the authentication sheet. See | ||||||||||||||
| /// ``AuthPickerView/pickerDestination(_:)``. | ||||||||||||||
| public func pickerDestination<NewDestinationContent: View>( | ||||||||||||||
| @ViewBuilder _ destination: @escaping (AuthView) -> NewDestinationContent | ||||||||||||||
| ) -> AuthPickerContent<Content, PickerContent, NewDestinationContent> { | ||||||||||||||
| AuthPickerContent<Content, PickerContent, NewDestinationContent>( | ||||||||||||||
| content: content, | ||||||||||||||
| pickerContent: pickerContent, | ||||||||||||||
| destination: destination | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /// Closure for reporting errors from child views | ||||||||||||||
| private func reportError(_ error: Error) { | ||||||||||||||
| Task { @MainActor in | ||||||||||||||
| self.error = AlertError( | ||||||||||||||
| message: authService.string.localizedErrorMessage(for: error), | ||||||||||||||
| underlyingError: error | ||||||||||||||
| ) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @ToolbarContentBuilder | ||||||||||||||
| private var toolbar: some ToolbarContent { | ||||||||||||||
| ToolbarItem(placement: .topBarTrailing) { | ||||||||||||||
| if !authService.configuration.shouldHideCancelButton { | ||||||||||||||
| Button { | ||||||||||||||
| authService.isPresented = false | ||||||||||||||
| } label: { | ||||||||||||||
| Image(systemName: "xmark") | ||||||||||||||
| .foregroundStyle(Color(UIColor.label)) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+150
to
+152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Since this is a SwiftUI view, you can use the platform-agnostic
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
AuthPickerContentis annotated with@MainActor, thereportErrormethod is already guaranteed to run on the main actor. Spawning a new asynchronousTaskto updateself.erroris redundant and introduces an unnecessary dispatch hop. You can update the state directly.