Merge pull request #8734 from Stypox/feed-group-factory

Improve `FeedGroupDialogViewModel` factory
This commit is contained in:
Tobi 2022-10-09 19:35:47 +02:00 committed by GitHub
commit ed4eb124e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 16 deletions

View File

@ -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) {

View File

@ -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 <T : ViewModel> create(modelClass: Class<T>): 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
)
}
}
}
}