Skip to content

Commit

Permalink
Improve download size handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Iamlooker committed Oct 18, 2023
1 parent d6ee1ab commit c23fbc9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ object RepositoryUpdater {
authentication(repository.authentication)
}
) { read, total ->
callback(Stage.DOWNLOAD, read, total)
callback(Stage.DOWNLOAD, read.value, total.value)
}

when (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
sealed class State(val packageName: String) {
data object Idle : State("")
data class Connecting(val name: String) : State(name)
data class Downloading(val name: String, val read: Long, val total: Long?) : State(name)
data class Downloading(val name: String, val read: DataSize, val total: DataSize?) : State(name)
data class Error(val name: String) : State(name)
data class Cancel(val name: String) : State(name)
data class Success(val name: String, val release: Release) : State(name)
Expand Down Expand Up @@ -337,10 +337,10 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
is State.Downloading -> {
setContentTitle(getString(stringRes.downloading_FORMAT, currentTask!!.task.name))
if (state.total != null) {
setContentText("${state.read.formatSize()} / ${state.total.formatSize()}")
setProgress(100, state.read percentBy state.total, false)
setContentText("${state.read} / ${state.total}")
setProgress(100, state.read.value percentBy state.total.value, false)
} else {
setContentText(state.read.formatSize())
setContentText(state.read.toString())
setProgress(0, 0, true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.materialswitch.MaterialSwitch
import com.google.android.material.progressindicator.LinearProgressIndicator
import com.google.android.material.snackbar.Snackbar
import com.looker.core.common.DataSize
import com.looker.core.common.extension.*
import com.looker.core.common.file.KParcelable
import com.looker.core.common.formatSize
Expand Down Expand Up @@ -92,7 +93,7 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
data object Idle : Status
data object Pending : Status
data object Connecting : Status
data class Downloading(val read: Long, val total: Long?) : Status
data class Downloading(val read: DataSize, val total: DataSize?) : Status
data object PendingInstall : Status
data object Installing : Status
}
Expand Down Expand Up @@ -1251,13 +1252,13 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
is Status.Downloading -> {
holder.statusText.text = context.getString(
stringRes.downloading_FORMAT, if (status.total == null)
status.read.formatSize() else "${status.read.formatSize()} / ${status.total.formatSize()}"
status.read.toString() else "${status.read} / ${status.total}"
)
holder.progress.isIndeterminate = status.total == null
if (status.total != null) {
holder.progress.progress =
(holder.progress.max.toFloat() * status.read / status.total).roundToInt()
} else Unit
(holder.progress.max.toFloat() * status.read.value / status.total.value).roundToInt()
}
}

Status.Installing -> {
Expand All @@ -1271,7 +1272,7 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
}

Status.Idle -> {}
}::class
}
}
Unit
}
Expand All @@ -1283,7 +1284,7 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
holder.button.apply {
isEnabled = action != null
if (action != null) {
icon = context.getDrawable(action.iconResId)
icon = context.getDrawableCompat(action.iconResId)
setText(action.titleResId)
setTextColor(
if (action == Action.CANCEL) holder.actionTintOnCancel
Expand All @@ -1294,14 +1295,13 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
iconTint = if (action == Action.CANCEL) holder.actionTintOnCancel
else holder.actionTintOnNormal
} else {
icon = context.getDrawable(drawableRes.ic_cancel)
icon = context.getDrawableCompat(drawableRes.ic_cancel)
setText(stringRes.cancel)
setTextColor(holder.actionTintOnDisabled)
backgroundTintList = holder.actionTintDisabled
iconTint = holder.actionTintOnDisabled
}
}
Unit
}

ViewType.SCREENSHOT -> {
Expand Down Expand Up @@ -1572,11 +1572,9 @@ class AppDetailAdapter(private val callbacks: Callbacks) :
if (item.repoAddress != null) {
holder.repoTitle.setText(stringRes.repository_not_found)
holder.repoAddress.text = item.repoAddress
} else {

}
}
}::class
}
}

private fun formatHtml(text: String): SpannableStringBuilder {
Expand Down
19 changes: 19 additions & 0 deletions core/common/src/main/java/com/looker/core/common/DataSize.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.looker.core.common

import java.util.Locale

@JvmInline
value class DataSize(val value: Long) {

companion object {
private val sizeFormats = listOf("%.0f B", "%.0f kB", "%.1f MB", "%.2f GB")
}

override fun toString(): String {
val (size, index) = generateSequence(Pair(value.toFloat(), 0)) { (size, index) ->
if (size >= 1024f)
Pair(size / 1024f, index + 1) else null
}.take(sizeFormats.size).last()
return sizeFormats[index].format(Locale.US, size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import android.net.ConnectivityManager
import android.view.inputmethod.InputMethodManager
import androidx.annotation.AttrRes
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.core.content.res.ResourcesCompat
import com.looker.core.common.R

inline val Context.clipboardManager: ClipboardManager?
Expand Down Expand Up @@ -68,8 +68,8 @@ private fun Context.getDrawableFromAttr(attrResId: Int): Drawable {
return getDrawableCompat(resId)
}

private fun Context.getDrawableCompat(@DrawableRes resId: Int = R.drawable.background_border): Drawable =
ResourcesCompat.getDrawable(resources, resId, theme) ?: ContextCompat.getDrawable(this, resId)!!
fun Context.getDrawableCompat(@DrawableRes resId: Int = R.drawable.background_border): Drawable =
AppCompatResources.getDrawable(this, resId) ?: throw IllegalStateException("Cannot find drawable, ID: $resId")

fun Context.getColorFromAttr(@AttrRes attrResId: Int): ColorStateList {
val typedArray = obtainStyledAttributes(intArrayOf(attrResId))
Expand Down
3 changes: 2 additions & 1 deletion core/network/src/main/java/com/looker/network/Downloader.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.looker.network

import com.looker.core.common.DataSize
import com.looker.core.common.signature.FileValidator
import com.looker.network.header.HeadersBuilder
import java.io.File
Expand All @@ -24,4 +25,4 @@ interface Downloader {

}

typealias ProgressListener = suspend (bytesReceived: Long, contentLength: Long) -> Unit
typealias ProgressListener = suspend (bytesReceived: DataSize, contentLength: DataSize) -> Unit
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.looker.network

import com.looker.core.common.DataSize
import com.looker.core.common.extension.exceptCancellation
import com.looker.core.common.extension.size
import com.looker.core.common.signature.FileValidator
Expand Down Expand Up @@ -111,7 +112,7 @@ internal class KtorDownloader : Downloader {
}
onDownload { read, total ->
if (block != null) {
block(read + (fileSize ?: 0L), total + (fileSize ?: 0L))
block(DataSize(read + (fileSize ?: 0L)), DataSize(total + (fileSize ?: 0L)))
}
}
}
Expand Down

0 comments on commit c23fbc9

Please sign in to comment.