perf: perform refresh in ui layer
Improve refresh performance
This commit is contained in:
parent
cc71af1402
commit
159bcb97e1
@ -141,12 +141,7 @@ class AppListFragment() : Fragment(), CursorOwner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
launch {
|
launch {
|
||||||
viewModel.sortOrderFlow.collect {
|
viewModel.state.collect {
|
||||||
updateRequest()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
launch {
|
|
||||||
viewModel.skipSignatureStream.collect {
|
|
||||||
updateRequest()
|
updateRequest()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -188,16 +183,12 @@ class AppListFragment() : Fragment(), CursorOwner.Callback {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun setSearchQuery(searchQuery: String) {
|
fun setSearchQuery(searchQuery: String) {
|
||||||
viewModel.setSearchQuery(searchQuery) {
|
viewModel.setSearchQuery(searchQuery)
|
||||||
updateRequest()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun setSection(section: ProductItem.Section) {
|
fun setSection(section: ProductItem.Section) {
|
||||||
viewModel.setSection(section) {
|
viewModel.setSection(section)
|
||||||
updateRequest()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateRequest() {
|
private fun updateRequest() {
|
||||||
|
@ -16,9 +16,9 @@ import com.looker.droidify.service.Connection
|
|||||||
import com.looker.droidify.service.SyncService
|
import com.looker.droidify.service.SyncService
|
||||||
import com.looker.droidify.utility.common.extension.asStateFlow
|
import com.looker.droidify.utility.common.extension.asStateFlow
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.flatMapConcat
|
import kotlinx.coroutines.flow.flatMapConcat
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -30,14 +30,32 @@ class AppListViewModel
|
|||||||
settingsRepository: SettingsRepository,
|
settingsRepository: SettingsRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
val skipSignatureStream = settingsRepository
|
private val skipSignatureStream = settingsRepository
|
||||||
.get { ignoreSignature }
|
.get { ignoreSignature }
|
||||||
.asStateFlow(false)
|
.asStateFlow(false)
|
||||||
|
|
||||||
val sortOrderFlow = settingsRepository
|
private val sortOrderFlow = settingsRepository
|
||||||
.get { sortOrder }
|
.get { sortOrder }
|
||||||
.asStateFlow(SortOrder.UPDATED)
|
.asStateFlow(SortOrder.UPDATED)
|
||||||
|
|
||||||
|
private val sections = MutableStateFlow<ProductItem.Section>(All)
|
||||||
|
|
||||||
|
val searchQuery = MutableStateFlow("")
|
||||||
|
|
||||||
|
val state = combine(
|
||||||
|
skipSignatureStream,
|
||||||
|
sortOrderFlow,
|
||||||
|
sections,
|
||||||
|
searchQuery,
|
||||||
|
) { skipSignature, sortOrder, section, query ->
|
||||||
|
AppListState(
|
||||||
|
searchQuery = query,
|
||||||
|
sections = section,
|
||||||
|
skipSignatureCheck = skipSignature,
|
||||||
|
sortOrder = sortOrder,
|
||||||
|
)
|
||||||
|
}.asStateFlow(AppListState())
|
||||||
|
|
||||||
val reposStream = Database.RepositoryAdapter
|
val reposStream = Database.RepositoryAdapter
|
||||||
.getAllStream()
|
.getAllStream()
|
||||||
.asStateFlow(emptyList())
|
.asStateFlow(emptyList())
|
||||||
@ -49,10 +67,6 @@ class AppListViewModel
|
|||||||
.map { it.isNotEmpty() }
|
.map { it.isNotEmpty() }
|
||||||
}.asStateFlow(false)
|
}.asStateFlow(false)
|
||||||
|
|
||||||
private val sections = MutableStateFlow<ProductItem.Section>(All)
|
|
||||||
|
|
||||||
val searchQuery = MutableStateFlow("")
|
|
||||||
|
|
||||||
val syncConnection = Connection(SyncService::class.java)
|
val syncConnection = Connection(SyncService::class.java)
|
||||||
|
|
||||||
fun updateAll() {
|
fun updateAll() {
|
||||||
@ -84,21 +98,22 @@ class AppListViewModel
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setSection(newSection: ProductItem.Section, perform: () -> Unit) {
|
fun setSection(newSection: ProductItem.Section) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
if (newSection != sections.value) {
|
|
||||||
sections.emit(newSection)
|
sections.emit(newSection)
|
||||||
launch(Dispatchers.Main) { perform() }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setSearchQuery(newSearchQuery: String) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
searchQuery.emit(newSearchQuery)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setSearchQuery(newSearchQuery: String, perform: () -> Unit) {
|
data class AppListState(
|
||||||
viewModelScope.launch {
|
val searchQuery: String = "",
|
||||||
if (newSearchQuery != searchQuery.value) {
|
val sections: ProductItem.Section = All,
|
||||||
searchQuery.emit(newSearchQuery)
|
val skipSignatureCheck: Boolean = false,
|
||||||
launch(Dispatchers.Main) { perform() }
|
val sortOrder: SortOrder = SortOrder.UPDATED,
|
||||||
}
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user