Advertisement
Guest User

MovieBuffRootImpl

a guest
Mar 23rd, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package decompose
  2.  
  3. import com.arkivanov.decompose.ComponentContext
  4. import com.arkivanov.decompose.router.stack.*
  5. import com.arkivanov.decompose.value.Value
  6. import kotlinx.serialization.Serializable
  7.  
  8. class MovieBuffRootImpl(
  9. componentContext: ComponentContext,
  10. private val mainScreen: (ComponentContext, (movieId: Int) -> Unit) -> MainScreenComponent,
  11. private val movieDetails: (
  12. ComponentContext, movieId: Int, onBackPressed: () -> Unit
  13. ) -> DetailsScreenComponent
  14. ) : MovieBuffRoot, ComponentContext by componentContext {
  15.  
  16. constructor(
  17. componentContext: ComponentContext
  18. ) : this(componentContext,
  19. mainScreen = { childContext, onMovieSelected ->
  20. MainScreenComponentImpl(childContext, onMovieSelected)
  21. },
  22. movieDetails = { childContext, movieId, onBackPressed ->
  23. DetailsScreenComponentImpl(childContext, movieId) {
  24. onBackPressed.invoke()
  25. }
  26. }
  27. )
  28.  
  29. private val navigation = StackNavigation<Configuration>()
  30.  
  31. private val stack = childStack(
  32. key = "RootComponent",
  33. source = navigation,
  34. serializer = Configuration.serializer(),
  35. initialConfiguration = Configuration.Dashboard,
  36. handleBackButton = true,
  37. childFactory = ::createChild
  38. )
  39.  
  40. private fun createChild(
  41. configuration: Configuration,
  42. componentContext: ComponentContext
  43. ): MovieBuffRoot.Child =
  44. when (configuration) {
  45. Configuration.Dashboard -> MovieBuffRoot.Child.MainScreen(
  46. mainScreen(componentContext, ::onMovieSelected)
  47. )
  48.  
  49. is Configuration.Details -> MovieBuffRoot.Child.DetailScreen(
  50. movieDetails(componentContext, configuration.movieId, ::onDetailsScreenBackPressed)
  51. )
  52. }
  53.  
  54. private fun onMovieSelected(movieId: Int) {
  55. println("Ansh: onMovieSelected-$movieId")
  56. navigation.push(Configuration.Details(movieId), onComplete = {
  57. println("Ansh: isMovieSelected ")
  58. })
  59. }
  60.  
  61.  
  62. private fun onDetailsScreenBackPressed(){
  63. navigation.pop()
  64. }
  65.  
  66.  
  67. override val childStack: Value<ChildStack<*, MovieBuffRoot.Child>>
  68. get() = value()
  69.  
  70. private fun value() = stack
  71.  
  72. @Serializable
  73. private sealed class Configuration {
  74. @Serializable
  75. data object Dashboard : Configuration()
  76.  
  77. @Serializable
  78. data class Details(
  79. val movieId: Int
  80. ) : Configuration()
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement