Don't create DownloadFile instances unnecessarily

This commit is contained in:
tzugen 2021-11-14 23:21:53 +01:00
parent d0e39efc50
commit 19d014709f
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 33 additions and 21 deletions

View File

@ -35,8 +35,9 @@ import timber.log.Timber
*/
class DownloadFile(
val song: MusicDirectory.Entry,
private val save: Boolean
save: Boolean
) : KoinComponent, Identifiable {
var shouldSave = save
val partialFile: File
val completeFile: File
private val saveFile: File = FileUtil.getSongFile(song)
@ -114,7 +115,7 @@ class DownloadFile(
@get:Synchronized
val isWorkDone: Boolean
get() = saveFile.exists() || completeFile.exists() && !save ||
get() = saveFile.exists() || completeFile.exists() && !shouldSave ||
saveWhenDone || completeWhenDone
@get:Synchronized
@ -125,10 +126,6 @@ class DownloadFile(
val isDownloadCancelled: Boolean
get() = downloadTask != null && downloadTask!!.isCancelled
fun shouldSave(): Boolean {
return save
}
fun shouldRetry(): Boolean {
return (retryCount > 0)
}
@ -188,7 +185,7 @@ class DownloadFile(
Util.renameFile(completeFile, saveFile)
saveWhenDone = false
} else if (completeWhenDone) {
if (save) {
if (shouldSave) {
Util.renameFile(partialFile, saveFile)
Util.scanMedia(saveFile)
} else {
@ -216,11 +213,12 @@ class DownloadFile(
if (saveFile.exists()) {
Timber.i("%s already exists. Skipping.", saveFile)
status.postValue(DownloadStatus.DONE)
Timber.i("UPDATING STATUS")
return
}
if (completeFile.exists()) {
if (save) {
if (shouldSave) {
if (isPlaying) {
saveWhenDone = true
} else {
@ -230,6 +228,7 @@ class DownloadFile(
Timber.i("%s already exists. Skipping.", completeFile)
}
status.postValue(DownloadStatus.DONE)
Timber.i("UPDATING STATUS")
return
}
@ -252,7 +251,7 @@ class DownloadFile(
if (needsDownloading) {
// Attempt partial HTTP GET, appending to the file if it exists.
val (inStream, partial) = musicService.getDownloadInputStream(
song, partialFile.length(), desiredBitRate, save
song, partialFile.length(), desiredBitRate, shouldSave
)
inputStream = inStream
@ -293,7 +292,7 @@ class DownloadFile(
if (isPlaying) {
completeWhenDone = true
} else {
if (save) {
if (shouldSave) {
Util.renameFile(partialFile, saveFile)
Util.scanMedia(saveFile)
} else {
@ -379,6 +378,7 @@ class DownloadFile(
private fun setProgress(totalBytesCopied: Long) {
if (song.size != null) {
progress.postValue((totalBytesCopied * 100 / song.size!!).toInt())
Timber.i("UPDATING PROGESS")
}
}

View File

@ -55,6 +55,8 @@ class Downloader(
RxBus.playlistPublisher.onNext(playlist)
}
var backgroundPriorityCounter = 100
val downloadChecker = Runnable {
try {
Timber.w("Checking Downloads")
@ -303,6 +305,8 @@ class Downloader(
activelyDownloading.remove(download)
}
}
backgroundPriorityCounter = 100
}
@Synchronized
@ -327,7 +331,7 @@ class Downloader(
@Synchronized
fun addToPlaylist(
songs: List<MusicDirectory.Entry?>,
songs: List<MusicDirectory.Entry>,
save: Boolean,
autoPlay: Boolean,
playNext: Boolean,
@ -346,13 +350,13 @@ class Downloader(
offset = 0
}
for (song in songs) {
val downloadFile = DownloadFile(song!!, save)
val downloadFile = song.getDownloadFile(save)
playlist.add(currentPlayingIndex + offset, downloadFile)
offset++
}
} else {
for (song in songs) {
val downloadFile = DownloadFile(song!!, save)
val downloadFile = song.getDownloadFile(save)
playlist.add(downloadFile)
}
}
@ -380,7 +384,10 @@ class Downloader(
// Because of the priority handling we add the songs in the reverse order they
// were requested, then it is correct in the end.
for (song in songs.asReversed()) {
downloadQueue.add(DownloadFile(song, save))
val file = song.getDownloadFile()
file.shouldSave = save
file.priority = backgroundPriorityCounter++
downloadQueue.add(file)
}
checkDownloads()
@ -436,7 +443,7 @@ class Downloader(
val size = playlist.size
if (size < listSize) {
for (song in shufflePlayBuffer[listSize - size]) {
val downloadFile = DownloadFile(song, false)
val downloadFile = song.getDownloadFile(false)
playlist.add(downloadFile)
playlistUpdateRevision++
}
@ -448,7 +455,7 @@ class Downloader(
if (currIndex > SHUFFLE_BUFFER_LIMIT) {
val songsToShift = currIndex - 2
for (song in shufflePlayBuffer[songsToShift]) {
playlist.add(DownloadFile(song, false))
playlist.add(song.getDownloadFile(false))
playlist[0].cancelDownload()
playlist.removeAt(0)
playlistUpdateRevision++
@ -475,9 +482,14 @@ class Downloader(
const val SHUFFLE_BUFFER_LIMIT = 4
}
// Extension function
fun MusicDirectory.Entry.downloadFile(): DownloadFile {
return getDownloadFileForSong(this)
/**
* Extension function
* Gathers the donwload file for a given song, and modifies shouldSave if provided.
*/
fun MusicDirectory.Entry.getDownloadFile(save: Boolean? = null): DownloadFile {
return getDownloadFileForSong(this).apply {
if (save != null) this.shouldSave = save
}
}
}

View File

@ -17,18 +17,18 @@
a:id="@+id/song_track"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:paddingEnd="6dip"
a:layout_gravity="left|center_vertical"
a:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
a:id="@+id/song_title"
a:layout_width="0dip"
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_gravity="left|center_vertical"
a:layout_weight="1"
a:drawablePadding="4dip"
a:ellipsize="end"
a:paddingStart="6dip"
a:paddingEnd="4dip"
a:singleLine="true"
a:textAppearance="?android:attr/textAppearanceMedium"/>