Skip to content

Commit

Permalink
refactor: reduce coupling in Parcelables
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Oct 3, 2023
1 parent 26195e8 commit 2dd0e1f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
13 changes: 12 additions & 1 deletion app/src/main/java/com/geeksville/mesh/DataPacket.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@ package com.geeksville.mesh

import android.os.Parcel
import android.os.Parcelable
import com.geeksville.mesh.util.readParcelableCompat
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable

/**
* Generic [Parcel.readParcelable] Android 13 compatibility extension.
*/
private inline fun <reified T : Parcelable> Parcel.readParcelableCompat(loader: ClassLoader?): T? {
return if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.TIRAMISU) {
@Suppress("DEPRECATION")
readParcelable(loader)
} else {
readParcelable(loader, T::class.java)
}
}

@Parcelize
enum class MessageStatus : Parcelable {
UNKNOWN, // Not set for this message
Expand Down
26 changes: 21 additions & 5 deletions app/src/main/java/com/geeksville/mesh/NodeInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import android.os.Parcelable
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.geeksville.mesh.MeshProtos.User
import com.geeksville.mesh.util.GPSFormat
import com.geeksville.mesh.util.bearing
import com.geeksville.mesh.util.latLongToMeter
import com.geeksville.mesh.util.anonymize
import kotlinx.parcelize.Parcelize

/**
* Room [Embedded], [Entity] and [PrimaryKey] annotations and imports can be removed when only using the API.
* For details check the AIDL interface in [com.geeksville.mesh.IMeshService]
* Room [Embedded], [Entity] and [PrimaryKey] annotations and imports, as well as any protobuf
* reference [MeshProtos], [TelemetryProtos], [ConfigProtos] can be removed when only using the API.
* For details check the AIDL interface in [com.geeksville.mesh.IMeshService]
*/

//
Expand All @@ -34,8 +34,24 @@ data class MeshUser(
return "MeshUser(id=${id.anonymize}, longName=${longName.anonymize}, shortName=${shortName.anonymize}, hwModel=${hwModelString}, isLicensed=${isLicensed})"
}

fun toProto(): User = User.newBuilder().setId(id).setLongName(longName).setShortName(shortName)
.setHwModel(hwModel).setIsLicensed(isLicensed).build()
/** Create our model object from a protobuf.
*/
constructor(p: MeshProtos.User) : this(
p.id,
p.longName,
p.shortName,
p.hwModel,
p.isLicensed,
)

fun toProto(): MeshProtos.User =
MeshProtos.User.newBuilder()
.setId(id)
.setLongName(longName)
.setShortName(shortName)
.setHwModel(hwModel)
.setIsLicensed(isLicensed)
.build()

/** a string version of the hardware model, converted into pretty lowercase and changing _ to -, and p to dot
* or null if unset
Expand Down
12 changes: 3 additions & 9 deletions app/src/main/java/com/geeksville/mesh/service/MeshService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1225,15 +1225,9 @@ class MeshService : Service(), Logging {
private fun installNodeInfo(info: MeshProtos.NodeInfo) {
// Just replace/add any entry
updateNodeInfo(info.num) {
if (info.hasUser())
it.user =
MeshUser(
info.user.id,
info.user.longName,
info.user.shortName,
info.user.hwModel,
info.user.isLicensed
)
if (info.hasUser()) {
it.user = MeshUser(info.user)
}

if (info.hasPosition()) {
// For the local node, it might not be able to update its times because it doesn't have a valid GPS reading yet
Expand Down

0 comments on commit 2dd0e1f

Please sign in to comment.