refactor: use MeshUser object as setOwner parameter

master
andrekir 2023-03-27 15:30:42 -03:00
rodzic 451a20d207
commit 2d9f0a56f2
5 zmienionych plików z 26 dodań i 33 usunięć

Wyświetl plik

@ -4,6 +4,7 @@ package com.geeksville.mesh;
// Declare any non-default types here with import statements // Declare any non-default types here with import statements
parcelable DataPacket; parcelable DataPacket;
parcelable NodeInfo; parcelable NodeInfo;
parcelable MeshUser;
parcelable MyNodeInfo; parcelable MyNodeInfo;
/** /**
@ -52,11 +53,9 @@ interface IMeshService {
void subscribeReceiver(String packageName, String receiverName); void subscribeReceiver(String packageName, String receiverName);
/** /**
* Set the ID info for this node * Set the user info for this node
If myId is null, then the existing unique node ID is preserved, only the human visible longName/shortName is changed
*/ */
void setOwner(String myId, String longName, String shortName, boolean isLicensed); void setOwner(in MeshUser user);
/// Return my unique user ID string /// Return my unique user ID string
String getMyId(); String getMyId();

Wyświetl plik

@ -460,10 +460,9 @@ class UIViewModel @Inject constructor(
} }
} }
// clean up all this nasty owner state management FIXME fun setOwner(user: MeshUser) = with(user) {
fun setOwner(longName: String? = null, shortName: String? = null, isLicensed: Boolean? = null) {
longName?.trim()?.let { ownerName -> longName.trim().let { ownerName ->
// note: we allow an empty user string to be written to prefs // note: we allow an empty user string to be written to prefs
_ownerName.value = ownerName _ownerName.value = ownerName
preferences.edit { putString("owner", ownerName) } preferences.edit { putString("owner", ownerName) }
@ -472,12 +471,8 @@ class UIViewModel @Inject constructor(
// Note: we are careful to not set a new unique ID // Note: we are careful to not set a new unique ID
if (_ownerName.value!!.isNotEmpty()) if (_ownerName.value!!.isNotEmpty())
try { try {
meshService?.setOwner( // Note: we use ?. here because we might be running in the emulator
null, meshService?.setOwner(user)
_ownerName.value,
shortName?.trim() ?: getInitials(_ownerName.value!!),
isLicensed ?: false
) // Note: we use ?. here because we might be running in the emulator
} catch (ex: RemoteException) { } catch (ex: RemoteException) {
errormsg("Can't set username on device, is device offline? ${ex.message}") errormsg("Can't set username on device, is device offline? ${ex.message}")
} }

Wyświetl plik

@ -1513,32 +1513,28 @@ class MeshService : Service(), Logging {
} }
/** /**
* Set our owner with either the new or old API * Send setOwner admin packet with [MeshProtos.User] protobuf
*/ */
fun setOwner(myId: String?, longName: String, shortName: String, isLicensed: Boolean) { fun setOwner(meshUser: MeshUser) = with(meshUser) {
val myNode = myNodeInfo val dest = nodeDBbyID[id]
if (myNode != null) { if (dest != null) {
val my = localNodeInfo?.user val old = dest.user
if (longName == my?.longName && shortName == my.shortName && isLicensed == my.isLicensed) if (longName == old?.longName && shortName == old.shortName && isLicensed == old.isLicensed)
debug("Ignoring nop owner change") debug("Ignoring nop owner change")
else { else {
debug("SetOwner Id: $myId longName: ${longName.anonymize} shortName: $shortName isLicensed: $isLicensed") debug("SetOwner Id: $id longName: ${longName.anonymize} shortName: $shortName isLicensed: $isLicensed")
val user = MeshProtos.User.newBuilder().also { val user = MeshProtos.User.newBuilder().also {
if (myId != null) // Only set the id if it was provided
it.id = myId
it.longName = longName it.longName = longName
it.shortName = shortName it.shortName = shortName
it.hwModel = my?.hwModel
it.isLicensed = isLicensed it.isLicensed = isLicensed
}.build() }.build()
// Also update our own map for our nodenum, by handling the packet just like packets from other users // Also update our own map for our nodenum, by handling the packet just like packets from other users
handleReceivedUser(dest.num, user)
handleReceivedUser(myNode.myNodeNum, user)
// encapsulate our payload in the proper protobufs and fire it off // encapsulate our payload in the proper protobufs and fire it off
val packet = newMeshPacketTo(myNodeNum).buildAdminPacket { val packet = newMeshPacketTo(dest.num).buildAdminPacket {
setOwner = user setOwner = user
} }
@ -1648,10 +1644,9 @@ class MeshService : Service(), Logging {
override fun getPacketId() = toRemoteExceptions { generatePacketId() } override fun getPacketId() = toRemoteExceptions { generatePacketId() }
override fun setOwner(myId: String?, longName: String, shortName: String, isLicensed: Boolean) = override fun setOwner(user: MeshUser) = toRemoteExceptions {
toRemoteExceptions { this@MeshService.setOwner(user)
this@MeshService.setOwner(myId, longName, shortName, isLicensed) }
}
override fun send(p: DataPacket) { override fun send(p: DataPacket) {
toRemoteExceptions { toRemoteExceptions {

Wyświetl plik

@ -125,7 +125,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel = viewModel()) {
userInput = ourNodeInfo?.user userInput = ourNodeInfo?.user
}, onSaveClicked = { }, onSaveClicked = {
focusManager.clearFocus() focusManager.clearFocus()
userInput?.let { viewModel.setOwner(it.longName, it.shortName, it.isLicensed) } userInput?.let { viewModel.setOwner(it) }
}) })
} }

Wyświetl plik

@ -37,6 +37,7 @@ import com.geeksville.mesh.databinding.SettingsFragmentBinding
import com.geeksville.mesh.model.BTScanModel import com.geeksville.mesh.model.BTScanModel
import com.geeksville.mesh.model.BluetoothViewModel import com.geeksville.mesh.model.BluetoothViewModel
import com.geeksville.mesh.model.UIViewModel import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.model.getInitials
import com.geeksville.mesh.repository.location.LocationRepository import com.geeksville.mesh.repository.location.LocationRepository
import com.geeksville.mesh.repository.radio.MockInterface import com.geeksville.mesh.repository.radio.MockInterface
import com.geeksville.mesh.repository.usb.UsbRepository import com.geeksville.mesh.repository.usb.UsbRepository
@ -169,7 +170,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
binding.provideLocationCheckbox.visibility = if (model.isConnected()) View.VISIBLE else View.GONE binding.provideLocationCheckbox.visibility = if (model.isConnected()) View.VISIBLE else View.GONE
if (connected == MeshService.ConnectionState.DISCONNECTED) if (connected == MeshService.ConnectionState.DISCONNECTED)
model.setOwner("") binding.usernameEditText.setText("")
if (requireContext().hasGps()) { if (requireContext().hasGps()) {
binding.provideLocationCheckbox.isEnabled = true binding.provideLocationCheckbox.isEnabled = true
@ -369,7 +370,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
binding.usernameEditText.onEditorAction(EditorInfo.IME_ACTION_DONE) { binding.usernameEditText.onEditorAction(EditorInfo.IME_ACTION_DONE) {
debug("received IME_ACTION_DONE") debug("received IME_ACTION_DONE")
val n = binding.usernameEditText.text.toString().trim() val n = binding.usernameEditText.text.toString().trim()
if (n.isNotEmpty()) model.setOwner(n) model.ourNodeInfo.value?.user?.let {
val user = it.copy(longName = n, shortName = getInitials(n))
if (n.isNotEmpty()) model.setOwner(user)
}
requireActivity().hideKeyboard() requireActivity().hideKeyboard()
} }