Skip to content

Commit

Permalink
Replace Koin with Kotlin inject
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Oct 8, 2024
1 parent d930a99 commit 56ed776
Show file tree
Hide file tree
Showing 46 changed files with 380 additions and 241 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ android {

packaging {
resources {


excludes += "/META-INF/{AL2.0,LGPL2.1}"
excludes += "/okhttp3/internal/publicsuffix/NOTICE"
excludes += "/kotlin/**"
Expand All @@ -88,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 All @@ -15,9 +14,7 @@ class NewsApplication @JvmOverloads constructor(
debug = BuildConfig.DEBUG,
newsApiKey = BuildConfig.NEWS_API_KEY,
newsApiBaseUrl = BuildConfig.NEWS_API_BASE_URL,
targetAppDeclaration = {
androidContext(this@NewsApplication)
}
platformContext = this
)
}
}
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
4 changes: 1 addition & 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,14 +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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.androidbroadcast.common

import android.content.Context

public actual typealias PlatformContext = Context
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.androidbroadcast.common

import me.tatarka.inject.annotations.Qualifier

@Qualifier
@Target(
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.FUNCTION,
AnnotationTarget.VALUE_PARAMETER,
AnnotationTarget.TYPE
)
public annotation class Named(val value: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dev.androidbroadcast.common

public expect abstract class PlatformContext
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.androidbroadcast.common

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
public annotation class Singleton
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.androidbroadcast.common

public actual abstract class PlatformContext {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.androidbroadcast.common

public actual abstract class PlatformContext {
}
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
@@ -1,6 +1,7 @@
package dev.androidbroadcast.news.data

import dev.androidbroadcast.common.Logger
import dev.androidbroadcast.common.Singleton
import dev.androidbroadcast.news.data.model.Article
import dev.androidbroadcast.news.database.NewsDatabase
import dev.androidbroadcast.news.database.models.ArticleDBO
Expand All @@ -17,7 +18,9 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import me.tatarka.inject.annotations.Inject

@Inject
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
11 changes: 8 additions & 3 deletions core/platform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.ksp)
}

kotlin {
Expand All @@ -21,7 +22,6 @@ kotlin {
jvm()

listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach { iosTarget ->
Expand All @@ -36,8 +36,6 @@ kotlin {
api(projects.core.common)
implementation(libs.coil.compose)
implementation(projects.core.data)
implementation(projects.core.database)
implementation(projects.core.opennewsApi)
}

jvmMain.dependencies {
Expand All @@ -63,3 +61,10 @@ android {
targetCompatibility = JavaVersion.VERSION_1_8
}
}

dependencies {
"kspAndroid"(libs.kotlinInject.compiler)
"kspIosArm64"(libs.kotlinInject.compiler)
"kspIosSimulatorArm64"(libs.kotlinInject.compiler)
"kspJvm"(libs.kotlinInject.compiler)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.androidbroadcast.news.core

import androidx.room.Room
import androidx.room.RoomDatabase
import dev.androidbroadcast.common.AndroidLogcatLogger
import dev.androidbroadcast.common.Logger
import dev.androidbroadcast.common.PlatformContext
import dev.androidbroadcast.news.database.NewsRoomDatabase

internal actual fun newsRoomDatabaseBuilder(context: PlatformContext): RoomDatabase.Builder<NewsRoomDatabase> {
return Room.databaseBuilder(
context = context,
klass = NewsRoomDatabase::class.java,
name = "news"
)
}

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package dev.androidbroadcast.news.core

import androidx.room.RoomDatabase
import dev.androidbroadcast.common.AppDispatchers
import dev.androidbroadcast.common.Logger
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.Provides
import kotlin.properties.Delegates.notNull

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

internal expect fun newsRoomDatabaseBuilder(context: PlatformContext): RoomDatabase.Builder<NewsRoomDatabase>

internal expect fun newLogger(): Logger

@Component
@Singleton
public abstract class AppComponent(
public val debuggable: Boolean,
protected val baseUrl: String,
protected val apiKey: String,
protected val platformContext: PlatformContext,
) {

public abstract val articlesRepository: ArticlesRepository

public abstract val newsDatabase: NewsDatabase

@ExperimentalSerializationApi
@Singleton
@Provides
internal fun providesJson(): Json {
return Json {
isLenient = true
ignoreUnknownKeys = true
explicitNulls = false
}
}

@Singleton
@Provides
internal fun providesNewsApi(
json: Json,
): NewsApi {
return NewsApi(
baseUrl = baseUrl,
apiKey = apiKey,
json = json
)
}

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

@Singleton
@Provides
internal fun providesNewsDatabase(
dispatchers: AppDispatchers,
): NewsDatabase {
return NewsDatabase(
databaseBuilder = newsRoomDatabaseBuilder(platformContext),
dispatcher = dispatchers.io
)
}

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

public companion object {

public var appComponent: AppComponent by notNull()
private set

internal fun create(
debuggable: Boolean,
baseUrl: String,
apiKey: String,
platformContext: PlatformContext,
): AppComponent {
appComponent = createAppComponent(debuggable, baseUrl, apiKey, platformContext)
return appComponent
}
}
}
Loading

0 comments on commit 56ed776

Please sign in to comment.