Skip to content

0.6.2

Compare
Choose a tag to compare
@github-actions github-actions released this 05 Feb 11:48
· 213 commits to master since this release

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 of kmp-viewmodel, kmp-viewmodel-compose and Koin,
    helps us to retrieve ViewModel 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 access SavedStateHandle 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 rememberViewModelFactorys to remember the ViewModelFactorys in @Composable functions.
    They accept builder: @DisallowComposableCalls CreationExtras.() -> VMs.
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 accepts factory: @DisallowComposableCalls CreationExtras.() -> VM
    (Previously, it only accepts factory: ViewModelFactory<VM>).

    class MyViewModel(savedStateHandle: SavedStateHandle): ViewModel()
    
    @Composable
    fun MyScreen(
      viewModel: MyViewModel = kmpViewModel {
        MyViewModel(savedStateHandle = createSavedStateHandle())
      }
    ) {
      // ...
    }

The above kmpViewModel uses rememberViewModelFactory internally.
Use rememberViewModelFactory { ... } and kmpViewModel(factory = factory)
is the same as using kmpViewModel { ... }.