forked from upstream-mirrors/quik
#1276 - Delete message option in QK Reply
This commit is contained in:
@@ -26,6 +26,7 @@ import com.moez.QKSMS.common.base.QkViewModel
|
||||
import com.moez.QKSMS.compat.SubscriptionManagerCompat
|
||||
import com.moez.QKSMS.extensions.asObservable
|
||||
import com.moez.QKSMS.extensions.mapNotNull
|
||||
import com.moez.QKSMS.interactor.DeleteMessages
|
||||
import com.moez.QKSMS.interactor.MarkRead
|
||||
import com.moez.QKSMS.interactor.SendMessage
|
||||
import com.moez.QKSMS.model.Message
|
||||
@@ -45,8 +46,9 @@ import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
class QkReplyViewModel @Inject constructor(
|
||||
@Named("threadId") threadId: Long,
|
||||
@Named("threadId") private val threadId: Long,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val deleteMessages: DeleteMessages,
|
||||
private val markRead: MarkRead,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val navigator: Navigator,
|
||||
@@ -110,10 +112,8 @@ class QkReplyViewModel @Inject constructor(
|
||||
// Mark read
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.read }
|
||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
||||
.map { conversation -> conversation.id }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { threadId ->
|
||||
.subscribe {
|
||||
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
|
||||
}
|
||||
|
||||
@@ -129,8 +129,7 @@ class QkReplyViewModel @Inject constructor(
|
||||
// Show all messages
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.expand }
|
||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
||||
.map { conversation -> messageRepo.getMessages(conversation.id) }
|
||||
.map { messageRepo.getMessages(threadId) }
|
||||
.doOnNext(messages::onNext)
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(expanded = true) } }
|
||||
@@ -138,18 +137,24 @@ class QkReplyViewModel @Inject constructor(
|
||||
// Show unread messages only
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.collapse }
|
||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
||||
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
|
||||
.map { messageRepo.getUnreadMessages(threadId) }
|
||||
.doOnNext(messages::onNext)
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(expanded = false) } }
|
||||
|
||||
// Delete new messages
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.delete }
|
||||
.observeOn(Schedulers.io())
|
||||
.map { messageRepo.getUnreadMessages(threadId).map { it.id } }
|
||||
.map { messages -> DeleteMessages.Params(messages, threadId) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { deleteMessages.execute(it) { newState { copy(hasError = true) } } }
|
||||
|
||||
// View conversation
|
||||
view.menuItemIntent
|
||||
.filter { id -> id == R.id.view }
|
||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
||||
.map { conversation -> conversation.id }
|
||||
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
||||
.doOnNext { navigator.showConversation(threadId) }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { copy(hasError = true) } }
|
||||
|
||||
@@ -178,18 +183,13 @@ class QkReplyViewModel @Inject constructor(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { remaining -> newState { copy(remaining = remaining) } }
|
||||
|
||||
// This allows the conversation to be mapped to a threadId in the correct thread
|
||||
val safeThreadId = conversation.map { it.takeIf { it.isValid }?.id ?: -1 }
|
||||
|
||||
// Update the draft whenever the text is changed
|
||||
view.textChangedIntent
|
||||
.debounce(100, TimeUnit.MILLISECONDS)
|
||||
.map { draft -> draft.toString() }
|
||||
.observeOn(Schedulers.io())
|
||||
.withLatestFrom(safeThreadId) { draft, threadId -> Pair(draft, threadId) }
|
||||
.filter { (_, threadId) -> threadId != -1L }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { (draft, threadId) -> conversationRepo.saveDraft(threadId, draft) }
|
||||
.subscribe { draft -> conversationRepo.saveDraft(threadId, draft) }
|
||||
|
||||
// Toggle to the next sim slot
|
||||
view.changeSimIntent
|
||||
@@ -212,16 +212,12 @@ class QkReplyViewModel @Inject constructor(
|
||||
.map { body -> body.toString() }
|
||||
.withLatestFrom(state, conversation) { body, state, conversation ->
|
||||
val subId = state.subscription?.subscriptionId ?: -1
|
||||
val threadId = conversation.id
|
||||
val addresses = conversation.recipients.map { it.address }
|
||||
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
|
||||
view.setDraft("")
|
||||
threadId
|
||||
}
|
||||
.doOnNext { threadId ->
|
||||
markRead.execute(listOf(threadId)) {
|
||||
newState { copy(hasError = true) }
|
||||
}
|
||||
.doOnNext {
|
||||
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
|
||||
}
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
@@ -42,6 +42,10 @@
|
||||
android:id="@+id/call"
|
||||
android:title="@string/qkreply_menu_call" />
|
||||
|
||||
<item
|
||||
android:id="@+id/delete"
|
||||
android:title="@string/qkreply_menu_delete" />
|
||||
|
||||
<item
|
||||
android:id="@+id/view"
|
||||
android:title="@string/qkreply_menu_view" />
|
||||
|
||||
@@ -301,6 +301,7 @@
|
||||
|
||||
<string name="qkreply_menu_read">Mark read</string>
|
||||
<string name="qkreply_menu_call">Call</string>
|
||||
<string name="qkreply_menu_delete">Delete</string>
|
||||
<string name="qkreply_menu_expand">Show more</string>
|
||||
<string name="qkreply_menu_collapse">Show less</string>
|
||||
<string name="qkreply_menu_view">Open conversation</string>
|
||||
|
||||
Reference in New Issue
Block a user