Skip to content

Commit

Permalink
Version bump
Browse files Browse the repository at this point in the history
Added custom Double round function
  • Loading branch information
Deishelon committed Jan 26, 2019
1 parent 7e1fdc3 commit 2d57d5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Step 2. Add the dependency

```gradle
dependencies{
implementation 'com.github.Deishelon:RxDownload:Tag'
implementation 'com.github.Deishelon:RxDownload:1.2.9'
}
```

Expand Down
34 changes: 17 additions & 17 deletions rxdownload3/src/main/java/zlc/season/rxdownload3/core/Status.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zlc.season.rxdownload3.core

import zlc.season.rxdownload3.helper.formatSize
import zlc.season.rxdownload3.helper.roundTo
import java.text.NumberFormat.getPercentInstance


Expand All @@ -10,29 +11,28 @@ open class Status(var downloadSize: Long = 0L,

constructor(status: Status) : this(status.downloadSize, status.totalSize, status.chunkFlag)

fun formatTotalSize(): String {
return formatSize(totalSize)
fun formatTotalSize(s: Int = 1): String {
return formatSize(totalSize, s)
}

fun formatDownloadSize(): String {
return formatSize(downloadSize)
fun formatDownloadSize(s: Int = 1): String {
return formatSize(downloadSize, s)
}

fun formatString(): String {
return formatDownloadSize() + "/" + formatTotalSize()
}

fun percent(): String {
val percent: String
val result = if (totalSize == 0L) {
fun percent(): Double {
return if (totalSize == 0L) {
0.0
} else {
downloadSize * 1.0 / totalSize
}
val nf = getPercentInstance()
nf.minimumFractionDigits = 2
percent = nf.format(result)
return percent
}

fun percentPretty(s: Int = 1): String {
return "${percent().roundTo(s)}"
}
}

Expand All @@ -42,37 +42,37 @@ class Normal(status: Status) : Status(status) {
}
}

class Suspend(status: Status) : Status(status){
class Suspend(status: Status) : Status(status) {
override fun toString(): String {
return "Suspend"
}
}

class Waiting(status: Status) : Status(status){
class Waiting(status: Status) : Status(status) {
override fun toString(): String {
return "Waiting"
}
}

class Downloading(status: Status) : Status(status){
class Downloading(status: Status) : Status(status) {
override fun toString(): String {
return "Downloading: ${formatString()}"
}
}

class Failed(status: Status, val throwable: Throwable) : Status(status){
class Failed(status: Status, val throwable: Throwable) : Status(status) {
override fun toString(): String {
return "Failed"
}
}

class Succeed(status: Status) : Status(status){
class Succeed(status: Status) : Status(status) {
override fun toString(): String {
return "Succeed"
}
}

class Deleted(status: Status) : Status(status){
class Deleted(status: Status) : Status(status) {
override fun toString(): String {
return "Deleted"
}
Expand Down
21 changes: 14 additions & 7 deletions rxdownload3/src/main/java/zlc/season/rxdownload3/helper/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.pm.PackageManager
import io.reactivex.disposables.Disposable
import java.io.File
import java.text.DecimalFormat
import kotlin.math.pow
import kotlin.math.round

internal val ANY = Any()

Expand All @@ -14,25 +16,30 @@ fun dispose(disposable: Disposable?) {
}
}

fun formatSize(size: Long): String {
fun formatSize(size: Long, s: Int = 1): String {
val b = size.toDouble()
val k = size / 1024.0
val m = size / 1024.0 / 1024.0
val g = size / 1024.0 / 1024.0 / 1024.0
val t = size / 1024.0 / 1024.0 / 1024.0 / 1024.0
val dec = DecimalFormat("0.00")

return when {
t > 1 -> dec.format(t) + " TB"
g > 1 -> dec.format(g) + " GB"
m > 1 -> dec.format(m) + " MB"
k > 1 -> dec.format(k) + " KB"
else -> dec.format(b) + " B"
t > 1 -> "${t.roundTo(s)} TB"
g > 1 -> "${g.roundTo(s)} GB"
m > 1 -> "${m.roundTo(s)} MB"
k > 1 -> "${k.roundTo(s)} KB"
else -> "${b.roundTo(s)} B"
}
}

fun getPackageName(context: Context, apkFile: File): String {
val pm = context.packageManager
val apkInfo = pm.getPackageArchiveInfo(apkFile.path, PackageManager.GET_META_DATA)
return apkInfo.packageName
}

fun Double.roundTo(s: Int = 1): Double {
if (s == 0) return round(this)
val power = (10.0).pow(s)
return round(this * power) / power
}

0 comments on commit 2d57d5a

Please sign in to comment.