mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Improve: Hold state on Device State Change
Improve: Dont emit after 5 seconds of onPause() (Fix Memory Leak & Potential Crashes)
This commit is contained in:
parent
8f6429cfc1
commit
99aae396b5
@ -50,19 +50,19 @@ class ProductsFragment() : BaseFragment(), CursorOwner.Callback {
|
|||||||
private val searchQuery: String
|
private val searchQuery: String
|
||||||
get() {
|
get() {
|
||||||
var _searchQuery = ""
|
var _searchQuery = ""
|
||||||
lifecycleScope.launchWhenStarted { viewModel.searchQuery.collect { _searchQuery = it } }
|
lifecycleScope.launchWhenCreated { viewModel.searchQuery.collect { _searchQuery = it } }
|
||||||
return _searchQuery
|
return _searchQuery
|
||||||
}
|
}
|
||||||
private val section: ProductItem.Section
|
private val section: ProductItem.Section
|
||||||
get() {
|
get() {
|
||||||
var _section: ProductItem.Section = ProductItem.Section.All
|
var _section: ProductItem.Section = ProductItem.Section.All
|
||||||
lifecycleScope.launchWhenStarted { viewModel.sections.collect { _section = it } }
|
lifecycleScope.launchWhenCreated { viewModel.sections.collect { _section = it } }
|
||||||
return _section
|
return _section
|
||||||
}
|
}
|
||||||
private val order: ProductItem.Order
|
private val order: ProductItem.Order
|
||||||
get() {
|
get() {
|
||||||
var _order: ProductItem.Order = ProductItem.Order.LAST_UPDATE
|
var _order: ProductItem.Order = ProductItem.Order.LAST_UPDATE
|
||||||
lifecycleScope.launchWhenStarted { viewModel.order.collect { _order = it } }
|
lifecycleScope.launchWhenCreated { viewModel.order.collect { _order = it } }
|
||||||
return _order
|
return _order
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ class ProductsFragment() : BaseFragment(), CursorOwner.Callback {
|
|||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?,
|
||||||
): View {
|
): View {
|
||||||
return RecyclerView(requireContext()).apply {
|
return RecyclerView(requireContext()).apply {
|
||||||
id = android.R.id.list
|
id = android.R.id.list
|
||||||
|
@ -5,7 +5,9 @@ import androidx.lifecycle.viewModelScope
|
|||||||
import com.looker.droidify.entity.ProductItem
|
import com.looker.droidify.entity.ProductItem
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class ProductsViewModel : ViewModel() {
|
class ProductsViewModel : ViewModel() {
|
||||||
@ -14,9 +16,21 @@ class ProductsViewModel : ViewModel() {
|
|||||||
private val _sections = MutableStateFlow<ProductItem.Section>(ProductItem.Section.All)
|
private val _sections = MutableStateFlow<ProductItem.Section>(ProductItem.Section.All)
|
||||||
private val _searchQuery = MutableStateFlow("")
|
private val _searchQuery = MutableStateFlow("")
|
||||||
|
|
||||||
val order: StateFlow<ProductItem.Order> = _order
|
val order: StateFlow<ProductItem.Order> = _order.stateIn(
|
||||||
val sections: StateFlow<ProductItem.Section> = _sections
|
initialValue = ProductItem.Order.LAST_UPDATE,
|
||||||
val searchQuery: StateFlow<String> = _searchQuery
|
scope = viewModelScope,
|
||||||
|
started = SharingStarted.WhileSubscribed(5000)
|
||||||
|
)
|
||||||
|
val sections: StateFlow<ProductItem.Section> = _sections.stateIn(
|
||||||
|
initialValue = ProductItem.Section.All,
|
||||||
|
scope = viewModelScope,
|
||||||
|
started = SharingStarted.WhileSubscribed(5000)
|
||||||
|
)
|
||||||
|
val searchQuery: StateFlow<String> = _searchQuery.stateIn(
|
||||||
|
initialValue = "",
|
||||||
|
scope = viewModelScope,
|
||||||
|
started = SharingStarted.WhileSubscribed(5000)
|
||||||
|
)
|
||||||
|
|
||||||
fun setSection(newSection: ProductItem.Section, perform: () -> Unit) {
|
fun setSection(newSection: ProductItem.Section, perform: () -> Unit) {
|
||||||
viewModelScope.launch(Dispatchers.Main) {
|
viewModelScope.launch(Dispatchers.Main) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user