[go: nahoru, domu]

blob: fa2cfce38cdc2a23e029b4a52987f2842070e52c [file] [log] [blame]
/*
* Copyright 2020 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 android.content.Context
import android.content.res.Configuration
import android.view.View
import androidx.compose.animation.core.InternalAnimationApi
import androidx.compose.animation.core.rootAnimationClockFactory
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.ExperimentalComposeApi
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.neverEqualPolicy
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.LocalSaveableStateRegistry
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelStoreOwner
import androidx.savedstate.SavedStateRegistryOwner
/**
* The Android [Configuration]. The [Configuration] is useful for determining how to organize the
* UI.
*/
val LocalConfiguration = compositionLocalOf<Configuration>(
@OptIn(ExperimentalComposeApi::class)
neverEqualPolicy()
)
/**
* Provides a [Context] that can be used by Android applications.
*/
val LocalContext = staticCompositionLocalOf<Context>()
/**
* The CompositionLocal containing the current [LifecycleOwner].
*/
val LocalLifecycleOwner = staticCompositionLocalOf<LifecycleOwner>()
/**
* The CompositionLocal containing the current [SavedStateRegistryOwner].
*/
val LocalSavedStateRegistryOwner = staticCompositionLocalOf<SavedStateRegistryOwner>()
/**
* The CompositionLocal containing the current Compose [View].
*/
val LocalView = staticCompositionLocalOf<View>()
/**
* The CompositionLocal containing the current [ViewModelStoreOwner].
*/
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated(
"It was moved to androidx.lifecycle.viewmodel.compose package. You should add a " +
"dependency on androidx.lifecycle:lifecycle-viewmodel.compose:1.0.0-alpha01 in order to " +
"use it"
)
val LocalViewModelStoreOwner = staticCompositionLocalOf<ViewModelStoreOwner>()
@Composable
@OptIn(ExperimentalComposeUiApi::class, InternalAnimationApi::class)
internal fun ProvideAndroidCompositionLocals(
owner: AndroidComposeView,
content: @Composable () -> Unit
) {
val view = owner
val context = view.context
val scope = rememberCoroutineScope()
val rootAnimationClock = remember(scope) {
rootAnimationClockFactory(scope)
}
var configuration by remember {
mutableStateOf(
context.resources.configuration,
@OptIn(ExperimentalComposeApi::class)
neverEqualPolicy()
)
}
owner.configurationChangeObserver = { configuration = it }
val uriHandler = remember { AndroidUriHandler(context) }
val viewTreeOwners = owner.viewTreeOwners ?: throw IllegalStateException(
"Called when the ViewTreeOwnersAvailability is not yet in Available state"
)
val saveableStateRegistry = remember {
DisposableSaveableStateRegistry(view, viewTreeOwners.savedStateRegistryOwner)
}
DisposableEffect(Unit) {
onDispose {
saveableStateRegistry.dispose()
}
}
CompositionLocalProvider(
LocalConfiguration provides configuration,
LocalContext provides context,
LocalLifecycleOwner provides viewTreeOwners.lifecycleOwner,
LocalSavedStateRegistryOwner provides viewTreeOwners.savedStateRegistryOwner,
LocalSaveableStateRegistry provides saveableStateRegistry,
LocalView provides owner.view,
@Suppress("DEPRECATION")
LocalViewModelStoreOwner provides viewTreeOwners.viewModelStoreOwner
) {
ProvideCommonCompositionLocals(
owner = owner,
animationClock = rootAnimationClock,
uriHandler = uriHandler,
content = content
)
}
}