mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Update: Search query, section & order logic in MainNavVM. Remove: PagedList usage in MainNavVM.
This commit is contained in:
parent
cfa68524f3
commit
1fdc74731e
@ -87,6 +87,16 @@ interface ProductDao : BaseDao<Product> {
|
||||
buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems)
|
||||
)
|
||||
|
||||
@RawQuery(observedEntities = [Product::class])
|
||||
fun queryLiveList(query: SupportSQLiteQuery): LiveData<List<Product>>
|
||||
|
||||
fun queryLiveList(
|
||||
installed: Boolean, updates: Boolean, searchQuery: String,
|
||||
section: Section, order: Order, numberOfItems: Int = 0
|
||||
): LiveData<List<Product>> = queryLiveList(
|
||||
buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems)
|
||||
)
|
||||
|
||||
@RawQuery(observedEntities = [Product::class])
|
||||
fun queryList(
|
||||
query: SupportSQLiteQuery
|
||||
|
@ -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>(Section.All)
|
||||
private val _searchQuery = MutableStateFlow("")
|
||||
|
||||
val order: StateFlow<Order> = _order.stateIn(
|
||||
initialValue = Order.LAST_UPDATE,
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000)
|
||||
)
|
||||
|
||||
val sections: StateFlow<Section> = _sections.stateIn(
|
||||
initialValue = Section.All,
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000)
|
||||
)
|
||||
val searchQuery: StateFlow<String> = _searchQuery.stateIn(
|
||||
initialValue = "",
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000)
|
||||
)
|
||||
private val order = MutableLiveData(Order.LAST_UPDATE)
|
||||
private val sections = MutableLiveData<Section>(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<PagedList<Product>> 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<PagedList<Product>> 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<List<Product>>()
|
||||
private var secondaryRequest = request(secondarySource)
|
||||
val secondaryProducts = MediatorLiveData<List<Product>>()
|
||||
|
||||
val repositories = MediatorLiveData<List<Repository>>()
|
||||
|
||||
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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user