Skip to content

Commit

Permalink
Merge pull request #52 from stslex/feature/mvi_dagger
Browse files Browse the repository at this point in the history
feature/mvi dagger
  • Loading branch information
stslex authored Sep 18, 2023
2 parents 5752235 + d572364 commit 48c1272
Show file tree
Hide file tree
Showing 183 changed files with 3,457 additions and 1,259 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
dependencies {
implementation(project(":core:core"))
implementation(project(":core:ui"))

implementation(project(":core:collection"))
implementation(project(":core:navigation"))
implementation(project(":core:network"))
Expand Down
50 changes: 16 additions & 34 deletions app/src/main/java/st/slex/csplashscreen/SplashApplication.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
package st.slex.csplashscreen

import android.app.Application
import st.slex.csplashscreen.core.collection.di.ModuleCoreCollection.moduleCoreCollection
import st.slex.csplashscreen.core.favourite.di.ModuleCoreFavourite.moduleCoreFavourite
import st.slex.csplashscreen.core.network.di.ModuleCoreNetwork.moduleCoreNetwork
import st.slex.csplashscreen.core.photos.di.ModuleCorePhotos.moduleCorePhotos
import st.slex.csplashscreen.feature.collection.di.singleCollectionModule
import st.slex.csplashscreen.feature.favourite.di.moduleFeatureFavourite
import st.slex.csplashscreen.feature.feature_photo_detail.di.moduleFeaturePhoto
import st.slex.csplashscreen.feature.home.di.moduleFeatureHome
import st.slex.csplashscreen.feature.search.di.moduleFeatureSearchPhotos
import st.slex.csplashscreen.feature.user.di.moduleFeatureUser
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import st.slex.csplashscreen.core.core.AppApi
import st.slex.csplashscreen.core.core.ApplicationApiProvider
import st.slex.csplashscreen.di.app.AppComponent
import st.slex.csplashscreen.di.app.DaggerAppComponent

class SplashApplication : Application() {
class SplashApplication : Application(), ApplicationApiProvider {

override fun onCreate() {
setUpKoin()
super.onCreate()
private val appComponent: AppComponent by lazy {
DaggerAppComponent
.builder()
.context(this)
.build()
}

private fun setUpKoin() {
startKoin {
androidLogger()
androidContext(this@SplashApplication)
modules(
singleCollectionModule,
moduleCoreCollection,
moduleCorePhotos,
moduleCoreNetwork,
moduleFeaturePhoto,
moduleFeatureSearchPhotos,
moduleFeatureUser,
moduleFeatureHome,
moduleCoreFavourite,
moduleFeatureFavourite
)
}
override val appApi: AppApi
get() = appComponent

override fun onCreate() {
appComponent.inject(this)
super.onCreate()
}
}
9 changes: 0 additions & 9 deletions app/src/main/java/st/slex/csplashscreen/di/AppModule.kt

This file was deleted.

24 changes: 24 additions & 0 deletions app/src/main/java/st/slex/csplashscreen/di/app/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package st.slex.csplashscreen.di.app

import android.content.Context
import dagger.BindsInstance
import dagger.Component
import st.slex.csplashscreen.SplashApplication
import st.slex.csplashscreen.core.core.AppApi
import javax.inject.Singleton

@Singleton
@Component
interface AppComponent : AppApi {

@Component.Builder
interface Builder {

@BindsInstance
fun context(context: Context): Builder

fun build(): AppComponent
}

fun inject(application: SplashApplication)
}
33 changes: 33 additions & 0 deletions app/src/main/java/st/slex/csplashscreen/di/main/MainComponent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package st.slex.csplashscreen.di.main

import androidx.lifecycle.ViewModelProvider
import dagger.Component
import st.slex.csplashscreen.core.core.AppApi
import st.slex.csplashscreen.core.ui.di.NavigationApi

@Component(
dependencies = [MainDependencies::class],
modules = [MainModule::class]
)
interface MainComponent {

@Component.Factory
interface Factory {
fun create(dependencies: MainDependencies): MainComponent
}

@Component(dependencies = [AppApi::class, NavigationApi::class])
interface MainDependenciesComponent : MainDependencies {

@Component.Factory
interface Factory {
fun create(
appApi: AppApi,
navigationApi: NavigationApi
): MainDependenciesComponent
}
}

val viewModelFactory: ViewModelProvider.Factory
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package st.slex.csplashscreen.di.main

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import st.slex.csplashscreen.core.core.AppApi
import st.slex.csplashscreen.core.core.appApi
import st.slex.csplashscreen.core.ui.base.daggerViewModel
import st.slex.csplashscreen.core.ui.di.NavigationApi
import st.slex.csplashscreen.ui.InitialAppViewModel

object MainComponentBuilder {

fun build(
appApi: AppApi,
navigationApi: NavigationApi
): MainComponent = DaggerMainComponent
.factory()
.create(
DaggerMainComponent_MainDependenciesComponent.factory()
.create(
appApi = appApi,
navigationApi = navigationApi
)
)
}

@Composable
fun setupMainComponent(navigationApi: NavigationApi): InitialAppViewModel {
val context = LocalContext.current
return daggerViewModel {
MainComponentBuilder
.build(
appApi = context.appApi,
navigationApi = navigationApi
)
.viewModelFactory
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package st.slex.csplashscreen.di.main

import st.slex.csplashscreen.core.ui.di.Navigator

interface MainDependencies {

val navigator: Navigator
}
22 changes: 22 additions & 0 deletions app/src/main/java/st/slex/csplashscreen/di/main/MainModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package st.slex.csplashscreen.di.main

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.stslex93.notes.core.ui.base.ViewModelFactory
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
import st.slex.csplashscreen.core.ui.di.ViewModelKey
import st.slex.csplashscreen.ui.InitialAppViewModel

@Module
interface MainModule {

@Binds
@IntoMap
@ViewModelKey(InitialAppViewModel::class)
fun bindsViewModel(impl: InitialAppViewModel): ViewModel

@Binds
fun bindsFactory(impl: ViewModelFactory): ViewModelProvider.Factory
}
8 changes: 4 additions & 4 deletions app/src/main/java/st/slex/csplashscreen/ui/InitialApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import org.koin.androidx.compose.koinViewModel
import st.slex.csplashscreen.core.navigation.AppDestination
import st.slex.csplashscreen.navigation.NavigationHost
import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.ui.components.NavigationHost
import st.slex.csplashscreen.ui.components.bottom_appbar.BottomAppBarResource
import st.slex.csplashscreen.ui.components.bottom_appbar.MainBottomAppBar

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun InitialApp(
navController: NavHostController,
onBottomAppBarClick: (NavigationScreen) -> Unit,
modifier: Modifier = Modifier,
) {
val systemUiController = rememberSystemUiController()
val isDarkTheme = isSystemInDarkTheme()
val viewModel = koinViewModel<InitialAppViewModel>()

var currentDestination by remember {
mutableStateOf<AppDestination?>(AppDestination.HOME)
Expand Down Expand Up @@ -65,7 +65,7 @@ fun InitialApp(
exit = slideOutVertically(tween(300)) { it }
) {
MainBottomAppBar(
onBottomAppBarClick = viewModel::navigate,
onBottomAppBarClick = onBottomAppBarClick,
currentDestination = currentDestination
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package st.slex.csplashscreen.ui

import androidx.lifecycle.ViewModel
import st.slex.csplashscreen.core.navigation.NavigationScreen
import st.slex.csplashscreen.core.navigation.navigator.Navigator
import st.slex.csplashscreen.core.ui.di.Navigator
import javax.inject.Inject

class InitialAppViewModel(
class InitialAppViewModel @Inject constructor(
private val navigator: Navigator
) : ViewModel() {

fun navigate(screen: NavigationScreen) {
navigator.navigate(screen)
navigator(screen)
}
}
41 changes: 22 additions & 19 deletions app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package st.slex.csplashscreen.ui
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.core.view.WindowCompat
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import org.koin.androidx.compose.getKoin
import st.slex.csplashscreen.core.navigation.di.moduleCoreNavigation
import st.slex.csplashscreen.core.navigation.di.NavigationComponentBuilder
import st.slex.csplashscreen.core.ui.di.MainUiApi
import st.slex.csplashscreen.core.ui.di.NavigationApi
import st.slex.csplashscreen.core.ui.theme.AppTheme
import st.slex.csplashscreen.di.main.setupMainComponent

class MainActivity : ComponentActivity() {
class MainActivity : ComponentActivity(), MainUiApi {

private var _navigationApi: NavigationApi? = null
override val navigationApi: NavigationApi
get() = requireNotNull(_navigationApi)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -20,21 +25,19 @@ class MainActivity : ComponentActivity() {
setContent {
AppTheme {
val navController = rememberNavController()
SetupComposeDependencies(navController)
InitialApp(navController)
_navigationApi = remember(navController) {
NavigationComponentBuilder.build(navController)
}

val viewModel = setupMainComponent(navigationApi)

InitialApp(
navController = navController,
onBottomAppBarClick = remember {
{ viewModel.navigate(it) }
}
)
}
}
}

@Composable
private fun SetupComposeDependencies(
navController: NavHostController
) {
getKoin().loadModules(
listOf(
moduleCoreNavigation(navController),
st.slex.csplashscreen.di.appModule
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package st.slex.csplashscreen.navigation
package st.slex.csplashscreen.ui.components

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand Down
53 changes: 0 additions & 53 deletions app/src/test/java/st/slex/csplashscreen/DiKoinModuleTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
with(pluginManager) {
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
apply("com.google.devtools.ksp")
}

extensions.configure<ApplicationExtension> {
Expand Down
Loading

0 comments on commit 48c1272

Please sign in to comment.