mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-24 03:42:15 +00:00
Update: Revamp the Request class and add two new subclasses (ProductsUpdated, ProductsNew)
This commit is contained in:
parent
cb56f9b053
commit
251b6195d6
@ -48,33 +48,70 @@ enum class Source(val titleResId: Int, val sections: Boolean, val order: Boolean
|
|||||||
|
|
||||||
sealed class Request {
|
sealed class Request {
|
||||||
internal abstract val id: Int
|
internal abstract val id: Int
|
||||||
|
internal abstract val installed: Boolean
|
||||||
|
internal abstract val updates: Boolean
|
||||||
|
internal abstract val searchQuery: String
|
||||||
|
internal abstract val section: ProductItem.Section
|
||||||
|
internal abstract val order: ProductItem.Order
|
||||||
|
internal open val numberOfItems: Int = 0
|
||||||
|
|
||||||
data class ProductsAvailable(
|
data class ProductsAll(
|
||||||
val searchQuery: String, val section: ProductItem.Section,
|
override val searchQuery: String, override val section: ProductItem.Section,
|
||||||
val order: ProductItem.Order,
|
override val order: ProductItem.Order,
|
||||||
) : Request() {
|
) : Request() {
|
||||||
override val id: Int
|
override val id: Int
|
||||||
get() = 1
|
get() = 1
|
||||||
|
override val installed: Boolean
|
||||||
|
get() = false
|
||||||
|
override val updates: Boolean
|
||||||
|
get() = false
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ProductsInstalled(
|
data class ProductsInstalled(
|
||||||
val searchQuery: String, val section: ProductItem.Section,
|
override val searchQuery: String, override val section: ProductItem.Section,
|
||||||
val order: ProductItem.Order,
|
override val order: ProductItem.Order,
|
||||||
) : Request() {
|
) : Request() {
|
||||||
override val id: Int
|
override val id: Int
|
||||||
get() = 2
|
get() = 2
|
||||||
|
override val installed: Boolean
|
||||||
|
get() = true
|
||||||
|
override val updates: Boolean
|
||||||
|
get() = false
|
||||||
}
|
}
|
||||||
|
|
||||||
data class ProductsUpdates(
|
data class ProductsUpdates(
|
||||||
val searchQuery: String, val section: ProductItem.Section,
|
override val searchQuery: String, override val section: ProductItem.Section,
|
||||||
val order: ProductItem.Order,
|
override val order: ProductItem.Order,
|
||||||
) : Request() {
|
) : Request() {
|
||||||
override val id: Int
|
override val id: Int
|
||||||
get() = 3
|
get() = 3
|
||||||
|
override val installed: Boolean
|
||||||
|
get() = true
|
||||||
|
override val updates: Boolean
|
||||||
|
get() = true
|
||||||
}
|
}
|
||||||
|
|
||||||
object Repositories : Request() {
|
data class ProductsUpdated(
|
||||||
|
override val searchQuery: String, override val section: ProductItem.Section,
|
||||||
|
override val order: ProductItem.Order, override val numberOfItems: Int,
|
||||||
|
) : Request() {
|
||||||
override val id: Int
|
override val id: Int
|
||||||
get() = 4
|
get() = 4
|
||||||
|
override val installed: Boolean
|
||||||
|
get() = false
|
||||||
|
override val updates: Boolean
|
||||||
|
get() = false
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ProductsNew(
|
||||||
|
override val searchQuery: String, override val section: ProductItem.Section,
|
||||||
|
override val order: ProductItem.Order, override val numberOfItems: Int,
|
||||||
|
) : Request() {
|
||||||
|
override val id: Int
|
||||||
|
get() = 5
|
||||||
|
override val installed: Boolean
|
||||||
|
get() = false
|
||||||
|
override val updates: Boolean
|
||||||
|
get() = false
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -50,7 +50,7 @@ class MainNavFragmentViewModelX(val db: DatabaseX) : ViewModel() {
|
|||||||
launch { order.collect { if (source.order) mOrder = it } }
|
launch { order.collect { if (source.order) mOrder = it } }
|
||||||
}
|
}
|
||||||
return when (source) {
|
return when (source) {
|
||||||
Source.AVAILABLE -> Request.ProductsAvailable(
|
Source.AVAILABLE -> Request.ProductsAll(
|
||||||
mSearchQuery,
|
mSearchQuery,
|
||||||
mSections,
|
mSections,
|
||||||
mOrder
|
mOrder
|
||||||
@ -77,34 +77,16 @@ class MainNavFragmentViewModelX(val db: DatabaseX) : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun query(request: Request): List<Product>? {
|
private suspend fun query(request: Request): List<Product>? {
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.Default) {
|
||||||
when (request) {
|
db.productDao
|
||||||
is Request.ProductsAvailable -> db.productDao
|
.queryList(
|
||||||
.queryList(
|
installed = request.installed,
|
||||||
installed = false,
|
updates = request.updates,
|
||||||
updates = false,
|
searchQuery = request.searchQuery,
|
||||||
searchQuery = request.searchQuery,
|
section = request.section,
|
||||||
section = request.section,
|
order = request.order,
|
||||||
order = request.order
|
numberOfItems = request.numberOfItems
|
||||||
)
|
)
|
||||||
is Request.ProductsInstalled -> db.productDao
|
|
||||||
.queryList(
|
|
||||||
installed = true,
|
|
||||||
updates = false,
|
|
||||||
searchQuery = request.searchQuery,
|
|
||||||
section = request.section,
|
|
||||||
order = request.order
|
|
||||||
)
|
|
||||||
is Request.ProductsUpdates -> db.productDao
|
|
||||||
.queryList(
|
|
||||||
installed = true,
|
|
||||||
updates = true,
|
|
||||||
searchQuery = request.searchQuery,
|
|
||||||
section = request.section,
|
|
||||||
order = request.order
|
|
||||||
)
|
|
||||||
else -> listOf()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user