-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathKoinIntegrationScreen.kt
91 lines (85 loc) · 3.8 KB
/
KoinIntegrationScreen.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package composegears.tiamat.sample.koin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.composegears.tiamat.*
import com.composegears.tiamat.koin.koinSaveableTiamatViewModel
import com.composegears.tiamat.koin.koinSharedTiamatViewModel
import com.composegears.tiamat.koin.koinTiamatViewModel
import composegears.tiamat.example.ui.core.*
import composegears.tiamat.sample.koin.viewmodel.KoinDetailViewModel
import composegears.tiamat.sample.koin.viewmodel.KoinDetailViewModel.Companion.KoinDetailState.Loading
import composegears.tiamat.sample.koin.viewmodel.KoinDetailViewModel.Companion.KoinDetailState.Success
import composegears.tiamat.sample.koin.viewmodel.SaveableViewModel
import composegears.tiamat.sample.koin.viewmodel.SharedViewModel
import org.koin.core.parameter.parametersOf
val KoinIntegrationScreen by navDestination<Unit>(webPathExtension()) {
val navController = rememberNavController(
key = "KoinNavController",
startDestination = KoinListScreen,
destinations = arrayOf(KoinListScreen, KoinDetailScreen)
)
Navigation(
navController = navController,
modifier = Modifier.fillMaxSize()
)
}
private val KoinListScreen by navDestination<Unit> {
val navController = navController()
val saveableViewModel = koinSaveableTiamatViewModel<SaveableViewModel>()
val sharedViewModel = koinSharedTiamatViewModel<SharedViewModel>()
SimpleScreen("Koin integration") {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val timer1 by sharedViewModel.timer.collectAsState()
val timer2 by saveableViewModel.timer.collectAsState()
ViewModelInfo("SharedViewModel", hashCode = sharedViewModel.hashCode(), timer = timer1)
Spacer(8.dp)
ViewModelInfo("SaveableViewModel", hashCode = saveableViewModel.hashCode(), timer = timer2)
Spacer(8.dp)
TextCaption("Pass value to detail screen")
NextButton(
text = "Open detail",
onClick = {
navController.navigate(
dest = KoinDetailScreen,
navArgs = "dynamic_argument"
)
}
)
BackButton(onClick = navController::back)
}
}
}
private val KoinDetailScreen by navDestination<String> {
val params = navArgs()
val navController = navController()
val viewModel = koinTiamatViewModel<KoinDetailViewModel> { parametersOf(params) }
val sharedViewModel = koinSharedTiamatViewModel<SharedViewModel>()
val state by viewModel.state.collectAsState()
SimpleScreen("KoinDetail Screen") {
when (val detailState = state) {
is Loading -> CircularProgressIndicator()
is Success -> {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically),
horizontalAlignment = Alignment.CenterHorizontally
) {
val timer by sharedViewModel.timer.collectAsState()
ViewModelInfo("SharedViewModel", hashCode = sharedViewModel.hashCode(), timer = timer)
TextBody(text = detailState.result)
BackButton(onClick = navController::back)
}
}
}
}
}