From 9753d0c6082040652b80ea5aa3af7b46f2fe7eed Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 24 Oct 2021 22:14:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/local/ImportBookAdapter.kt | 2 +- .../{ConvertUtils.kt => ConvertExtensions.kt} | 37 ++++++++++++++----- .../java/io/legado/app/utils/FileUtils.kt | 2 +- .../java/io/legado/app/utils/IntExtensions.kt | 20 ---------- .../java/io/legado/app/utils/StringUtils.kt | 27 -------------- 5 files changed, 29 insertions(+), 59 deletions(-) rename app/src/main/java/io/legado/app/utils/{ConvertUtils.kt => ConvertExtensions.kt} (73%) delete mode 100644 app/src/main/java/io/legado/app/utils/IntExtensions.kt diff --git a/app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt index 800faf5e2..91d35d437 100644 --- a/app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt @@ -51,7 +51,7 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) : } llBrief.visible() tvTag.text = item.name.substringAfterLast(".") - tvSize.text = StringUtils.toSize(item.size) + tvSize.text = ConvertUtils.formatFileSize(item.size) tvDate.text = AppConst.dateFormat.format(item.date) cbSelect.isChecked = selectedUris.contains(item.toString()) } diff --git a/app/src/main/java/io/legado/app/utils/ConvertUtils.kt b/app/src/main/java/io/legado/app/utils/ConvertExtensions.kt similarity index 73% rename from app/src/main/java/io/legado/app/utils/ConvertUtils.kt rename to app/src/main/java/io/legado/app/utils/ConvertExtensions.kt index 4609a3711..9a7e63722 100644 --- a/app/src/main/java/io/legado/app/utils/ConvertUtils.kt +++ b/app/src/main/java/io/legado/app/utils/ConvertExtensions.kt @@ -9,6 +9,8 @@ import java.io.BufferedReader import java.io.InputStream import java.io.InputStreamReader import java.text.DecimalFormat +import kotlin.math.log10 +import kotlin.math.pow /** * 数据类型转换、单位转换 @@ -80,15 +82,13 @@ object ConvertUtils { return toDrawable(toBitmap(bytes)) } - fun toFileSizeString(fileSize: Long): String { - val df = DecimalFormat("0.00") - val fileSizeString: String = when { - fileSize < KB -> fileSize.toString() + "B" - fileSize < MB -> df.format(fileSize.toDouble() / KB) + "K" - fileSize < GB -> df.format(fileSize.toDouble() / MB) + "M" - else -> df.format(fileSize.toDouble() / GB) + "G" - } - return fileSizeString + fun formatFileSize(length: Long): String { + if (length <= 0) return "0" + val units = arrayOf("b", "kb", "M", "G", "T") + //计算单位的,原理是利用lg,公式是 lg(1024^n) = nlg(1024),最后 nlg(1024)/lg(1024) = n。 + val digitGroups = (log10(length.toDouble()) / log10(1024.0)).toInt() + //计算原理是,size/单位值。单位值指的是:比如说b = 1024,KB = 1024^2 + return DecimalFormat("#,##0.##").format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups] } @JvmOverloads @@ -110,4 +110,21 @@ object ConvertUtils { return sb.toString() } -} \ No newline at end of file +} + +val Int.dp: Int + get() = android.util.TypedValue.applyDimension( + android.util.TypedValue.COMPLEX_UNIT_DIP, + this.toFloat(), + Resources.getSystem().displayMetrics + ).toInt() + +val Int.sp: Int + get() = android.util.TypedValue.applyDimension( + android.util.TypedValue.COMPLEX_UNIT_SP, + this.toFloat(), + Resources.getSystem().displayMetrics + ).toInt() + +val Int.hexString: String + get() = Integer.toHexString(this) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index b654fd158..288e6da50 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -622,7 +622,7 @@ object FileUtils { */ fun getSize(path: String): String { val fileSize = getLength(path) - return ConvertUtils.toFileSizeString(fileSize) + return ConvertUtils.formatFileSize(fileSize) } /** diff --git a/app/src/main/java/io/legado/app/utils/IntExtensions.kt b/app/src/main/java/io/legado/app/utils/IntExtensions.kt deleted file mode 100644 index dc03d0258..000000000 --- a/app/src/main/java/io/legado/app/utils/IntExtensions.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.legado.app.utils - -import android.content.res.Resources - -val Int.dp: Int - get() = android.util.TypedValue.applyDimension( - android.util.TypedValue.COMPLEX_UNIT_DIP, - this.toFloat(), - Resources.getSystem().displayMetrics - ).toInt() - -val Int.sp: Int - get() = android.util.TypedValue.applyDimension( - android.util.TypedValue.COMPLEX_UNIT_SP, - this.toFloat(), - Resources.getSystem().displayMetrics - ).toInt() - -val Int.hexString: String - get() = Integer.toHexString(this) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index 6d9d49eca..43440fa86 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -9,8 +9,6 @@ import java.util.* import java.util.regex.Matcher import java.util.regex.Pattern import kotlin.math.abs -import kotlin.math.log10 -import kotlin.math.pow @Suppress("unused", "MemberVisibilityCanBePrivate") object StringUtils { @@ -42,17 +40,6 @@ object StringUtils { return map } - /** - * 将时间转换成日期 - */ - fun dateConvert(time: Long, pattern: String): String { - val date = Date(time) - - @SuppressLint("SimpleDateFormat") - val format = SimpleDateFormat(pattern) - return format.format(date) - } - /** * 将日期转换成昨天、今天、明天 */ @@ -101,20 +88,6 @@ object StringUtils { return "" } - /** - * 单位转换 - */ - fun toSize(length: Long): String { - if (length <= 0) return "0" - val units = arrayOf("b", "kb", "M", "G", "T") - //计算单位的,原理是利用lg,公式是 lg(1024^n) = nlg(1024),最后 nlg(1024)/lg(1024) = n。 - val digitGroups = - (log10(length.toDouble()) / log10(1024.0)).toInt() - //计算原理是,size/单位值。单位值指的是:比如说b = 1024,KB = 1024^2 - return DecimalFormat("#,##0.##") - .format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups] - } - /** * 首字母大写 */