kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: simplify setOwner logic
rodzic
956db658e9
commit
7ca724142f
|
@ -2,6 +2,7 @@ package com.geeksville.mesh
|
||||||
|
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.os.Parcelable
|
import android.os.Parcelable
|
||||||
|
import com.geeksville.mesh.MeshProtos.User
|
||||||
import com.geeksville.mesh.util.bearing
|
import com.geeksville.mesh.util.bearing
|
||||||
import com.geeksville.mesh.util.latLongToMeter
|
import com.geeksville.mesh.util.latLongToMeter
|
||||||
import com.geeksville.mesh.util.anonymize
|
import com.geeksville.mesh.util.anonymize
|
||||||
|
@ -27,6 +28,9 @@ 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)
|
||||||
|
.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
|
||||||
* */
|
* */
|
||||||
|
|
|
@ -426,8 +426,6 @@ class UIViewModel @Inject constructor(
|
||||||
try {
|
try {
|
||||||
// Pull down our real node ID - This must be done AFTER reading the nodedb because we need the DB to find our nodeinof object
|
// Pull down our real node ID - This must be done AFTER reading the nodedb because we need the DB to find our nodeinof object
|
||||||
nodeDB.setMyId(service.myId)
|
nodeDB.setMyId(service.myId)
|
||||||
val ownerName = nodes[service.myId]?.user?.longName
|
|
||||||
_ownerName.value = ownerName
|
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
warn("Ignoring failure to get myId, service is probably just uninited... ${ex.message}")
|
warn("Ignoring failure to get myId, service is probably just uninited... ${ex.message}")
|
||||||
}
|
}
|
||||||
|
@ -520,12 +518,6 @@ class UIViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// our name in hte radio
|
|
||||||
/// Note, we generate owner initials automatically for now
|
|
||||||
/// our activity will read this from prefs or set it to the empty string
|
|
||||||
private val _ownerName = MutableLiveData<String?>()
|
|
||||||
val ownerName: LiveData<String?> get() = _ownerName
|
|
||||||
|
|
||||||
val provideLocation = object : MutableLiveData<Boolean>(preferences.getBoolean("provide-location", false)) {
|
val provideLocation = object : MutableLiveData<Boolean>(preferences.getBoolean("provide-location", false)) {
|
||||||
override fun setValue(value: Boolean) {
|
override fun setValue(value: Boolean) {
|
||||||
super.setValue(value)
|
super.setValue(value)
|
||||||
|
@ -536,22 +528,8 @@ class UIViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setOwner(user: MeshUser) = with(user) {
|
fun setOwner(user: User) {
|
||||||
|
setRemoteOwner(myNodeNum ?: return, user)
|
||||||
longName.trim().let { ownerName ->
|
|
||||||
// note: we allow an empty user string to be written to prefs
|
|
||||||
_ownerName.value = ownerName
|
|
||||||
preferences.edit { putString("owner", ownerName) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Note: we are careful to not set a new unique ID
|
|
||||||
if (_ownerName.value!!.isNotEmpty())
|
|
||||||
try {
|
|
||||||
// Note: we use ?. here because we might be running in the emulator
|
|
||||||
meshService?.setOwner(user)
|
|
||||||
} catch (ex: RemoteException) {
|
|
||||||
errormsg("Can't set username on device, is device offline? ${ex.message}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setRemoteOwner(destNum: Int, user: User) {
|
fun setRemoteOwner(destNum: Int, user: User) {
|
||||||
|
@ -726,7 +704,7 @@ class UIViewModel @Inject constructor(
|
||||||
longName = if (hasLongName()) longName else it.longName,
|
longName = if (hasLongName()) longName else it.longName,
|
||||||
shortName = if (hasShortName()) shortName else it.shortName
|
shortName = if (hasShortName()) shortName else it.shortName
|
||||||
)
|
)
|
||||||
setOwner(user)
|
setOwner(user.toProto())
|
||||||
}
|
}
|
||||||
if (hasChannelUrl()) {
|
if (hasChannelUrl()) {
|
||||||
setChannels(ChannelSet(Uri.parse(channelUrl)))
|
setChannels(ChannelSet(Uri.parse(channelUrl)))
|
||||||
|
|
|
@ -280,7 +280,8 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
||||||
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||||
spinner.adapter = regionAdapter
|
spinner.adapter = regionAdapter
|
||||||
|
|
||||||
model.ownerName.observe(viewLifecycleOwner) { name ->
|
model.ourNodeInfo.asLiveData().observe(viewLifecycleOwner) { node ->
|
||||||
|
val name = node?.user?.longName
|
||||||
binding.usernameEditText.isEnabled = !name.isNullOrEmpty() && !model.isManaged
|
binding.usernameEditText.isEnabled = !name.isNullOrEmpty() && !model.isManaged
|
||||||
binding.usernameEditText.setText(name)
|
binding.usernameEditText.setText(name)
|
||||||
}
|
}
|
||||||
|
@ -361,7 +362,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
||||||
val n = binding.usernameEditText.text.toString().trim()
|
val n = binding.usernameEditText.text.toString().trim()
|
||||||
model.ourNodeInfo.value?.user?.let {
|
model.ourNodeInfo.value?.user?.let {
|
||||||
val user = it.copy(longName = n, shortName = getInitials(n))
|
val user = it.copy(longName = n, shortName = getInitials(n))
|
||||||
if (n.isNotEmpty()) model.setOwner(user)
|
if (n.isNotEmpty()) model.setOwner(user.toProto())
|
||||||
}
|
}
|
||||||
requireActivity().hideKeyboard()
|
requireActivity().hideKeyboard()
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue