Skip to content

Commit

Permalink
refractor(utils): Replace if with when
Browse files Browse the repository at this point in the history
  • Loading branch information
axel358 committed Mar 21, 2024
1 parent 8e479fc commit d869872
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions app/src/main/java/cu/axel/smartdock/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -126,45 +126,46 @@ 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
val layoutParams = WindowManager.LayoutParams()
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
}

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) {
Expand All @@ -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
Expand All @@ -193,7 +194,7 @@ object Utils {
if (os != null) {
try {
os.close()
} catch (e: IOException) {
} catch (_: IOException) {
}
}
}
Expand All @@ -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()
Expand Down

0 comments on commit d869872

Please sign in to comment.