Skip to content

Commit

Permalink
refactor: migrate DebugPanel to Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Nov 19, 2024
1 parent 91c8c78 commit e33cf85
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 258 deletions.
33 changes: 19 additions & 14 deletions app/src/main/java/com/geeksville/mesh/database/MeshLogRepository.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
package com.geeksville.mesh.database

import com.geeksville.mesh.CoroutineDispatchers
import com.geeksville.mesh.Portnums
import com.geeksville.mesh.MeshProtos.MeshPacket
import com.geeksville.mesh.TelemetryProtos.Telemetry
import com.geeksville.mesh.database.dao.MeshLogDao
import com.geeksville.mesh.database.entity.MeshLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.withContext
import javax.inject.Inject

class MeshLogRepository @Inject constructor(private val meshLogDaoLazy: dagger.Lazy<MeshLogDao>) {
class MeshLogRepository @Inject constructor(
private val meshLogDaoLazy: dagger.Lazy<MeshLogDao>,
private val dispatchers: CoroutineDispatchers,
) {
private val meshLogDao by lazy {
meshLogDaoLazy.get()
}

suspend fun getAllLogs(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> = withContext(Dispatchers.IO) {
meshLogDao.getAllLogs(maxItems)
}
fun getAllLogs(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> = meshLogDao.getAllLogs(maxItems)
.flowOn(dispatchers.io)
.conflate()

suspend fun getAllLogsInReceiveOrder(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> = withContext(Dispatchers.IO) {
fun getAllLogsInReceiveOrder(maxItems: Int = MAX_ITEMS): Flow<List<MeshLog>> =
meshLogDao.getAllLogsInReceiveOrder(maxItems)
}
.flowOn(dispatchers.io)
.conflate()

private fun parseTelemetryLog(log: MeshLog): Telemetry? = runCatching {
Telemetry.parseFrom(log.fromRadio.packet.decoded.payload)
Expand All @@ -37,15 +42,15 @@ class MeshLogRepository @Inject constructor(private val meshLogDaoLazy: dagger.L
meshLogDao.getLogsFrom(nodeNum, Portnums.PortNum.TELEMETRY_APP_VALUE, MAX_MESH_PACKETS)
.distinctUntilChanged()
.mapLatest { list -> list.mapNotNull(::parseTelemetryLog) }
.flowOn(Dispatchers.IO)
.flowOn(dispatchers.io)

fun getLogsFrom(
nodeNum: Int,
portNum: Int = Portnums.PortNum.UNKNOWN_APP_VALUE,
maxItem: Int = MAX_MESH_PACKETS,
): Flow<List<MeshLog>> = meshLogDao.getLogsFrom(nodeNum, portNum, maxItem)
.distinctUntilChanged()
.flowOn(Dispatchers.IO)
.flowOn(dispatchers.io)

/*
* Retrieves MeshPackets matching 'nodeNum' and 'portNum'.
Expand All @@ -57,21 +62,21 @@ class MeshLogRepository @Inject constructor(private val meshLogDaoLazy: dagger.L
portNum: Int = Portnums.PortNum.UNKNOWN_APP_VALUE,
): Flow<List<MeshPacket>> = getLogsFrom(nodeNum, portNum)
.mapLatest { list -> list.map { it.fromRadio.packet } }
.flowOn(Dispatchers.IO)
.flowOn(dispatchers.io)

suspend fun insert(log: MeshLog) = withContext(Dispatchers.IO) {
suspend fun insert(log: MeshLog) = withContext(dispatchers.io) {
meshLogDao.insert(log)
}

suspend fun deleteAll() = withContext(Dispatchers.IO) {
suspend fun deleteAll() = withContext(dispatchers.io) {
meshLogDao.deleteAll()
}

suspend fun deleteLog(uuid: String) = withContext(Dispatchers.IO) {
suspend fun deleteLog(uuid: String) = withContext(dispatchers.io) {
meshLogDao.deleteLog(uuid)
}

suspend fun deleteLogs(nodeNum: Int, portNum: Int) = withContext(Dispatchers.IO) {
suspend fun deleteLogs(nodeNum: Int, portNum: Int) = withContext(dispatchers.io) {
meshLogDao.deleteLogs(nodeNum, portNum)
}

Expand Down
12 changes: 4 additions & 8 deletions app/src/main/java/com/geeksville/mesh/model/DebugViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ import com.geeksville.mesh.database.MeshLogRepository
import com.geeksville.mesh.database.entity.MeshLog
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DebugViewModel @Inject constructor(
private val meshLogRepository: MeshLogRepository,
) : ViewModel(), Logging {

private val _meshLog = MutableStateFlow<List<MeshLog>>(emptyList())
val meshLog: StateFlow<List<MeshLog>> = _meshLog
val meshLog: StateFlow<List<MeshLog>> = meshLogRepository.getAllLogs()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

init {
viewModelScope.launch {
meshLogRepository.getAllLogs().collect { _meshLog.value = it }
}

debug("DebugViewModel created")
}

Expand Down
Loading

0 comments on commit e33cf85

Please sign in to comment.