Improve FileSizeFormatter.format API.
This commit is contained in:
parent
ac60ef0cbe
commit
2809d6fc2f
3 changed files with 23 additions and 4 deletions
|
|
@ -20,5 +20,5 @@ interface FileSizeFormatter {
|
||||||
/**
|
/**
|
||||||
* Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
|
* Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
|
||||||
*/
|
*/
|
||||||
fun format(fileSize: Long): String
|
fun format(fileSize: Long, useShortFormat: Boolean = true): String
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package io.element.android.libraries.androidtools.impl
|
package io.element.android.libraries.androidtools.impl
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
import android.text.format.Formatter
|
import android.text.format.Formatter
|
||||||
import com.squareup.anvil.annotations.ContributesBinding
|
import com.squareup.anvil.annotations.ContributesBinding
|
||||||
import io.element.android.libraries.androidtools.api.FileSizeFormatter
|
import io.element.android.libraries.androidtools.api.FileSizeFormatter
|
||||||
|
|
@ -26,7 +27,25 @@ import javax.inject.Inject
|
||||||
|
|
||||||
@ContributesBinding(AppScope::class)
|
@ContributesBinding(AppScope::class)
|
||||||
class AndroidFileSizeFormatter @Inject constructor(@ApplicationContext private val context: Context) : FileSizeFormatter {
|
class AndroidFileSizeFormatter @Inject constructor(@ApplicationContext private val context: Context) : FileSizeFormatter {
|
||||||
override fun format(fileSize: Long): String {
|
override fun format(fileSize: Long, useShortFormat: Boolean): String {
|
||||||
return Formatter.formatShortFileSize(context, fileSize)
|
// Since Android O, the system considers that 1ko = 1000 bytes instead of 1024 bytes.
|
||||||
|
// We want to avoid that.
|
||||||
|
val normalizedSize = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
|
||||||
|
fileSize
|
||||||
|
} else {
|
||||||
|
// First convert the size
|
||||||
|
when {
|
||||||
|
fileSize < 1024 -> fileSize
|
||||||
|
fileSize < 1024 * 1024 -> fileSize * 1000 / 1024
|
||||||
|
fileSize < 1024 * 1024 * 1024 -> fileSize * 1000 / 1024 * 1000 / 1024
|
||||||
|
else -> fileSize * 1000 / 1024 * 1000 / 1024 * 1000 / 1024
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return if (useShortFormat) {
|
||||||
|
Formatter.formatShortFileSize(context, normalizedSize)
|
||||||
|
} else {
|
||||||
|
Formatter.formatFileSize(context, normalizedSize)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ package io.element.android.libraries.androidtools.test
|
||||||
import io.element.android.libraries.androidtools.api.FileSizeFormatter
|
import io.element.android.libraries.androidtools.api.FileSizeFormatter
|
||||||
|
|
||||||
class FakeFileSizeFormatter : FileSizeFormatter {
|
class FakeFileSizeFormatter : FileSizeFormatter {
|
||||||
override fun format(fileSize: Long): String {
|
override fun format(fileSize: Long, useShortFormat: Boolean): String {
|
||||||
return "$fileSize Bytes"
|
return "$fileSize Bytes"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue