Clear drafts before saving new one

This may resolve https://github.com/FossifyOrg/Messages/issues/13
This commit is contained in:
Naveen Singh 2024-12-27 15:35:19 +05:30
parent 0294ada284
commit 6fba0bfdd8
No known key found for this signature in database
GPG key ID: AF5D43C216778C0B
2 changed files with 9 additions and 5 deletions

View file

@ -307,9 +307,9 @@ class ThreadActivity : SimpleActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
val draftMessage = binding.messageHolder.threadTypeMessage.value
if (binding.messageHolder.threadTypeMessage.value != "" && getAttachmentSelections().isEmpty()) { if (draftMessage.isNotEmpty() && getAttachmentSelections().isEmpty()) {
saveSmsDraft(binding.messageHolder.threadTypeMessage.value, threadId) saveSmsDraft(draftMessage, threadId)
} else { } else {
deleteSmsDraft(threadId) deleteSmsDraft(threadId)
} }

View file

@ -1081,6 +1081,8 @@ fun Context.getAllDrafts(): HashMap<Long, String?> {
} }
fun Context.saveSmsDraft(body: String, threadId: Long) { fun Context.saveSmsDraft(body: String, threadId: Long) {
deleteSmsDraft(threadId)
val uri = Sms.Draft.CONTENT_URI val uri = Sms.Draft.CONTENT_URI
val contentValues = ContentValues().apply { val contentValues = ContentValues().apply {
put(Sms.BODY, body) put(Sms.BODY, body)
@ -1092,6 +1094,7 @@ fun Context.saveSmsDraft(body: String, threadId: Long) {
try { try {
contentResolver.insert(uri, contentValues) contentResolver.insert(uri, contentValues)
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e)
} }
} }
@ -1102,14 +1105,15 @@ fun Context.deleteSmsDraft(threadId: Long) {
val selectionArgs = arrayOf(threadId.toString()) val selectionArgs = arrayOf(threadId.toString())
try { try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null) val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor.use { cursor?.use {
if (cursor?.moveToFirst() == true) { while (cursor.moveToNext()) {
val draftId = cursor.getLong(0) val draftId = cursor.getLong(0)
val draftUri = Uri.withAppendedPath(Sms.CONTENT_URI, "/${draftId}") val draftUri = Uri.withAppendedPath(Sms.CONTENT_URI, "/${draftId}")
contentResolver.delete(draftUri, null, null) contentResolver.delete(draftUri, null, null)
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e)
} }
} }