archive-97f2350/app/src/main/java/uk/orllewin/sianel/ui/Format.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package uk.orllewin.sianel.ui
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import java.text.DateFormat
import java.util.Date
internal fun openUrl(ctx: Context, url: String) {
val uri = Uri.parse(url)
runCatching {
CustomTabsIntent.Builder().build().launchUrl(ctx, uri)
}.onFailure {
ctx.startActivity(Intent(Intent.ACTION_VIEW, uri))
}
}
internal fun fmtSize(b: Long): String = when {
b < 1024 -> "$b B"
b < 1024 * 1024 -> "%.1f KB".format(b / 1024.0)
else -> "%.1f MB".format(b / 1024.0 / 1024.0)
}
internal fun fmtDate(epochSec: Long): String =
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
.format(Date(epochSec * 1000))