Long-press to delete message

This commit is contained in:
Moez Bhatti
2017-11-23 01:12:00 -05:00
parent 4d7a3276cc
commit 95ca4d05b4
3 changed files with 29 additions and 0 deletions
@@ -182,6 +182,15 @@ class MessageRepository @Inject constructor(
updateMessageFromUri(uri, values)
}
fun deleteMessage(messageId: Long) {
val realm = Realm.getDefaultInstance()
realm.where(Message::class.java).equalTo("id", messageId).findFirst()?.let { message ->
context.contentResolver.delete(message.getUri(), null, null)
realm.executeTransaction { message.deleteFromRealm() }
}
realm.close()
}
fun updateMessageFromUri(uri: Uri, values: ContentValues) {
val contentResolver = context.contentResolver
Flowable.just(values)
@@ -0,0 +1,14 @@
package com.moez.QKSMS.domain.interactor
import com.moez.QKSMS.data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class DeleteMessage @Inject constructor(private val messageRepository: MessageRepository): Interactor<Long, Long>() {
override fun buildObservable(params: Long): Flowable<Long> {
return Flowable.just(params)
.doOnNext { messageId -> messageRepository.deleteMessage(messageId) }
}
}
@@ -7,6 +7,7 @@ import com.moez.QKSMS.common.util.ClipboardUtils
import com.moez.QKSMS.common.util.extensions.makeToast
import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.repository.MessageRepository
import com.moez.QKSMS.domain.interactor.DeleteMessage
import com.moez.QKSMS.domain.interactor.MarkRead
import com.moez.QKSMS.domain.interactor.SendMessage
import com.moez.QKSMS.presentation.base.QkViewModel
@@ -19,6 +20,7 @@ class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, Messages
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var sendMessage: SendMessage
@Inject lateinit var markRead: MarkRead
@Inject lateinit var deleteMessage: DeleteMessage
private var conversation: Conversation? = null
@@ -62,6 +64,10 @@ class MessagesViewModel(val threadId: Long) : QkViewModel<MessagesView, Messages
ClipboardUtils.copy(context, message.body)
context.makeToast(R.string.toast_copied)
}
intents += view.deleteMessageIntent.subscribe { message ->
deleteMessage.execute(message.id)
}
}
fun dataChanged() {