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 package com.looker.droidify.database
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.* import androidx.room.*
import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery
@ -9,6 +8,7 @@ import com.looker.droidify.*
import com.looker.droidify.database.entity.* import com.looker.droidify.database.entity.*
import com.looker.droidify.entity.Order import com.looker.droidify.entity.Order
import com.looker.droidify.entity.Section import com.looker.droidify.entity.Section
import com.looker.droidify.ui.fragments.Request
interface BaseDao<T> { interface BaseDao<T> {
@Insert @Insert
@ -79,6 +79,15 @@ interface ProductDao : BaseDao<Product> {
@RawQuery @RawQuery
fun queryObject(query: SupportSQLiteQuery): List<Product> 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 @Transaction
fun queryObject( fun queryObject(
installed: Boolean, updates: Boolean, searchQuery: String, installed: Boolean, updates: Boolean, searchQuery: String,
@ -90,11 +99,15 @@ interface ProductDao : BaseDao<Product> {
@RawQuery(observedEntities = [Product::class]) @RawQuery(observedEntities = [Product::class])
fun queryLiveList(query: SupportSQLiteQuery): LiveData<List<Product>> fun queryLiveList(query: SupportSQLiteQuery): LiveData<List<Product>>
fun queryLiveList( fun queryLiveList(request: Request): LiveData<List<Product>> = queryLiveList(
installed: Boolean, updates: Boolean, searchQuery: String, buildProductQuery(
section: Section, order: Order, numberOfItems: Int = 0 request.installed,
): LiveData<List<Product>> = queryLiveList( request.updates,
buildProductQuery(installed, updates, searchQuery, section, order, numberOfItems) request.searchQuery,
request.section,
request.order,
request.numberOfItems
)
) )
// TODO add an UpdateCategory argument // TODO add an UpdateCategory argument
@ -189,7 +202,7 @@ interface ProductDao : BaseDao<Product> {
}::class }::class
builder += "product.${ROW_NAME} COLLATE LOCALIZED ASC${if (numberOfItems > 0) " LIMIT $numberOfItems" else ""}" 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.entity.Section
import com.looker.droidify.ui.fragments.Request import com.looker.droidify.ui.fragments.Request
import com.looker.droidify.ui.fragments.Source import com.looker.droidify.ui.fragments.Source
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class MainNavFragmentViewModelX( class MainNavFragmentViewModelX(
val db: DatabaseX, val db: DatabaseX,
primarySource: Source, primarySource: Source,
secondarySource: Source secondarySource: Source
) : ) : ViewModel() {
ViewModel() {
private val order = MutableLiveData(Order.LAST_UPDATE) private val order = MutableLiveData(Order.LAST_UPDATE)
private val sections = MutableLiveData<Section>(Section.All) 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>>() val primaryProducts = MediatorLiveData<List<Product>>()
private var secondaryRequest = request(secondarySource) private var secondaryRequest = request(secondarySource)
val secondaryProducts = MediatorLiveData<List<Product>>() val secondaryProducts = MediatorLiveData<List<Product>>()
@ -71,15 +72,31 @@ class MainNavFragmentViewModelX(
init { init {
primaryProducts.addSource( primaryProducts.addSource(
productsListMediator(primaryRequest), db.productDao.queryLiveList(primaryRequest.value ?: request(primarySource)),
primaryProducts::setValue primaryProducts::setValue
) )
secondaryProducts.addSource( secondaryProducts.addSource(
productsListMediator(secondaryRequest), db.productDao.queryLiveList(secondaryRequest),
secondaryProducts::setValue secondaryProducts::setValue
) )
repositories.addSource(db.repositoryDao.allLive, repositories::setValue) repositories.addSource(db.repositoryDao.allLive, repositories::setValue)
categories.addSource(db.categoryDao.allNamesLive, categories::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) { 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( class Factory(
val db: DatabaseX, val db: DatabaseX,
private val primarySource: Source, private val primarySource: Source,