Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
ViewModel.viewModel()
建立於
2 年前
差異永不過期
清除
匯出
分享
解釋
67 刪除
行
總計
刪除
字符
總計
刪除
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
218 行
全部複製
14 新增
行
總計
新增
字符
總計
新增
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
165 行
全部複製
/*
/*
複製
已複製
複製
已複製
* Copyright 202
1
The Android Open Source Project
* Copyright 202
4
The Android Open Source Project
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
*/
複製
已複製
複製
已複製
@file:JvmName("ViewModelKt")
@file:JvmMultifileClass
package androidx.lifecycle.viewmodel.compose
package androidx.lifecycle.viewmodel.compose
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composable
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.lifecycle.viewmodel.viewModelFactory
複製
已複製
複製
已複製
import kotlin.reflect.KC
lass
/**
* 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).
*
* @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]
* @return A [ViewModel] that is an instance of the given [VM] type.
*/
@Deprecated(
"Superseded by viewModel that takes CreationExtras",
level = DeprecationLevel.HIDDEN
)
@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
): VM = viewModel(VM::c
lass
.java, viewModelStoreOwner, key, factory)
/**
/**
* Returns an existing [ViewModel] or creates a new one in the given owner (usually, a fragment or
* 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].
* an activity), defaulting to the owner provided by [LocalViewModelStoreOwner].
*
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* 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
* as long as the owner is alive (e.g. if it is an activity, until it is
* finished or process is killed).
* finished or process is killed).
*
*
* If default arguments are provided via the [CreationExtras], they will be available to the
* If default arguments are provided via the [CreationExtras], they will be available to the
* appropriate factory when the [ViewModel] is created.
* appropriate factory when the [ViewModel] is created.
*
*
* @param viewModelStoreOwner The owner of the [ViewModel] that controls the scope and lifetime
* @param viewModelStoreOwner The owner of the [ViewModel] that controls the scope and lifetime
* of the returned [ViewModel]. Defaults to using [LocalViewModelStoreOwner].
* of the returned [ViewModel]. Defaults to using [LocalViewModelStoreOwner].
* @param key The key to use to identify the [ViewModel].
* @param key The key to use to identify the [ViewModel].
* @param factory The [ViewModelProvider.Factory] that should be used to create 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]
* or null if you would like to use the default factory from the [LocalViewModelStoreOwner]
* @param extras The default extras used to create the [ViewModel].
* @param extras The default extras used to create the [ViewModel].
* @return A [ViewModel] that is an instance of the given [VM] type.
* @return A [ViewModel] that is an instance of the given [VM] type.
*
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
*/
*/
@Suppress("MissingJvmstatic")
@Suppress("MissingJvmstatic")
@Composable
@Composable
public inline fun <reified VM : ViewModel> viewModel(
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
},
key: String? = null,
key: String? = null,
factory: ViewModelProvider.Factory? = null,
factory: ViewModelProvider.Factory? = null,
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
} else {
CreationExtras.Empty
CreationExtras.Empty
}
}
複製
已複製
複製
已複製
): VM = viewModel(VM::class
.java
, viewModelStoreOwner, key, factory, extras)
): 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).
*
* @param modelClass The class of the [ViewModel] to create an instance of it if it is not
* present.
* @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param key The key to use to identify the [ViewModel].
* @return A [ViewModel] that is an instance of the given [VM] type.
*/
@Deprecated(
"Superseded by viewModel that takes CreationExtras",
level = DeprecationLevel.HIDDEN
)
@Suppress("MissingJvmstatic")
@Composable
public fun <VM : ViewModel> viewModel(
modelClass: Class<VM>,
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
factory: ViewModelProvider.Factory? = null
): VM = viewModelStoreOwner.get(modelClass, key, factory)
/**
/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* an activity)
* an activity)
*
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* 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
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
* finished or process is killed).
*
*
* If default arguments are provided via the [CreationExtras], they will be available to the
* If default arguments are provided via the [CreationExtras], they will be available to the
* appropriate factory when the [ViewModel] is created.
* appropriate factory when the [ViewModel] is created.
*
*
* @param modelClass The class of the [ViewModel] to create an instance of it if it is not
* @param modelClass The class of the [ViewModel] to create an instance of it if it is not
* present.
* present.
* @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param key The key to use to identify the [ViewModel].
* @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].
* @param extras The default extras used to create the [ViewModel].
* @return A [ViewModel] that is an instance of the given [VM] type.
* @return A [ViewModel] that is an instance of the given [VM] type.
*
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModel
*/
*/
@Suppress("MissingJvmstatic")
@Suppress("MissingJvmstatic")
@Composable
@Composable
public fun <VM : ViewModel> viewModel(
public fun <VM : ViewModel> viewModel(
複製
已複製
複製
已複製
modelClass:
Class<VM>,
modelClass:
K
Class<VM>,
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
},
key: String? = null,
key: String? = null,
factory: ViewModelProvider.Factory? = null,
factory: ViewModelProvider.Factory? = null,
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
} else {
CreationExtras.Empty
CreationExtras.Empty
}
}
): VM = viewModelStoreOwner.get(modelClass, key, factory, extras)
): VM = viewModelStoreOwner.get(modelClass, key, factory, extras)
/**
/**
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* Returns an existing [ViewModel] or creates a new one in the scope (usually, a fragment or
* an activity)
* an activity)
*
*
* The created [ViewModel] is associated with the given [viewModelStoreOwner] and will be retained
* 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
* as long as the scope is alive (e.g. if it is an activity, until it is
* finished or process is killed).
* finished or process is killed).
*
*
* If the [viewModelStoreOwner] implements [HasDefaultViewModelProviderFactory] its default
* If the [viewModelStoreOwner] implements [HasDefaultViewModelProviderFactory] its default
* [CreationExtras] are the ones that will be provided to the receiver scope from the [initializer]
* [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 viewModelStoreOwner The scope that the created [ViewModel] should be associated with.
* @param key The key to use to identify the [ViewModel].
* @param key The key to use to identify the [ViewModel].
* @param initializer lambda used to create an instance of the ViewModel class
* @param initializer lambda used to create an instance of the ViewModel class
* @return A [ViewModel] that is an instance of the given [VM] type.
* @return A [ViewModel] that is an instance of the given [VM] type.
*
*
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModelInitializer
* @sample androidx.lifecycle.viewmodel.compose.samples.CreationExtrasViewModelInitializer
*/
*/
@Composable
@Composable
public inline fun <reified VM : ViewModel> viewModel(
public inline fun <reified VM : ViewModel> viewModel(
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
},
key: String? = null,
key: String? = null,
noinline initializer: CreationExtras.() -> VM
noinline initializer: CreationExtras.() -> VM
): VM = viewModel(
): VM = viewModel(
複製
已複製
複製
已複製
VM::class
.java
,
VM::class
,
viewModelStoreOwner,
viewModelStoreOwner,
key,
key,
viewModelFactory { initializer(initializer) },
viewModelFactory { initializer(initializer) },
if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
} else {
CreationExtras.Empty
CreationExtras.Empty
}
}
)
)
複製
已複製
複製
已複製
private
fun <VM : ViewModel> ViewModelStoreOwner.get(
internal
fun <VM : ViewModel> ViewModelStoreOwner.get(
javaClass:
Class<VM>,
modelClass: K
Class<VM>,
key: String? = null,
key: String? = null,
factory: ViewModelProvider.Factory? = null,
factory: ViewModelProvider.Factory? = null,
extras: CreationExtras = if (this is HasDefaultViewModelProviderFactory) {
extras: CreationExtras = if (this is HasDefaultViewModelProviderFactory) {
this.defaultViewModelCreationExtras
this.defaultViewModelCreationExtras
} else {
} else {
CreationExtras.Empty
CreationExtras.Empty
}
}
): VM {
): VM {
val provider = if (factory != null) {
val provider = if (factory != null) {
複製
已複製
複製
已複製
ViewModelProvider
(this.viewModelStore, factory, extras)
ViewModelProvider
.create
(this.viewModelStore, factory, extras)
} else if (this is HasDefaultViewModelProviderFactory) {
} else if (this is HasDefaultViewModelProviderFactory) {
複製
已複製
複製
已複製
ViewModelProvider
(this.viewModelStore, this.defaultViewModelProviderFactory, extras)
ViewModelProvider
.create
(this.viewModelStore, this.defaultViewModelProviderFactory, extras)
} else {
} else {
複製
已複製
複製
已複製
ViewModelProvider
(this)
ViewModelProvider
.create
(this)
}
}
return if (key != null) {
return if (key != null) {
複製
已複製
複製
已複製
provider[key,
javaClass
]
provider[key,
modelClass
]
} else {
} else {
複製
已複製
複製
已複製
provider[
javaClass
]
provider[
modelClass
]
}
}
}
}
已保存差異
原始文本
開啟檔案
/* * Copyright 2021 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.lifecycle.viewmodel.compose import androidx.compose.runtime.Composable import androidx.lifecycle.HasDefaultViewModelProviderFactory 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 /** * 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). * * @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] * @return A [ViewModel] that is an instance of the given [VM] type. */ @Deprecated( "Superseded by viewModel that takes CreationExtras", level = DeprecationLevel.HIDDEN ) @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 ): VM = viewModel(VM::class.java, viewModelStoreOwner, 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.java, 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). * * @param modelClass The class of the [ViewModel] to create an instance of it if it is not * present. * @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with. * @param key The key to use to identify the [ViewModel]. * @return A [ViewModel] that is an instance of the given [VM] type. */ @Deprecated( "Superseded by viewModel that takes CreationExtras", level = DeprecationLevel.HIDDEN ) @Suppress("MissingJvmstatic") @Composable public fun <VM : ViewModel> viewModel( modelClass: Class<VM>, viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) { "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner" }, key: String? = null, factory: ViewModelProvider.Factory? = null ): VM = viewModelStoreOwner.get(modelClass, key, factory) /** * 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 default arguments are provided via the [CreationExtras], they will be available to the * appropriate factory when the [ViewModel] is created. * * @param modelClass The class of the [ViewModel] to create an instance of it if it is not * present. * @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with. * @param key The key to use to identify the [ViewModel]. * @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 fun <VM : ViewModel> viewModel( modelClass: Class<VM>, 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 = 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.java, viewModelStoreOwner, key, viewModelFactory { initializer(initializer) }, if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) { viewModelStoreOwner.defaultViewModelCreationExtras } else { CreationExtras.Empty } ) private fun <VM : ViewModel> ViewModelStoreOwner.get( javaClass: Class<VM>, key: String? = null, factory: ViewModelProvider.Factory? = null, extras: CreationExtras = if (this is HasDefaultViewModelProviderFactory) { this.defaultViewModelCreationExtras } else { CreationExtras.Empty } ): VM { val provider = if (factory != null) { ViewModelProvider(this.viewModelStore, factory, extras) } else if (this is HasDefaultViewModelProviderFactory) { ViewModelProvider(this.viewModelStore, this.defaultViewModelProviderFactory, extras) } else { ViewModelProvider(this) } return if (key != null) { provider[key, javaClass] } else { provider[javaClass] } }
更改後文本
開啟檔案
/* * 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. */ @file:JvmName("ViewModelKt") @file:JvmMultifileClass package androidx.lifecycle.viewmodel.compose import androidx.compose.runtime.Composable import androidx.lifecycle.HasDefaultViewModelProviderFactory 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.reflect.KClass /** * 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 default arguments are provided via the [CreationExtras], they will be available to the * appropriate factory when the [ViewModel] is created. * * @param modelClass The class of the [ViewModel] to create an instance of it if it is not * present. * @param viewModelStoreOwner The scope that the created [ViewModel] should be associated with. * @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 fun <VM : ViewModel> viewModel( modelClass: KClass<VM>, 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 = 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, factory: ViewModelProvider.Factory? = null, extras: CreationExtras = if (this is HasDefaultViewModelProviderFactory) { this.defaultViewModelCreationExtras } else { CreationExtras.Empty } ): VM { val provider = if (factory != null) { ViewModelProvider.create(this.viewModelStore, factory, extras) } else if (this is HasDefaultViewModelProviderFactory) { ViewModelProvider.create(this.viewModelStore, this.defaultViewModelProviderFactory, extras) } else { ViewModelProvider.create(this) } return if (key != null) { provider[key, modelClass] } else { provider[modelClass] } }
尋找差異