From 802f2cf3eb113c8107f60945af0b2a5b909626bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A4=CE=BF=20=CE=BC=CE=BF=CF=87=CE=B8=CE=B7=CF=81=CF=8C?= =?UTF-8?q?=20=5E=5F=5E?= <69906215+Malus-risus@users.noreply.github.com> Date: Sun, 25 Feb 2024 19:05:48 +0800 Subject: [PATCH] Update _Ext.kt --- .../kotlin/com/v2ray/ang/extension/_Ext.kt | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt index 4b7ff2a0..9b764c0d 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/extension/_Ext.kt @@ -1,6 +1,7 @@ package com.v2ray.ang.extension import android.content.Context +import android.os.Build import android.widget.Toast import com.v2ray.ang.AngApplication import me.drakeet.support.toast.ToastCompat @@ -9,47 +10,60 @@ import java.net.URI import java.net.URLConnection val Context.v2RayApplication: AngApplication - get() = applicationContext as? AngApplication ?: throw IllegalStateException("Not an AngApplication") + get() = applicationContext as AngApplication -fun Context.toast(message: Int) = showToast(message) -fun Context.toast(message: CharSequence) = showToast(message) - -private fun Context.showToast(message: Any) { - ToastCompat.makeText(this, message.toString(), Toast.LENGTH_SHORT).apply { show() } +fun Context.toast(message: Int) { + ToastCompat.makeText(this, message, Toast.LENGTH_SHORT).apply { show() } } -fun JSONObject.putOpt(pair: Pair) { - pair.second?.let { - put(pair.first, it) - } +fun Context.toast(message: CharSequence) { + ToastCompat.makeText(this, message, Toast.LENGTH_SHORT).apply { show() } } -fun JSONObject.putOpt(pairs: Map) { - pairs.forEach { (key, value) -> - putOpt(key to value) - } +fun JSONObject.putOpt(pair: Pair) { + put(pair.first, pair.second) } -private const val KILOBYTE = 1024.0 -private const val THRESHOLD = 1024 +fun JSONObject.putOpt(pairs: Map) { + pairs.forEach { put(it.key, it.value) } +} + +const val THRESHOLD = 1000L +const val DIVISOR = 1024.0 + +fun Long.toSpeedString(): String = this.toTrafficString() + "/s" fun Long.toTrafficString(): String { - var value = this.toDouble() - val units = listOf("B", "KB", "MB", "GB", "TB", "PB") - var unitIndex = 0 - - while (value >= THRESHOLD && unitIndex < units.lastIndex) { - value /= KILOBYTE - unitIndex++ + if (this < THRESHOLD) { + return "$this B" } - - return String.format("%.2f %s", value, units[unitIndex]) + val kb = this / DIVISOR + if (kb < THRESHOLD) { + return "${String.format("%.1f KB", kb)}" + } + val mb = kb / DIVISOR + if (mb < THRESHOLD) { + return "${String.format("%.1f MB", mb)}" + } + val gb = mb / DIVISOR + if (gb < THRESHOLD) { + return "${String.format("%.1f GB", gb)}" + } + val tb = gb / DIVISOR + if (tb < THRESHOLD) { + return "${String.format("%.1f TB", tb)}" + } + return String.format("%.1f PB", tb / DIVISOR) } val URLConnection.responseLength: Long - get() = if (Build.VERSION.SDK_INT >= 23) contentLengthLong else contentLength.toLong() + get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + contentLengthLong + } else { + contentLength.toLong() + } val URI.idnHost: String get() = host?.replace("[", "")?.replace("]", "") ?: "" -fun String.removeWhiteSpace() = replace("\\s+".toRegex(), "") +fun String.removeWhiteSpace(): String = replace("\\s+".toRegex(), "")