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

Support falling back to factory on platforms without AndroidX ViewModel #194

Open
jadar opened this issue Dec 4, 2024 · 0 comments
Open

Comments

@jadar
Copy link

jadar commented Dec 4, 2024

Is your feature request related to a problem? Please describe.
I specify my ViewModel modules differently on iOS and Android. On Android, I want the viewModel<> specifier. However, on iOS (I am using SwiftUI) I want this to be a normal factory<> specifier.

Describe the solution you'd like
I'd like to be able to make the @ViewModel annotation fall back to @Factory on platforms that doesn't support viewModel natively.

BaseViewModel.kt

expect abstract class BaseViewModel()

BaseViewModel.ios.kt

actual abstract class BaseViewModel()

BaseViewModel.ios.kt

import androidx.lifecycle.ViewModel
actual abstract class BaseViewModel actual constructor(): ViewModel()

MyViewModel.kt

@FactoryViewModel
class MyViewModel: BaseViewModel()
@Module
@ComponentScan
class MyModule

Describe alternatives you've considered
I can do this with an expect/actual module with pure Koin.

ViewModelModule.ios.kt

actual object MyModule {
  val module = module {
    factory<MyViewModel> { MyViewModel() }
  }
}

ViewModelModule.android.kt

actual object MyModule {
  val module = module {
    viewModel<MyViewModel> { MyViewModel() }
  }
}

Target Koin project
koin-annotations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant