Make SongView display a percentag while downloading

Closes #403
This commit is contained in:
tzugen 2021-04-10 20:51:57 +02:00
parent cf68038e20
commit aac74d1eef
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 130 additions and 118 deletions

View File

@ -98,7 +98,6 @@ class DownloadFile(
if (downloadTask != null) { if (downloadTask != null) {
downloadTask!!.cancel() downloadTask!!.cancel()
} }
} }
fun getCompleteFile(): File { fun getCompleteFile(): File {
@ -152,7 +151,7 @@ class DownloadFile(
fun unpin() { fun unpin() {
if (saveFile.exists()) { if (saveFile.exists()) {
if (!saveFile.renameTo(completeFile)){ if (!saveFile.renameTo(completeFile)) {
Timber.w( Timber.w(
"Renaming file failed. Original file: %s; Rename to: %s", "Renaming file failed. Original file: %s; Rename to: %s",
saveFile.name, completeFile.name saveFile.name, completeFile.name
@ -204,7 +203,6 @@ class DownloadFile(
} catch (ex: IOException) { } catch (ex: IOException) {
Timber.w("Failed to rename file %s to %s", completeFile, saveFile) Timber.w("Failed to rename file %s to %s", completeFile, saveFile)
} }
} }
override fun toString(): String { override fun toString(): String {
@ -272,8 +270,7 @@ class DownloadFile(
outputStream = FileOutputStream(partialFile, partial) outputStream = FileOutputStream(partialFile, partial)
val len = inputStream.copyTo(outputStream) { val len = inputStream.copyTo(outputStream) { totalBytesCopied ->
totalBytesCopied ->
setProgress(totalBytesCopied) setProgress(totalBytesCopied)
} }
@ -331,6 +328,7 @@ class DownloadFile(
} }
return wakeLock1 return wakeLock1
} }
override fun toString(): String { override fun toString(): String {
return String.format("DownloadTask (%s)", song) return String.format("DownloadTask (%s)", song)
} }
@ -344,7 +342,6 @@ class DownloadFile(
} catch (x: Exception) { } catch (x: Exception) {
Timber.e(x, "Failed to get cover art.") Timber.e(x, "Failed to get cover art.")
} }
} }
@Throws(IOException::class) @Throws(IOException::class)

View File

@ -343,6 +343,23 @@ public class Util
toast.show(); toast.show();
} }
/**
* Formats an Int to a percentage string
* For instance:
* <ul>
* <li><code>format(99)</code> returns <em>"99 %"</em>.</li>
* </ul>
*
* @param percent The percent as a range from 0 - 100
* @return The formatted string.
*/
public static synchronized String formatPercentage(int percent)
{
return Math.min(Math.max(percent,0),100) + " %";
}
/** /**
* Converts a byte-count to a formatted string suitable for display to the user. * Converts a byte-count to a formatted string suitable for display to the user.
* For instance: * For instance:

View File

@ -225,59 +225,7 @@ class SongView(context: Context) : UpdateView(context), Checkable {
downloadFile = mediaPlayerControllerLazy.value.getDownloadFileForSong(entry) downloadFile = mediaPlayerControllerLazy.value.getDownloadFileForSong(entry)
val partialFile = downloadFile!!.partialFile updateDownloadStatus(downloadFile!!)
if (downloadFile!!.isWorkDone) {
val newLeftImageType =
if (downloadFile!!.isSaved) ImageType.Pin else ImageType.Downloaded
if (leftImageType != newLeftImageType) {
leftImage = if (downloadFile!!.isSaved) pinImage else downloadedImage
leftImageType = newLeftImageType
}
} else {
leftImageType = ImageType.None
leftImage = null
}
val rightImageType: ImageType
val rightImage: Drawable?
if (
downloadFile!!.isDownloading &&
!downloadFile!!.isDownloadCancelled &&
partialFile.exists()
) {
viewHolder?.status?.text = Util.formatLocalizedBytes(
partialFile.length(), this.context
)
rightImageType = ImageType.Downloading
rightImage = downloadingImage
} else {
rightImageType = ImageType.None
rightImage = null
val statusText = viewHolder?.status?.text
if (!statusText.isNullOrEmpty()) viewHolder?.status?.text = null
}
if (previousLeftImageType != leftImageType || previousRightImageType != rightImageType) {
previousLeftImageType = leftImageType
previousRightImageType = rightImageType
if (viewHolder?.status != null) {
viewHolder?.status?.setCompoundDrawablesWithIntrinsicBounds(
leftImage, null, rightImage, null
)
if (rightImage === downloadingImage) {
val frameAnimation = rightImage as AnimationDrawable?
frameAnimation!!.setVisible(true, true)
frameAnimation.start()
}
}
}
if (entry?.starred != true) { if (entry?.starred != true) {
if (viewHolder?.star?.drawable !== starHollowDrawable) { if (viewHolder?.star?.drawable !== starHollowDrawable) {
@ -325,6 +273,56 @@ class SongView(context: Context) : UpdateView(context), Checkable {
} }
} }
private fun updateDownloadStatus(downloadFile: DownloadFile) {
if (downloadFile.isWorkDone) {
val newLeftImageType =
if (downloadFile.isSaved) ImageType.Pin else ImageType.Downloaded
if (leftImageType != newLeftImageType) {
leftImage = if (downloadFile.isSaved) pinImage else downloadedImage
leftImageType = newLeftImageType
}
} else {
leftImageType = ImageType.None
leftImage = null
}
val rightImageType: ImageType
val rightImage: Drawable?
if (downloadFile.isDownloading && !downloadFile.isDownloadCancelled) {
viewHolder?.status?.text = Util.formatPercentage(downloadFile.progress.value!!)
rightImageType = ImageType.Downloading
rightImage = downloadingImage
} else {
rightImageType = ImageType.None
rightImage = null
val statusText = viewHolder?.status?.text
if (!statusText.isNullOrEmpty()) viewHolder?.status?.text = null
}
if (previousLeftImageType != leftImageType || previousRightImageType != rightImageType) {
previousLeftImageType = leftImageType
previousRightImageType = rightImageType
if (viewHolder?.status != null) {
viewHolder?.status?.setCompoundDrawablesWithIntrinsicBounds(
leftImage, null, rightImage, null
)
if (rightImage === downloadingImage) {
val frameAnimation = rightImage as AnimationDrawable?
frameAnimation!!.setVisible(true, true)
frameAnimation.start()
}
}
}
}
override fun setChecked(b: Boolean) { override fun setChecked(b: Boolean) {
viewHolder?.check?.isChecked = b viewHolder?.check?.isChecked = b
} }