Skip to content

Commit

Permalink
Added shared viewModel + koin
Browse files Browse the repository at this point in the history
  • Loading branch information
LinX64 committed Oct 19, 2023
1 parent c3918f1 commit 048daaf
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 48 deletions.
2 changes: 1 addition & 1 deletion androidApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity
android:name=".ui.ui.MainActivity"
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -14,7 +14,7 @@ class NewsApp : Application() {

private fun initKoin() {
startKoin {
modules(appModule)
// modules(appModule)
}
}
}
11 changes: 10 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand All @@ -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" }

Expand Down
8 changes: 7 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -54,7 +60,7 @@ kotlin {
api(libs.androidx.lifecycle.runtimeCompose)

// Koin
api(libs.koin.android)
implementation(libs.koin.android)

// Retrofit
api(libs.retrofit.core)
Expand Down
13 changes: 13 additions & 0 deletions shared/src/androidMain/kotlin/di/Utils.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : ViewModel> Module.viewModelDefinition(
qualifier: Qualifier?,
noinline definition: Definition<T>,
): KoinDefinition<T> = viewModel(qualifier = qualifier, definition = definition)
10 changes: 5 additions & 5 deletions shared/src/androidMain/kotlin/main.android.kt
Original file line number Diff line number Diff line change
@@ -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()
}
58 changes: 33 additions & 25 deletions shared/src/commonMain/kotlin/NewsApp.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@

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()
}
}
}
}
}

@Composable
private fun MainContent(
//viewModel: MainViewModel
viewModel: MainViewModel = koinInject()
) {
val lazyListState = rememberLazyGridState()
LazyVerticalGrid(
Expand All @@ -55,8 +67,4 @@ private fun MainContent(
NewsItem()
}
}


}

expect fun getPlatformName(): String
}
12 changes: 12 additions & 0 deletions shared/src/commonMain/kotlin/di/Utils.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : ViewModel> Module.viewModelDefinition(
qualifier: Qualifier? = null,
noinline definition: Definition<T>
): KoinDefinition<T>
10 changes: 7 additions & 3 deletions shared/src/commonMain/kotlin/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -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() {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package components
package ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package components
package ui.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.CenterAlignedTopAppBar
Expand Down
12 changes: 12 additions & 0 deletions shared/src/iosMain/kotlin/di/Utils.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : ViewModel> Module.viewModelDefinition(
qualifier: Qualifier?,
noinline definition: Definition<T>,
): KoinDefinition<T> = factory(qualifier = qualifier, definition = definition)
2 changes: 0 additions & 2 deletions shared/src/iosMain/kotlin/main.ios.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import androidx.compose.ui.window.ComposeUIViewController

actual fun getPlatformName(): String = "iOS"

fun MainViewController() = ComposeUIViewController { NewsApp() }

0 comments on commit 048daaf

Please sign in to comment.