-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: Introduce dialog controller #2264
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
Merged
demolaf
merged 6 commits into
firebase:version-10.0.0-dev
from
demolaf:feat/fix-dialogs
Oct 30, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b24f20
fix: introduce dialog controller to avoid multiple dialogs
demolaf 9818065
Update composeapp/src/main/java/com/firebase/composeapp/HighLevelApiD…
demolaf d5480e0
update comments
demolaf bc50cc7
Merge remote-tracking branch 'origin/feat/fix-dialogs' into feat/fix-…
demolaf 48c530f
Merge branch 'version-10.0.0-dev' of https://github.com/firebase/Fire…
demolaf 5231cbe
fix CI
demolaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
auth/src/main/java/com/firebase/ui/auth/compose/ui/components/TopLevelDialogController.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /* | ||
| * Copyright 2025 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.firebase.ui.auth.compose.ui.components | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.compositionLocalOf | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import com.firebase.ui.auth.compose.AuthException | ||
| import com.firebase.ui.auth.compose.configuration.string_provider.AuthUIStringProvider | ||
|
|
||
| /** | ||
| * A top-level dialog controller that allows any child composable to show error recovery dialogs. | ||
| * | ||
| * This is the Compose equivalent of iOS's `windowScene.topViewController` - it provides a single | ||
| * point of control for showing dialogs from anywhere in the composition tree, preventing duplicate | ||
| * dialogs when multiple screens observe the same error state. | ||
| * | ||
| * **Usage:** | ||
| * ```kotlin | ||
| * // At the root of your auth flow (FirebaseAuthScreen): | ||
| * val dialogController = rememberTopLevelDialogController(stringProvider) | ||
| * | ||
| * CompositionLocalProvider(LocalTopLevelDialogController provides dialogController) { | ||
| * // Your auth screens... | ||
| * | ||
| * // Show dialog at root level (only one instance) | ||
| * dialogController.CurrentDialog() | ||
| * } | ||
| * | ||
| * // In any child screen (EmailAuthScreen, PhoneAuthScreen, etc.): | ||
| * val dialogController = LocalTopLevelDialogController.current | ||
| * | ||
| * LaunchedEffect(error) { | ||
| * error?.let { exception -> | ||
| * dialogController?.showErrorDialog( | ||
| * exception = exception, | ||
| * onRetry = { ... }, | ||
| * onRecover = { ... }, | ||
| * onDismiss = { ... } | ||
| * ) | ||
| * } | ||
| * } | ||
| * ``` | ||
| * | ||
| * @since 10.0.0 | ||
| */ | ||
| class TopLevelDialogController( | ||
| private val stringProvider: AuthUIStringProvider | ||
| ) { | ||
| private var dialogState by mutableStateOf<DialogState?>(null) | ||
|
|
||
| /** | ||
| * Shows an error recovery dialog at the top level using [ErrorRecoveryDialog]. | ||
| * | ||
| * @param exception The auth exception to display | ||
| * @param onRetry Callback when user clicks retry button | ||
| * @param onRecover Callback when user clicks recover button (e.g., navigate to different screen) | ||
| * @param onDismiss Callback when dialog is dismissed | ||
| */ | ||
| fun showErrorDialog( | ||
| exception: AuthException, | ||
| onRetry: (AuthException) -> Unit = {}, | ||
| onRecover: (AuthException) -> Unit = {}, | ||
| onDismiss: () -> Unit = {} | ||
| ) { | ||
| dialogState = DialogState.ErrorDialog( | ||
| exception = exception, | ||
| onRetry = onRetry, | ||
| onRecover = onRecover, | ||
| onDismiss = { | ||
| dialogState = null | ||
| onDismiss() | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Dismisses the currently shown dialog. | ||
| */ | ||
| fun dismissDialog() { | ||
| dialogState = null | ||
| } | ||
|
|
||
| /** | ||
| * Composable that renders the current dialog, if any. | ||
| * This should be called once at the root level of your auth flow. | ||
| * | ||
| * Uses the existing [ErrorRecoveryDialog] component. | ||
| */ | ||
| @Composable | ||
| fun CurrentDialog() { | ||
| val state = dialogState | ||
| when (state) { | ||
| is DialogState.ErrorDialog -> { | ||
| ErrorRecoveryDialog( | ||
| error = state.exception, | ||
| stringProvider = stringProvider, | ||
| onRetry = { exception -> | ||
| state.onRetry(exception) | ||
| state.onDismiss() | ||
| }, | ||
| onRecover = { exception -> | ||
| state.onRecover(exception) | ||
| state.onDismiss() | ||
| }, | ||
| onDismiss = state.onDismiss | ||
| ) | ||
| } | ||
| null -> { | ||
| // No dialog to show | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private sealed class DialogState { | ||
| data class ErrorDialog( | ||
| val exception: AuthException, | ||
| val onRetry: (AuthException) -> Unit, | ||
| val onRecover: (AuthException) -> Unit, | ||
| val onDismiss: () -> Unit | ||
| ) : DialogState() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates and remembers a [TopLevelDialogController]. | ||
| */ | ||
| @Composable | ||
| fun rememberTopLevelDialogController( | ||
| stringProvider: AuthUIStringProvider | ||
| ): TopLevelDialogController { | ||
| return remember(stringProvider) { | ||
| TopLevelDialogController(stringProvider) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * CompositionLocal for accessing the top-level dialog controller from any composable. | ||
| * | ||
| * Similar to iOS's window scene controller - provides global access to show dialogs | ||
| * from anywhere in the composition tree without causing duplicates. | ||
| */ | ||
| val LocalTopLevelDialogController = compositionLocalOf<TopLevelDialogController?> { | ||
| null | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.