File tree Expand file tree Collapse file tree 5 files changed +50
-12
lines changed
androidMain/kotlin/com/developersbreach/kotlindictionarymultiplatform
commonMain/kotlin/com/developersbreach/kotlindictionarymultiplatform
desktopMain/kotlin/com/developersbreach/kotlindictionarymultiplatform Expand file tree Collapse file tree 5 files changed +50
-12
lines changed Original file line number Diff line number Diff line change @@ -5,10 +5,19 @@ import androidx.activity.ComponentActivity
55import androidx.activity.compose.setContent
66import androidx.compose.runtime.Composable
77import 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
912class 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -5,7 +5,10 @@ import androidx.compose.runtime.remember
55import androidx.navigation.compose.NavHost
66import androidx.navigation.compose.composable
77import androidx.navigation.compose.rememberNavController
8+ import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailScreen
9+ import com.developersbreach.kotlindictionarymultiplatform.ui.screens.DetailViewModel
810import com.developersbreach.kotlindictionarymultiplatform.ui.screens.TopicListScreen
11+ import org.koin.compose.viewmodel.koinViewModel
912
1013@Composable
1114fun 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}
Original file line number Diff line number Diff line change 11package com.developersbreach.kotlindictionarymultiplatform.ui.screens
22
3+ import androidx.lifecycle.SavedStateHandle
34import androidx.lifecycle.ViewModel
45import androidx.lifecycle.viewModelScope
6+ import androidx.navigation.toRoute
57import com.developersbreach.kotlindictionarymultiplatform.core.KotlinTopicDetails
68import com.developersbreach.kotlindictionarymultiplatform.core.KtorHttpClient
79import kotlinx.coroutines.flow.MutableStateFlow
810import kotlinx.coroutines.flow.StateFlow
911import kotlinx.coroutines.launch
1012import com.developersbreach.kotlindictionarymultiplatform.core.API_KEY
13+ import com.developersbreach.kotlindictionarymultiplatform.ui.navigation.AppDestinations
1114
1215class 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 {
Original file line number Diff line number Diff line change @@ -2,12 +2,20 @@ package com.developersbreach.kotlindictionarymultiplatform
22
33import androidx.compose.ui.window.Window
44import 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}
You can’t perform that action at this time.
0 commit comments