evant
05/18/2023, 6:25 PMabstract val foo: Foo
StavFX
05/18/2023, 9:33 PMStavFX
05/18/2023, 9:37 PMfun foo(dep)
isn't exposed to consumers.
Right now I have something like this
//commonMain
interface MultiplatformComponent {
val foo: Foo
}
//androidApp
@Component
interface AndroidAppComponent : MultiplatformComponent, AndroidFooModule, SomeOtherModule
// I'm coming from Dagger, so I didn't really have a better name for these
interface AndroidFooModule {
fun provideFoo(dep...): Foo {...}
}
Is there a better way to modularize how dependencies are satisfied?Kamil K
06/26/2023, 1:04 PMSaveStateHandler
?
Following docs https://github.com/evant/kotlin-inject/blob/main/docs/android.md#compose
and https://developer.android.com/topic/libraries/architecture/viewmodel/viewmodel-factories#jetpack-compose
Do I need to update the higher order function viewModel
implementation to use AbstractSavedStateViewModelFactory
and downgrade dependency to older than 2.5.0
or this also would work
@Composable
inline fun <reified VM : ViewModel> injectViewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
crossinline factory: (SavedStateHandle) -> VM,
): VM = viewModel(
viewModelStoreOwner = viewModelStoreOwner,
key = key,
factory = viewModelFactory {
initializer {
val savedStateHandle = createSavedStateHandle()
factory(savedStateHandle)
}
}
)
And second questions:
In the docs there is example
@Inject
@Composable
fun Home(homeViewModel: () -> HomeViewModel) {
val viewModel = viewModel { homeViewModel() }
...
}
this factory method homeViewModel: () -> HomeViewModel
is this method generated and provided by plugin? From samples can’t find where is this method coming from.
Thanks!Kamil K
07/03/2023, 3:14 PMSaveStateHandler
at this time?adte
07/23/2023, 1:04 PMadte
07/23/2023, 1:49 PMtankistorep
08/10/2023, 10:27 AMSavedStateHandle
- say I have a ViewModel that needs a class that needs `SavedStateHandle`:
@Inject
class MyViewModel(private val test: SomeTestClass): ViewModel()
@Inject
class SomeTestClass(private val savedStateHandle: SavedStateHandle) {...}
And I have my own view model factory:
@Inject
class ViewModelFactory(
val myViewModel: (SavedStateHandle) -> MyViewModel
): ViewModelProvider.Factory {
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val savedStateHandle = extras.createSavedStateHandle()
return when (modelClass) {
MyViewModel::class.java -> myViewModel(savedStateHandle) as T
...
}
}
}
Is there a way to get the SavedStateHandle
to be automatically passed down to SomeTestClass
without me needing to create an instance of the class in the factory?Colton Idle
08/19/2023, 11:07 PMid("com.google.devtools.ksp") version "1.9.0-1.0.13"
then in my shared i added to the plugins block
id("com.google.devtools.ksp")
then I added kotlin-inject like this:Sudhanshu Singh
09/06/2023, 7:56 AM@HiltViewModel
) and Activity/Fragment (@AndroidEntryPoint
) for android module and us kotlin-inject for shared modules.Kyle
09/29/2023, 7:19 PMJosh Feinberg
09/30/2023, 5:20 PMInterfaceA
and InterfaceB
. i have one object that needs a set of InterfaceA
implementations and another that needs an implementation of InterfaceB
. not sure the best way to handle this (code in 🧵 in case this makes no sense)Pablichjenkov
10/09/2023, 6:20 AMevant
10/09/2023, 7:04 AMPablichjenkov
10/09/2023, 7:05 AMDmitry
10/10/2023, 2:30 AMSudhanshu Singh
10/10/2023, 5:03 PM@Module
@InstallIn(SingletonComponent::class)
class RepoModule {
@Provides
@Singleton
fun provideRepo(
remoteDataSource: UserRemoteDataSource,
localDataSource: UserLocalDataSource
): UserRepo =
UserRepoImpl(
remoteDataSource,
localDataSource
)
}
brandonmcansh
10/24/2023, 3:18 PMfranztesca
10/24/2023, 9:50 PM// in module C
interface C
@Inject
class CImpl
// in module B
interface B
@Inject
class BImpl(private val c: C)
// In module A
interface A
@Inject
class AImpl(private val b: B)
@Component
abstract class AppComponent {
val AImpl.bind: A
@Provides get() = this
}
Is KI able to automatically link the modules, assuming that I add a gradle dependency from A->C, or not?eygraber
02/02/2024, 8:54 PMeygraber
02/13/2024, 5:12 AMinterface BaseFooComponent {
@Provides @AppSingleton fun provideFoo(): Foo
}
abstract class FooComponent : BaseFooComponent {
override fun provideFoo(): Foo = MyFoo()
}
Foo
gets provided, but it is not scoped.eygraber
02/16/2024, 7:34 PMexpect
component in the middle of it, and the actual
implementations provide platform specific bindings. That requires that all downstream components be generated for all platforms, even if they themselves are common, because common generated code wouldn't be able to see these platform specific bindings.
Pre Kotlin 2.0 this isn't a problem in some cases because common source sets can (incorrectly) reference generated platform code. However once Kotlin 2.0 is released this won't work anymore for any of the cases.
To solve the issue, all downstream components (which is ~99% of the components in my project) will need to specify expect/actual
declarations for the create
functions for each platform (this project supports 5 platform targets) in each module (and I typically have 1 downstream component per module).
That's kind of painful, and I'm wondering if there's a way for kotlin-inject to detect (or be told about) this situation, and generate the expect/actual
declarations for create
.
I'm not sure if it could be done in the same processor, or if it would need a different processor, or if it's even possible with KSP at all (I think it would need something like Support source set-specific code generation in Multiplatform projects). In any case, I wanted to gather some thoughts and suggestions on this.evant
02/16/2024, 7:38 PMeygraber
02/16/2024, 7:40 PMcreate
function as actual
and then the user just needs to specify a common expect create
function.evant
02/16/2024, 7:43 PMevant
02/16/2024, 7:44 PMevant
02/16/2024, 7:45 PMeygraber
02/16/2024, 7:47 PMeygraber
02/16/2024, 8:55 PMMarco Pierucci
03/03/2024, 9:30 PM