feat: add clear logs with confirmation dialog and fix DB dispatchers

- Add clearLogs method to BridgeModule with proper coroutine scope
- Fix fetchLogs and clearLogs using Thread+runBlocking which caused app close
- Change serviceScope from Dispatchers.Main to Dispatchers.IO
- Add error logging in fetchLogs for better diagnostics
- Add clear logs button with confirmation dialog in StatusScreen
This commit is contained in:
fiatcode 2026-05-04 08:38:25 +07:00
parent 1b3440e2fe
commit 60d051ee7b
No known key found for this signature in database
2 changed files with 61 additions and 27 deletions

View file

@ -11,8 +11,9 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
@ -174,30 +175,28 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
}
private fun fetchLogs(limit: Int, result: MethodChannel.Result) {
Thread {
CoroutineScope(Dispatchers.IO).launch {
try {
val logs = runBlocking(Dispatchers.IO) {
val db = context?.let { AppDatabase.getInstance(it) }
if (db != null) {
val events = db.eventLogDao().getRecentEventsList(limit)
events.map { event ->
mapOf(
"id" to event.id,
"timestamp" to event.timestamp,
"eventType" to event.eventType,
"message" to event.message
)
}
} else {
emptyList()
val db = context?.let { AppDatabase.getInstance(it) }
val logs = if (db != null) {
val events = db.eventLogDao().getRecentEventsList(limit)
events.map { event ->
mapOf(
"id" to event.id,
"timestamp" to event.timestamp,
"eventType" to event.eventType,
"message" to event.message
)
}
} else {
emptyList()
}
mainHandler.post { result.success(logs) }
} catch (e: Exception) {
Log.e(TAG, "fetchLogs failed", e)
mainHandler.post { result.success(emptyList<Map<String, Any>>()) }
}
}.start()
}
}
private fun openBatteryOptimizationSettings(result: MethodChannel.Result) {
@ -234,21 +233,17 @@ class BridgeModule : FlutterPlugin, MethodChannel.MethodCallHandler {
}
private fun clearLogs(result: MethodChannel.Result) {
Thread {
CoroutineScope(Dispatchers.IO).launch {
try {
val deleted = runBlocking(Dispatchers.IO) {
val db = context?.let { AppDatabase.getInstance(it) }
if (db != null) {
db.eventLogDao().deleteAll()
} else {
0
}
val db = context?.let { AppDatabase.getInstance(it) }
if (db != null) {
db.eventLogDao().deleteAll()
}
mainHandler.post { result.success(deleted) }
mainHandler.post { result.success(0) }
} catch (e: Exception) {
Log.e(TAG, "clearLogs failed", e)
mainHandler.post { result.success(-1) }
}
}.start()
}
}
}