Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: update UI when download status changes #113

Merged
merged 3 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was very tempted to wrap one or both of these Strings with an inline class in order to make it clear what is what. I can be a little type-happy at times though, so I held off. It might be interesting to use some value classes for things like UUIDs throughout the app though. 🤔


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