Properly modify StateFlow value (#7059)

(cherry picked from commit ed8a54bd2ae58f9fb58d1d92318b5dcb4c7ce3f5)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/SourcePresenter.kt
This commit is contained in:
jobobby04 2022-05-02 08:31:50 -04:00 committed by Jobobby04
parent c6c6cb3033
commit 1c7a81b7ad
6 changed files with 22 additions and 25 deletions

View File

@ -26,10 +26,10 @@ class MigrationMangaPresenter(
getFavoritesBySourceId
.subscribe(sourceId)
.catch { exception ->
_state.emit(MigrateMangaState.Error(exception))
_state.value = MigrateMangaState.Error(exception)
}
.collectLatest { list ->
_state.emit(MigrateMangaState.Success(list))
_state.value = MigrateMangaState.Success(list)
}
}
}

View File

@ -28,10 +28,10 @@ class MigrationSourcesPresenter(
presenterScope.launchIO {
getSourcesWithFavoriteCount.subscribe()
.catch { exception ->
_state.emit(MigrateSourceState.Error(exception))
_state.value = MigrateSourceState.Error(exception)
}
.collectLatest { sources ->
_state.emit(MigrateSourceState.Success(sources))
_state.value = MigrateSourceState.Success(sources)
}
}
}

View File

@ -35,11 +35,11 @@ class SourceFilterPresenter(
presenterScope.launchIO {
getLanguagesWithSources.subscribe()
.catch { exception ->
_state.emit(SourceFilterState.Error(exception))
_state.value = SourceFilterState.Error(exception)
}
.collectLatest { sourceLangMap ->
val uiModels = sourceLangMap.toFilterUiModels()
_state.emit(SourceFilterState.Success(uiModels))
_state.value = SourceFilterState.Success(uiModels)
}
}
}

View File

@ -56,7 +56,7 @@ class SourcePresenter(
::collectLatestSources
)
.catch { exception ->
_state.emit(SourceState.Error(exception))
_state.value = SourceState.Error(exception)
}
.flowOn(Dispatchers.IO)
.launchIn(presenterScope)
@ -95,13 +95,11 @@ class SourcePresenter(
}.toTypedArray(),
)
}
_state.emit(
SourceState.Success(
uiModels,
categories.sortedWith(compareByDescending(String.CASE_INSENSITIVE_ORDER) { it }),
showLatest,
showPin
)
_state.value = SourceState.Success(
uiModels,
categories.sortedWith(compareByDescending(String.CASE_INSENSITIVE_ORDER) { it }),
showLatest,
showPin
)
}

View File

@ -8,6 +8,7 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.lang.launchIO
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
@ -29,7 +30,7 @@ class MorePresenter(
// SY <--
private var _state: MutableStateFlow<DownloadQueueState> = MutableStateFlow(DownloadQueueState.Stopped)
val downloadQueueState: StateFlow<DownloadQueueState> = _state
val downloadQueueState: StateFlow<DownloadQueueState> = _state.asStateFlow()
private var isDownloading: Boolean = false
private var downloadQueueSize: Int = 0
@ -71,14 +72,12 @@ class MorePresenter(
private fun updateDownloadQueueState() {
presenterScope.launchIO {
val pendingDownloadExists = downloadQueueSize != 0
_state.emit(
when {
!pendingDownloadExists -> DownloadQueueState.Stopped
!isDownloading && !pendingDownloadExists -> DownloadQueueState.Paused(0)
!isDownloading && pendingDownloadExists -> DownloadQueueState.Paused(downloadQueueSize)
else -> DownloadQueueState.Downloading(downloadQueueSize)
}
)
_state.value = when {
!pendingDownloadExists -> DownloadQueueState.Stopped
!isDownloading && !pendingDownloadExists -> DownloadQueueState.Paused(0)
!isDownloading && pendingDownloadExists -> DownloadQueueState.Paused(downloadQueueSize)
else -> DownloadQueueState.Downloading(downloadQueueSize)
}
}
}

View File

@ -53,14 +53,14 @@ class HistoryPresenter(
_query.collectLatest { query ->
getHistory.subscribe(query)
.catch { exception ->
_state.emit(HistoryState.Error(exception))
_state.value = HistoryState.Error(exception)
}
.map { pagingData ->
pagingData.toHistoryUiModels()
}
.cachedIn(presenterScope)
.let { uiModelsPagingDataFlow ->
_state.emit(HistoryState.Success(uiModelsPagingDataFlow))
_state.value = HistoryState.Success(uiModelsPagingDataFlow)
}
}
}