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