Created
July 6, 2022 19:17
-
-
Save VitalyPeryatin/5cdb138d01b4ad11442307099e9202a5 to your computer and use it in GitHub Desktop.
Open screen chain (Compose)
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
class AppActivity : BaseActivity() { | |
private val viewModel: AppViewModel by viewModel() | |
override fun onNewIntent(intent: Intent?) { | |
super.onNewIntent(intent) | |
tryOpenScreenChain(intent) | |
} | |
private fun tryOpenScreenChain(intent: Intent?) { | |
viewModel.tryOpenScreenChain( | |
routes = intent?.getStringArrayListExtra(ROUTES_KEY).orEmpty().filterNotNull() | |
) | |
} | |
@OptIn(ExperimentalAnimationApi::class) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
tryOpenScreenChain(intent) | |
setContent { | |
AfterglowTheme { | |
CompositionLocalProvider( | |
... | |
) { | |
AppNavGraph(navController = rememberAnimatedNavController()) | |
} | |
} | |
} | |
} | |
@OptIn(ExperimentalAnimationApi::class) | |
@Composable | |
fun AppNavGraph( | |
modifier: Modifier = Modifier, | |
navController: NavHostController | |
) { | |
LaunchedEffect(Unit) { | |
viewModel.getNavigationPendingRoutes() | |
.onEach { routes -> | |
navController.popBackStack(DesktopDestination.createRoute(), inclusive = false) | |
routes.forEach { navController.navigate(it)} | |
} | |
.launchIn(this) | |
} | |
AnimatedNavHost( | |
navController = navController, | |
... | |
) { | |
... | |
} | |
} | |
companion object { | |
private const val ROUTES_KEY = "routes" | |
fun getMessagesIntent(context: Context, chatId: Long?): Intent { | |
return Intent(context, AppActivity::class.java).apply { | |
putStringArrayListExtra( | |
ROUTES_KEY, arrayListOf( | |
ChatsDestination.createRoute(ChatsDestination.TabKey.Inbox), | |
MessagesDestination.createRoute(chatId = chatId ?: 0L) | |
)) | |
} | |
} | |
} | |
} |
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
class AppViewModel( | |
... | |
): BaseViewModel() { | |
private val _pendingNavigationRoutes = Channel<List<String>>( | |
capacity = 1, | |
onBufferOverflow = BufferOverflow.DROP_LATEST | |
) | |
fun tryOpenScreenChain(routes: List<String>) { | |
if (routes.isNotEmpty()) { | |
viewModelScope.launch { | |
_pendingNavigationRoutes.send(routes) | |
} | |
} | |
} | |
fun getNavigationPendingRoutes(): Flow<List<String>> { | |
return _pendingNavigationRoutes.receiveAsFlow() | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment