Skip to content
Open
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
1 change: 1 addition & 0 deletions compose/ui/ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
ohosArm64Main.dependsOn(nativeMain)
ohosArm64Main {
dependencies {
api "androidx.lifecycle:lifecycle-viewmodel:2.8.0-KBA-001"
api project(":compose:ui:ui-arkui")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import androidx.compose.ui.unit.LayoutDirection
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.lifecycle.ViewModelStoreOwner
import kotlin.coroutines.CoroutineContext
import kotlinx.cinterop.COpaquePointer
import kotlinx.cinterop.StableRef
Expand All @@ -70,7 +71,7 @@ import platform.ohos.napi_value
* @author gavinbaoliu
* @since 2024/5/9
*/
sealed interface ArkUIViewController : LifecycleOwner, OnBackPressedDispatcherOwner, ExtraStorageOwner {
sealed interface ArkUIViewController : LifecycleOwner, OnBackPressedDispatcherOwner, ExtraStorageOwner, ViewModelStoreOwner {

/**
* Return harmony ArkUI Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.platform.InterfaceOrientation
import androidx.compose.ui.platform.LocalArkUIViewController
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInterfaceOrientation
import androidx.compose.ui.platform.LocalInternalViewModelStoreOwner
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalNapiEnv
import androidx.compose.ui.platform.LocalPlatformInsetsHolder
Expand All @@ -59,6 +60,7 @@ import androidx.compose.ui.scene.SingleLayerComposeScene
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.lifecycle.ViewModelStore
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
import platform.ohos.napi_value
Expand Down Expand Up @@ -90,6 +92,13 @@ internal class ComposeArkUIViewContainer(
PlatformInsetsHolder(this)
}

override val viewModelStore: ViewModelStore by lazy { ViewModelStore() }

override fun aboutToDisappear() {
super.aboutToDisappear()
viewModelStore.clear()
}

override fun onSurfaceCreated(component: OHNativeXComponent, width: Int, height: Int) {
if (!nativeSurfaceHasBeenDestroyed) {
createMediatorIfNeeded(component)
Expand Down Expand Up @@ -218,6 +227,7 @@ internal fun ProvideContainerCompositionLocals(
CompositionLocalProvider(
LocalArkUIViewController provides this,
LocalLifecycleOwner provides this,
LocalInternalViewModelStoreOwner provides this,
LocalNapiEnv provides requiredEnv,
LocalContext provides context,
LocalUIContext provides uiContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.platform

import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.lifecycle.ViewModelStoreOwner

/**
* Internal helper to provide [ViewModelStoreOwner] from Compose UI module.
* In applications please use [androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner].
*
* @hide
*/
internal val LocalInternalViewModelStoreOwner = staticCompositionLocalOf<ViewModelStoreOwner?> {
null
}

@InternalComposeApi
@Composable
fun findComposeDefaultViewModelStoreOwner(): ViewModelStoreOwner? =
LocalInternalViewModelStoreOwner.current
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ kotlinBenchmark = "0.4.7"
kotlinNative = "2.0.21-KBA-009"
kotlinBinaryCompatibilityValidator = "0.13.2"
kotlinCompileTesting = "1.4.9"
kotlinCoroutines = "1.8.0-KBA-001"
kotlinCoroutines = "1.8.0-KBA-002"
kotlinSerialization = "1.6.2"
ksp = "2.0.20-1.0.25"
ktlint = "0.46.0"
Expand Down