Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ A library (previously known as KMM-ViewModel) that allows you to use AndroidX/Ko
## Compatibility

You can use this library in any KMP project,
but not all targets support AndroidX and/or SwiftUI interop:

| Target | Supported | AndroidX | SwiftUI |
|------------|:-----------:|:--------:|:-------:|
| Android | | ✅ | - |
| JVM | | ✅ | - |
| iOS | | ✅ | ✅ |
| macOS | | ✅ | ✅ |
| tvOS | | - | ✅ |
| watchOS | | - | ✅ |
| linuxX64 | | ✅ | - |
| linuxArm64 | | - | - |
| mingwX64 | | - | - |
| JS | | - | - |
| Wasm | | - | - |
but not all targets support SwiftUI interop:

| Target | Supported | AndroidX | SwiftUI |
|------------|:---------:|:--------:|:-------:|
| Android | ✅ | ✅ | - |
| JVM | ✅ | ✅ | - |
| iOS | ✅ | ✅ | ✅ |
| macOS | ✅ | ✅ | ✅ |
| tvOS | ✅ | | ✅ |
| watchOS | ✅ | | ✅ |
| linuxX64 | ✅ | ✅ | - |
| linuxArm64 | ✅ | | - |
| mingwX64 | ✅ | | - |
| JS | ✅ | | - |
| Wasm | ✅ | | - |

The latest version of the library uses Kotlin version `2.4.10`.
Compatibility versions for older and/or preview Kotlin versions are also available:
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[versions]
kotlin = "2.4.10"
kotlinx-coroutines = "1.10.1"
android = "8.5.2"
androidx-lifecycle = "2.8.7"
android = "8.6.0"
androidx-lifecycle = "2.9.2"
atomicfu = "0.32.1"

# Sample versions
Expand Down
22 changes: 1 addition & 21 deletions kmp-observableviewmodel-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@ kotlin {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("androidx") {
withAndroidTarget()
group("ios")
withJvm()
withLinuxX64()
group("macos")
}
group("nonAndroidx") {
withJs()
withLinuxArm64()
group("mingw")
group("tvos")
withWasmJs()
group("watchos")
}
group("nonApple") {
withAndroidTarget()
withJvm()
Expand Down Expand Up @@ -90,19 +75,14 @@ kotlin {
commonMain {
dependencies {
api(libs.kotlinx.coroutines.core)
api(libs.androidx.lifecycle.viewmodel)
}
}
commonTest {
dependencies {
implementation(libs.kotlin.test)
}
}

val androidxMain by getting {
dependencies {
api(libs.androidx.lifecycle.viewmodel)
}
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,53 +1,70 @@
package com.rickclephas.kmp.observableviewmodel

import androidx.lifecycle.ViewModel as AndroidXViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.viewmodel.CreationExtras
import kotlinx.coroutines.CoroutineScope
import kotlin.reflect.KClass

/**
* A Kotlin Multiplatform ViewModel.
*/
public expect abstract class ViewModel {
public abstract class ViewModel: AndroidXViewModel {

/**
* The [ViewModelScope] containing the [CoroutineScope] of this ViewModel.
*/
public val viewModelScope: ViewModelScope

public constructor()
public constructor(): this(DefaultCoroutineScope())

public constructor(coroutineScope: CoroutineScope)
public constructor(coroutineScope: CoroutineScope): super(coroutineScope) {
viewModelScope = ViewModelScope(coroutineScope)
}

public constructor(vararg closeables: AutoCloseable)
public constructor(vararg closeables: AutoCloseable): this(DefaultCoroutineScope(), *closeables)

public constructor(coroutineScope: CoroutineScope, vararg closeables: AutoCloseable)
public constructor(
coroutineScope: CoroutineScope,
vararg closeables: AutoCloseable
): super(coroutineScope, *closeables) {
viewModelScope = ViewModelScope(coroutineScope)
}

/**
* Called when this ViewModel is no longer used and will be destroyed.
* Internal KMP-ObservableViewModel function used by the Swift implementation to clear the ViewModel.
* Warning: you should NOT call this yourself!
*/
public open fun onCleared()
@InternalKMPObservableViewModelApi
public fun clear() {
// We can't directly call the internal clear function from AndroidX.
// To call it indirectly we use the public Store and Provider APIs instead.
val store = ViewModelStore()
ViewModelProvider.create(
store = store,
factory = object : ViewModelProvider.Factory {
override fun <T : AndroidXViewModel> create(modelClass: KClass<T>, extras: CreationExtras): T {
@Suppress("UNCHECKED_CAST")
return this@ViewModel as T
}
}
)[ViewModel::class]
store.clear()
}
}

/**
* Adds an [AutoCloseable] resource with an associated [key] to this [ViewModel].
* The resource will be closed right before the [onCleared][ViewModel.onCleared] method is called.
*
* If the [key] already has a resource associated with it, the old resource will be replaced and closed immediately.
*
* If [onCleared][ViewModel.onCleared] has already been called,
* the provided resource will not be added and will be closed immediately.
*/
public expect fun ViewModel.addCloseable(key: String, closeable: AutoCloseable)
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE", "DeprecatedCallableAddReplaceWith")
@Deprecated("Use addCloseable on AndroidX ViewModel directly")
public inline fun ViewModel.addCloseable(key: String, closeable: AutoCloseable): Unit =
addCloseable(key, closeable)

/**
* Adds an [AutoCloseable] resource to this [ViewModel].
* The resource will be closed right before the [onCleared][ViewModel.onCleared] method is called.
*
* If [onCleared][ViewModel.onCleared] has already been called,
* the provided resource will not be added and will be closed immediately.
*/
public expect fun ViewModel.addCloseable(closeable: AutoCloseable)
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE", "DeprecatedCallableAddReplaceWith")
@Deprecated("Use addCloseable on AndroidX ViewModel directly")
public inline fun ViewModel.addCloseable(closeable: AutoCloseable): Unit =
addCloseable(closeable)

/**
* Returns the [AutoCloseable] resource associated to the given [key],
* or `null` if such a [key] is not present in this [ViewModel].
*/
public expect fun <T : AutoCloseable> ViewModel.getCloseable(key: String): T?
@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "NOTHING_TO_INLINE", "DeprecatedCallableAddReplaceWith")
@Deprecated("Use getCloseable on AndroidX ViewModel directly")
public inline fun <T : AutoCloseable> ViewModel.getCloseable(key: String): T? =
getCloseable(key)

This file was deleted.

Loading