From e858179204d215b6ebe657e193d456e6d91c93b6 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 18:31:42 +0800 Subject: [PATCH] Update _Ext.kt --- .../kotlin/com/v2ray/ang/extension/_Ext.kt | 89 ++++++++----------- 1 file changed, 38 insertions(+), 51 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 2c2159aa..2e66d6fd 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 @@ -10,75 +10,62 @@ import java.net.URI import java.net.URLConnection /** - * Some extensions + * Some extensions for the AngApplication */ +// Extension property to retrieve AngApplication instance from Context val Context.v2RayApplication: AngApplication get() = applicationContext as AngApplication -fun Context.toast(message: Int): Toast = ToastCompat - .makeText(this, message, Toast.LENGTH_SHORT) - .apply { - show() - } +// Extension functions to show a toast message for Int and CharSequence types +fun Context.toast(message: Int, duration: Int = Toast.LENGTH_SHORT) = + ToastCompat.makeText(this, message, duration).show() -fun Context.toast(message: CharSequence): Toast = ToastCompat - .makeText(this, message, Toast.LENGTH_SHORT) - .apply { - show() - } +fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) = + ToastCompat.makeText(this, message, duration).show() +// JSONObject Extensions to put a Pair or Map of data fun JSONObject.putOpt(pair: Pair) = putOpt(pair.first, pair.second) -fun JSONObject.putOpt(pairs: Map) = pairs.forEach { putOpt(it.key to it.value) } +fun JSONObject.putOpt(pairs: Map) = pairs.forEach { (key, value) -> + putOpt(key to value) +} + +// Constants for traffic and speed conversions const val threshold = 1000 const val divisor = 1024F -fun Long.toSpeedString() = toTrafficString() + "/s" +// Extension function to convert Long byte value to a string with speed units +fun Long.toSpeedString() = "${toTrafficString()}/s" -fun Long.toTrafficString(): String { - if (this == 0L) - return "\t\t\t0\t B" - - if (this < threshold) - return "${this.toFloat().toShortString()}\t B" - - val kib = this / divisor - if (kib < threshold) - return "${kib.toShortString()}\t KB" - - val mib = kib / divisor - if (mib < threshold) - return "${mib.toShortString()}\t MB" - - val gib = mib / divisor - if (gib < threshold) - return "${gib.toShortString()}\t GB" - - val tib = gib / divisor - if (tib < threshold) - return "${tib.toShortString()}\t TB" - - val pib = tib / divisor - if (pib < threshold) - return "${pib.toShortString()}\t PB" - - return "∞" +// Extension function to convert Long byte value to a string with traffic units +fun Long.toTrafficString(): String = when { + this == 0L -> "0 B" + this < threshold -> "${this.formatBytes()} B" + else -> { + val (value, unit) = listOf( + divisor to "KB", + divisor * divisor to "MB", + divisor * divisor * divisor to "GB", + divisor * divisor * divisor * divisor to "TB", + divisor * divisor * divisor * divisor * divisor to "PB" + ).fold(this to "B") { (value, _), (div, unit) -> + if (value < threshold * divisor) return@fold value to unit + (value / divisor) to unit + } + "${value.formatBytes()} $unit" + } } -private fun Float.toShortString(): String { - val s = "%.2f".format(this) - if (s.length <= 4) - return s - return s.substring(0, 4).removeSuffix(".") -} +private fun Long.formatBytes() = "%.2f".format(this / divisor).trimEnd('0').trimEnd('.') +// URLConnection extension property to return the response length with backward compatibility val URLConnection.responseLength: Long get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) contentLengthLong else contentLength.toLong() +// URI extension property to return an IDN Hostname val URI.idnHost: String - get() = host!!.replace("[", "").replace("]", "") + get() = host?.replace("[", "")?.replace("]", "") ?: "" -fun String.removeWhiteSpace(): String { - return this.replace(" ", "") -} +// String extension function to remove all white spaces +fun String.removeWhiteSpace() = replace(" ", "")