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
parcelable DataPacket;
parcelable NodeInfo;
parcelable MeshUser;
parcelable MyNodeInfo;
/**
@ -52,11 +53,9 @@ interface IMeshService {
void subscribeReceiver(String packageName, String receiverName);
/**
* Set the ID info for this node
If myId is null, then the existing unique node ID is preserved, only the human visible longName/shortName is changed
* Set the user info for this node
*/
void setOwner(String myId, String longName, String shortName, boolean isLicensed);
void setOwner(in MeshUser user);
/// Return my unique user ID string
String getMyId();

Wyświetl plik

@ -460,10 +460,9 @@ class UIViewModel @Inject constructor(
}
}
// clean up all this nasty owner state management FIXME
fun setOwner(longName: String? = null, shortName: String? = null, isLicensed: Boolean? = null) {
fun setOwner(user: MeshUser) = with(user) {
longName?.trim()?.let { ownerName ->
longName.trim().let { ownerName ->
// note: we allow an empty user string to be written to prefs
_ownerName.value = 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
if (_ownerName.value!!.isNotEmpty())
try {
meshService?.setOwner(
null,
_ownerName.value,
shortName?.trim() ?: getInitials(_ownerName.value!!),
isLicensed ?: false
) // Note: we use ?. here because we might be running in the emulator
// 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}")
}

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) {
val myNode = myNodeInfo
if (myNode != null) {
val my = localNodeInfo?.user
if (longName == my?.longName && shortName == my.shortName && isLicensed == my.isLicensed)
fun setOwner(meshUser: MeshUser) = with(meshUser) {
val dest = nodeDBbyID[id]
if (dest != null) {
val old = dest.user
if (longName == old?.longName && shortName == old.shortName && isLicensed == old.isLicensed)
debug("Ignoring nop owner change")
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 {
if (myId != null) // Only set the id if it was provided
it.id = myId
it.longName = longName
it.shortName = shortName
it.hwModel = my?.hwModel
it.isLicensed = isLicensed
}.build()
// Also update our own map for our nodenum, by handling the packet just like packets from other users
handleReceivedUser(myNode.myNodeNum, user)
handleReceivedUser(dest.num, user)
// encapsulate our payload in the proper protobufs and fire it off
val packet = newMeshPacketTo(myNodeNum).buildAdminPacket {
val packet = newMeshPacketTo(dest.num).buildAdminPacket {
setOwner = user
}
@ -1648,10 +1644,9 @@ class MeshService : Service(), Logging {
override fun getPacketId() = toRemoteExceptions { generatePacketId() }
override fun setOwner(myId: String?, longName: String, shortName: String, isLicensed: Boolean) =
toRemoteExceptions {
this@MeshService.setOwner(myId, longName, shortName, isLicensed)
}
override fun setOwner(user: MeshUser) = toRemoteExceptions {
this@MeshService.setOwner(user)
}
override fun send(p: DataPacket) {
toRemoteExceptions {

Wyświetl plik

@ -125,7 +125,7 @@ fun DeviceSettingsItemList(viewModel: UIViewModel = viewModel()) {
userInput = ourNodeInfo?.user
}, onSaveClicked = {
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.BluetoothViewModel
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.model.getInitials
import com.geeksville.mesh.repository.location.LocationRepository
import com.geeksville.mesh.repository.radio.MockInterface
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
if (connected == MeshService.ConnectionState.DISCONNECTED)
model.setOwner("")
binding.usernameEditText.setText("")
if (requireContext().hasGps()) {
binding.provideLocationCheckbox.isEnabled = true
@ -369,7 +370,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
binding.usernameEditText.onEditorAction(EditorInfo.IME_ACTION_DONE) {
debug("received IME_ACTION_DONE")
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()
}