[+] Encapsulate Result.orTrace()

This commit is contained in:
Azalea Gui
2023-01-24 12:39:56 -05:00
parent ad3a4c2cb5
commit 7b3661a3c3
2 changed files with 4 additions and 6 deletions
@@ -21,6 +21,7 @@ import com.welie.blessed.BluetoothPeripheral
import kotlin.reflect.KProperty
import kotlin.reflect.full.isSubclassOf
fun <T> Result<T>.orTrace() = apply { exceptionOrNull()?.printStackTrace() }
fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
@@ -49,13 +49,10 @@ class MyService : Service()
private inline fun <reified T : IDecoder<out Any>> observe() {
ble.observeAny<T> {
scope.launch {
try {
runCatching {
println("Adding ${it.javaClass.simpleName} to influxdb")
influx add it
}
catch (e: Exception) {
e.printStackTrace()
}
}.orTrace()
}
}
}
@@ -65,7 +62,7 @@ class MyService : Service()
override fun onReceive(ctxt: Context, intent: Intent) {
val bi = intent.batteryInfo(bm)
Timber.d("Battery info recorded: $bi")
scope.launch { influx add bi }
scope.launch { runCatching { influx add bi }.orTrace() }
}
}