From 8dce66d76f821f46875bf0e18a0308fd924284df Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 4 Aug 2022 10:49:33 +0200 Subject: [PATCH] Improve FeedGroupDialogViewModel factory --- .../subscription/dialog/FeedGroupDialog.kt | 8 +++--- .../dialog/FeedGroupDialogViewModel.kt | 27 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialog.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialog.kt index 379b4c0d7..4b3c4ccc0 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialog.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialog.kt @@ -124,11 +124,13 @@ class FeedGroupDialog : DialogFragment(), BackPressable { viewModel = ViewModelProvider( this, - FeedGroupDialogViewModel.Factory( + FeedGroupDialogViewModel.getFactory( requireContext(), - groupId, subscriptionsCurrentSearchQuery, subscriptionsShowOnlyUngrouped + groupId, + subscriptionsCurrentSearchQuery, + subscriptionsShowOnlyUngrouped ) - ).get(FeedGroupDialogViewModel::class.java) + )[FeedGroupDialogViewModel::class.java] viewModel.groupLiveData.observe(viewLifecycleOwner, Observer(::handleGroup)) viewModel.subscriptionsLiveData.observe(viewLifecycleOwner) { diff --git a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt index dfdb2b47a..eff1a4400 100644 --- a/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt +++ b/app/src/main/java/org/schabi/newpipe/local/subscription/dialog/FeedGroupDialogViewModel.kt @@ -4,7 +4,8 @@ import android.content.Context import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory import io.reactivex.rxjava3.core.Completable import io.reactivex.rxjava3.core.Flowable import io.reactivex.rxjava3.disposables.Disposable @@ -115,18 +116,18 @@ class FeedGroupDialogViewModel( data class Filter(val query: String, val showOnlyUngrouped: Boolean) - class Factory( - private val context: Context, - private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID, - private val initialQuery: String = "", - private val initialShowOnlyUngrouped: Boolean = false - ) : ViewModelProvider.Factory { - @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T { - return FeedGroupDialogViewModel( - context.applicationContext, - groupId, initialQuery, initialShowOnlyUngrouped - ) as T + companion object { + fun getFactory( + context: Context, + groupId: Long, + initialQuery: String, + initialShowOnlyUngrouped: Boolean + ) = viewModelFactory { + initializer { + FeedGroupDialogViewModel( + context.applicationContext, groupId, initialQuery, initialShowOnlyUngrouped + ) + } } } }