forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make API for showing DialogNavigator destinations public
Previously, the only way to use DialogNavigator via public APIs was directly through usage of NavHost. By making the DialogNavigator->Dialog Composable function public, DialogNavigator can be reused in any custom NavHost without making its internal state public. Test: DialogNavigatorTest suite still passes Relnote: N/A Change-Id: I125838dc978ee014af46a264deb1458596cce691
- Loading branch information
1 parent
6fd54d3
commit f3adbbd
Showing
7 changed files
with
73 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
52 changes: 52 additions & 0 deletions
52
navigation/navigation-compose/src/main/java/androidx/navigation/compose/DialogHost.kt
This file contains 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,52 @@ | ||
/* | ||
* Copyright 2021 The Android Open Source Project | ||
* | ||
* 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 androidx.navigation.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.saveable.rememberSaveableStateHolder | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.navigation.compose.DialogNavigator.Destination | ||
|
||
/** | ||
* Show each [Destination] on the [DialogNavigator]'s back stack as a [Dialog]. | ||
* | ||
* Note that [NavHost] will call this for you; you do not need to call it manually. | ||
*/ | ||
@Composable | ||
public fun DialogHost(dialogNavigator: DialogNavigator) { | ||
val saveableStateHolder = rememberSaveableStateHolder() | ||
val dialogBackStack by dialogNavigator.backStack.collectAsState() | ||
|
||
dialogBackStack.filter { backStackEntry -> | ||
backStackEntry.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED) | ||
}.forEach { backStackEntry -> | ||
val destination = backStackEntry.destination as Destination | ||
Dialog( | ||
onDismissRequest = { dialogNavigator.dismiss(backStackEntry) }, | ||
properties = destination.dialogProperties | ||
) { | ||
// while in the scope of the composable, we provide the navBackStackEntry as the | ||
// ViewModelStoreOwner and LifecycleOwner | ||
backStackEntry.LocalOwnersProvider(saveableStateHolder) { | ||
destination.content(backStackEntry) | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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