Skip to content

Commit f039f9c

Browse files
Update module, navigation usage correction to receive args, fix entry points
1 parent b078d19 commit f039f9c

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

composeApp/src/androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/MainActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@ import androidx.activity.ComponentActivity
55
import androidx.activity.compose.setContent
66
import androidx.compose.runtime.Composable
77
import androidx.compose.ui.tooling.preview.Preview
8+
import com.developersbreach.kotlindictionarymultiplatform.di.appModule
9+
import org.koin.android.ext.koin.androidContext
10+
import org.koin.core.context.startKoin
811

912
class MainActivity : ComponentActivity() {
1013
override fun onCreate(savedInstanceState: Bundle?) {
1114
super.onCreate(savedInstanceState)
15+
16+
startKoin {
17+
androidContext(this@MainActivity)
18+
modules(appModule)
19+
}
20+
1221
setContent {
1322
App()
1423
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.developersbreach.kotlindictionarymultiplatform.di
2+
3+
import org.koin.core.module.dsl.viewModel
4+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailViewModel
5+
import org.koin.dsl.module
6+
7+
val appModule = module {
8+
9+
viewModel {
10+
DetailViewModel(get())
11+
}
12+
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/navigation/AppNavigation.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import androidx.compose.runtime.remember
55
import androidx.navigation.compose.NavHost
66
import androidx.navigation.compose.composable
77
import androidx.navigation.compose.rememberNavController
8+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailScreen
9+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailViewModel
810
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.TopicListScreen
11+
import org.koin.compose.viewmodel.koinViewModel
912

1013
@Composable
1114
fun AppNavigation(
@@ -27,9 +30,9 @@ fun AppNavigation(
2730
)
2831
}
2932

30-
// composable<AppDestinations.Detail> { backStackEntry ->
31-
// val viewModel = hiltViewModel<DetailViewModel>()
32-
// DetailScreen(viewModel = viewModel)
33-
// }
33+
composable<AppDestinations.Detail> {
34+
val viewModel: DetailViewModel = koinViewModel()
35+
DetailScreen(viewModel = viewModel)
36+
}
3437
}
3538
}

composeApp/src/commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/ui/screens/DetailViewModel.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package com.developersbreach.kotlindictionarymultiplatform.ui.screens
22

3+
import androidx.lifecycle.SavedStateHandle
34
import androidx.lifecycle.ViewModel
45
import androidx.lifecycle.viewModelScope
6+
import androidx.navigation.toRoute
57
import com.developersbreach.kotlindictionarymultiplatform.core.KotlinTopicDetails
68
import com.developersbreach.kotlindictionarymultiplatform.core.KtorHttpClient
79
import kotlinx.coroutines.flow.MutableStateFlow
810
import kotlinx.coroutines.flow.StateFlow
911
import kotlinx.coroutines.launch
1012
import com.developersbreach.kotlindictionarymultiplatform.core.API_KEY
13+
import com.developersbreach.kotlindictionarymultiplatform.ui.navigation.AppDestinations
1114

1215
class DetailViewModel(
13-
topicId: String
14-
16+
savedStateHandle: SavedStateHandle
1517
) : ViewModel() {
18+
19+
private val topicId = savedStateHandle.toRoute<AppDestinations.Detail>().topicId
20+
1621
private val _state = MutableStateFlow<KotlinTopicDetails?>(null)
1722
val state: StateFlow<KotlinTopicDetails?> = _state
1823

1924
init {
2025
fetchTopic(topicId)
2126
}
27+
2228
private fun fetchTopic(topicId: String) {
2329
viewModelScope.launch {
2430
try {

composeApp/src/desktopMain/kotlin/com/developersbreach/kotlindictionarymultiplatform/main.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ package com.developersbreach.kotlindictionarymultiplatform
22

33
import androidx.compose.ui.window.Window
44
import androidx.compose.ui.window.application
5+
import com.developersbreach.kotlindictionarymultiplatform.di.appModule
6+
import com.developersbreach.kotlindictionarymultiplatform.ui.screens.TopicListScreen
7+
import org.koin.core.context.startKoin
58

6-
fun main() = application {
7-
Window(
8-
onCloseRequest = ::exitApplication,
9-
title = "Kotlin Dictionary Multiplatform",
10-
) {
11-
App()
9+
fun main() {
10+
startKoin {
11+
modules(appModule)
12+
}
13+
application {
14+
Window(
15+
onCloseRequest = ::exitApplication,
16+
title = "Kotlin Dictionary Multiplatform",
17+
) {
18+
TopicListScreen { }
19+
}
1220
}
1321
}

0 commit comments

Comments
 (0)