mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-06-16 04:19:19 +00:00
Add: ViewModel for ProductsFragment
This commit is contained in:
47
src/main/kotlin/com/looker/droidify/ui/ProductsViewModel.kt
Normal file
47
src/main/kotlin/com/looker/droidify/ui/ProductsViewModel.kt
Normal file
@ -0,0 +1,47 @@
|
||||
package com.looker.droidify.ui
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.looker.droidify.entity.ProductItem
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ProductsViewModel : ViewModel() {
|
||||
|
||||
private val _order = MutableStateFlow(ProductItem.Order.LAST_UPDATE)
|
||||
private val _sections = MutableStateFlow<ProductItem.Section>(ProductItem.Section.All)
|
||||
private val _searchQuery = MutableStateFlow("")
|
||||
|
||||
val order: StateFlow<ProductItem.Order> = _order
|
||||
val sections: StateFlow<ProductItem.Section> = _sections
|
||||
val searchQuery: StateFlow<String> = _searchQuery
|
||||
|
||||
fun setSection(newSection: ProductItem.Section, perform: () -> Unit) {
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
if (newSection != sections.value) {
|
||||
_sections.emit(newSection)
|
||||
perform()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setOrder(newOrder: ProductItem.Order, perform: () -> Unit) {
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
if (newOrder != order.value) {
|
||||
_order.emit(newOrder)
|
||||
perform()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setSearchQuery(newSearchQuery: String, perform: () -> Unit) {
|
||||
viewModelScope.launch(Dispatchers.Main) {
|
||||
if (newSearchQuery != searchQuery.value) {
|
||||
_searchQuery.emit(newSearchQuery)
|
||||
perform()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user