From 048daafc240d5e0b933e8e7b31a7b215ba3a05dc Mon Sep 17 00:00:00 2001 From: Mohsen Rzna Date: Fri, 20 Oct 2023 01:04:21 +0200 Subject: [PATCH] Added shared viewModel + koin --- .../src/androidMain/AndroidManifest.xml | 2 +- .../client/news/{ui/ui => }/MainActivity.kt | 7 +-- .../com/client/news/{ui => }/NewsApp.kt | 6 +- gradle/libs.versions.toml | 11 +++- shared/build.gradle.kts | 8 ++- shared/src/androidMain/kotlin/di/Utils.kt | 13 +++++ shared/src/androidMain/kotlin/main.android.kt | 10 ++-- shared/src/commonMain/kotlin/NewsApp.kt | 58 +++++++++++-------- shared/src/commonMain/kotlin/di/Utils.kt | 12 ++++ .../src/commonMain/kotlin/ui/MainViewModel.kt | 10 +++- .../kotlin/{ => ui}/components/NewsItem.kt | 2 +- .../kotlin/{ => ui}/components/NewsTopBar.kt | 2 +- shared/src/iosMain/kotlin/di/Utils.kt | 12 ++++ shared/src/iosMain/kotlin/main.ios.kt | 2 - 14 files changed, 107 insertions(+), 48 deletions(-) rename androidApp/src/androidMain/kotlin/com/client/news/{ui/ui => }/MainActivity.kt (63%) rename androidApp/src/androidMain/kotlin/com/client/news/{ui => }/NewsApp.kt (73%) create mode 100644 shared/src/androidMain/kotlin/di/Utils.kt create mode 100644 shared/src/commonMain/kotlin/di/Utils.kt rename shared/src/commonMain/kotlin/{ => ui}/components/NewsItem.kt (97%) rename shared/src/commonMain/kotlin/{ => ui}/components/NewsTopBar.kt (97%) create mode 100644 shared/src/iosMain/kotlin/di/Utils.kt diff --git a/androidApp/src/androidMain/AndroidManifest.xml b/androidApp/src/androidMain/AndroidManifest.xml index cd4d76d..33b414b 100644 --- a/androidApp/src/androidMain/AndroidManifest.xml +++ b/androidApp/src/androidMain/AndroidManifest.xml @@ -9,7 +9,7 @@ android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> diff --git a/androidApp/src/androidMain/kotlin/com/client/news/ui/ui/MainActivity.kt b/androidApp/src/androidMain/kotlin/com/client/news/MainActivity.kt similarity index 63% rename from androidApp/src/androidMain/kotlin/com/client/news/ui/ui/MainActivity.kt rename to androidApp/src/androidMain/kotlin/com/client/news/MainActivity.kt index caf3945..a44eabe 100644 --- a/androidApp/src/androidMain/kotlin/com/client/news/ui/ui/MainActivity.kt +++ b/androidApp/src/androidMain/kotlin/com/client/news/MainActivity.kt @@ -1,19 +1,16 @@ -package com.client.news.ui.ui +package com.client.news import android.os.Bundle import androidx.activity.compose.setContent -import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { - private val viewModel: MainViewModel by viewModels() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - // MainView(viewModel = viewModel) + NewsApp() } } } \ No newline at end of file diff --git a/androidApp/src/androidMain/kotlin/com/client/news/ui/NewsApp.kt b/androidApp/src/androidMain/kotlin/com/client/news/NewsApp.kt similarity index 73% rename from androidApp/src/androidMain/kotlin/com/client/news/ui/NewsApp.kt rename to androidApp/src/androidMain/kotlin/com/client/news/NewsApp.kt index 0c96735..df8fb1d 100644 --- a/androidApp/src/androidMain/kotlin/com/client/news/ui/NewsApp.kt +++ b/androidApp/src/androidMain/kotlin/com/client/news/NewsApp.kt @@ -1,7 +1,7 @@ -package com.client.news.ui +package com.client.news import android.app.Application -import com.client.news.ui.di.appModule +import di.appModule import org.koin.core.context.GlobalContext.startKoin class NewsApp : Application() { @@ -14,7 +14,7 @@ class NewsApp : Application() { private fun initKoin() { startKoin { - modules(appModule) + // modules(appModule) } } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 984417d..bfbeb9a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -29,8 +29,10 @@ androidxWindowManager = "1.1.0" coil = "2.4.0" jetbrains-atomicfu = "0.22.0" koin = "3.5.0" +koin-compose = "1.1.0" loggingInterceptor = "4.12.0" +mokoMvvmVersion = "0.16.1" retrofit = "2.9.0" okhttp = "4.12.0" gson = "2.10.1" @@ -87,6 +89,10 @@ androidx-compose-ui-util = { group = "androidx.compose.ui", name = "ui-util" } androidx-compose-ui-unit = { group = "androidx.compose.ui", name = "ui-unit", version.ref = "androidxComposeUiUnit" } accompanist-permissions = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanist-permissions" } +# Moko +moko-core = { module = "dev.icerock.moko:mvvm-core", version.ref = "mokoMvvmVersion" } +moko-flow = { module = "dev.icerock.moko:mvvm-flow", version.ref = "mokoMvvmVersion" } + # Retrofit retrofit-core = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" } gson-core = { group = "com.google.code.gson", name = "gson", version.ref = "gson" } @@ -111,7 +117,10 @@ androidx-tracing-ktx = { group = "androidx.tracing", name = "tracing-ktx", versi turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine" } # DI & testing -koin-android = { group = "io.insert-koin", name = "koin-android", version.ref = "koin" } +koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" } +koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" } +koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin-compose"} + junit4 = { group = "junit", name = "junit", version.ref = "junit4" } kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk8", version.ref = "kotlin" } diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 7dc5719..cf660f6 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -38,6 +38,12 @@ kotlin { @OptIn(ExperimentalComposeLibrary::class) implementation(compose.components.resources) implementation(libs.jetbrains.atomicfu) + + implementation(libs.koin.core) + implementation(libs.koin.compose) + + implementation(libs.moko.core) + implementation(libs.moko.flow) } } val androidMain by getting { @@ -54,7 +60,7 @@ kotlin { api(libs.androidx.lifecycle.runtimeCompose) // Koin - api(libs.koin.android) + implementation(libs.koin.android) // Retrofit api(libs.retrofit.core) diff --git a/shared/src/androidMain/kotlin/di/Utils.kt b/shared/src/androidMain/kotlin/di/Utils.kt new file mode 100644 index 0000000..0197a23 --- /dev/null +++ b/shared/src/androidMain/kotlin/di/Utils.kt @@ -0,0 +1,13 @@ +package di + +import dev.icerock.moko.mvvm.viewmodel.ViewModel +import org.koin.androidx.viewmodel.dsl.viewModel +import org.koin.core.definition.Definition +import org.koin.core.definition.KoinDefinition +import org.koin.core.module.Module +import org.koin.core.qualifier.Qualifier + +actual inline fun Module.viewModelDefinition( + qualifier: Qualifier?, + noinline definition: Definition, +): KoinDefinition = viewModel(qualifier = qualifier, definition = definition) \ No newline at end of file diff --git a/shared/src/androidMain/kotlin/main.android.kt b/shared/src/androidMain/kotlin/main.android.kt index 3b8cae3..678c8ad 100644 --- a/shared/src/androidMain/kotlin/main.android.kt +++ b/shared/src/androidMain/kotlin/main.android.kt @@ -1,6 +1,6 @@ -actual fun getPlatformName(): String = "Android" +import androidx.compose.runtime.Composable -/*@Composable -fun MainView(viewModel: MainViewModel) { - NewsApp(viewModel = viewModel) -}*/ +@Composable +fun MainView() { + NewsApp() +} diff --git a/shared/src/commonMain/kotlin/NewsApp.kt b/shared/src/commonMain/kotlin/NewsApp.kt index 70a5b7d..8a6b4ed 100644 --- a/shared/src/commonMain/kotlin/NewsApp.kt +++ b/shared/src/commonMain/kotlin/NewsApp.kt @@ -1,35 +1,47 @@ - -import androidx.compose.foundation.layout.* +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.lazy.grid.GridCells import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.rememberLazyGridState -import androidx.compose.material3.* +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp -import components.NewsItem -import components.NewsTopBar +import ui.components.NewsItem +import ui.components.NewsTopBar +import di.appModule +import org.koin.compose.KoinApplication +import org.koin.compose.koinInject +import ui.MainViewModel -@OptIn(ExperimentalMaterial3Api::class) @Composable fun NewsApp( - modifier: Modifier = Modifier, - // viewModel: MainViewModel + modifier: Modifier = Modifier ) { - MaterialTheme { - val snackBarHostState = remember { SnackbarHostState() } + KoinApplication(application = { + modules(appModule) + }) { + MaterialTheme { + val snackBarHostState = remember { SnackbarHostState() } - Scaffold( - containerColor = Color.Transparent, - contentColor = MaterialTheme.colorScheme.onBackground, - snackbarHost = { SnackbarHost(snackBarHostState) }, - contentWindowInsets = WindowInsets(0, 0, 0, 0) - ) { - Column(modifier = modifier.fillMaxSize()) { - NewsTopBar() - //MainContent(viewModel = viewModel) + Scaffold( + containerColor = Color.Transparent, + contentColor = MaterialTheme.colorScheme.onBackground, + snackbarHost = { SnackbarHost(snackBarHostState) }, + contentWindowInsets = WindowInsets(0, 0, 0, 0) + ) { + Column(modifier = modifier.fillMaxSize()) { + NewsTopBar() + MainContent() + } } } } @@ -37,7 +49,7 @@ fun NewsApp( @Composable private fun MainContent( - //viewModel: MainViewModel + viewModel: MainViewModel = koinInject() ) { val lazyListState = rememberLazyGridState() LazyVerticalGrid( @@ -55,8 +67,4 @@ private fun MainContent( NewsItem() } } - - -} - -expect fun getPlatformName(): String \ No newline at end of file +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/di/Utils.kt b/shared/src/commonMain/kotlin/di/Utils.kt new file mode 100644 index 0000000..b156342 --- /dev/null +++ b/shared/src/commonMain/kotlin/di/Utils.kt @@ -0,0 +1,12 @@ +package di + +import dev.icerock.moko.mvvm.viewmodel.ViewModel +import org.koin.core.definition.Definition +import org.koin.core.definition.KoinDefinition +import org.koin.core.module.Module +import org.koin.core.qualifier.Qualifier + +expect inline fun Module.viewModelDefinition( + qualifier: Qualifier? = null, + noinline definition: Definition +): KoinDefinition \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/ui/MainViewModel.kt b/shared/src/commonMain/kotlin/ui/MainViewModel.kt index 50c1b1d..2911391 100644 --- a/shared/src/commonMain/kotlin/ui/MainViewModel.kt +++ b/shared/src/commonMain/kotlin/ui/MainViewModel.kt @@ -1,6 +1,10 @@ package ui -/* -expect class MainViewModel() { +import data.repository.MainRepository +import dev.icerock.moko.mvvm.viewmodel.ViewModel -}*/ +class MainViewModel( + private val repository: MainRepository +) : ViewModel() { + +} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/components/NewsItem.kt b/shared/src/commonMain/kotlin/ui/components/NewsItem.kt similarity index 97% rename from shared/src/commonMain/kotlin/components/NewsItem.kt rename to shared/src/commonMain/kotlin/ui/components/NewsItem.kt index 992caf0..aaac380 100644 --- a/shared/src/commonMain/kotlin/components/NewsItem.kt +++ b/shared/src/commonMain/kotlin/ui/components/NewsItem.kt @@ -1,4 +1,4 @@ -package components +package ui.components import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Column diff --git a/shared/src/commonMain/kotlin/components/NewsTopBar.kt b/shared/src/commonMain/kotlin/ui/components/NewsTopBar.kt similarity index 97% rename from shared/src/commonMain/kotlin/components/NewsTopBar.kt rename to shared/src/commonMain/kotlin/ui/components/NewsTopBar.kt index 75504c5..3c26a38 100644 --- a/shared/src/commonMain/kotlin/components/NewsTopBar.kt +++ b/shared/src/commonMain/kotlin/ui/components/NewsTopBar.kt @@ -1,4 +1,4 @@ -package components +package ui.components import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material3.CenterAlignedTopAppBar diff --git a/shared/src/iosMain/kotlin/di/Utils.kt b/shared/src/iosMain/kotlin/di/Utils.kt new file mode 100644 index 0000000..39dedc2 --- /dev/null +++ b/shared/src/iosMain/kotlin/di/Utils.kt @@ -0,0 +1,12 @@ +package di + +import dev.icerock.moko.mvvm.viewmodel.ViewModel +import org.koin.core.definition.Definition +import org.koin.core.definition.KoinDefinition +import org.koin.core.module.Module +import org.koin.core.qualifier.Qualifier + +actual inline fun Module.viewModelDefinition( + qualifier: Qualifier?, + noinline definition: Definition, +): KoinDefinition = factory(qualifier = qualifier, definition = definition) diff --git a/shared/src/iosMain/kotlin/main.ios.kt b/shared/src/iosMain/kotlin/main.ios.kt index 518eccf..dfa4f29 100644 --- a/shared/src/iosMain/kotlin/main.ios.kt +++ b/shared/src/iosMain/kotlin/main.ios.kt @@ -1,5 +1,3 @@ import androidx.compose.ui.window.ComposeUIViewController -actual fun getPlatformName(): String = "iOS" - fun MainViewController() = ComposeUIViewController { NewsApp() } \ No newline at end of file