Created
November 18, 2021 12:40
-
-
Save sichacvah/b19374aa9e70969e8f2bd8f474c4d130 to your computer and use it in GitHub Desktop.
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
| package ru.sergey_kurochkin.navigationcompose | |
| import android.app.PendingIntent | |
| import android.content.Intent | |
| import android.os.Bundle | |
| import androidx.activity.ComponentActivity | |
| import androidx.activity.compose.setContent | |
| import androidx.compose.foundation.background | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.material.Button | |
| import androidx.compose.material.Text | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.platform.LocalContext | |
| import androidx.core.app.TaskStackBuilder | |
| import androidx.core.net.toUri | |
| import androidx.navigation.* | |
| import androidx.navigation.compose.NavHost | |
| import androidx.navigation.compose.composable | |
| import androidx.navigation.compose.rememberNavController | |
| import ru.sergey_kurochkin.navigationcompose.ui.theme.NavigationComposeTheme | |
| import kotlin.random.Random | |
| val uri = "https://www.example.com" | |
| class MainActivity : ComponentActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContent { | |
| NavigationComposeTheme { | |
| val navigationController = rememberNavController() | |
| NavHost(navController = navigationController, startDestination = "home") { | |
| composable("home") { | |
| HomeScreen(navigationController) | |
| } | |
| navigation(startDestination = "A", route = "nested1") { | |
| navigation(route = "nested2", startDestination = "C") { | |
| composable("B", deepLinks = listOf(navDeepLink { uriPattern = "$uri/B" })) { | |
| Screen(name = "B") | |
| } | |
| composable("C") { | |
| Screen(name = "C") | |
| } | |
| } | |
| composable("A") { | |
| Screen(name = "A") | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| object ScreenBackground { | |
| val nextInt = { | |
| Random.Default.nextInt(256) | |
| } | |
| val color = { | |
| Color( | |
| nextInt(), nextInt(), nextInt(), 255 | |
| ) | |
| } | |
| val screenByNameMap = mutableMapOf<String, Color>() | |
| fun getColor(name: String): Color { | |
| val cachedColor = screenByNameMap.get(name) | |
| if (cachedColor != null) { | |
| return cachedColor | |
| } | |
| val next = color() | |
| screenByNameMap.put(name, next) | |
| return next | |
| } | |
| } | |
| @Composable | |
| fun HomeScreen(navController: NavController) { | |
| val backgroundColor = ScreenBackground.getColor("Home") | |
| val context = LocalContext.current | |
| Box(modifier = Modifier | |
| .fillMaxSize() | |
| .background(color = backgroundColor)) { | |
| Button(onClick = { | |
| val deepLinkIntent = Intent( | |
| Intent.ACTION_VIEW, | |
| "https://www.example.com/B".toUri(), | |
| context, | |
| MainActivity::class.java | |
| ) | |
| TaskStackBuilder.create(context).run { | |
| addNextIntentWithParentStack(deepLinkIntent) | |
| getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)?.send() | |
| } | |
| }, modifier = Modifier.align(Alignment.Center)) { | |
| Text("NAVIGATE") | |
| } | |
| } | |
| } | |
| @Composable | |
| fun Screen(name: String) { | |
| val backgroundColor = ScreenBackground.getColor(name) | |
| Box(modifier = Modifier | |
| .fillMaxSize() | |
| .background(color = backgroundColor)) { | |
| Text(text = name, modifier = Modifier.align(Alignment.Center)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment