Skip to content

Commit

Permalink
refactor: implement repository pattern replacement for AIDL methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Nov 21, 2024
1 parent f73d909 commit 80f8f2a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/geeksville/mesh/model/UIState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?) {
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/geeksville/mesh/service/MeshService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 80f8f2a

Please sign in to comment.