move the Last modified date and time formatting to an extension

This commit is contained in:
tibbi 2016-10-16 21:44:01 +02:00
parent 7571ffc124
commit 5cbc13885a
2 changed files with 9 additions and 9 deletions

View File

@ -3,7 +3,6 @@ package com.simplemobiletools.filemanager.dialogs
import android.content.Context
import android.content.res.Resources
import android.support.v7.app.AlertDialog
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.ViewGroup
import com.simplemobiletools.filemanager.Config
@ -12,7 +11,6 @@ import com.simplemobiletools.filemanager.extensions.*
import kotlinx.android.synthetic.main.item_properties.view.*
import kotlinx.android.synthetic.main.property_item.view.*
import java.io.File
import java.util.*
class PropertiesDialog() {
lateinit var mContext: Context
@ -36,7 +34,7 @@ class PropertiesDialog() {
addProperty(R.string.name, file.name)
addProperty(R.string.path, file.parent)
addProperty(R.string.size, getItemSize(file))
addProperty(R.string.last_modified, formatLastModified(file.lastModified()))
addProperty(R.string.last_modified, file.lastModified().formatLastModified())
if (file.isDirectory) {
addProperty(R.string.files_count, mFilesCnt.toString())
@ -73,12 +71,6 @@ class PropertiesDialog() {
return file.length().formatSize()
}
private fun formatLastModified(ts: Long): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = ts
return DateFormat.format("dd.MM.yyyy HH:mm", cal).toString()
}
private fun getDirectorySize(dir: File): Long {
var size = 0L
if (dir.exists()) {

View File

@ -1,6 +1,8 @@
package com.simplemobiletools.filemanager.extensions
import android.text.format.DateFormat
import java.text.DecimalFormat
import java.util.*
fun Long.formatSize(): String {
if (this <= 0)
@ -10,3 +12,9 @@ fun Long.formatSize(): String {
val digitGroups = (Math.log10(toDouble()) / Math.log10(1024.0)).toInt()
return DecimalFormat("#,##0.#").format(this / Math.pow(1024.0, digitGroups.toDouble())) + " " + units[digitGroups]
}
fun Long.formatLastModified(): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this
return DateFormat.format("dd.MM.yyyy HH:mm", cal).toString()
}