Merge pull request #356 from ScriptTactics/feature/delete-messages

Merge feature/delete-messages into Master
This commit is contained in:
Andre Kirchhoff 2022-01-31 23:07:42 -03:00 committed by GitHub
commit 3e323683e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 8 deletions

View file

@ -66,6 +66,11 @@ interface IMeshService {
*/
void send(inout DataPacket packet);
void delete(int position);
void deleteAllMessages();
/**
Get the IDs of everyone on the mesh. You should also subscribe for NODE_CHANGE broadcasts.
*/

View file

@ -46,6 +46,20 @@ class MessagesState(private val ui: UIViewModel) : Logging {
messages.value = messagesList
}
fun removeMessage(m: DataPacket) {
debug("Removing message from view id=${m.id}")
messagesList.remove(m)
messages.value = messagesList
}
private fun removeAllMessages() {
debug("Removing all messages")
messagesList.clear()
messages.value = messagesList
}
fun updateStatus(id: Int, status: MessageStatus) {
// Super inefficent but this is rare
debug("Handling message status change $id: $status")
@ -80,4 +94,31 @@ class MessagesState(private val ui: UIViewModel) : Logging {
// FIXME - why is the first time we are called p is already in the list at this point?
addMessage(p)
}
fun deleteMessage(packet: DataPacket, position: Int) {
val service = ui.meshService
if (service != null) {
try {
service.delete(position)
} catch (ex: RemoteException) {
packet.errorMessage = "Error: ${ex.message}"
}
} else {
packet.errorMessage = "Error: No Mesh service"
}
removeMessage(packet)
}
fun deleteAllMessages() {
val service = ui.meshService
if (service != null) {
try {
service.deleteAllMessages()
} catch (ex: RemoteException) {
}
removeAllMessages()
}
}
}

View file

@ -1018,10 +1018,10 @@ class MeshService : Service(), Logging {
else
broadcastSecs * 1000L
if (prefs.locationShare == RadioConfigProtos.LocationSharing.LocDisabled) {
info("GPS location sharing is disabled")
desiredInterval = 0
}
if (prefs.locationShare == RadioConfigProtos.LocationSharing.LocDisabled) {
info("GPS location sharing is disabled")
desiredInterval = 0
}
// if (prefs.fixedPosition) {
// info("Node has fixed position, therefore not overriding position")
@ -1783,6 +1783,16 @@ class MeshService : Service(), Logging {
this@MeshService.setOwner(myId, longName, shortName)
}
override fun delete(position: Int) {
if (position >= 0) {
recentDataPackets.removeAt(position)
}
}
override fun deleteAllMessages() {
recentDataPackets.clear()
}
override fun send(p: DataPacket) {
toRemoteExceptions {
// Init from and id

View file

@ -1,5 +1,6 @@
package com.geeksville.mesh.ui
import android.app.AlertDialog
import android.graphics.Color
import android.os.Bundle
import android.text.InputType
@ -48,7 +49,7 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
private val binding get() = _binding!!
private val model: UIViewModel by activityViewModels()
// Allows textMultiline with IME_ACTION_SEND
fun EditText.onActionSend(func: () -> Unit) {
setImeOptions(EditorInfo.IME_ACTION_SEND)
@ -164,6 +165,27 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
// Set cardview offset and color.
val marginParams = holder.card.layoutParams as ViewGroup.MarginLayoutParams
val messageOffset = resources.getDimensionPixelOffset(R.dimen.message_offset)
holder.card.setOnLongClickListener {
val deleteMessageDialog = AlertDialog.Builder(context)
deleteMessageDialog.setMessage(R.string.delete_selected_message)
deleteMessageDialog.setPositiveButton(
R.string.delete
) { _, _ ->
model.messagesState.deleteMessage((messages[position]), position)
}
deleteMessageDialog.setNeutralButton(
R.string.cancel
) { _, _ ->
}
deleteMessageDialog.setNegativeButton(
R.string.delete_all_messages
) { _, _ ->
model.messagesState.deleteAllMessages()
}
deleteMessageDialog.create()
deleteMessageDialog.show()
true
}
if (isMe) {
marginParams.leftMargin = messageOffset
marginParams.rightMargin = 0
@ -244,7 +266,6 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.sendButton.setOnClickListener {
debug("sendButton click")
@ -282,8 +303,8 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
binding.textInputLayout.isEnabled =
model.isConnected.value != MeshService.ConnectionState.DISCONNECTED
// Just being connected is enough to allow sending texts I think
// && model.nodeDB.myId.value != null && model.radioConfig.value != null
// Just being connected is enough to allow sending texts I think
// && model.nodeDB.myId.value != null && model.radioConfig.value != null
}
model.isConnected.observe(viewLifecycleOwner, Observer { _ ->

View file

@ -120,4 +120,7 @@
<string name="why_camera_required">We must be granted access to the camera to read QR codes. No pictures or videos will be saved.</string>
<string name="modem_config_slow_short">Short Range / Slow</string>
<string name="modem_config_slow_medium">Medium Range / Slow</string>
<string name="delete_selected_message">Delete selected message?</string>
<string name="delete">Delete</string>
<string name="delete_all_messages">Delete All Messages</string>
</resources>