pull/1403/head
gedoor 3 years ago
parent a57eaf6303
commit 9753d0c608
  1. 2
      app/src/main/java/io/legado/app/ui/book/local/ImportBookAdapter.kt
  2. 37
      app/src/main/java/io/legado/app/utils/ConvertExtensions.kt
  3. 2
      app/src/main/java/io/legado/app/utils/FileUtils.kt
  4. 20
      app/src/main/java/io/legado/app/utils/IntExtensions.kt
  5. 27
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -51,7 +51,7 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
} }
llBrief.visible() llBrief.visible()
tvTag.text = item.name.substringAfterLast(".") tvTag.text = item.name.substringAfterLast(".")
tvSize.text = StringUtils.toSize(item.size) tvSize.text = ConvertUtils.formatFileSize(item.size)
tvDate.text = AppConst.dateFormat.format(item.date) tvDate.text = AppConst.dateFormat.format(item.date)
cbSelect.isChecked = selectedUris.contains(item.toString()) cbSelect.isChecked = selectedUris.contains(item.toString())
} }

@ -9,6 +9,8 @@ import java.io.BufferedReader
import java.io.InputStream import java.io.InputStream
import java.io.InputStreamReader import java.io.InputStreamReader
import java.text.DecimalFormat import java.text.DecimalFormat
import kotlin.math.log10
import kotlin.math.pow
/** /**
* 数据类型转换单位转换 * 数据类型转换单位转换
@ -80,15 +82,13 @@ object ConvertUtils {
return toDrawable(toBitmap(bytes)) return toDrawable(toBitmap(bytes))
} }
fun toFileSizeString(fileSize: Long): String { fun formatFileSize(length: Long): String {
val df = DecimalFormat("0.00") if (length <= 0) return "0"
val fileSizeString: String = when { val units = arrayOf("b", "kb", "M", "G", "T")
fileSize < KB -> fileSize.toString() + "B" //计算单位的,原理是利用lg,公式是 lg(1024^n) = nlg(1024),最后 nlg(1024)/lg(1024) = n。
fileSize < MB -> df.format(fileSize.toDouble() / KB) + "K" val digitGroups = (log10(length.toDouble()) / log10(1024.0)).toInt()
fileSize < GB -> df.format(fileSize.toDouble() / MB) + "M" //计算原理是,size/单位值。单位值指的是:比如说b = 1024,KB = 1024^2
else -> df.format(fileSize.toDouble() / GB) + "G" return DecimalFormat("#,##0.##").format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups]
}
return fileSizeString
} }
@JvmOverloads @JvmOverloads
@ -110,4 +110,21 @@ object ConvertUtils {
return sb.toString() return sb.toString()
} }
} }
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)

@ -622,7 +622,7 @@ object FileUtils {
*/ */
fun getSize(path: String): String { fun getSize(path: String): String {
val fileSize = getLength(path) val fileSize = getLength(path)
return ConvertUtils.toFileSizeString(fileSize) return ConvertUtils.formatFileSize(fileSize)
} }
/** /**

@ -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)

@ -9,8 +9,6 @@ import java.util.*
import java.util.regex.Matcher import java.util.regex.Matcher
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.log10
import kotlin.math.pow
@Suppress("unused", "MemberVisibilityCanBePrivate") @Suppress("unused", "MemberVisibilityCanBePrivate")
object StringUtils { object StringUtils {
@ -42,17 +40,6 @@ object StringUtils {
return map 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 "" 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]
}
/** /**
* 首字母大写 * 首字母大写
*/ */

Loading…
Cancel
Save