@@ -4,27 +4,19 @@ import com.arkivanov.decompose.ComponentContext
44import com.arkivanov.decompose.router.stack.ChildStack
55import com.arkivanov.decompose.router.stack.StackNavigation
66import com.arkivanov.decompose.router.stack.childStack
7- import com.arkivanov.decompose.router.stack.push
87import com.arkivanov.decompose.router.stack.replaceAll
9- import com.arkivanov.decompose.router.stack.replaceCurrent
108import com.arkivanov.essenty.lifecycle.doOnCreate
11- import com.arkivanov.essenty.lifecycle.doOnResume
12- import com.nekodev.hackathonapp.data.OrderRepository
13- import com.nekodev.hackathonapp.model.OrderState
14- import com.nekodev.hackathonapp.navigation.root.RootComponent.Child.*
9+ import com.nekodev.hackathonapp.navigation.root.RootComponent.Child.DetailsScreenChild
10+ import com.nekodev.hackathonapp.navigation.root.RootComponent.Child.MainScreenChild
11+ import com.nekodev.hackathonapp.navigation.root.RootComponent.Child.SplashScreenChild
1512import com.nekodev.hackathonapp.screens.details.RealDetailsScreenComponent
1613import com.nekodev.hackathonapp.screens.main.RealMainScreenComponent
1714import com.nekodev.hackathonapp.screens.splash.RealSplashScreenComponent
1815import com.nekodev.hackathonapp.util.BaseComponent
1916import com.nekodev.hackathonapp.util.toStateFlow
20- import kotlinx.coroutines.Dispatchers
2117import kotlinx.coroutines.delay
22- import kotlinx.coroutines.flow.MutableStateFlow
2318import kotlinx.coroutines.flow.StateFlow
24- import kotlinx.coroutines.flow.update
2519import kotlinx.coroutines.launch
26- import kotlinx.coroutines.withContext
27- import org.koin.core.component.inject
2820
2921class DefaultRootComponent (
3022 componentContext : ComponentContext
@@ -42,16 +34,11 @@ class DefaultRootComponent(
4234
4335 override val stack: StateFlow <ChildStack <* , RootComponent .Child >> = _stack
4436
45- private val _states : MutableStateFlow <List <OrderState >> = MutableStateFlow (emptyList())
46-
47- private val repo: OrderRepository by inject()
48-
4937 private fun child (config : RootConfig , componentContext : ComponentContext ): RootComponent .Child {
5038 return when (config){
5139 is RootConfig .MainScreen -> MainScreenChild (
5240 RealMainScreenComponent (
5341 componentContext = componentContext,
54- _states = _states ,
5542 navigateToDetails = {
5643 mainScope.launch {
5744 navigation.replaceAll(RootConfig .MainScreen , RootConfig .DetailsScreen (it))
@@ -69,48 +56,8 @@ class DefaultRootComponent(
6956 RealDetailsScreenComponent (
7057 componentContext = componentContext,
7158 orderId = config.orderId,
72- updateOrders = { state ->
73- _states .update {
74- if (
75- it.indexOfFirst { state ->
76- when (state) {
77- is OrderState .OnlyOrder -> {
78- state.orderId == config.orderId
79- }
80-
81- is OrderState .OrderAndState -> {
82- state.orderId == config.orderId
83- }
84- }
85- } == - 1
86- ) {
87- val newList: MutableList <OrderState > = it.toMutableList()
88- newList + = state
89- return @update newList
90- } else {
91- val newList = it.toMutableList()
92- val lol = newList.indexOfFirst {
93- when (state) {
94- is OrderState .OnlyOrder -> {
95- state.orderId == config.orderId
96- }
97-
98- is OrderState .OrderAndState -> {
99- state.orderId == config.orderId
100- }
101- }
102- }
103- if (lol == - 1 ) return @update newList
104- newList.removeAt(lol)
105- newList + = state
106- return @update newList
107- }
108- }
109- },
11059 navigateToMain = {
11160 mainScope.launch {
112- val result = withContext(Dispatchers .IO ){ repo.getAllStates() }
113- _states .value = result
11461 navigation.replaceAll(RootConfig .MainScreen )
11562 }
11663 }
@@ -122,21 +69,8 @@ class DefaultRootComponent(
12269 init {
12370 lifecycle.doOnCreate {
12471 mainScope.launch {
125- val result = withContext(Dispatchers .IO ){ repo.getAllStates() }
126- _states .value = result
12772 delay(1_000L )
128- navigation.replaceCurrent(RootConfig .MainScreen )
129- }
130- }
131- lifecycle.doOnResume(isOneTime = false ) {
132- mainScope.launch {
133- if (
134- stack.value.active.configuration is RootConfig .MainScreen ||
135- stack.value.active.configuration is RootConfig .DetailsScreen
136- ) {
137- val result = withContext(Dispatchers .IO ){ repo.getAllStates() }
138- _states .value = result
139- }
73+ navigation.replaceAll(RootConfig .MainScreen )
14074 }
14175 }
14276 }
0 commit comments