Skip to content

Commit

Permalink
[fix] Fix an issue that using the debounce operator on the download p…
Browse files Browse the repository at this point in the history
…age causes some information not to be updated
  • Loading branch information
SkyD666 committed Sep 14, 2024
1 parent c9a5a93 commit 327bdd2
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = 24
versionName = "2.1-beta04"
versionName = "2.1-beta05"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/skyd/anivu/base/mvi/MviIntent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import androidx.compose.runtime.remember
import com.skyd.anivu.ext.startWith
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/skyd/anivu/base/mvi/MviViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.skyd.anivu.base.mvi

import androidx.annotation.MainThread
import kotlinx.coroutines.MainCoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow

Expand All @@ -18,7 +19,7 @@ interface MviViewModel<I : MviIntent, S : MviViewState, E : MviSingleEvent> {
val singleEvent: Flow<E>

/**
* Must be called in [kotlinx.coroutines.Dispatchers.Main.immediate],
* Must be called in [MainCoroutineDispatcher.immediate],
* otherwise it will throw an exception.
*
* If you want to process an intent from other [kotlinx.coroutines.CoroutineDispatcher],
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/skyd/anivu/ext/FlowExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
Expand All @@ -21,6 +20,7 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.produceIn
import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -87,6 +87,6 @@ fun <T> Flow<T>.collectIn(
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action)
}

fun <T> Flow<T>.debounceWithoutFirst(timeoutMillis: Long) = merge(
take(1), drop(1).debounce(timeoutMillis)
fun <T> Flow<T>.sampleWithoutFirst(timeoutMillis: Long) = merge(
take(1), drop(1).sample(timeoutMillis)
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.parcelize.Parcelize
import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException

@Parcelize
sealed class ArticleSort(open val asc: Boolean) : Parcelable {
Expand Down Expand Up @@ -105,8 +106,10 @@ class ArticleRepository @Inject constructor(
feedDao.updateFeed(feedWithArticle.feed)
}?.articles
}.onFailure { e ->
e.printStackTrace()
(feedUrl + "\n" + e.message).showToast()
if (e !is CancellationException) {
e.printStackTrace()
(feedUrl + "\n" + e.message).showToast()
}
}.getOrNull()
}
val articleBeanList = articleBeanListAsync.await() ?: return@async
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.skyd.anivu.model.repository.download

import androidx.work.WorkManager
import com.skyd.anivu.appContext
import com.skyd.anivu.ext.debounceWithoutFirst
import com.skyd.anivu.ext.sampleWithoutFirst
import com.skyd.anivu.model.bean.download.DownloadInfoBean
import com.skyd.anivu.model.bean.download.DownloadInfoBean.DownloadState
import com.skyd.anivu.model.bean.download.DownloadLinkUuidMapBean
Expand All @@ -21,8 +21,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.launchIn
Expand Down Expand Up @@ -62,7 +60,7 @@ object DownloadManager {
private fun Flow<DownloadManagerIntent>.onEachIntent(): Flow<DownloadManagerIntent> {
return merge(
filterIsInstance<DownloadManagerIntent.UpdateDownloadInfo>()
.debounceWithoutFirst(100)
.sampleWithoutFirst(100)
.onEach { intent ->
downloadInfoDao.updateDownloadInfo(intent.downloadInfoBean)
putDownloadInfoToMap(
Expand All @@ -82,7 +80,7 @@ object DownloadManager {
}.catch { it.printStackTrace() },

filterIsInstance<DownloadManagerIntent.UpdateDownloadProgress>()
.debounceWithoutFirst(1000)
.sampleWithoutFirst(1000)
.onEach { intent ->
val result = downloadInfoDao.updateDownloadProgress(
link = intent.link,
Expand All @@ -105,7 +103,7 @@ object DownloadManager {
}.catch { it.printStackTrace() },

filterIsInstance<DownloadManagerIntent.UpdateDownloadSize>()
.debounceWithoutFirst(1000)
.sampleWithoutFirst(1000)
.onEach { intent ->
val result = downloadInfoDao.updateDownloadSize(
link = intent.link, size = intent.size,
Expand All @@ -116,7 +114,7 @@ object DownloadManager {
}.catch { it.printStackTrace() },

filterIsInstance<DownloadManagerIntent.UpdateDownloadName>()
.debounceWithoutFirst(200)
.sampleWithoutFirst(200)
.onEach { intent ->
if (intent.name.isNullOrBlank()) return@onEach
val result = downloadInfoDao.updateDownloadName(
Expand Down Expand Up @@ -146,7 +144,7 @@ object DownloadManager {
.catch { it.printStackTrace() },

filterIsInstance<DownloadManagerIntent.UpdateDownloadDescription>()
.debounceWithoutFirst(500)
.sampleWithoutFirst(500)
.onEach { intent ->
val result = downloadInfoDao.updateDownloadDescription(
link = intent.link,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.skyd.anivu.model.repository.download

import com.skyd.anivu.base.BaseRepository
import com.skyd.anivu.config.Const
import com.skyd.anivu.ext.debounceWithoutFirst
import com.skyd.anivu.ext.sampleWithoutFirst
import com.skyd.anivu.model.bean.download.DownloadInfoBean
import com.skyd.anivu.model.worker.download.DownloadTorrentWorker
import kotlinx.coroutines.Dispatchers
Expand All @@ -18,8 +18,8 @@ class DownloadRepository @Inject constructor() : BaseRepository() {
suspend fun requestDownloadingVideos(): Flow<List<DownloadInfoBean>> {
return combine(
DownloadManager.getDownloadInfoList().distinctUntilChanged(),
DownloadTorrentWorker.peerInfoMapFlow.debounceWithoutFirst(1000),
DownloadTorrentWorker.torrentStatusMapFlow.debounceWithoutFirst(1000),
DownloadTorrentWorker.peerInfoMapFlow.sampleWithoutFirst(1000),
DownloadTorrentWorker.torrentStatusMapFlow.sampleWithoutFirst(1000),
) { list, peerInfoMap, uploadPayloadRateMap ->
list.map { downloadInfoBean ->
downloadInfoBean.copy().apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.skyd.anivu.ui.screen.feed

import androidx.activity.compose.BackHandler
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.skyd.anivu.ui.screen.feed.item
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.skyd.anivu.ui.screen.feed.reorder

import com.skyd.anivu.model.bean.GroupVo
import com.skyd.anivu.ui.screen.feed.reorder.ReorderGroupPartialStateChange.GroupList.Success


internal sealed interface ReorderGroupPartialStateChange {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.skyd.anivu.ui.screen.feed.requestheaders

import com.skyd.anivu.model.bean.FeedBean
import com.skyd.anivu.ui.screen.feed.reorder.ReorderGroupPartialStateChange.GroupList.Success


internal sealed interface RequestHeadersPartialStateChange {
Expand Down

0 comments on commit 327bdd2

Please sign in to comment.