commonMain.com.adeo.kviewmodel.odyssey.StoredViewModel.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kviewmodel-odyssey Show documentation
Show all versions of kviewmodel-odyssey Show documentation
ViewModel for Multiplatform
The newest version!
package com.adeo.kviewmodel.odyssey
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisallowComposableCalls
import androidx.compose.runtime.remember
import com.adeo.kviewmodel.KViewModel
import ru.alexgladkov.odyssey.compose.local.LocalRootController
@Composable
public inline fun StoredViewModel(
noinline factory: @DisallowComposableCalls () -> T,
viewModelKey: String? = null,
noinline content: @Composable (T) -> Unit,
) {
val currentScreen = LocalRootController.current.currentScreen
val screenKey = viewModelKey ?: currentScreen.value?.screen?.key ?: return
val viewModel = remember { ViewModelStore.getOrPut(screenKey, factory) }
content(viewModel)
}