diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt index 6d1527dc..542d9e22 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt @@ -1,6 +1,8 @@ package org.moire.ultrasonic.service import android.net.wifi.WifiManager +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import java.util.ArrayList import java.util.PriorityQueue import java.util.concurrent.Executors @@ -20,7 +22,6 @@ import timber.log.Timber * This class is responsible for maintaining the playlist and downloading * its items from the network to the filesystem. * - * TODO: Implement LiveData * TODO: Move away from managing the queue with scheduled checks, instead use callbacks when * Downloads are finished */ @@ -35,6 +36,8 @@ class Downloader( private val downloadQueue: PriorityQueue = PriorityQueue() private val activelyDownloading: MutableList = ArrayList() + val observableList: MutableLiveData> = MutableLiveData>() + private val jukeboxMediaPlayer: JukeboxMediaPlayer by inject() private val downloadFileCache = LRUCache(100) @@ -58,6 +61,7 @@ class Downloader( stop() clearPlaylist() clearBackground() + observableList.value = listOf() Timber.i("Downloader destroyed") } @@ -111,6 +115,9 @@ class Downloader( return } + // Flag to know if changes have occured + var listChanged = false + // Check the active downloads for failures or completions and remove them cleanupActiveDownloads() @@ -134,6 +141,7 @@ class Downloader( !activelyDownloading.contains(download) && !downloadQueue.contains(download) ) { + listChanged = true downloadQueue.add(download) } } @@ -148,12 +156,17 @@ class Downloader( if (playlist.indexOf(task) == 1) { localMediaPlayer.setNextPlayerState(PlayerState.DOWNLOADING) } + listChanged = true } // Stop Executor service when done downloading if (activelyDownloading.size == 0) { stop() } + + if (listChanged) { + observableList.value = downloads + } } private fun startDownloadOnService(task: DownloadFile) { @@ -201,13 +214,13 @@ class Downloader( } @get:Synchronized - val downloads: List + val downloads: List get() { - val temp: MutableList = ArrayList() - temp.addAll(playlist) + val temp: MutableList = ArrayList() temp.addAll(activelyDownloading) temp.addAll(downloadQueue) - return temp.distinct() + temp.addAll(playlist) + return temp.distinct().sorted() } @Synchronized @@ -221,6 +234,7 @@ class Downloader( } playlistUpdateRevision++ + checkDownloads() } @Synchronized @@ -241,6 +255,7 @@ class Downloader( for (download in activelyDownloading) { download.cancelDownload() } + checkDownloads() } @Synchronized @@ -250,6 +265,7 @@ class Downloader( } playlist.remove(downloadFile) playlistUpdateRevision++ + checkDownloads() } @Synchronized diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt index 61a36389..2a96b79e 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/MediaPlayerController.kt @@ -421,14 +421,13 @@ class MediaPlayerController( } } - @Suppress("TooGenericExceptionCaught") // The interface throws only generic exceptions val isJukeboxAvailable: Boolean get() { try { val username = activeServerProvider.getActiveServer().userName return getMusicService().getUser(username).jukeboxRole - } catch (e: Exception) { - Timber.w(e, "Error getting user information") + } catch (all: Exception) { + Timber.w(all, "Error getting user information") } return false }