Skip to content

Commit

Permalink
#1276 - Delete message option in QK Reply
Browse files Browse the repository at this point in the history
  • Loading branch information
moezbhatti committed Sep 19, 2018
1 parent f5ddca8 commit abff121
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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) } }
}

Expand All @@ -129,27 +129,32 @@ 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) } }

// 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) } }

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions presentation/src/main/res/menu/qkreply.xml
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions presentation/src/main/res/values/strings.xml
Expand Up @@ -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>
Expand Down

0 comments on commit abff121

Please sign in to comment.