Skip to content

Commit b34ebcb

Browse files
Implemented ScreenA with shared ViewModel;
1 parent c0f7850 commit b34ebcb

File tree

11 files changed

+112
-49
lines changed

11 files changed

+112
-49
lines changed

composeApp/src/androidMain/kotlin/Platform.android.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

composeApp/src/commonMain/kotlin/App.kt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
import androidx.compose.animation.AnimatedVisibility
2-
import androidx.compose.foundation.Image
3-
import androidx.compose.foundation.layout.Column
4-
import androidx.compose.foundation.layout.fillMaxWidth
5-
import androidx.compose.material.Button
61
import androidx.compose.material.MaterialTheme
7-
import androidx.compose.material.Text
8-
import androidx.compose.runtime.*
9-
import androidx.compose.ui.Alignment
10-
import androidx.compose.ui.Modifier
11-
import org.jetbrains.compose.resources.painterResource
2+
import androidx.compose.runtime.Composable
3+
import androidx.navigation.compose.NavHost
4+
import androidx.navigation.compose.composable
5+
import androidx.navigation.compose.rememberNavController
126
import org.jetbrains.compose.ui.tooling.preview.Preview
13-
14-
import viewmodel_compose_multiplatform.composeapp.generated.resources.Res
15-
import viewmodel_compose_multiplatform.composeapp.generated.resources.compose_multiplatform
7+
import org.koin.compose.KoinContext
168

179
@Composable
1810
@Preview
1911
fun App() {
2012
MaterialTheme {
21-
var showContent by remember { mutableStateOf(false) }
22-
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
23-
Button(onClick = { showContent = !showContent }) {
24-
Text("Click me!")
25-
}
26-
AnimatedVisibility(showContent) {
27-
val greeting = remember { Greeting().greet() }
28-
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
29-
Image(painterResource(Res.drawable.compose_multiplatform), null)
30-
Text("Compose: $greeting")
13+
KoinContext {
14+
val controller = rememberNavController()
15+
16+
NavHost(
17+
navController = controller,
18+
startDestination = "ScreenA"
19+
) {
20+
composable(
21+
route = "ScreenA"
22+
) {
23+
ScreenA()
3124
}
3225
}
3326
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import org.koin.core.module.Module
2+
import org.koin.dsl.module
3+
4+
val applicationModule: Module = module {
5+
single<String> { "Raheem" }
6+
}

composeApp/src/commonMain/kotlin/Greeting.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
expect class KoinInitializer {
2+
fun init()
3+
}

composeApp/src/commonMain/kotlin/Platform.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import androidx.compose.foundation.layout.Arrangement
2+
import androidx.compose.foundation.layout.Column
3+
import androidx.compose.foundation.layout.fillMaxSize
4+
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.material.Scaffold
6+
import androidx.compose.material.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.collectAsState
9+
import androidx.compose.runtime.getValue
10+
import androidx.compose.ui.Alignment
11+
import androidx.compose.ui.Modifier
12+
import androidx.compose.ui.unit.dp
13+
14+
@Composable
15+
fun ScreenA(
16+
viewmodel: ScreenAViewModel = koinViewModel()
17+
) {
18+
val text by viewmodel.text.collectAsState()
19+
val timer by viewmodel.timer.collectAsState()
20+
21+
Scaffold { padding ->
22+
Column(
23+
horizontalAlignment = Alignment.CenterHorizontally,
24+
verticalArrangement = Arrangement.spacedBy(
25+
space = 10.dp,
26+
alignment = Alignment.CenterVertically
27+
),
28+
modifier = Modifier
29+
.fillMaxSize()
30+
.padding(padding)
31+
) {
32+
Text(
33+
text = text
34+
)
35+
Text(
36+
text = timer.toString()
37+
)
38+
}
39+
}
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import androidx.lifecycle.ViewModel
2+
import androidx.lifecycle.viewModelScope
3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.StateFlow
6+
import kotlinx.coroutines.flow.asStateFlow
7+
import kotlinx.coroutines.flow.update
8+
import kotlinx.coroutines.isActive
9+
import kotlinx.coroutines.launch
10+
import kotlin.time.Duration.Companion.seconds
11+
12+
class ScreenAViewModel(
13+
name: String
14+
) : ViewModel() {
15+
16+
private val _text = MutableStateFlow(name)
17+
val text: StateFlow<String> = _text.asStateFlow()
18+
19+
private val _timer = MutableStateFlow(0)
20+
val timer: StateFlow<Int> = _timer.asStateFlow()
21+
22+
init {
23+
startTimer()
24+
}
25+
26+
private fun startTimer() {
27+
viewModelScope.launch {
28+
while (isActive) {
29+
delay(1.seconds)
30+
_timer.update { it + 1 }
31+
}
32+
}
33+
}
34+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import org.koin.core.module.Module
2+
3+
expect val viewModelModule: Module
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import androidx.compose.runtime.Composable
2+
import androidx.lifecycle.ViewModel
3+
import androidx.lifecycle.viewmodel.compose.viewModel
4+
import org.koin.compose.currentKoinScope
5+
6+
@Composable
7+
inline fun <reified T : ViewModel> koinViewModel(): T {
8+
val scope = currentKoinScope()
9+
return viewModel { scope.get() }
10+
}

0 commit comments

Comments
 (0)