-
Notifications
You must be signed in to change notification settings - Fork 226
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -159,7 +162,8 @@ class DownloadManagerImpl @Inject constructor( | |
|
||
episodeManager.findPlayableByUuid(episodeUUID)?.let { | ||
episodeManager.updateDownloadTaskId(it, null) | ||
if (!episode.isDownloaded && it.episodeStatus != EpisodeStatusEnum.NOT_DOWNLOADED) { | ||
val episode = episodeManager.findPlayableByUuid(episodeUUID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason for getting the episode out of the database again after the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, there is not. I just totally overlooked that we already had the episode here as |
||
if (episode?.isDownloaded == false && it.episodeStatus != EpisodeStatusEnum.NOT_DOWNLOADED) { | ||
episodeManager.updateEpisodeStatus(it, EpisodeStatusEnum.NOT_DOWNLOADED) | ||
} | ||
} | ||
|
@@ -177,7 +181,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) | ||
} | ||
|
There was a problem hiding this comment.
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. 🤔