Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Dependency Injection Framework from Hilt to Koin #237

Open
wants to merge 1 commit into
base: kmp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app-watch/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("composenews.android.application")
id("composenews.android.application.compose")
id("composenews.android.hilt")
id("composenews.android.koin")
}

android {
Expand Down Expand Up @@ -43,9 +43,7 @@ dependencies {
}
libs.apply {
implementation(androidx.ktx)
implementation(hilt.work)
implementation(lifecycle.runtime.ktx)
implementation(hilt.navigation.compose)
implementation(work.runtime.ktx)
implementation(compose.activity)
implementation(compose.ui.preview.wear)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package ir.composenews

import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import ir.composenews.data.di.repositoryModule
import ir.composenews.marketdetail.marketDetailFeatureModule
import ir.composenews.marketlist.marketListFeatureModule
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.logger.Level

@HiltAndroidApp
class ComposeNewsWearApplication : Application()
class ComposeNewsWearApplication : Application() {
override fun onCreate() {
super.onCreate()

startKoin {
androidContext(this@ComposeNewsWearApplication)
androidLogger(Level.DEBUG)

modules(
listOf(
mainViewModelModule,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break it into two lines.

marketListFeatureModule, marketDetailFeatureModule,

repositoryModule,
)
)
}
}
}
10 changes: 5 additions & 5 deletions app-watch/app/src/main/java/ir/composenews/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ir.composenews

import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import ir.composenews.appwatch.navigation.MainContract
import ir.composenews.base.BaseViewModel
import ir.composenews.core_test.dispatcher.DispatcherProvider
Expand All @@ -10,12 +9,13 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
import org.koin.android.annotation.KoinViewModel
import org.koin.core.component.KoinComponent

@HiltViewModel
class MainViewModel @Inject constructor(
@KoinViewModel
class MainViewModel(
dispatcherProvider: DispatcherProvider,
) : BaseViewModel(dispatcherProvider), MainContract {
) : BaseViewModel(dispatcherProvider), MainContract{

private val mutableState = MutableStateFlow(MainContract.State())
override val state: StateFlow<MainContract.State> = mutableState.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ir.composenews

import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module

val mainViewModelModule =module {
viewModel {
MainViewModel(get())}
}
8 changes: 3 additions & 5 deletions app-watch/app/src/main/java/ir/composenews/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ir.composenews.ui
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
Expand All @@ -13,17 +12,16 @@ import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
import androidx.wear.tooling.preview.devices.WearDevices
import com.google.android.horologist.compose.layout.AppScaffold
import com.google.android.horologist.compose.layout.ScreenScaffold
import dagger.hilt.android.AndroidEntryPoint
import ir.composenews.MainViewModel
import ir.composenews.appwatch.navigation.graph.Destinations
import ir.composenews.appwatch.navigation.MainContract
import ir.composenews.appwatch.navigation.graph.Destinations
import ir.composenews.designsystem.theme.ComposeNewsTheme
import ir.composenews.navigation.ComposeNewsWearNavHost
import org.koin.androidx.viewmodel.ext.android.viewModel

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val mainViewModel: MainViewModel by viewModels()
private val mainViewModel: MainViewModel by viewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down
2 changes: 1 addition & 1 deletion app-watch/navigation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

dependencies {
projects.apply {
implementation(appWatch.ui)
api(appWatch.ui)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you make it api rather than implementation?

implementation(core.uimarket)
}
implementation(libs.navigation.compose.wear)
Expand Down
5 changes: 3 additions & 2 deletions app-watch/ui/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("composenews.android.feature")
id("composenews.android.koin")
}

android {
Expand All @@ -14,8 +15,8 @@ configurations.all {

dependencies {
projects.apply {
implementation(feature.marketlist)
implementation(feature.marketdetail)
api(feature.marketlist)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And Why api?

api(feature.marketdetail)
implementation(core.uimarket)
implementation(core.extensions)
implementation(data.marketRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import androidx.wear.tooling.preview.devices.WearDevices
Expand All @@ -42,12 +41,13 @@ import ir.composenews.marketdetail.MarketDetailViewModel
import ir.composenews.marketdetail.formatNumber
import ir.composenews.marketdetail.preview_provider.MarketDetailStateProvider
import ir.composenews.uimarket.model.MarketModel
import org.koin.androidx.compose.koinViewModel

@Composable
fun MarketDetailWearRoute(
market: MarketModel?,
) {
val viewModel: MarketDetailViewModel = hiltViewModel()
val viewModel: MarketDetailViewModel = koinViewModel()
val (state, event) = use(viewModel = viewModel)
LaunchedEffect(key1 = market) {
event.invoke(MarketDetailContract.Event.SetMarket(market = market))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.wear.compose.foundation.lazy.items
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.compose.layout.ScalingLazyColumn
Expand All @@ -22,13 +21,14 @@ import ir.composenews.extensions.roundToTwoDecimalPlaces
import ir.composenews.marketlist.MarketListContract
import ir.composenews.marketlist.MarketListViewModel
import ir.composenews.uimarket.model.MarketModel
import org.koin.androidx.compose.koinViewModel

@Composable
fun MarketListWearRoute(
showFavoriteList: Boolean = false,
onNavigateToDetailScreen: (market: MarketModel) -> Unit,
) {
val viewModel: MarketListViewModel = hiltViewModel()
val viewModel: MarketListViewModel = koinViewModel()
val (state, event) = use(viewModel = viewModel)
LaunchedEffect(key1 = Unit) {
event.invoke(MarketListContract.Event.OnSetShowFavoriteList(showFavoriteList = showFavoriteList))
Expand Down
4 changes: 1 addition & 3 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("composenews.android.application")
id("composenews.android.application.compose")
id("composenews.android.hilt")
id("composenews.android.koin")
}

android {
Expand Down Expand Up @@ -44,10 +44,8 @@ dependencies {
libs.apply {
implementation(compose.activity)
implementation(androidx.ktx)
implementation(hilt.work)
implementation(lifecycle.runtime.ktx)
implementation(work.runtime.ktx)
implementation(hilt.navigation.compose)
implementation(compose.material3.adaptive.navigation.suite)
implementation(compose.material3.adaptive.navigation)
implementation(espresso.core)
Expand Down
5 changes: 0 additions & 5 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@

-keep class * extends androidx.compose.runtime.Composable { *; }

-keepclassmembers class * {
@dagger.hilt.android.lifecycle.HiltViewModel *;
}
-keep,@dagger.hilt.InstallIn class * { *; }
-keep class dagger.hilt.** { *; }
-dontwarn dagger.internal.codegen.ComponentProcessor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this line too?


-keep class retrofit2.** { *; }
Expand Down
22 changes: 19 additions & 3 deletions app/src/main/java/ir/composenews/ComposeNewsApplication.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package ir.composenews

import android.app.Application
import dagger.hilt.android.HiltAndroidApp
import ir.composenews.data.di.repositoryModule
import ir.composenews.marketdetail.marketDetailFeatureModule
import ir.composenews.marketlist.marketListFeatureModule
import ir.composenews.sync.Sync
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level

@HiltAndroidApp
class ComposeNewsApplication : Application() {

override fun onCreate() {
super.onCreate()
Sync.init(this)
}

startKoin {
androidContext(this@ComposeNewsApplication)
androidLogger(Level.DEBUG)

modules(
listOf(
marketListFeatureModule, marketDetailFeatureModule,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break this line into two lines

repositoryModule,
)
)
}
}
}
2 changes: 0 additions & 2 deletions app/src/main/java/ir/composenews/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import dagger.hilt.android.AndroidEntryPoint
import ir.composenews.designsystem.theme.ComposeNewsTheme
import ir.composenews.permission.enum.PermissionType
import ir.composenews.permission.manager.PermissionManager
import ir.composenews.permission.manager.PermissionManagerImpl

@AndroidEntryPoint
class MainActivity : ComponentActivity(), PermissionManager by PermissionManagerImpl() {

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
7 changes: 3 additions & 4 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ gradlePlugin {
id = "composenews.android.application.compose"
implementationClass = "AndroidApplicationComposeConventionPlugin"
}

register("androidHilt") {
id = "composenews.android.hilt"
implementationClass = "HiltConventionPlugin"
register("androidKoin") {
id = "composenews.android.koin"
implementationClass = "KoinConventionPlugin"
}
register("androidLibrary") {
id = "composenews.android.library"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ class AndroidFeatureConventionPlugin : Plugin<Project> {
pluginManager.apply {
apply("composenews.android.library")
apply("composenews.android.library.compose")
apply("composenews.android.hilt")
apply("composenews.android.koin")
}
}

private fun Project.applyDependencies() {
dependencies {
"implementation"(libs.findLibrary("hilt.navigation.compose").get())
"androidTestImplementation"(libs.findLibrary("runner").get())

"testImplementation"(project(":core:test"))
Expand Down
29 changes: 0 additions & 29 deletions build-logic/convention/src/main/kotlin/HiltConventionPlugin.kt

This file was deleted.

24 changes: 24 additions & 0 deletions build-logic/convention/src/main/kotlin/KoinConventionPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ir.composenews.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies

class KoinConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.run {
applyDependencies()
}
}

private fun Project.applyPlugins() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function can be removed.

pluginManager.apply {
// apply("org.jetbrains.kotlin.kapt")
}
}

private fun Project.applyDependencies() {
dependencies {
"implementation"(libs.findBundle("koin").get())
}
}
}
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ plugins {
alias(kotlin.parcelize) apply false
alias(android.library) apply false
alias(kotlin.android) apply false
alias(hilt.android) apply false
alias(kotliner) apply false
alias(detekt) apply false
// alias(ksp) apply false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put back this comment. We are currently working on an issue about KSP.

alias(compose) apply false
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/sync/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id("composenews.android.library")
id("composenews.android.hilt")
id("composenews.android.koin")
}

android {
Expand All @@ -10,7 +10,6 @@ android {
dependencies {
api(projects.domain.market)
libs.apply {
implementation(hilt.work)
implementation(startup.runtime)
implementation(work.runtime.ktx)
}
Expand Down
1 change: 0 additions & 1 deletion core/sync/src/main/java/ir/composenews/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private fun Context.syncWorkNotification(): Notification {
// TODO
return NotificationCompat
.Builder(this, SyncNotificationChannelID)
// .setSmallIcon(androidx.hilt.work.R.drawable.notification_action_background)
.setContentTitle("Background tasks for Compose News")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
Expand Down
Loading
Loading