Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert commonization of inline fun <reified VM> viewModel(..)" #1290

Merged
merged 1 commit into from
Apr 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,79 +85,6 @@ public fun <VM : ViewModel> viewModel(
factory: ViewModelProvider.Factory? = null
): VM = viewModelStoreOwner.get(modelClass.kotlin, key, factory)

/**
* Returns an existing [ViewModel] or creates a new one in the given owner (usually, a fragment or
* an activity), defaulting to the owner provided by [LocalViewModelStoreOwner].
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* as long as the owner is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* If default arguments are provided via the [CreationExtras], they will be available to the
* appropriate factory when the [ViewModel] is created.
*
* @param viewModelStoreOwner The owner of the [ViewModel] that controls the scope and lifetime
* of the returned [ViewModel]. Defaults to using [LocalViewModelStoreOwner].
* @param key The key to use to identify the [ViewModel].
* @param factory The [ViewModelProvider.Factory] that should be used to create the [ViewModel]
* or null if you would like to use the default factory from the [LocalViewModelStoreOwner]
* @param extras The default extras used to create the [ViewModel].
* @return A [ViewModel] that is an instance of the given [VM] type.
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
*/
@Suppress("MissingJvmstatic")
@Composable
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
factory: ViewModelProvider.Factory? = null,
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
CreationExtras.Empty
}
): VM = viewModel(VM::class, viewModelStoreOwner, key, factory, extras)

/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* an activity)
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* If the [viewModelStoreOwner] implements [HasDefaultViewModelProviderFactory] its default
* [CreationExtras] are the ones that will be provided to the receiver scope from the [initializer]
*
* @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param key The key to use to identify the [ViewModel].
* @param initializer lambda used to create an instance of the ViewModel class
* @return A [ViewModel] that is an instance of the given [VM] type.
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModelInitializer
*/
@Composable
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
noinline initializer: CreationExtras.() -> VM
): VM = viewModel(
VM::class,
viewModelStoreOwner,
key,
viewModelFactory { initializer(initializer) },
if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
CreationExtras.Empty
}
)

/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* an activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,47 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.reflect.KClass

// TODO: Move inline fun <reified VM> viewModel(..) to common once
// "Compilation failed: Symbol for ... is unbound" is fixed
// https://github.com/JetBrains/compose-multiplatform/issues/3147
/**
* Returns an existing [ViewModel] or creates a new one in the given owner (usually, a fragment or
* an activity), defaulting to the owner provided by [LocalViewModelStoreOwner].
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* as long as the owner is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* If default arguments are provided via the [CreationExtras], they will be available to the
* appropriate factory when the [ViewModel] is created.
*
* @param viewModelStoreOwner The owner of the [ViewModel] that controls the scope and lifetime
* of the returned [ViewModel]. Defaults to using [LocalViewModelStoreOwner].
* @param key The key to use to identify the [ViewModel].
* @param factory The [ViewModelProvider.Factory] that should be used to create the [ViewModel]
* or null if you would like to use the default factory from the [LocalViewModelStoreOwner]
* @param extras The default extras used to create the [ViewModel].
* @return A [ViewModel] that is an instance of the given [VM] type.
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
*/
@Suppress("MissingJvmstatic")
@Composable
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
factory: ViewModelProvider.Factory? = null,
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
CreationExtras.Empty
}
): VM = viewModel(VM::class, viewModelStoreOwner, key, factory, extras)

/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
Expand Down Expand Up @@ -70,6 +104,43 @@ public fun <VM : ViewModel> viewModel(
}
): VM = viewModelStoreOwner.get(modelClass, key, factory, extras)

/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* an activity)
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
*
* If the [viewModelStoreOwner] implements [HasDefaultViewModelProviderFactory] its default
* [CreationExtras] are the ones that will be provided to the receiver scope from the [initializer]
*
* @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param key The key to use to identify the [ViewModel].
* @param initializer lambda used to create an instance of the ViewModel class
* @return A [ViewModel] that is an instance of the given [VM] type.
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModelInitializer
*/
@Composable
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
noinline initializer: CreationExtras.() -> VM
): VM = viewModel(
VM::class,
viewModelStoreOwner,
key,
viewModelFactory { initializer(initializer) },
if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
CreationExtras.Empty
}
)

internal fun <VM : ViewModel> ViewModelStoreOwner.get(
modelClass: KClass<VM>,
key: String? = null,
Expand Down
Loading