|
19 | 19 | package com.saulhdev.feeder.db
|
20 | 20 |
|
21 | 21 | import android.content.Context
|
| 22 | +import androidx.work.WorkInfo |
22 | 23 | import androidx.work.WorkManager
|
23 | 24 | import com.saulhdev.feeder.db.dao.insertOrUpdate
|
24 | 25 | import com.saulhdev.feeder.db.models.Feed
|
25 | 26 | import com.saulhdev.feeder.db.models.FeedArticle
|
26 | 27 | import com.saulhdev.feeder.db.models.FeedItemIdWithLink
|
27 | 28 | import com.saulhdev.feeder.sdk.FeedItem
|
| 29 | +import com.saulhdev.feeder.sync.FeedSyncer |
28 | 30 | import kotlinx.coroutines.CoroutineName
|
29 | 31 | import kotlinx.coroutines.CoroutineScope
|
30 | 32 | import kotlinx.coroutines.Dispatchers
|
31 | 33 | import kotlinx.coroutines.ExperimentalCoroutinesApi
|
32 | 34 | import kotlinx.coroutines.flow.Flow
|
| 35 | +import kotlinx.coroutines.flow.SharingStarted |
| 36 | +import kotlinx.coroutines.flow.StateFlow |
33 | 37 | import kotlinx.coroutines.flow.combine
|
34 | 38 | import kotlinx.coroutines.flow.debounce
|
35 |
| -import kotlinx.coroutines.flow.distinctUntilChanged |
| 39 | +import kotlinx.coroutines.flow.map |
36 | 40 | import kotlinx.coroutines.flow.mapLatest
|
| 41 | +import kotlinx.coroutines.flow.stateIn |
37 | 42 | import kotlinx.coroutines.launch
|
38 | 43 | import kotlinx.coroutines.plus
|
39 | 44 | import kotlinx.coroutines.withContext
|
@@ -88,14 +93,20 @@ class ArticleRepository(context: Context) {
|
88 | 93 | return feedSourceDao.getFeedById(id)
|
89 | 94 | }
|
90 | 95 |
|
91 |
| - val isSyncing: Flow<Boolean> |
92 |
| - get() = workManager.getWorkInfosByTagFlow("FeedSyncer") |
93 |
| - .mapLatest { |
| 96 | + val isSyncing: StateFlow<Boolean> = |
| 97 | + workManager.getWorkInfosByTagFlow(FeedSyncer::class.qualifiedName!!) |
| 98 | + .map { |
94 | 99 | workManager.pruneWork()
|
95 |
| - it.any { work -> !work.state.isFinished } |
| 100 | + it.any { work -> |
| 101 | + work.state == WorkInfo.State.RUNNING || work.state == WorkInfo.State.BLOCKED |
| 102 | + } |
96 | 103 | }
|
97 |
| - .distinctUntilChanged() |
98 | 104 | .debounce(1000L)
|
| 105 | + .stateIn( |
| 106 | + scope, |
| 107 | + SharingStarted.Lazily, |
| 108 | + false |
| 109 | + ) |
99 | 110 |
|
100 | 111 | fun setCurrentlySyncingOn(feedId: Long, syncing: Boolean) {
|
101 | 112 | scope.launch {
|
|
0 commit comments