From 1fdc74731e9d5737c61e5db15ecf8c284b957a74 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Thu, 17 Feb 2022 23:59:23 +0100 Subject: [PATCH] Update: Search query, section & order logic in MainNavVM. Remove: PagedList usage in MainNavVM. --- .../com/looker/droidify/database/DAOs.kt | 10 ++ .../viewmodels/MainNavFragmentViewModelX.kt | 113 ++++++------------ 2 files changed, 48 insertions(+), 75 deletions(-) diff --git a/src/main/kotlin/com/looker/droidify/database/DAOs.kt b/src/main/kotlin/com/looker/droidify/database/DAOs.kt index 78c030e9..e585c7e8 100644 --- a/src/main/kotlin/com/looker/droidify/database/DAOs.kt +++ b/src/main/kotlin/com/looker/droidify/database/DAOs.kt @@ -87,6 +87,16 @@ interface ProductDao : BaseDao { buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems) ) + @RawQuery(observedEntities = [Product::class]) + fun queryLiveList(query: SupportSQLiteQuery): LiveData> + + fun queryLiveList( + installed: Boolean, updates: Boolean, searchQuery: String, + section: Section, order: Order, numberOfItems: Int = 0 + ): LiveData> = queryLiveList( + buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems) + ) + @RawQuery(observedEntities = [Product::class]) fun queryList( query: SupportSQLiteQuery diff --git a/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt b/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt index 653e8070..de495c5d 100644 --- a/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt +++ b/src/main/kotlin/com/looker/droidify/ui/viewmodels/MainNavFragmentViewModelX.kt @@ -1,8 +1,6 @@ package com.looker.droidify.ui.viewmodels import androidx.lifecycle.* -import androidx.paging.LivePagedListBuilder -import androidx.paging.PagedList import com.looker.droidify.content.Preferences import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Product @@ -11,46 +9,26 @@ import com.looker.droidify.entity.Order import com.looker.droidify.entity.Section import com.looker.droidify.ui.fragments.Request import com.looker.droidify.ui.fragments.Source -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.SharingStarted -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch -class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, secondarySource: Source) : +class MainNavFragmentViewModelX( + val db: DatabaseX, + primarySource: Source, + secondarySource: Source +) : ViewModel() { - private val _order = MutableStateFlow(Order.LAST_UPDATE) - private val _sections = MutableStateFlow
(Section.All) - private val _searchQuery = MutableStateFlow("") - - val order: StateFlow = _order.stateIn( - initialValue = Order.LAST_UPDATE, - scope = viewModelScope, - started = SharingStarted.WhileSubscribed(5000) - ) - - val sections: StateFlow
= _sections.stateIn( - initialValue = Section.All, - scope = viewModelScope, - started = SharingStarted.WhileSubscribed(5000) - ) - val searchQuery: StateFlow = _searchQuery.stateIn( - initialValue = "", - scope = viewModelScope, - started = SharingStarted.WhileSubscribed(5000) - ) + private val order = MutableLiveData(Order.LAST_UPDATE) + private val sections = MutableLiveData
(Section.All) + private val searchQuery = MutableLiveData("") fun request(source: Source): Request { var mSearchQuery = "" var mSections: Section = Section.All var mOrder: Order = Order.NAME - viewModelScope.launch { - launch { searchQuery.collect { if (source.sections) mSearchQuery = it } } - launch { sections.collect { if (source.sections) mSections = it } } - launch { order.collect { if (source.order) mOrder = it } } - } + sections.value?.let { if (source.sections) mSections = it } + order.value?.let { if (source.order) mOrder = it } + searchQuery.value?.let { if (source.sections) mSearchQuery = it } return when (source) { Source.AVAILABLE -> Request.ProductsAll( mSearchQuery, @@ -83,73 +61,58 @@ class MainNavFragmentViewModelX(val db: DatabaseX, primarySource: Source, second } } - private val pagedListConfig by lazy { - PagedList.Config.Builder() - .setPageSize(30) - .setPrefetchDistance(30) - .setEnablePlaceholders(false) - .build() - } - private val primaryRequest = request(primarySource) - val primaryProducts: LiveData> by lazy { - LivePagedListBuilder( - db.productDao.queryList( - installed = primaryRequest.installed, - updates = primaryRequest.updates, - searchQuery = primaryRequest.searchQuery, - section = primaryRequest.section, - order = primaryRequest.order, - numberOfItems = primaryRequest.numberOfItems - ), pagedListConfig - ).build() - } - private val secondaryRequest = request(secondarySource) - val secondaryProducts: LiveData> by lazy { - LivePagedListBuilder( - db.productDao.queryList( - installed = secondaryRequest.installed, - updates = secondaryRequest.updates, - searchQuery = secondaryRequest.searchQuery, - section = secondaryRequest.section, - order = secondaryRequest.order, - numberOfItems = secondaryRequest.numberOfItems - ), pagedListConfig - ).build() - } + private var primaryRequest = request(primarySource) + val primaryProducts = MediatorLiveData>() + private var secondaryRequest = request(secondarySource) + val secondaryProducts = MediatorLiveData>() val repositories = MediatorLiveData>() init { + primaryProducts.addSource( + productsListMediator(primaryRequest), + primaryProducts::setValue + ) + secondaryProducts.addSource( + productsListMediator(secondaryRequest), + secondaryProducts::setValue + ) repositories.addSource(db.repositoryDao.allLive, repositories::setValue) } - fun setSection(newSection: Section, perform: () -> Unit) { + fun setSection(newSection: Section) { viewModelScope.launch { if (newSection != sections.value) { - _sections.emit(newSection) - launch(Dispatchers.Main) { perform() } + sections.value = newSection } } } - fun setOrder(newOrder: Order, perform: () -> Unit) { + fun setSection(newOrder: Order) { viewModelScope.launch { if (newOrder != order.value) { - _order.emit(newOrder) - launch(Dispatchers.Main) { perform() } + order.value = newOrder } } } - fun setSearchQuery(newSearchQuery: String, perform: () -> Unit) { + fun setSection(newSearchQuery: String) { viewModelScope.launch { if (newSearchQuery != searchQuery.value) { - _searchQuery.emit(newSearchQuery) - launch(Dispatchers.Main) { perform() } + searchQuery.value = newSearchQuery } } } + private fun productsListMediator(request: Request) = db.productDao.queryLiveList( + installed = request.installed, + updates = request.updates, + searchQuery = request.searchQuery, + section = request.section, + order = request.order, + numberOfItems = request.numberOfItems + ) + class Factory( val db: DatabaseX, private val primarySource: Source,