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) {
downloadTask!!.cancel()
}
}
fun getCompleteFile(): File {
@ -204,7 +203,6 @@ class DownloadFile(
} catch (ex: IOException) {
Timber.w("Failed to rename file %s to %s", completeFile, saveFile)
}
}
override fun toString(): String {
@ -272,8 +270,7 @@ class DownloadFile(
outputStream = FileOutputStream(partialFile, partial)
val len = inputStream.copyTo(outputStream) {
totalBytesCopied ->
val len = inputStream.copyTo(outputStream) { totalBytesCopied ->
setProgress(totalBytesCopied)
}
@ -331,6 +328,7 @@ class DownloadFile(
}
return wakeLock1
}
override fun toString(): String {
return String.format("DownloadTask (%s)", song)
}
@ -344,7 +342,6 @@ class DownloadFile(
} catch (x: Exception) {
Timber.e(x, "Failed to get cover art.")
}
}
@Throws(IOException::class)

View File

@ -343,6 +343,23 @@ public class Util
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.
* For instance:

View File

@ -225,59 +225,7 @@ class SongView(context: Context) : UpdateView(context), Checkable {
downloadFile = mediaPlayerControllerLazy.value.getDownloadFileForSong(entry)
val partialFile = downloadFile!!.partialFile
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()
}
}
}
updateDownloadStatus(downloadFile!!)
if (entry?.starred != true) {
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) {
viewHolder?.check?.isChecked = b
}