Add: Categories filter in explore fragment

This commit is contained in:
machiav3lli 2022-02-20 02:30:33 +01:00
parent 5f923f70dd
commit 555bf1f14f
3 changed files with 52 additions and 21 deletions

View File

@ -1,7 +1,6 @@
package com.looker.droidify.database
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.*
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
@ -9,6 +8,7 @@ import com.looker.droidify.*
import com.looker.droidify.database.entity.*
import com.looker.droidify.entity.Order
import com.looker.droidify.entity.Section
import com.looker.droidify.ui.fragments.Request
interface BaseDao<T> {
@Insert
@ -79,6 +79,15 @@ interface ProductDao : BaseDao<Product> {
@RawQuery
fun queryObject(query: SupportSQLiteQuery): List<Product>
fun queryObject(request: Request): List<Product> = queryObject(
request.installed,
request.updates,
request.searchQuery,
request.section,
request.order,
request.numberOfItems
)
@Transaction
fun queryObject(
installed: Boolean, updates: Boolean, searchQuery: String,
@ -90,11 +99,15 @@ interface ProductDao : BaseDao<Product> {
@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)
fun queryLiveList(request: Request): LiveData<List<Product>> = queryLiveList(
buildProductQuery(
request.installed,
request.updates,
request.searchQuery,
request.section,
request.order,
request.numberOfItems
)
)
// TODO add an UpdateCategory argument
@ -189,7 +202,7 @@ interface ProductDao : BaseDao<Product> {
}::class
builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}"
return SimpleSQLiteQuery(builder.build(),builder.arguments.toTypedArray())
return SimpleSQLiteQuery(builder.build(), builder.arguments.toTypedArray())
}
}

View File

@ -74,5 +74,15 @@ class ExploreFragment : MainNavFragmentX() {
})
}
}
binding.categories.setOnCheckedChangeListener { group, checkedId ->
group.findViewById<Chip>(checkedId).let {
viewModel.setSection(
if (it.text.equals(getString(R.string.all_applications)))
Section.All
else
Section.Category(it.text.toString())
)
}
}
}
}

View File

@ -9,14 +9,15 @@ 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.launch
import kotlinx.coroutines.withContext
class MainNavFragmentViewModelX(
val db: DatabaseX,
primarySource: Source,
secondarySource: Source
) :
ViewModel() {
) : ViewModel() {
private val order = MutableLiveData(Order.LAST_UPDATE)
private val sections = MutableLiveData<Section>(Section.All)
@ -61,7 +62,7 @@ class MainNavFragmentViewModelX(
}
}
private var primaryRequest = request(primarySource)
private var primaryRequest = MediatorLiveData<Request>()
val primaryProducts = MediatorLiveData<List<Product>>()
private var secondaryRequest = request(secondarySource)
val secondaryProducts = MediatorLiveData<List<Product>>()
@ -71,15 +72,31 @@ class MainNavFragmentViewModelX(
init {
primaryProducts.addSource(
productsListMediator(primaryRequest),
db.productDao.queryLiveList(primaryRequest.value ?: request(primarySource)),
primaryProducts::setValue
)
secondaryProducts.addSource(
productsListMediator(secondaryRequest),
db.productDao.queryLiveList(secondaryRequest),
secondaryProducts::setValue
)
repositories.addSource(db.repositoryDao.allLive, repositories::setValue)
categories.addSource(db.categoryDao.allNamesLive, categories::setValue)
listOf(sections, order, searchQuery).forEach {
primaryRequest.addSource(it) {
primaryRequest.value = request(primarySource)
}
}
primaryProducts.addSource(primaryRequest) { request ->
viewModelScope.launch {
withContext(Dispatchers.IO) {
primaryProducts.postValue(
db.productDao.queryObject(
request
)
)
}
}
}
}
fun setSection(newSection: Section) {
@ -106,15 +123,6 @@ class MainNavFragmentViewModelX(
}
}
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,