Skip to content

Releases: hoc081098/kmp-viewmodel

0.7.1

02 Mar 09:21
Compare
Choose a tag to compare

kmp-viewmodel-compose

Added kmp-viewmodel-koject and kmp-viewmodel-koject-compose artifacts

  • For more information check out the docs/0.x/viewmodel-koject-compose

  • The Koject dependency are used in kmp-viewmodel-koject-compose: com.moriatsushi.koject:koject-core:1.3.0.

  • New The kmp-viewmodel-koject artifact provides the integration of kmp-viewmodel, kmp-viewmodel-compose and Koject,
    helps us to retrieve ViewModel from the Koject DI container without manually dependency injection.

    @Provides
    @Singleton
    class MyRepository
    
    @Provides
    @ViewModelComponent // <-- To inject SavedStateHandle
    class MyViewModel(
      val myRepository: MyRepository,
      val savedStateHandle: SavedStateHandle,
    ) : ViewModel() {
      // ...
    }
    
    @Composable
    fun MyScreen(
      viewModel: MyViewModel = kojectKmpViewModel(),
    ) {
      // ...
    }

Example, docs and tests

0.7.0

17 Feb 06:35
Compare
Choose a tag to compare

Update dependencies

kmp-viewmodel and kmp-viewmodel-savedstate

  • New: Add support for Kotlin/Wasm (wasmJs target) 🎉.
  • The behavior of ViewModel.addCloseable(Closeable) on non-Android targets has been changed to be consistent with Android target.
    ViewModel's addCloseable() now immediately closes the Closeable if the ViewModel has been cleared.
    This behavior is the same across all targets ✅.

kmp-viewmodel-koin

  • Fixed: koinViewModelFactory: CreationExtras passed to ViewModelFactory.create will now be
    passed to the constructor of the ViewModel if it's requested.

    class MyViewModel(val extras: CreationExtras) : ViewModel()
    val myModule: Module = module {
      factoryOf(::MyViewModel)
    }
    
    val factory = koinViewModelFactory<MyViewModel>(
      scope = KoinPlatformTools.defaultContext().get().scopeRegistry.rootScope,
    )
    val extras = buildCreationExtras { /* ... */ }
    
    val viewModel: MyViewModel = factory.create(extras)
    viewModel.extras === extras // true <--- `viewModel.extras` is the same as `extras` passed to `factory.create(extras)`

Example, docs and tests

  • Add more tests to kmp-viewmodel-compose (android & jvm), kmp-viewmodel-koin (common),
    and kmp-viewmodel-koin-compose (common & jvm).

0.6.2

05 Feb 11:48
Compare
Choose a tag to compare

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 { ... }.

0.6.1

10 Dec 15:16
Compare
Choose a tag to compare

viewmodel

  • On non-Android targets: ViewModel.viewModelScope does not use Dispatchers.Default as a fallback.
    That means the CoroutineDispatcher of ViewModel.viewModelScope is Dispatchers.Main.immediate or Dispatchers.Main.

Example, docs

  • Refactor example code.
  • Add NOTE about the kotlinx-coroutines dependency when targeting Desktop (aka. jvm).

0.6.0

08 Dec 14:08
Compare
Choose a tag to compare

Update dependencies

Removed

  • Remove now-unsupported targets: iosArm32, watchosX86.

viewmodel

  • MutableCreationExtras has been renamed to MutableCreationExtrasBuilder,
    and it does not inherit from CreationExtras anymore.
    Because of this, a new method
    MutableCreationExtrasBuilder.asCreationExtras() has been introduced can be used to convert
    a builder back to CreationExtras as needed.

    NOTE: buildCreationExtras and CreationExtras.edit methods are still the same as before.

    // Old version (0.5.0)
    val creationExtras: CreationExtras = MutableCreationExtras().apply {
      // ...
    }
    
    // New version (0.6.0): `MutableCreationExtras` does not inherit from `CreationExtras` anymore.
    val creationExtras: CreationExtras = MutableCreationExtrasBuilder().apply {
      // ...
    }.asCreationExtras() // <--- asCreationExtras: convert a builder back to `CreationExtras` as needed.

    More details: with Kotlin 1.9.20, an expect with default arguments are no longer
    permitted when an actual is a typealias
    (see KT-57614),
    we cannot
    use actual typealias MutableCreationExtras = androidx.lifecycle.viewmodel.MutableCreationExtras.
    So we have to use wrapper class instead.

  • Update the docs of ViewModel.viewModelScope to clarify that the scope is thread-safe
    on both Android and non-Android targets.

  • On non-Android targets

    • ViewModel.clear() method has been refactored to improve the performance.
    • Any Exception thrown from Closeable.close() will be re-thrown as RuntimeException.

0.5.0

27 Sep 09:56
Compare
Choose a tag to compare

Update dependencies

  • Kotlin 1.9.0.
  • AndroidX Lifecycle 2.6.1.
  • KotlinX Coroutines 1.7.3.
  • Android Gradle Plugin 8.1.0.

viewmodel

  • Add ViewModelStore and ViewModelStoreOwner.
  • Add ViewModelFactory and VIEW_MODEL_KEY.
  • Add CreationExtras and CreationExtrasKey.
  • Add buildCreationExtras and CreationExtras.edit.
  • Add ViewModel.isCleared() method to check if the ViewModel is cleared, only available on
    non-Android targets
    .
  • Add MainThread (moved from viewmodel-savedstate module).

viewmodel-savedstate

  • Remove MainThread (moved to viewmodel module).
  • Add SavedStateHandleFactory interface.
  • Add SAVED_STATE_HANDLE_FACTORY_KEY and CreationExtras.createSavedStateHandle().

viewmodel-compose

  • A new module allows to access ViewModels in Jetpack Compose Multiplatform.

    • kmpViewModel to retrieve ViewModels in @composable functions.
    • LocalSavedStateHandleFactory and SavedStateHandleFactoryProvider to
      get/provide SavedStateHandleFactory in @composable functions.
      It allows integration with any navigation library.
    • LocalViewModelStoreOwner and ViewModelStoreOwnerProvider to
      get/provide ViewModelStoreOwner in @composable functions.
      It allows integration with any navigation library.
    • defaultPlatformCreationExtras and defaultPlatformViewModelStoreOwner
      to get the default CreationExtras and ViewModelStoreOwner,
      which depends on the platform.
  • Dependencies: Compose Multiplatform 1.5.0.

  • Docs: 0.x Viewmodel-Compose docs.

Example, docs and tests

0.4.0

07 Apr 06:28
Compare
Choose a tag to compare

Changed

Update dependencies

  • Kotlin 1.8.10.
  • Target Java 11.
  • Touchlab Stately 1.2.5.
  • AndroidX Lifecycle 2.6.0.
  • Android Gradle Plugin 7.4.2.

Flow wrappers

  • Add NonNullStateFlowWrapper and NullableFlowWrapper to common source set.

  • Move all Flow wrappers to common source set.
    Previously, they were only available for Darwin targets (iOS, macOS, tvOS, watchOS).

  • Add Flow.wrap() extension methods to wrap Flows sources:

    • Flow<T: Any>.wrap(): NonNullFlowWrapper<T>.
    • Flow<T>.wrap(): NullableFlowWrapper<T>.
    • StateFlow<T: Any>.wrap(): NonNullStateFlowWrapper<T>.
    • StateFlow<T>.wrap(): NullableStateFlowWrapper<T>.

    In common code, you can use these methods to wrap Flow sources and use them in Swift code easily.

    // Kotlin code
    data class State(...)
    
    class SharedViewModel : ViewModel() {
      private val _state = MutableStateFlow(State(...))
      val stateFlow: NonNullStateFlowWrapper<State> = _state.wrap()
    }
    // Swift code
    @MainActor class IosViewModel: ObservableObject {
      private let vm: SharedViewModel
    
      @Published private(set) var state: State
    
      init(viewModel: SharedViewModel) {
        vm = viewModel
    
        state = vm.stateFlow.value       //  <--- Use `value` property with type safety (do not need to cast).
        vm.stateFlow.subscribe(          //  <--- Use `subscribe(scope:onValue:)` method directly.
          scope: vm.viewModelScope,
          onValue: { [weak self] in self?.state = $0 }
        )
      }
    
      deinit { vm.clear() }
    }

Example, docs and tests

  • Refactor example code.
  • Add more docs: 0.x docs.
  • Add more tests.

0.3.0

18 Mar 05:51
Compare
Choose a tag to compare

Added

  • Add NonNullFlowWrapper and NullableFlowWrapper, that are wrappers for Flows
    that provides a more convenient API for subscribing to the Flows on Darwin targets (iOS, macOS, tvOS, watchOS)
    // Kotlin code
    val flow: StateFlow<Int>
    // Swift code
    NonNullFlowWrapper<KotlinInt>(flow: flow).subscribe(
      scope: scope,
      onValue: { print("Received ", $0) }
    )

Changed

  • Add more example, refactor example code.
  • Add more docs: 0.x docs.
  • Add more tests.
  • Gradle 8.0.2.
  • Dokka 1.8.10.

0.2.0

05 Mar 11:50
Compare
Choose a tag to compare

Added

Changed

  • Add more example, refactor example code.
  • Add more docs: 0.x docs.

0.1.0

18 Feb 19:48
Compare
Choose a tag to compare

Changed

  • Make ViewModel.viewModelScope public.

Added

  • Add an ViewModel.addCloseable API and a new constructor overload constructor(vararg closeables: Closeable),
    that allow you to add one or more Closeable objects to the ViewModel
    that will be closed when the ViewModel is cleared without requiring any manual work in onCleared().