perf: perform refresh in ui layer

Improve refresh performance
This commit is contained in:
LooKeR 2025-06-08 18:24:04 +05:30
parent cc71af1402
commit 159bcb97e1
No known key found for this signature in database
GPG Key ID: 6B59369FDB608FB9
2 changed files with 37 additions and 31 deletions

View File

@ -141,12 +141,7 @@ class AppListFragment() : Fragment(), CursorOwner.Callback {
}
}
launch {
viewModel.sortOrderFlow.collect {
updateRequest()
}
}
launch {
viewModel.skipSignatureStream.collect {
viewModel.state.collect {
updateRequest()
}
}
@ -188,16 +183,12 @@ class AppListFragment() : Fragment(), CursorOwner.Callback {
}
}
internal fun setSearchQuery(searchQuery: String) {
viewModel.setSearchQuery(searchQuery) {
updateRequest()
}
fun setSearchQuery(searchQuery: String) {
viewModel.setSearchQuery(searchQuery)
}
internal fun setSection(section: ProductItem.Section) {
viewModel.setSection(section) {
updateRequest()
}
fun setSection(section: ProductItem.Section) {
viewModel.setSection(section)
}
private fun updateRequest() {

View File

@ -16,9 +16,9 @@ import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService
import com.looker.droidify.utility.common.extension.asStateFlow
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@ -30,14 +30,32 @@ class AppListViewModel
settingsRepository: SettingsRepository,
) : ViewModel() {
val skipSignatureStream = settingsRepository
private val skipSignatureStream = settingsRepository
.get { ignoreSignature }
.asStateFlow(false)
val sortOrderFlow = settingsRepository
private val sortOrderFlow = settingsRepository
.get { sortOrder }
.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
.getAllStream()
.asStateFlow(emptyList())
@ -49,10 +67,6 @@ class AppListViewModel
.map { it.isNotEmpty() }
}.asStateFlow(false)
private val sections = MutableStateFlow<ProductItem.Section>(All)
val searchQuery = MutableStateFlow("")
val syncConnection = Connection(SyncService::class.java)
fun updateAll() {
@ -84,21 +98,22 @@ class AppListViewModel
}
}
fun setSection(newSection: ProductItem.Section, perform: () -> Unit) {
fun setSection(newSection: ProductItem.Section) {
viewModelScope.launch {
if (newSection != sections.value) {
sections.emit(newSection)
launch(Dispatchers.Main) { perform() }
}
sections.emit(newSection)
}
}
fun setSearchQuery(newSearchQuery: String, perform: () -> Unit) {
fun setSearchQuery(newSearchQuery: String) {
viewModelScope.launch {
if (newSearchQuery != searchQuery.value) {
searchQuery.emit(newSearchQuery)
launch(Dispatchers.Main) { perform() }
}
searchQuery.emit(newSearchQuery)
}
}
}
data class AppListState(
val searchQuery: String = "",
val sections: ProductItem.Section = All,
val skipSignatureCheck: Boolean = false,
val sortOrder: SortOrder = SortOrder.UPDATED,
)