diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchViewModel.kt b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchViewModel.kt new file mode 100644 index 00000000000..0e0a5f14f2c --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchViewModel.kt @@ -0,0 +1,99 @@ +package org.schabi.newpipe.fragments.list.search + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewmodel.initializer +import androidx.lifecycle.viewmodel.viewModelFactory +import org.schabi.newpipe.extractor.NewPipe +import org.schabi.newpipe.extractor.search.filter.FilterItem +import org.schabi.newpipe.fragments.list.search.filter.InjectFilterItem +import org.schabi.newpipe.fragments.list.search.filter.SearchFilterLogic +import org.schabi.newpipe.fragments.list.search.filter.SearchFilterLogic.Factory.Variant + +/** + * This class hosts the search filters logic. It facilitates + * the communication with the SearchFragment* and the *DialogFragment + * based search filter UI's + */ +class SearchViewModel( + val serviceId: Int, + logicVariant: Variant, + userSelectedContentFilterList: List, + userSelectedSortFilterList: List +) : ViewModel() { + + private val selectedContentFilterMutableLiveData: MutableLiveData> = + MutableLiveData() + private var selectedSortFilterLiveData: MutableLiveData> = + MutableLiveData() + private var userSelectedSortFilterListMutableLiveData: MutableLiveData> = + MutableLiveData() + private var userSelectedContentFilterListMutableLiveData: MutableLiveData> = + MutableLiveData() + private var doSearchMutableLiveData: MutableLiveData = MutableLiveData() + + val selectedContentFilterItemListLiveData: LiveData> + get() = selectedContentFilterMutableLiveData + val selectedSortFilterItemListLiveData: LiveData> + get() = selectedSortFilterLiveData + val userSelectedContentFilterListLiveData: LiveData> + get() = userSelectedContentFilterListMutableLiveData + val userSelectedSortFilterListLiveData: LiveData> + get() = userSelectedSortFilterListMutableLiveData + val doSearchLiveData: LiveData + get() = doSearchMutableLiveData + + var searchFilterLogic: SearchFilterLogic + + init { + // inject before creating SearchFilterLogic + InjectFilterItem.DividerBetweenYoutubeAndYoutubeMusic.run() + + searchFilterLogic = SearchFilterLogic.Factory.create( + logicVariant, + NewPipe.getService(serviceId).searchQHFactory, null + ) + searchFilterLogic.restorePreviouslySelectedFilters( + userSelectedContentFilterList, + userSelectedSortFilterList + ) + + searchFilterLogic.setCallback { userSelectedContentFilter: List, + userSelectedSortFilter: List -> + selectedContentFilterMutableLiveData.value = + userSelectedContentFilter as MutableList + selectedSortFilterLiveData.value = + userSelectedSortFilter as MutableList + userSelectedContentFilterListMutableLiveData.value = + searchFilterLogic.selectedContentFilters + userSelectedSortFilterListMutableLiveData.value = + searchFilterLogic.selectedSortFilters + + doSearchMutableLiveData.value = true + } + } + + fun weConsumedDoSearchLiveData() { + doSearchMutableLiveData.value = false + } + + companion object { + + fun getFactory( + serviceId: Int, + logicVariant: Variant, + userSelectedContentFilterList: ArrayList, + userSelectedSortFilterList: ArrayList + ) = viewModelFactory { + initializer { + SearchViewModel( + serviceId, + logicVariant, + userSelectedContentFilterList, + userSelectedSortFilterList + ) + } + } + } +}