mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 06:55:41 +00:00
Improve FileSizeFormatter.format API.
This commit is contained in:
committed by
Benoit Marty
parent
529720079a
commit
c847ac3a98
+1
-1
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-2
@@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user