Skip to content

Commit

Permalink
Merge pull request #113 from Automattic/fix/update-ui-when-switch-to-…
Browse files Browse the repository at this point in the history
…metered

Fix: update UI when download status changes
  • Loading branch information
mchowning authored Jul 15, 2022
2 parents 8a3b79f + 6684ff9 commit f56315a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
([#83](https://github.com/Automattic/pocket-casts-android/pull/83)).
* Fix back navigation for full screen player 'Rearrange Actions' page
([#76](https://github.com/Automattic/pocket-casts-android/pull/76)).
* Fix showing paused downloads as in progress
([#113](https://github.com/Automattic/pocket-casts-android/pull/113)).

### 7.19.2 (2022-02-11)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DownloadManagerImpl @Inject constructor(
override val progressUpdates: MutableMap<String, DownloadProgressUpdate> = mutableMapOf()
override val progressUpdateRelay: Subject<DownloadProgressUpdate> = ReplaySubject.createWithSize(20)

private var workManagerListener: LiveData<Pair<List<WorkInfo>, Map<String?, Playable>>>? = null
private var workManagerListener: LiveData<Pair<List<WorkInfo>, Map<String?, String>>>? = null

override fun setup(episodeManager: EpisodeManager, podcastManager: PodcastManager, playlistManager: PlaylistManager, playbackManager: PlaybackManager) {
this.episodeManager = episodeManager
Expand All @@ -108,7 +108,9 @@ class DownloadManagerImpl @Inject constructor(
.distinctUntilChanged { t1, t2 -> // We only really need to make sure we have all the downloading episodes available, we don't care when their metadata changes
t1.map { it.uuid }.toSet() == t2.map { it.uuid }.toSet()
}
.map { it.associateBy { it.downloadTaskId } } // Convert to map for easy lookup
.map { list ->
list.associateBy({ it.downloadTaskId }, { it.uuid }) // Convert to map for easy lookup
}

launch(downloadsCoroutineContext) {
cleanUpStaleDownloads(workManager)
Expand All @@ -117,18 +119,19 @@ class DownloadManagerImpl @Inject constructor(
val episodeLiveData = LiveDataReactiveStreams.fromPublisher(episodeFlowable)
workManagerListener = workManager.getWorkInfosByTagLiveData(DownloadManager.WORK_MANAGER_DOWNLOAD_TAG).combineLatest(episodeLiveData)

workManagerListener?.observeForever { (tasks, episodes) ->
workManagerListener?.observeForever { (tasks, episodeUuids) ->
tasks.forEach { workInfo ->
val taskId = workInfo.id.toString()
val episode = episodes[taskId]
val episodeUUID = episode?.uuid
val episodeUUID = episodeUuids[taskId]
if (episodeUUID != null) {
val info = DownloadingInfo(episodeUUID, workInfo.id)
when (workInfo.state) {
WorkInfo.State.ENQUEUED, WorkInfo.State.BLOCKED -> {
launch(downloadsCoroutineContext) {
pendingQueue[episodeUUID] = DownloadingInfo(episodeUUID, workInfo.id)
getRequirementsAndSetStatusAsync(episode)
episodeManager.findPlayableByUuid(episodeUUID)?.let { episode ->
getRequirementsAndSetStatusAsync(episode)
}
synchronized(downloadingQueue) {
if (downloadingQueue.contains(info)) {
downloadingQueue.remove(info)
Expand Down Expand Up @@ -159,7 +162,7 @@ class DownloadManagerImpl @Inject constructor(

episodeManager.findPlayableByUuid(episodeUUID)?.let {
episodeManager.updateDownloadTaskId(it, null)
if (!episode.isDownloaded && it.episodeStatus != EpisodeStatusEnum.NOT_DOWNLOADED) {
if (!it.isDownloaded && it.episodeStatus != EpisodeStatusEnum.NOT_DOWNLOADED) {
episodeManager.updateEpisodeStatus(it, EpisodeStatusEnum.NOT_DOWNLOADED)
}
}
Expand All @@ -177,7 +180,7 @@ class DownloadManagerImpl @Inject constructor(
}
WorkInfo.State.SUCCEEDED -> {
launch(downloadsCoroutineContext) {
Timber.d("Worker succeeded: ${episode.title}")
Timber.d("Worker succeeded: $episodeUUID")
synchronized(downloadingQueue) {
downloadingQueue.remove(info)
}
Expand Down

0 comments on commit f56315a

Please sign in to comment.