Skip to content

Commit 4ce88b9

Browse files
committed
ViewModel Support
1 parent 59440e3 commit 4ce88b9

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

compose/ui/ui/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
217217
ohosArm64Main.dependsOn(nativeMain)
218218
ohosArm64Main {
219219
dependencies {
220+
api "androidx.lifecycle:lifecycle-viewmodel:2.8.0-KBA-001"
220221
api project(":compose:ui:ui-arkui")
221222
}
222223
}

compose/ui/ui/src/ohosArm64Main/kotlin/androidx/compose/ui/arkui/ArkUIViewController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import androidx.compose.ui.unit.LayoutDirection
5353
import androidx.lifecycle.Lifecycle
5454
import androidx.lifecycle.LifecycleOwner
5555
import androidx.lifecycle.LifecycleRegistry
56+
import androidx.lifecycle.ViewModelStoreOwner
5657
import kotlin.coroutines.CoroutineContext
5758
import kotlinx.cinterop.COpaquePointer
5859
import kotlinx.cinterop.StableRef
@@ -70,7 +71,7 @@ import platform.ohos.napi_value
7071
* @author gavinbaoliu
7172
* @since 2024/5/9
7273
*/
73-
sealed interface ArkUIViewController : LifecycleOwner, OnBackPressedDispatcherOwner, ExtraStorageOwner {
74+
sealed interface ArkUIViewController : LifecycleOwner, OnBackPressedDispatcherOwner, ExtraStorageOwner, ViewModelStoreOwner {
7475

7576
/**
7677
* Return harmony ArkUI Context

compose/ui/ui/src/ohosArm64Main/kotlin/androidx/compose/ui/window/ComposeArkUIViewContainer.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import androidx.compose.ui.platform.InterfaceOrientation
4141
import androidx.compose.ui.platform.LocalArkUIViewController
4242
import androidx.compose.ui.platform.LocalContext
4343
import androidx.compose.ui.platform.LocalInterfaceOrientation
44+
import androidx.compose.ui.platform.LocalInternalViewModelStoreOwner
4445
import androidx.compose.ui.platform.LocalLifecycleOwner
4546
import androidx.compose.ui.platform.LocalNapiEnv
4647
import androidx.compose.ui.platform.LocalPlatformInsetsHolder
@@ -59,6 +60,7 @@ import androidx.compose.ui.scene.SingleLayerComposeScene
5960
import androidx.compose.ui.unit.Density
6061
import androidx.compose.ui.unit.IntSize
6162
import androidx.compose.ui.unit.LayoutDirection
63+
import androidx.lifecycle.ViewModelStore
6264
import kotlin.coroutines.CoroutineContext
6365
import kotlinx.coroutines.Dispatchers
6466
import platform.ohos.napi_value
@@ -90,6 +92,13 @@ internal class ComposeArkUIViewContainer(
9092
PlatformInsetsHolder(this)
9193
}
9294

95+
override val viewModelStore: ViewModelStore by lazy { ViewModelStore() }
96+
97+
override fun aboutToDisappear() {
98+
super.aboutToDisappear()
99+
viewModelStore.clear()
100+
}
101+
93102
override fun onSurfaceCreated(component: OHNativeXComponent, width: Int, height: Int) {
94103
if (!nativeSurfaceHasBeenDestroyed) {
95104
createMediatorIfNeeded(component)
@@ -218,6 +227,7 @@ internal fun ProvideContainerCompositionLocals(
218227
CompositionLocalProvider(
219228
LocalArkUIViewController provides this,
220229
LocalLifecycleOwner provides this,
230+
LocalInternalViewModelStoreOwner provides this,
221231
LocalNapiEnv provides requiredEnv,
222232
LocalContext provides context,
223233
LocalUIContext provides uiContext,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package androidx.compose.ui.platform
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.InternalComposeApi
21+
import androidx.compose.runtime.staticCompositionLocalOf
22+
import androidx.lifecycle.ViewModelStoreOwner
23+
24+
/**
25+
* Internal helper to provide [ViewModelStoreOwner] from Compose UI module.
26+
* In applications please use [androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner].
27+
*
28+
* @hide
29+
*/
30+
internal val LocalInternalViewModelStoreOwner = staticCompositionLocalOf<ViewModelStoreOwner?> {
31+
null
32+
}
33+
34+
@InternalComposeApi
35+
@Composable
36+
fun findComposeDefaultViewModelStoreOwner(): ViewModelStoreOwner? =
37+
LocalInternalViewModelStoreOwner.current

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ kotlinBenchmark = "0.4.7"
4242
kotlinNative = "2.0.21-KBA-009"
4343
kotlinBinaryCompatibilityValidator = "0.13.2"
4444
kotlinCompileTesting = "1.4.9"
45-
kotlinCoroutines = "1.8.0-KBA-001"
45+
kotlinCoroutines = "1.8.0-KBA-002"
4646
kotlinSerialization = "1.6.2"
4747
ksp = "2.0.20-1.0.25"
4848
ktlint = "0.46.0"

0 commit comments

Comments
 (0)