• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.accompanist.navigation.animation
18 
19 import android.annotation.SuppressLint
20 import androidx.compose.animation.AnimatedVisibilityScope
21 import androidx.compose.animation.ExperimentalAnimationApi
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.navigation.NavBackStackEntry
25 import androidx.navigation.NavDestination
26 import androidx.navigation.NavOptions
27 import androidx.navigation.Navigator
28 import kotlin.collections.forEach
29 
30 /**
31  * Navigator that navigates through [Composable]s. Every destination using this Navigator must
32  * set a valid [Composable] by setting it directly on an instantiated [Destination] or calling
33  * [composable].
34  */
35 @Deprecated(
36     message = "Replace with ComposeNavigator from Androidx Navigation and change import " +
37         "from com.google.accompanist.navigation.animation.AnimatedComposeNavigator to " +
38         "androidx.navigation.compose.ComposeNavigator.",
39     replaceWith = ReplaceWith(
40         "ComposeNavigator",
41         "androidx.navigation.compose.ComposeNavigator"
42     )
43 )
44 @ExperimentalAnimationApi
45 @Navigator.Name("animatedComposable")
46 @Suppress("DEPRECATION")
47 public class AnimatedComposeNavigator private constructor() : Navigator<AnimatedComposeNavigator.Destination>() {
48 
49     internal val transitionsInProgress get() = state.transitionsInProgress
50 
51     internal val backStack get() = state.backStack
52 
53     internal val isPop = mutableStateOf(false)
54 
55     @SuppressLint("NewApi") // b/187418647
56     override fun navigate(
57         entries: List<NavBackStackEntry>,
58         navOptions: NavOptions?,
59         navigatorExtras: Extras?
60     ) {
61         entries.forEach { entry ->
62             state.pushWithTransition(entry)
63         }
64         isPop.value = false
65     }
66 
67     @Deprecated(
68         message = "Replace with ComposeNavigator.createDestination from " +
69             "Androidx Navigation"
70     )
71     override fun createDestination(): Destination {
72         return Destination(this, content = { })
73     }
74 
75     override fun popBackStack(popUpTo: NavBackStackEntry, savedState: Boolean) {
76         state.popWithTransition(popUpTo, savedState)
77         isPop.value = true
78     }
79 
80     internal fun markTransitionComplete(entry: NavBackStackEntry) {
81         state.markTransitionComplete(entry)
82     }
83 
84     /**
85      * NavDestination specific to [AnimatedComposeNavigator]
86      */
87     @Deprecated(
88         message = "Replace with Androidx ComposeNavigator.Destination and change import to " +
89             "androidx.navigation.compose.ComposeNavigator.",
90         replaceWith = ReplaceWith(
91             "ComposeNavigator.Destination",
92             "androidx.navigation.compose.ComposeNavigator"
93         )
94     )
95     @ExperimentalAnimationApi
96     @NavDestination.ClassType(Composable::class)
97     public class Destination(
98         navigator: AnimatedComposeNavigator,
99         internal val content: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit
100     ) : NavDestination(navigator)
101 
102     public companion object {
103         internal const val NAME = "animatedComposable"
104 
105         @Deprecated(
106             message = "Replace with Androidx ComposeNavigator and change import to " +
107                 "androidx.navigation.compose.ComposeNavigator.",
108             replaceWith = ReplaceWith(
109                 "ComposeNavigator()",
110                 "androidx.navigation.compose.ComposeNavigator"
111             )
112         )
113         public operator fun invoke(): AnimatedComposeNavigator = AnimatedComposeNavigator()
114     }
115 }
116