refactor: implement repository pattern replacement for AIDL methods

This commit is contained in:
andrekir 2024-11-21 20:30:25 -03:00
parent f73d909cd0
commit 80f8f2a591
4 changed files with 45 additions and 0 deletions

View file

@ -28,6 +28,7 @@ import com.geeksville.mesh.database.entity.QuickChatAction
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
import com.geeksville.mesh.repository.radio.RadioInterfaceService
import com.geeksville.mesh.service.MeshService
import com.geeksville.mesh.service.ServiceAction
import com.geeksville.mesh.ui.map.MAP_STYLE_ID
import com.geeksville.mesh.util.positionToMeter
import dagger.hilt.android.lifecycle.HiltViewModel
@ -383,6 +384,12 @@ class UIViewModel @Inject constructor(
}
}
fun sendTapback(emoji: String, replyId: Int, contactKey: String) {
viewModelScope.launch {
radioConfigRepository.onServiceAction(ServiceAction.Tapback(emoji, replyId, contactKey))
}
}
fun requestTraceroute(destNum: Int) {
info("Requesting traceroute for '$destNum'")
try {

View file

@ -16,6 +16,7 @@ import com.geeksville.mesh.deviceProfile
import com.geeksville.mesh.model.NodeDB
import com.geeksville.mesh.model.getChannelUrl
import com.geeksville.mesh.service.MeshService.ConnectionState
import com.geeksville.mesh.service.ServiceAction
import com.geeksville.mesh.service.ServiceRepository
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
@ -177,6 +178,12 @@ class RadioConfigRepository @Inject constructor(
serviceRepository.emitMeshPacket(packet)
}
val serviceAction: SharedFlow<ServiceAction> get() = serviceRepository.serviceAction
suspend fun onServiceAction(action: ServiceAction) = coroutineScope {
serviceRepository.onServiceAction(action)
}
val tracerouteResponse: StateFlow<String?> get() = serviceRepository.tracerouteResponse
fun setTracerouteResponse(value: String?) {

View file

@ -58,6 +58,10 @@ import java.util.concurrent.TimeoutException
import javax.inject.Inject
import kotlin.math.absoluteValue
sealed class ServiceAction {
data class Tapback(val emoji: String, val replyId: Int, val contactKey: String) : ServiceAction()
}
/**
* Handles all the communication with android apps. Also keeps an internal model
* of the network state.
@ -279,6 +283,11 @@ class MeshService : Service(), Logging {
.launchIn(serviceScope)
radioConfigRepository.channelSetFlow.onEach { channelSet = it }
.launchIn(serviceScope)
radioConfigRepository.serviceAction.onEach { action ->
when (action) {
is ServiceAction.Tapback -> sendTapback(action)
}
}.launchIn(serviceScope)
loadSettings() // Load our last known node DB
@ -1724,6 +1733,21 @@ class MeshService : Service(), Logging {
}
}
private fun sendTapback(tapback: ServiceAction.Tapback) = toRemoteExceptions {
// contactKey: unique contact key filter (channel)+(nodeId)
val channel = tapback.contactKey[0].digitToInt()
val destNum = tapback.contactKey.substring(1)
sendToRadio(newMeshPacketTo(destNum).buildMeshPacket(
channel = channel,
priority = MeshPacket.Priority.BACKGROUND,
) {
replyId = tapback.replyId
portnumValue = Portnums.PortNum.TEXT_MESSAGE_APP_VALUE
payload = ByteString.copyFrom(tapback.emoji.encodeToByteArray())
})
}
private val binder = object : IMeshService.Stub() {
override fun setDeviceAddress(deviceAddr: String?) = toRemoteExceptions {

View file

@ -68,4 +68,11 @@ class ServiceRepository @Inject constructor() : Logging {
fun clearTracerouteResponse() {
setTracerouteResponse(null)
}
private val _serviceAction = MutableSharedFlow<ServiceAction>()
val serviceAction: SharedFlow<ServiceAction> get() = _serviceAction
suspend fun onServiceAction(action: ServiceAction) {
_serviceAction.emit(action)
}
}