Improve FeedViewModel factory
This commit is contained in:
parent
5c68c8ece8
commit
404c13d4c1
2 changed files with 14 additions and 18 deletions
|
@ -135,8 +135,8 @@ class FeedFragment : BaseStateFragment<FeedState>() {
|
||||||
_feedBinding = FragmentFeedBinding.bind(rootView)
|
_feedBinding = FragmentFeedBinding.bind(rootView)
|
||||||
super.onViewCreated(rootView, savedInstanceState)
|
super.onViewCreated(rootView, savedInstanceState)
|
||||||
|
|
||||||
val factory = FeedViewModel.Factory(requireContext(), groupId)
|
val factory = FeedViewModel.getFactory(requireContext(), groupId)
|
||||||
viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java)
|
viewModel = ViewModelProvider(this, factory)[FeedViewModel::class.java]
|
||||||
showPlayedItems = viewModel.getShowPlayedItemsFromPreferences()
|
showPlayedItems = viewModel.getShowPlayedItemsFromPreferences()
|
||||||
showFutureItems = viewModel.getShowFutureItemsFromPreferences()
|
showFutureItems = viewModel.getShowFutureItemsFromPreferences()
|
||||||
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(::handleResult) }
|
viewModel.stateLiveData.observe(viewLifecycleOwner) { it?.let(::handleResult) }
|
||||||
|
|
|
@ -5,7 +5,8 @@ import androidx.core.content.edit
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.viewmodel.initializer
|
||||||
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.rxjava3.core.Flowable
|
import io.reactivex.rxjava3.core.Flowable
|
||||||
|
@ -139,21 +140,16 @@ class FeedViewModel(
|
||||||
private fun getShowFutureItemsFromPreferences(context: Context) =
|
private fun getShowFutureItemsFromPreferences(context: Context) =
|
||||||
PreferenceManager.getDefaultSharedPreferences(context)
|
PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
.getBoolean(context.getString(R.string.feed_show_future_items_key), true)
|
.getBoolean(context.getString(R.string.feed_show_future_items_key), true)
|
||||||
}
|
fun getFactory(context: Context, groupId: Long) = viewModelFactory {
|
||||||
|
initializer {
|
||||||
class Factory(
|
FeedViewModel(
|
||||||
private val context: Context,
|
|
||||||
private val groupId: Long = FeedGroupEntity.GROUP_ALL_ID
|
|
||||||
) : ViewModelProvider.Factory {
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
|
||||||
return FeedViewModel(
|
|
||||||
context.applicationContext,
|
context.applicationContext,
|
||||||
groupId,
|
groupId,
|
||||||
// Read initial value from preferences
|
// Read initial value from preferences
|
||||||
getShowPlayedItemsFromPreferences(context.applicationContext),
|
getShowPlayedItemsFromPreferences(context.applicationContext),
|
||||||
getShowFutureItemsFromPreferences(context.applicationContext)
|
getShowFutureItemsFromPreferences(context.applicationContext)
|
||||||
) as T
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue