0.6.2
Update dependencies
Added kmp-viewmodel-koin
and kmp-viewmodel-koin-compose
artifacts
-
For more information check out the docs/0.x/viewmodel-koin-compose
-
The Koin dependencies are used in
kmp-viewmodel-koin-compose
:io.insert-koin:koin-core:3.5.3
.io.insert-koin:koin-compose:1.1.2
.
-
New The
kmp-viewmodel-koin
artifact provides the integration ofkmp-viewmodel
,kmp-viewmodel-compose
andKoin
,
helps us to retrieveViewModel
from the Koin DI container without manually dependency injection.class MyRepository class MyViewModel( val myRepository: MyRepository, val savedStateHandle: SavedStateHandle, val id: Int, ) : ViewModel() { // ... } val myModule: Module = module { factoryOf(::MyRepository) factoryOf(::MyViewModel) } @Composable fun MyScreen( id: Int, viewModel: MyViewModel = koinKmpViewModel( key = "MyViewModel-$id", parameters = { parametersOf(id) } ) ) { // ... }
Added type-safe API for SavedStateHandle
-
For more information check out the docs/0.x/viewmodel-savedstate-safe
-
New The
kmp-viewmodel-savedstate
artifact provides the type-safe API
that allows you to accessSavedStateHandle
in a type-safe way.private val searchTermKey: NonNullSavedStateHandleKey<String> = NonNullSavedStateHandleKey.string( key = "search_term", defaultValue = "" ) // Use `SavedStateHandle.safe` extension function to access `SavedStateHandle` in a type-safe way. savedStateHandle.safe { it[searchTermKey] = searchTerm } savedStateHandle.safe { it.getStateFlow(searchTermKey) } // Or use `SavedStateHandle.safe` extension property to access `SavedStateHandle` in a type-safe way. savedStateHandle.safe[searchTermKey] = searchTerm savedStateHandle.safe.getStateFlow(searchTermKey)
kmp-viewmodel-compose
artifact
- New Add
rememberViewModelFactory
s to remember theViewModelFactory
s in@Composable
functions.
They acceptbuilder: @DisallowComposableCalls CreationExtras.() -> VM
s.
class MyViewModel(savedStateHandle: SavedStateHandle): ViewModel()
@Composable
fun MyScreen() {
val factory: ViewModelFactory<MyViewModel> = rememberViewModelFactory {
MyViewModel(savedStateHandle = createSavedStateHandle())
}
val viewModel: MyViewModel = kmpViewModel(factory = factory)
// ...
}
-
New Add a new
kmpViewModel
overload that acceptsfactory: @DisallowComposableCalls CreationExtras.() -> VM
(Previously, it only acceptsfactory: ViewModelFactory<VM>
).class MyViewModel(savedStateHandle: SavedStateHandle): ViewModel() @Composable fun MyScreen( viewModel: MyViewModel = kmpViewModel { MyViewModel(savedStateHandle = createSavedStateHandle()) } ) { // ... }
The above
kmpViewModel
usesrememberViewModelFactory
internally.
UserememberViewModelFactory { ... }
andkmpViewModel(factory = factory)
is the same as usingkmpViewModel { ... }
.