mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 21:15:48 +00:00
Remove deprecated view model factory.
This commit is contained in:
committed by
Greyson Parrelli
parent
1b2e80d2c8
commit
07d270a82d
@@ -65,6 +65,7 @@ import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.createSavedStateHandle
|
||||
import androidx.lifecycle.flowWithLifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
@@ -347,7 +348,6 @@ import org.thoughtcrime.securesms.util.hasLinkPreview
|
||||
import org.thoughtcrime.securesms.util.hasNonTextSlide
|
||||
import org.thoughtcrime.securesms.util.isValidReactionTarget
|
||||
import org.thoughtcrime.securesms.util.padding
|
||||
import org.thoughtcrime.securesms.util.savedStateViewModel
|
||||
import org.thoughtcrime.securesms.util.setIncognitoKeyboardEnabled
|
||||
import org.thoughtcrime.securesms.util.toMillis
|
||||
import org.thoughtcrime.securesms.util.viewModel
|
||||
@@ -453,8 +453,8 @@ class ConversationFragment :
|
||||
)
|
||||
}
|
||||
|
||||
private val linkPreviewViewModel: LinkPreviewViewModelV2 by savedStateViewModel {
|
||||
LinkPreviewViewModelV2(it, enablePlaceholder = false)
|
||||
private val linkPreviewViewModel: LinkPreviewViewModelV2 by viewModel {
|
||||
LinkPreviewViewModelV2(it.createSavedStateHandle(), enablePlaceholder = false)
|
||||
}
|
||||
|
||||
private val groupCallViewModel: ConversationGroupCallViewModel by viewModel {
|
||||
|
||||
@@ -11,6 +11,7 @@ import androidx.core.view.postDelayed
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.createSavedStateHandle
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
@@ -32,7 +33,7 @@ import org.thoughtcrime.securesms.mediasend.v2.text.send.TextStoryPostSendReposi
|
||||
import org.thoughtcrime.securesms.mediasend.v2.text.send.TextStoryPostSendResult
|
||||
import org.thoughtcrime.securesms.safety.SafetyNumberBottomSheet
|
||||
import org.thoughtcrime.securesms.stories.Stories
|
||||
import org.thoughtcrime.securesms.util.activitySavedStateViewModel
|
||||
import org.thoughtcrime.securesms.util.activityViewModel
|
||||
import org.thoughtcrime.securesms.util.visible
|
||||
import java.util.Optional
|
||||
|
||||
@@ -56,8 +57,8 @@ class TextStoryPostCreationFragment : Fragment(R.layout.stories_text_post_creati
|
||||
}
|
||||
)
|
||||
|
||||
private val linkPreviewViewModel: LinkPreviewViewModelV2 by activitySavedStateViewModel { handle ->
|
||||
LinkPreviewViewModelV2(handle, enablePlaceholder = true)
|
||||
private val linkPreviewViewModel: LinkPreviewViewModelV2 by activityViewModel { extras ->
|
||||
LinkPreviewViewModelV2(extras.createSavedStateHandle(), enablePlaceholder = true)
|
||||
}
|
||||
|
||||
private val lifecycleDisposable = LifecycleDisposable()
|
||||
|
||||
@@ -6,11 +6,9 @@ import androidx.annotation.MainThread
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.AbstractSavedStateViewModelFactory
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.savedstate.SavedStateRegistryOwner
|
||||
import androidx.lifecycle.viewmodel.CreationExtras
|
||||
|
||||
/**
|
||||
* Simplifies [ViewModel] creation by providing default implementations of [ViewModelProvider.Factory]
|
||||
@@ -20,41 +18,22 @@ import androidx.savedstate.SavedStateRegistryOwner
|
||||
*
|
||||
* private val viewModel: MyViewModel by viewModels(factoryProducer = ViewModelFactory.factoryProducer { MyViewModel(inputParams) })
|
||||
*/
|
||||
class ViewModelFactory<MODEL : ViewModel>(private val create: () -> MODEL) : ViewModelProvider.Factory {
|
||||
class ViewModelFactory<MODEL : ViewModel>(private val create: (CreationExtras) -> MODEL) : ViewModelProvider.Factory {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
return create() as T
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T {
|
||||
return create(extras) as T
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <MODEL : ViewModel> factoryProducer(create: () -> MODEL): () -> ViewModelProvider.Factory {
|
||||
fun <MODEL : ViewModel> factoryProducer(create: (CreationExtras) -> MODEL): () -> ViewModelProvider.Factory {
|
||||
return { ViewModelFactory(create) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SavedStateViewModelFactory<MODEL : ViewModel>(
|
||||
private val create: (SavedStateHandle) -> MODEL,
|
||||
registryOwner: SavedStateRegistryOwner
|
||||
) : AbstractSavedStateViewModelFactory(registryOwner, null) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel> create(key: String, modelClass: Class<T>, handle: SavedStateHandle): T {
|
||||
return create(handle) as T
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun <MODEL : ViewModel> factoryProducer(
|
||||
create: (SavedStateHandle) -> MODEL,
|
||||
registryOwnerProducer: () -> SavedStateRegistryOwner
|
||||
): () -> ViewModelProvider.Factory {
|
||||
return { SavedStateViewModelFactory(create, registryOwnerProducer()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> ComponentActivity.viewModel(
|
||||
noinline create: () -> VM
|
||||
noinline create: (CreationExtras) -> VM
|
||||
): Lazy<VM> {
|
||||
return viewModels(
|
||||
factoryProducer = ViewModelFactory.factoryProducer(create)
|
||||
@@ -63,51 +42,18 @@ inline fun <reified VM : ViewModel> ComponentActivity.viewModel(
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> Fragment.viewModel(
|
||||
noinline create: () -> VM
|
||||
noinline create: (CreationExtras) -> VM
|
||||
): Lazy<VM> {
|
||||
return viewModels(
|
||||
factoryProducer = ViewModelFactory.factoryProducer(create)
|
||||
)
|
||||
}
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> Fragment.savedStateViewModel(
|
||||
noinline create: (SavedStateHandle) -> VM
|
||||
): Lazy<VM> {
|
||||
return viewModels(
|
||||
factoryProducer = SavedStateViewModelFactory.factoryProducer(create) { this }
|
||||
)
|
||||
}
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> Fragment.activitySavedStateViewModel(
|
||||
noinline create: (SavedStateHandle) -> VM
|
||||
): Lazy<VM> {
|
||||
return activityViewModels(
|
||||
factoryProducer = SavedStateViewModelFactory.factoryProducer(create) { requireActivity() }
|
||||
)
|
||||
}
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> ComponentActivity.savedStateViewModel(
|
||||
noinline create: (SavedStateHandle) -> VM
|
||||
): Lazy<VM> {
|
||||
return viewModels(
|
||||
factoryProducer = SavedStateViewModelFactory.factoryProducer(create) { this }
|
||||
)
|
||||
}
|
||||
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> Fragment.activityViewModel(
|
||||
noinline create: () -> VM
|
||||
noinline create: (CreationExtras) -> VM
|
||||
): Lazy<VM> {
|
||||
return activityViewModels(
|
||||
factoryProducer = ViewModelFactory.factoryProducer(create)
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("ReplaceGetOrSet")
|
||||
@MainThread
|
||||
inline fun <reified VM : ViewModel> Fragment.createActivityViewModel(noinline create: () -> VM): VM {
|
||||
return ViewModelProvider(requireActivity().viewModelStore, ViewModelFactory { create() }).get(VM::class.java)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user