refactor: simplify setOwner logic

This commit is contained in:
andrekir 2023-05-26 16:18:02 -03:00
parent 956db658e9
commit 7ca724142f
3 changed files with 10 additions and 27 deletions

View file

@ -426,8 +426,6 @@ class UIViewModel @Inject constructor(
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
nodeDB.setMyId(service.myId)
val ownerName = nodes[service.myId]?.user?.longName
_ownerName.value = ownerName
} catch (ex: Exception) {
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)) {
override fun setValue(value: Boolean) {
super.setValue(value)
@ -536,22 +528,8 @@ class UIViewModel @Inject constructor(
}
}
fun setOwner(user: MeshUser) = with(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 setOwner(user: User) {
setRemoteOwner(myNodeNum ?: return, user)
}
fun setRemoteOwner(destNum: Int, user: User) {
@ -726,7 +704,7 @@ class UIViewModel @Inject constructor(
longName = if (hasLongName()) longName else it.longName,
shortName = if (hasShortName()) shortName else it.shortName
)
setOwner(user)
setOwner(user.toProto())
}
if (hasChannelUrl()) {
setChannels(ChannelSet(Uri.parse(channelUrl)))