refactor: reduce coupling in Parcelables

pull/748/head
andrekir 2023-10-03 17:27:16 -03:00
rodzic 26195e8d39
commit 2dd0e1f1e2
3 zmienionych plików z 36 dodań i 15 usunięć

Wyświetl plik

@ -2,10 +2,21 @@ package com.geeksville.mesh
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import com.geeksville.mesh.util.readParcelableCompat
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable 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 @Parcelize
enum class MessageStatus : Parcelable { enum class MessageStatus : Parcelable {
UNKNOWN, // Not set for this message UNKNOWN, // Not set for this message

Wyświetl plik

@ -5,7 +5,6 @@ import android.os.Parcelable
import androidx.room.Embedded import androidx.room.Embedded
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.geeksville.mesh.MeshProtos.User
import com.geeksville.mesh.util.GPSFormat import com.geeksville.mesh.util.GPSFormat
import com.geeksville.mesh.util.bearing import com.geeksville.mesh.util.bearing
import com.geeksville.mesh.util.latLongToMeter import com.geeksville.mesh.util.latLongToMeter
@ -13,8 +12,9 @@ import com.geeksville.mesh.util.anonymize
import kotlinx.parcelize.Parcelize import kotlinx.parcelize.Parcelize
/** /**
* Room [Embedded], [Entity] and [PrimaryKey] annotations and imports can be removed when only using the API. * Room [Embedded], [Entity] and [PrimaryKey] annotations and imports, as well as any protobuf
* For details check the AIDL interface in [com.geeksville.mesh.IMeshService] * reference [MeshProtos], [TelemetryProtos], [ConfigProtos] can be removed when only using the API.
* For details check the AIDL interface in [com.geeksville.mesh.IMeshService]
*/ */
// //
@ -34,8 +34,24 @@ data class MeshUser(
return "MeshUser(id=${id.anonymize}, longName=${longName.anonymize}, shortName=${shortName.anonymize}, hwModel=${hwModelString}, isLicensed=${isLicensed})" 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) /** Create our model object from a protobuf.
.setHwModel(hwModel).setIsLicensed(isLicensed).build() */
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 /** a string version of the hardware model, converted into pretty lowercase and changing _ to -, and p to dot
* or null if unset * or null if unset

Wyświetl plik

@ -1225,15 +1225,9 @@ class MeshService : Service(), Logging {
private fun installNodeInfo(info: MeshProtos.NodeInfo) { private fun installNodeInfo(info: MeshProtos.NodeInfo) {
// Just replace/add any entry // Just replace/add any entry
updateNodeInfo(info.num) { updateNodeInfo(info.num) {
if (info.hasUser()) if (info.hasUser()) {
it.user = it.user = MeshUser(info.user)
MeshUser( }
info.user.id,
info.user.longName,
info.user.shortName,
info.user.hwModel,
info.user.isLicensed
)
if (info.hasPosition()) { 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 // For the local node, it might not be able to update its times because it doesn't have a valid GPS reading yet