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

Scope Injected ScreenModels in DI graph properly #65

Merged
merged 1 commit into from
Nov 17, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.DealsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@DealsFeatureScope
abstract class DealsFeatureComponent(
@Component val parent: HostActivityComponent
) : SharedDealsFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.DiscoverFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@DiscoverFeatureScope
abstract class DiscoverFeatureComponent(
@Component val parent: HostActivityComponent
) : SharedDiscoverFeatureComponent
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.mr3y.ludi.shared.di

import android.app.Activity
import com.mr3y.ludi.shared.ui.navigation.PreferencesType
import com.mr3y.ludi.shared.ui.presenter.EditPreferencesViewModel
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides

@Component
abstract class HostActivityComponent(
@get:Provides val activity: Activity,
@Component val parent: AndroidApplicationComponent
) : ScreenModelsComponent
) {
abstract val editPreferencesViewModelFactory: (PreferencesType) -> EditPreferencesViewModel
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.NewsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@NewsFeatureScope
abstract class NewsFeatureComponent(
@Component val parent: HostActivityComponent
) : SharedNewsFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.OnboardingFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@OnboardingFeatureScope
abstract class OnboardingFeatureComponent(
@Component val parent: HostActivityComponent
) : SharedOnboardingFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.SettingsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@SettingsFeatureScope
abstract class SettingsFeatureComponent(
@Component val parent: HostActivityComponent
) : SharedSettingsFeatureComponent
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import com.mr3y.ludi.shared.ui.navigation.PreferencesType
import com.mr3y.ludi.shared.ui.presenter.DealsViewModel
import com.mr3y.ludi.shared.ui.presenter.DiscoverViewModel
import com.mr3y.ludi.shared.ui.presenter.EditPreferencesViewModel
import com.mr3y.ludi.shared.ui.presenter.NewsViewModel
import com.mr3y.ludi.shared.ui.presenter.OnBoardingViewModel
import com.mr3y.ludi.shared.ui.presenter.SettingsViewModel

/**
* Provide a [ScreenModel] getting from kotlin-inject graph.
Expand All @@ -19,14 +24,29 @@ actual inline fun <reified T : ScreenModel> Screen.getScreenModel(
): T {
val hostActivityComponent = (LocalContext.current as HostActivityComponentOwner).hostActivityComponent
return rememberScreenModel(tag) {
val screenModels = hostActivityComponent.screenModels
val model = screenModels[T::class]?.invoke()
?: error(
"${T::class} not found in kotlin-inject graph.\nPlease, check if you have a Multibinding " +
"declaration to your ScreenModel using @IntoMap with " +
"key ${T::class.qualifiedName}::class)"
)
model as T
when {
T::class.java.isAssignableFrom(OnBoardingViewModel::class.java) -> {
OnboardingFeatureComponent::class.create(hostActivityComponent).bind as T
}
T::class.java.isAssignableFrom(DiscoverViewModel::class.java) -> {
DiscoverFeatureComponent::class.create(hostActivityComponent).bind as T
}
T::class.java.isAssignableFrom(NewsViewModel::class.java) -> {
NewsFeatureComponent::class.create(hostActivityComponent).bind as T
}
T::class.java.isAssignableFrom(DealsViewModel::class.java) -> {
DealsFeatureComponent::class.create(hostActivityComponent).bind as T
}
T::class.java.isAssignableFrom(SettingsViewModel::class.java) -> {
SettingsFeatureComponent::class.create(hostActivityComponent).bind as T
}
else -> {
error(
"${T::class} not found in kotlin-inject graph.\nPlease, check if you have declared a Feature Component " +
"for your ScreenModel & its required dependencies"
)
}
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mr3y.ludi.shared.di

import cafe.adriel.voyager.core.model.ScreenModel
import com.mr3y.ludi.shared.di.annotations.DealsFeatureScope
import com.mr3y.ludi.shared.ui.presenter.DealsViewModel
import me.tatarka.inject.annotations.Provides

interface SharedDealsFeatureComponent {

val bind: ScreenModel

@Provides
@DealsFeatureScope
fun DealsViewModel.provideDealsViewModelInstance(): ScreenModel = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mr3y.ludi.shared.di

import cafe.adriel.voyager.core.model.ScreenModel
import com.mr3y.ludi.shared.di.annotations.DiscoverFeatureScope
import com.mr3y.ludi.shared.ui.presenter.DefaultDiscoverPagingFactory
import com.mr3y.ludi.shared.ui.presenter.DiscoverPagingFactory
import com.mr3y.ludi.shared.ui.presenter.DiscoverViewModel
import me.tatarka.inject.annotations.Provides

interface SharedDiscoverFeatureComponent {

val bind: ScreenModel

@Provides
@DiscoverFeatureScope
fun DiscoverViewModel.provideDiscoverViewModelInstance(): ScreenModel = this

@Provides
@DiscoverFeatureScope
fun DefaultDiscoverPagingFactory.bind(): DiscoverPagingFactory = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mr3y.ludi.shared.di

import cafe.adriel.voyager.core.model.ScreenModel
import com.mr3y.ludi.shared.di.annotations.NewsFeatureScope
import com.mr3y.ludi.shared.ui.presenter.NewsViewModel
import me.tatarka.inject.annotations.Provides

interface SharedNewsFeatureComponent {

val bind: ScreenModel

@Provides
@NewsFeatureScope
fun NewsViewModel.provideNewsViewModelInstance(): ScreenModel = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mr3y.ludi.shared.di

import cafe.adriel.voyager.core.model.ScreenModel
import com.mr3y.ludi.shared.di.annotations.OnboardingFeatureScope
import com.mr3y.ludi.shared.ui.presenter.OnBoardingViewModel
import me.tatarka.inject.annotations.Provides

interface SharedOnboardingFeatureComponent {

val bind: ScreenModel

@Provides
@OnboardingFeatureScope
fun OnBoardingViewModel.provideOnboardingViewModelInstance(): ScreenModel = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mr3y.ludi.shared.di

import cafe.adriel.voyager.core.model.ScreenModel
import com.mr3y.ludi.shared.di.annotations.SettingsFeatureScope
import com.mr3y.ludi.shared.ui.presenter.SettingsViewModel
import me.tatarka.inject.annotations.Provides

interface SharedSettingsFeatureComponent {

val bind: ScreenModel

@Provides
@SettingsFeatureScope
fun SettingsViewModel.provideSettingsViewModelInstance(): ScreenModel = this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mr3y.ludi.shared.di.annotations

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class DealsFeatureScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mr3y.ludi.shared.di.annotations

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class DiscoverFeatureScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mr3y.ludi.shared.di.annotations

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class NewsFeatureScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mr3y.ludi.shared.di.annotations

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class OnboardingFeatureScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mr3y.ludi.shared.di.annotations

import me.tatarka.inject.annotations.Scope

@Scope
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER)
annotation class SettingsFeatureScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.DealsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@DealsFeatureScope
abstract class DealsFeatureComponent(
@Component val parent: HostWindowComponent
) : SharedDealsFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.DiscoverFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@DiscoverFeatureScope
abstract class DiscoverFeatureComponent(
@Component val parent: HostWindowComponent
) : SharedDiscoverFeatureComponent
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package com.mr3y.ludi.shared.di

import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.awt.ComposeWindow
import com.mr3y.ludi.shared.ui.navigation.PreferencesType
import com.mr3y.ludi.shared.ui.presenter.EditPreferencesViewModel
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides

@Component
abstract class HostWindowComponent(
@get:Provides val window: ComposeWindow,
@Component val parent: DesktopApplicationComponent
) : ScreenModelsComponent
) {
abstract val editPreferencesViewModelFactory: (PreferencesType) -> EditPreferencesViewModel
}

val LocalHostWindowComponent = staticCompositionLocalOf<HostWindowComponent> { error("HostWindowComponent isn't provided!") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.NewsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@NewsFeatureScope
abstract class NewsFeatureComponent(
@Component val parent: HostWindowComponent
) : SharedNewsFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.OnboardingFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@OnboardingFeatureScope
abstract class OnboardingFeatureComponent(
@Component val parent: HostWindowComponent
) : SharedOnboardingFeatureComponent
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mr3y.ludi.shared.di

import com.mr3y.ludi.shared.di.annotations.SettingsFeatureScope
import me.tatarka.inject.annotations.Component

@Component
@SettingsFeatureScope
abstract class SettingsFeatureComponent(
@Component val parent: HostWindowComponent
) : SharedSettingsFeatureComponent
Loading