From dc7768f0d10b578a7ecd3a64df26bdf942ca2e20 Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Fri, 16 Feb 2024 14:48:04 +0100 Subject: [PATCH] solve performance issue --- .../screens/about/LogsViewModelImpl.kt | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModelImpl.kt b/app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModelImpl.kt index 4e7ce2026f..54dadd083d 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModelImpl.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/screens/about/LogsViewModelImpl.kt @@ -10,10 +10,13 @@ import de.westnordost.streetcomplete.util.ktx.toLocalDate import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.transformLatest import kotlinx.coroutines.plus @@ -24,15 +27,9 @@ class LogsViewModelImpl( private val logsController: LogsController, ) : LogsViewModel() { - override val filters: MutableStateFlow = - MutableStateFlow( - LogsFilters( - timestampNewerThan = LocalDateTime( - systemTimeNow().toLocalDate(), - LocalTime(0, 0, 0) - ) - ) - ) + override val filters = MutableStateFlow(LogsFilters( + timestampNewerThan = LocalDateTime(systemTimeNow().toLocalDate(), LocalTime(0, 0, 0)) + )) /** * Produce a call back flow of all incoming logs matching the given [filters]. @@ -55,21 +52,28 @@ class LogsViewModelImpl( } @OptIn(ExperimentalCoroutinesApi::class) - override val logs: StateFlow> = + private val _logs: SharedFlow> = filters.transformLatest { filters -> // get prior logs into a backing state // There will be duplication regardless. - var logs: List = logsController.getLogs(filters) + val logs = logsController.getLogs(filters).toMutableList() // emit the logs for the first view emit(logs) // start listening to new logs getIncomingLogs(filters).collect { - logs = logs + it + logs.add(it) emit(logs) } - }.stateIn(viewModelScope + Dispatchers.IO, SharingStarted.Eagerly, emptyList()) + }.shareIn(viewModelScope + Dispatchers.IO, SharingStarted.Eagerly, 1) + + + override val logs: StateFlow> = object : + StateFlow>, + SharedFlow> by _logs { + override val value: List get() = replayCache[0] + } } private fun LogsController.getLogs(filters: LogsFilters) =