Skip to content

Commit

Permalink
Migrate to Kotlin inject
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Oct 7, 2024
1 parent 4245e29 commit c60c9a6
Show file tree
Hide file tree
Showing 31 changed files with 96 additions and 93 deletions.
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ dependencies {
implementation(projects.core.platform)

implementation(projects.composeApp)
implementation(libs.koin.android)
implementation(libs.coil.core)
implementation(projects.core.common)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.androidbroadcast.newssearchapp
import android.app.Application
import coil3.SingletonImageLoader
import dev.androidbroadcast.news.core.NewsAppPlatform
import org.koin.android.ext.koin.androidContext

class NewsApplication @JvmOverloads constructor(
private val platform: NewsAppPlatform = NewsAppPlatform()
Expand Down
2 changes: 0 additions & 2 deletions compose-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand All @@ -39,7 +38,6 @@ kotlin {
implementation(projects.features.newsMain.ui)
implementation(projects.core.common)
implementation(projects.core.uikit)
implementation(libs.koin.compose)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
import dev.androidbroadcast.news.NewsTheme

@Composable
Expand All @@ -19,7 +18,7 @@ internal actual fun NewsPlatformTheme(
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
// WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}
content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import dev.androidbroadcast.news.NewsTheme
import dev.androidbroadcast.news.main.NewsMainScreen
import org.koin.compose.KoinContext

/**
* Setup theme for platform
Expand All @@ -29,9 +28,7 @@ public fun NewsApp() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
KoinContext {
NewsMainScreen()
}
NewsMainScreen()
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand All @@ -37,15 +36,13 @@ kotlin {
api(libs.kotlinx.immutable)
api(libs.kotlinx.datetime)
api(libs.coil.core)
api(libs.koin.core)
api(libs.kotlinInject.runtime)
implementation(libs.coil.network.ktor)
}

androidMain.dependencies {
implementation(libs.androidx.core.ktx)
api(libs.kotlinx.coroutines.android)
api(libs.koin.android)
}

jvmMain.dependencies {
Expand Down
1 change: 0 additions & 1 deletion core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import kotlinx.coroutines.flow.onEach
import me.tatarka.inject.annotations.Inject

@Inject
@Singleton
public class ArticlesRepository(
private val database: NewsDatabase,
private val api: NewsApi,
Expand Down
2 changes: 0 additions & 2 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand Down Expand Up @@ -81,7 +80,6 @@ dependencies {
add("kspJvm", libs.androidx.room.compiler)
add("kspAndroid", libs.androidx.room.compiler)
add("kspIosSimulatorArm64", libs.androidx.room.compiler)
add("kspIosX64", libs.androidx.room.compiler)
add("kspIosArm64", libs.androidx.room.compiler)
// add("kspCommonMainMetadata", libs.androidx.room.compiler)
}
1 change: 0 additions & 1 deletion core/opennews-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand Down
6 changes: 0 additions & 6 deletions core/platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand Down Expand Up @@ -66,12 +65,7 @@ android {
}

dependencies {
// 1. Configure code generation into the common source set
kspCommonMainMetadata(libs.kotlinInject.compiler)

// 2. Configure code generation into each KMP target source set
"kspAndroid"(libs.kotlinInject.compiler)
"kspIosX64"(libs.kotlinInject.compiler)
"kspIosArm64"(libs.kotlinInject.compiler)
"kspIosSimulatorArm64"(libs.kotlinInject.compiler)
"kspJvm"(libs.kotlinInject.compiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.androidbroadcast.common.AndroidLogcatLogger
import dev.androidbroadcast.common.Logger
import dev.androidbroadcast.common.PlatformContext
import dev.androidbroadcast.news.database.NewsRoomDatabase
import org.koin.android.ext.koin.androidContext

internal actual fun newsRoomDatabaseBuilder(context: PlatformContext): RoomDatabase.Builder<NewsRoomDatabase> {
return Room.databaseBuilder(
Expand All @@ -17,3 +16,12 @@ internal actual fun newsRoomDatabaseBuilder(context: PlatformContext): RoomDatab
}

internal actual fun newLogger(): Logger = AndroidLogcatLogger()

internal actual fun createAppComponent(
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext
): AppComponent {
return AppComponent::class.create(debuggable, baseUrl, apiKey, platformContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import dev.androidbroadcast.common.Logger
import dev.androidbroadcast.common.Named
import dev.androidbroadcast.common.PlatformContext
import dev.androidbroadcast.common.Singleton
import dev.androidbroadcast.news.data.ArticlesRepository
import dev.androidbroadcast.news.database.NewsDatabase
import dev.androidbroadcast.news.database.NewsRoomDatabase
import dev.androidbroadcast.newsapi.NewsApi
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.KmpComponentCreate
import me.tatarka.inject.annotations.Provides
import kotlin.properties.Delegates.notNull

@KmpComponentCreate
internal expect fun createAppComponent(
// debuggable: Boolean,
// baseUrl: String,
// apiKey: String,
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext,
): AppComponent

Expand All @@ -30,16 +30,20 @@ internal expect fun newLogger(): Logger
@Component
@Singleton
public abstract class AppComponent(
// @get:[Provides Named(ConfigProperties.NewsPlatform.Debug)] public val debuggable: Boolean,
// @get:[Provides Named(ConfigProperties.NewsApi.BaseUrl)] protected val baseUrl: String,
// @get:[Provides Named(ConfigProperties.NewsApi.ApiKey)] protected val apiKey: String,
@get:[Provides Named(ConfigProperties.NewsPlatform.Debug)] public val debuggable: Boolean,
@get:[Provides Named(ConfigProperties.NewsApi.BaseUrl)] protected val baseUrl: String,
@get:[Provides Named(ConfigProperties.NewsApi.ApiKey)] protected val apiKey: String,
@get:Provides protected val platformContext: PlatformContext,
) {

public abstract val articlesRepository: ArticlesRepository

public abstract val newsDatabase: NewsDatabase

@ExperimentalSerializationApi
@Singleton
@Provides
public fun json(): Json {
internal fun providesJson(): Json {
return Json {
isLenient = true
ignoreUnknownKeys = true
Expand All @@ -49,7 +53,7 @@ public abstract class AppComponent(

@Singleton
@Provides
public fun newsApi(
internal fun providesNewsApi(
json: Json,
): NewsApi {
return NewsApi(
Expand All @@ -61,11 +65,11 @@ public abstract class AppComponent(

@Singleton
@Provides
public fun appDispatchers(): AppDispatchers = AppDispatchers()
internal fun providesAppDispatchers(): AppDispatchers = AppDispatchers()

@Singleton
@Provides
public fun newsDatabase(
internal fun providesNewsDatabase(
dispatchers: AppDispatchers,
context: PlatformContext,
): NewsDatabase {
Expand All @@ -77,24 +81,20 @@ public abstract class AppComponent(

@Singleton
@Provides
public fun logger(): Logger = newLogger()
internal fun providesLogger(): Logger = newLogger()

public companion object {

public val appComponent: AppComponent
get() = checkNotNull(_appComponent)

private var _appComponent: AppComponent? = null
public var appComponent: AppComponent by notNull()
private set

internal fun create(
// debuggable: Boolean,
// baseUrl: String,
// apiKey: String,
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext,
): AppComponent {
this._appComponent = createAppComponent(
// debuggable, baseUrl, apiKey,
platformContext)
appComponent = createAppComponent(debuggable, baseUrl, apiKey, platformContext)
return appComponent
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class NewsAppPlatform :
platformContext: PlatformContext,
) {
appComponent = AppComponent.create(
// debug, newsApiBaseUrl, newsApiKey,
platformContext)
debug, newsApiBaseUrl, newsApiKey,
platformContext
)
}

override fun newImageLoader(context: coil3.PlatformContext): ImageLoader {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.androidbroadcast.news.core

import dev.androidbroadcast.common.PlatformContext

internal actual fun createAppComponent(
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext
): AppComponent {
return AppComponent::class.create(debuggable, baseUrl, apiKey, platformContext)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.androidbroadcast.news.core

import dev.androidbroadcast.common.PlatformContext

internal actual fun createAppComponent(
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext
): AppComponent {
return AppComponent::class.create(debuggable, baseUrl, apiKey, platformContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ internal actual fun newsRoomDatabaseBuilder(context: PlatformContext): RoomDatab
}

internal actual fun newLogger(): Logger = PrintLogger()

internal actual fun createAppComponent(
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext
): AppComponent {
return AppComponent::class.create(debuggable, baseUrl, apiKey, platformContext)
}
1 change: 0 additions & 1 deletion core/uikit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand Down
5 changes: 0 additions & 5 deletions features/news-main/ui-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand All @@ -38,15 +37,12 @@ kotlin {
api(projects.core.common)
implementation(libs.androidx.lifecycle.runtime)
implementation(libs.androidx.lifecycle.viewmodel)
api(libs.koin.compose.viewmodel)

implementation(projects.core.platform)
}

androidMain.dependencies {
implementation(libs.androidx.core.ktx)
api(libs.koin.androidx.compose)
api(libs.koin.android)
}
}
}
Expand Down Expand Up @@ -82,7 +78,6 @@ dependencies {

// 2. Configure code generation into each KMP target source set
"kspAndroid"(libs.kotlinInject.compiler)
"kspIosX64"(libs.kotlinInject.compiler)
"kspIosArm64"(libs.kotlinInject.compiler)
"kspIosSimulatorArm64"(libs.kotlinInject.compiler)
"kspJvm"(libs.kotlinInject.compiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package dev.androidbroadcast.news.main.di

import dev.androidbroadcast.news.core.AppComponent

actual fun createNewsMainComponent(): NewsMainComponent {
return NewsMainComponent::class.create(AppComponent.appComponent)
actual fun createNewsMainComponent(component: AppComponent): NewsMainComponent {
return NewsMainComponent::class.create(component.articlesRepository)
}
Loading

0 comments on commit c60c9a6

Please sign in to comment.