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() {
super.onPause()
if (binding.messageHolder.threadTypeMessage.value != "" && getAttachmentSelections().isEmpty()) {
saveSmsDraft(binding.messageHolder.threadTypeMessage.value, threadId)
val draftMessage = binding.messageHolder.threadTypeMessage.value
if (draftMessage.isNotEmpty() && getAttachmentSelections().isEmpty()) {
saveSmsDraft(draftMessage, threadId)
} else {
deleteSmsDraft(threadId)
}

View file

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