|
| 1 | +package org.schabi.newpipe.fragments.list.search |
| 2 | + |
| 3 | +import androidx.lifecycle.LiveData |
| 4 | +import androidx.lifecycle.MutableLiveData |
| 5 | +import androidx.lifecycle.ViewModel |
| 6 | +import androidx.lifecycle.viewmodel.initializer |
| 7 | +import androidx.lifecycle.viewmodel.viewModelFactory |
| 8 | +import org.schabi.newpipe.extractor.NewPipe |
| 9 | +import org.schabi.newpipe.extractor.search.filter.FilterItem |
| 10 | +import org.schabi.newpipe.fragments.list.search.filter.InjectFilterItem |
| 11 | +import org.schabi.newpipe.fragments.list.search.filter.SearchFilterLogic |
| 12 | +import org.schabi.newpipe.fragments.list.search.filter.SearchFilterLogic.Factory.Variant |
| 13 | + |
| 14 | +/** |
| 15 | + * This class hosts the search filters logic. It facilitates |
| 16 | + * the communication with the SearchFragment* and the *DialogFragment |
| 17 | + * based search filter UI's |
| 18 | + */ |
| 19 | +class SearchViewModel( |
| 20 | + val serviceId: Int, |
| 21 | + logicVariant: Variant, |
| 22 | + userSelectedContentFilterList: List<Int>, |
| 23 | + userSelectedSortFilterList: List<Int> |
| 24 | +) : ViewModel() { |
| 25 | + |
| 26 | + private val selectedContentFilterMutableLiveData: MutableLiveData<MutableList<FilterItem>> = |
| 27 | + MutableLiveData() |
| 28 | + private var selectedSortFilterLiveData: MutableLiveData<MutableList<FilterItem>> = |
| 29 | + MutableLiveData() |
| 30 | + private var userSelectedSortFilterListMutableLiveData: MutableLiveData<ArrayList<Int>> = |
| 31 | + MutableLiveData() |
| 32 | + private var userSelectedContentFilterListMutableLiveData: MutableLiveData<ArrayList<Int>> = |
| 33 | + MutableLiveData() |
| 34 | + private var doSearchMutableLiveData: MutableLiveData<Boolean> = MutableLiveData() |
| 35 | + |
| 36 | + val selectedContentFilterItemListLiveData: LiveData<MutableList<FilterItem>> |
| 37 | + get() = selectedContentFilterMutableLiveData |
| 38 | + val selectedSortFilterItemListLiveData: LiveData<MutableList<FilterItem>> |
| 39 | + get() = selectedSortFilterLiveData |
| 40 | + val userSelectedContentFilterListLiveData: LiveData<ArrayList<Int>> |
| 41 | + get() = userSelectedContentFilterListMutableLiveData |
| 42 | + val userSelectedSortFilterListLiveData: LiveData<ArrayList<Int>> |
| 43 | + get() = userSelectedSortFilterListMutableLiveData |
| 44 | + val doSearchLiveData: LiveData<Boolean> |
| 45 | + get() = doSearchMutableLiveData |
| 46 | + |
| 47 | + var searchFilterLogic: SearchFilterLogic |
| 48 | + |
| 49 | + init { |
| 50 | + // inject before creating SearchFilterLogic |
| 51 | + InjectFilterItem.DividerBetweenYoutubeAndYoutubeMusic.run() |
| 52 | + |
| 53 | + searchFilterLogic = SearchFilterLogic.Factory.create( |
| 54 | + logicVariant, |
| 55 | + NewPipe.getService(serviceId).searchQHFactory, null |
| 56 | + ) |
| 57 | + searchFilterLogic.restorePreviouslySelectedFilters( |
| 58 | + userSelectedContentFilterList, |
| 59 | + userSelectedSortFilterList |
| 60 | + ) |
| 61 | + |
| 62 | + searchFilterLogic.setCallback { userSelectedContentFilter: List<FilterItem?>, |
| 63 | + userSelectedSortFilter: List<FilterItem?> -> |
| 64 | + selectedContentFilterMutableLiveData.value = |
| 65 | + userSelectedContentFilter as MutableList<FilterItem> |
| 66 | + selectedSortFilterLiveData.value = |
| 67 | + userSelectedSortFilter as MutableList<FilterItem> |
| 68 | + userSelectedContentFilterListMutableLiveData.value = |
| 69 | + searchFilterLogic.selectedContentFilters |
| 70 | + userSelectedSortFilterListMutableLiveData.value = |
| 71 | + searchFilterLogic.selectedSortFilters |
| 72 | + |
| 73 | + doSearchMutableLiveData.value = true |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + fun weConsumedDoSearchLiveData() { |
| 78 | + doSearchMutableLiveData.value = false |
| 79 | + } |
| 80 | + |
| 81 | + companion object { |
| 82 | + |
| 83 | + fun getFactory( |
| 84 | + serviceId: Int, |
| 85 | + logicVariant: Variant, |
| 86 | + userSelectedContentFilterList: ArrayList<Int>, |
| 87 | + userSelectedSortFilterList: ArrayList<Int> |
| 88 | + ) = viewModelFactory { |
| 89 | + initializer { |
| 90 | + SearchViewModel( |
| 91 | + serviceId, |
| 92 | + logicVariant, |
| 93 | + userSelectedContentFilterList, |
| 94 | + userSelectedSortFilterList |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments