From d869872673b5327c476a30c78e62143dbb7d5ad6 Mon Sep 17 00:00:00 2001 From: Axel358 Date: Thu, 21 Mar 2024 08:29:43 -0400 Subject: [PATCH] refractor(utils): Replace if with when --- .../java/cu/axel/smartdock/utils/Utils.kt | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/cu/axel/smartdock/utils/Utils.kt b/app/src/main/java/cu/axel/smartdock/utils/Utils.kt index 1928ee45..bbe29a39 100644 --- a/app/src/main/java/cu/axel/smartdock/utils/Utils.kt +++ b/app/src/main/java/cu/axel/smartdock/utils/Utils.kt @@ -52,7 +52,7 @@ object Utils { val bitmapCopy = bitmap.copy(Bitmap.Config.ARGB_8888, false) bitmap.recycle() val result = - Bitmap.createBitmap(bitmapCopy.width, bitmapCopy.height, Bitmap.Config.ARGB_8888) + Bitmap.createBitmap(bitmapCopy.width, bitmapCopy.height, Bitmap.Config.ARGB_8888) val canvas = Canvas(result) val color = -0xbdbdbe val paint = Paint() @@ -61,10 +61,10 @@ object Utils { canvas.drawARGB(0, 0, 0, 0) paint.color = color canvas.drawCircle( - (bitmapCopy.width / 2).toFloat(), - (bitmapCopy.height / 2).toFloat(), - (bitmap.width / 2).toFloat(), - paint + (bitmapCopy.width / 2).toFloat(), + (bitmapCopy.height / 2).toFloat(), + (bitmap.width / 2).toFloat(), + paint ) paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN)) canvas.drawBitmap(bitmapCopy, rect, rect, paint) @@ -117,7 +117,7 @@ object Utils { fun saveLog(context: Context, name: String, log: String) { try { val fw = FileWriter( - File(context.getExternalFilesDir(null), name + "_" + currentDateString + ".log") + File(context.getExternalFilesDir(null), name + "_" + currentDateString + ".log") ) fw.write(log) fw.close() @@ -126,11 +126,12 @@ object Utils { } fun makeWindowParams( - width: Int, height: Int, context: Context, - secondary: Boolean = false + width: Int, height: Int, context: Context, + secondary: Boolean = false ): WindowManager.LayoutParams { - val displayId = if (secondary) DeviceUtils.getSecondaryDisplay(context).displayId else Display.DEFAULT_DISPLAY + val displayId = + if (secondary) DeviceUtils.getSecondaryDisplay(context).displayId else Display.DEFAULT_DISPLAY val displayWidth = DeviceUtils.getDisplayMetrics(context, displayId).widthPixels val displayHeight = DeviceUtils.getDisplayMetrics(context, displayId).heightPixels @@ -138,10 +139,10 @@ object Utils { layoutParams.format = PixelFormat.TRANSLUCENT layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE layoutParams.type = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY - else - WindowManager.LayoutParams.TYPE_PHONE + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY + else + WindowManager.LayoutParams.TYPE_PHONE layoutParams.width = displayWidth.coerceAtMost(width) layoutParams.height = displayHeight.coerceAtMost(height) return layoutParams @@ -149,22 +150,22 @@ object Utils { fun solve(expression: String): Double { if (expression.contains("+")) return expression.split("\\+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray()[0].toDouble() + expression.split("\\+".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray()[1].toDouble() else if (expression.contains("-")) return expression.split( - "\\-".toRegex() + .dropLastWhile { it.isEmpty() } + .toTypedArray()[0].toDouble() + expression.split("\\+".toRegex()) + .dropLastWhile { it.isEmpty() } + .toTypedArray()[1].toDouble() else if (expression.contains("-")) return expression.split( + "\\-".toRegex() ).dropLastWhile { it.isEmpty() } - .toTypedArray()[0].toDouble() - expression.split("\\-".toRegex()) - .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() + .toTypedArray()[0].toDouble() - expression.split("\\-".toRegex()) + .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() if (expression.contains("/")) return expression.split("\\/".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray()[0].toDouble() / expression.split("\\/".toRegex()) - .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() + .dropLastWhile { it.isEmpty() } + .toTypedArray()[0].toDouble() / expression.split("\\/".toRegex()) + .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() return if (expression.contains("*")) expression.split("\\*".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray()[0].toDouble() * expression.split("\\*".toRegex()) - .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() else 0.0 + .dropLastWhile { it.isEmpty() } + .toTypedArray()[0].toDouble() * expression.split("\\*".toRegex()) + .dropLastWhile { it.isEmpty() }.toTypedArray()[1].toDouble() else 0.0 } fun backupPreferences(context: Context, backupUri: Uri) { @@ -178,7 +179,7 @@ object Utils { type = "integer" } stringBuilder.append(type).append(" ").append(key).append(" ") - .append(value.toString()).append("\n") + .append(value.toString()).append("\n") } val content = stringBuilder.toString().trim { it <= ' ' } var os: OutputStream? = null @@ -193,7 +194,7 @@ object Utils { if (os != null) { try { os.close() - } catch (e: IOException) { + } catch (_: IOException) { } } } @@ -209,18 +210,16 @@ object Utils { val editor = sharedPreferences.edit() while (br.readLine().also { line = it } != null) { val contents = - line.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + line.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() if (contents.size > 2) { val type = contents[0] val key = contents[1] val value = contents[2] - if (type == "boolean") editor.putBoolean( - key, - java.lang.Boolean.parseBoolean(value) - ) else if (type == "integer") editor.putInt( - key, - value.toInt() - ) else editor.putString(key, value) + when (type) { + "boolean" -> editor.putBoolean(key, java.lang.Boolean.parseBoolean(value)) + "integer" -> editor.putInt(key, value.toInt()) + else -> editor.putString(key, value) + } } } br.close()