mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 13:48:52 +01:00
#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.compat.SubscriptionManagerCompat
|
||||||
import com.moez.QKSMS.extensions.asObservable
|
import com.moez.QKSMS.extensions.asObservable
|
||||||
import com.moez.QKSMS.extensions.mapNotNull
|
import com.moez.QKSMS.extensions.mapNotNull
|
||||||
|
import com.moez.QKSMS.interactor.DeleteMessages
|
||||||
import com.moez.QKSMS.interactor.MarkRead
|
import com.moez.QKSMS.interactor.MarkRead
|
||||||
import com.moez.QKSMS.interactor.SendMessage
|
import com.moez.QKSMS.interactor.SendMessage
|
||||||
import com.moez.QKSMS.model.Message
|
import com.moez.QKSMS.model.Message
|
||||||
@@ -45,8 +46,9 @@ import javax.inject.Inject
|
|||||||
import javax.inject.Named
|
import javax.inject.Named
|
||||||
|
|
||||||
class QkReplyViewModel @Inject constructor(
|
class QkReplyViewModel @Inject constructor(
|
||||||
@Named("threadId") threadId: Long,
|
@Named("threadId") private val threadId: Long,
|
||||||
private val conversationRepo: ConversationRepository,
|
private val conversationRepo: ConversationRepository,
|
||||||
|
private val deleteMessages: DeleteMessages,
|
||||||
private val markRead: MarkRead,
|
private val markRead: MarkRead,
|
||||||
private val messageRepo: MessageRepository,
|
private val messageRepo: MessageRepository,
|
||||||
private val navigator: Navigator,
|
private val navigator: Navigator,
|
||||||
@@ -110,10 +112,8 @@ class QkReplyViewModel @Inject constructor(
|
|||||||
// Mark read
|
// Mark read
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.read }
|
.filter { id -> id == R.id.read }
|
||||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
|
||||||
.map { conversation -> conversation.id }
|
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { threadId ->
|
.subscribe {
|
||||||
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
|
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +129,7 @@ class QkReplyViewModel @Inject constructor(
|
|||||||
// Show all messages
|
// Show all messages
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.expand }
|
.filter { id -> id == R.id.expand }
|
||||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
.map { messageRepo.getMessages(threadId) }
|
||||||
.map { conversation -> messageRepo.getMessages(conversation.id) }
|
|
||||||
.doOnNext(messages::onNext)
|
.doOnNext(messages::onNext)
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { newState { copy(expanded = true) } }
|
.subscribe { newState { copy(expanded = true) } }
|
||||||
@@ -138,18 +137,24 @@ class QkReplyViewModel @Inject constructor(
|
|||||||
// Show unread messages only
|
// Show unread messages only
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.collapse }
|
.filter { id -> id == R.id.collapse }
|
||||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
.map { messageRepo.getUnreadMessages(threadId) }
|
||||||
.map { conversation -> messageRepo.getUnreadMessages(conversation.id) }
|
|
||||||
.doOnNext(messages::onNext)
|
.doOnNext(messages::onNext)
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { newState { copy(expanded = false) } }
|
.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 conversation
|
||||||
view.menuItemIntent
|
view.menuItemIntent
|
||||||
.filter { id -> id == R.id.view }
|
.filter { id -> id == R.id.view }
|
||||||
.withLatestFrom(conversation) { _, conversation -> conversation }
|
.doOnNext { navigator.showConversation(threadId) }
|
||||||
.map { conversation -> conversation.id }
|
|
||||||
.doOnNext { threadId -> navigator.showConversation(threadId) }
|
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { newState { copy(hasError = true) } }
|
.subscribe { newState { copy(hasError = true) } }
|
||||||
|
|
||||||
@@ -178,18 +183,13 @@ class QkReplyViewModel @Inject constructor(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { remaining -> newState { copy(remaining = remaining) } }
|
.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
|
// Update the draft whenever the text is changed
|
||||||
view.textChangedIntent
|
view.textChangedIntent
|
||||||
.debounce(100, TimeUnit.MILLISECONDS)
|
.debounce(100, TimeUnit.MILLISECONDS)
|
||||||
.map { draft -> draft.toString() }
|
.map { draft -> draft.toString() }
|
||||||
.observeOn(Schedulers.io())
|
.observeOn(Schedulers.io())
|
||||||
.withLatestFrom(safeThreadId) { draft, threadId -> Pair(draft, threadId) }
|
|
||||||
.filter { (_, threadId) -> threadId != -1L }
|
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { (draft, threadId) -> conversationRepo.saveDraft(threadId, draft) }
|
.subscribe { draft -> conversationRepo.saveDraft(threadId, draft) }
|
||||||
|
|
||||||
// Toggle to the next sim slot
|
// Toggle to the next sim slot
|
||||||
view.changeSimIntent
|
view.changeSimIntent
|
||||||
@@ -212,16 +212,12 @@ class QkReplyViewModel @Inject constructor(
|
|||||||
.map { body -> body.toString() }
|
.map { body -> body.toString() }
|
||||||
.withLatestFrom(state, conversation) { body, state, conversation ->
|
.withLatestFrom(state, conversation) { body, state, conversation ->
|
||||||
val subId = state.subscription?.subscriptionId ?: -1
|
val subId = state.subscription?.subscriptionId ?: -1
|
||||||
val threadId = conversation.id
|
|
||||||
val addresses = conversation.recipients.map { it.address }
|
val addresses = conversation.recipients.map { it.address }
|
||||||
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
|
sendMessage.execute(SendMessage.Params(subId, threadId, addresses, body))
|
||||||
view.setDraft("")
|
view.setDraft("")
|
||||||
threadId
|
|
||||||
}
|
}
|
||||||
.doOnNext { threadId ->
|
.doOnNext {
|
||||||
markRead.execute(listOf(threadId)) {
|
markRead.execute(listOf(threadId)) { newState { copy(hasError = true) } }
|
||||||
newState { copy(hasError = true) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|||||||
@@ -42,6 +42,10 @@
|
|||||||
android:id="@+id/call"
|
android:id="@+id/call"
|
||||||
android:title="@string/qkreply_menu_call" />
|
android:title="@string/qkreply_menu_call" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/delete"
|
||||||
|
android:title="@string/qkreply_menu_delete" />
|
||||||
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/view"
|
android:id="@+id/view"
|
||||||
android:title="@string/qkreply_menu_view" />
|
android:title="@string/qkreply_menu_view" />
|
||||||
|
|||||||
@@ -301,6 +301,7 @@
|
|||||||
|
|
||||||
<string name="qkreply_menu_read">Mark read</string>
|
<string name="qkreply_menu_read">Mark read</string>
|
||||||
<string name="qkreply_menu_call">Call</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_expand">Show more</string>
|
||||||
<string name="qkreply_menu_collapse">Show less</string>
|
<string name="qkreply_menu_collapse">Show less</string>
|
||||||
<string name="qkreply_menu_view">Open conversation</string>
|
<string name="qkreply_menu_view">Open conversation</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user