forked from upstream-mirrors/quik
#42 - Multi selection for messages
This commit is contained in:
@@ -29,7 +29,6 @@ import com.mlsdev.rximagepicker.Sources
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.MenuItem
|
||||
import common.Navigator
|
||||
import common.base.QkViewModel
|
||||
import common.util.ClipboardUtils
|
||||
@@ -38,7 +37,7 @@ import common.util.filter.ContactFilter
|
||||
import injection.appComponent
|
||||
import interactor.CancelDelayedMessage
|
||||
import interactor.ContactSync
|
||||
import interactor.DeleteMessage
|
||||
import interactor.DeleteMessages
|
||||
import interactor.MarkRead
|
||||
import interactor.RetrySending
|
||||
import interactor.SendMessage
|
||||
@@ -79,7 +78,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
@Inject lateinit var sendMessage: SendMessage
|
||||
@Inject lateinit var retrySending: RetrySending
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var deleteMessage: DeleteMessage
|
||||
@Inject lateinit var deleteMessages: DeleteMessages
|
||||
|
||||
private var sharedText: String = ""
|
||||
private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(ArrayList())
|
||||
@@ -89,10 +88,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
private val conversation: Observable<Conversation>
|
||||
private val messages: Observable<List<Message>>
|
||||
|
||||
private val menuCopy by lazy { MenuItem(context.getString(R.string.compose_menu_copy), 0) }
|
||||
private val menuForward by lazy { MenuItem(context.getString(R.string.compose_menu_forward), 1) }
|
||||
private val menuDelete by lazy { MenuItem(context.getString(R.string.compose_menu_delete), 2) }
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
|
||||
@@ -178,7 +173,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
disposables += conversation
|
||||
.distinctUntilChanged { conversation -> conversation.getTitle() }
|
||||
.subscribe { conversation ->
|
||||
newState { it.copy(title = conversation.getTitle()) }
|
||||
newState { it.copy(conversationtitle = conversation.getTitle()) }
|
||||
}
|
||||
|
||||
disposables += attachments
|
||||
@@ -276,7 +271,8 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.subscribe { newState { it.copy() } }
|
||||
|
||||
// Open the phone dialer if the call button is clicked
|
||||
view.callIntent
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.call }
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.map { conversation -> conversation.recipients.first() }
|
||||
.map { recipient -> recipient.address }
|
||||
@@ -284,11 +280,45 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.subscribe { address -> navigator.makePhoneCall(address) }
|
||||
|
||||
// Open the conversation settings if info button is clicked
|
||||
view.infoIntent
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.info }
|
||||
.withLatestFrom(conversation, { _, conversation -> conversation })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { conversation -> navigator.showConversationInfo(conversation.id) }
|
||||
|
||||
// Copy the message contents
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.copy }
|
||||
.withLatestFrom(view.messagesSelectedIntent, { _, messages ->
|
||||
messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message ->
|
||||
ClipboardUtils.copy(context, message.getText())
|
||||
context.makeToast(R.string.toast_copied)
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
// Delete the messages
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.delete }
|
||||
.withLatestFrom(view.messagesSelectedIntent, conversation, { _, messages, conversation ->
|
||||
deleteMessages.execute(DeleteMessages.Params(messages, conversation.id))
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
// Forward the message
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.forward }
|
||||
.withLatestFrom(view.messagesSelectedIntent, { _, messages ->
|
||||
messages?.firstOrNull()?.let { messageRepo.getMessage(it) }?.let { message ->
|
||||
val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() }
|
||||
navigator.showCompose(message.body, images)
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
// Retry sending
|
||||
view.messageClickIntent
|
||||
.filter { message -> message.isFailedMessage() }
|
||||
@@ -296,10 +326,11 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Show menu
|
||||
view.messageLongClickIntent
|
||||
// Update the State when the message selected count changes
|
||||
view.messagesSelectedIntent
|
||||
.map { selection -> selection.size }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.showMenu(listOf(menuCopy, menuForward, menuDelete)) }
|
||||
.subscribe { messages -> newState { it.copy(selectedMessages = messages, editingMode = false) } }
|
||||
|
||||
// Cancel sending a message
|
||||
view.cancelSendingIntent
|
||||
@@ -307,26 +338,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { message -> cancelMessage.execute(message.id) }
|
||||
|
||||
// Handle long-press menu item click
|
||||
view.menuItemIntent
|
||||
.withLatestFrom(view.messageLongClickIntent, { actionId, message ->
|
||||
when (actionId) {
|
||||
menuCopy.actionId -> {
|
||||
ClipboardUtils.copy(context, message.getText())
|
||||
context.makeToast(R.string.toast_copied)
|
||||
}
|
||||
|
||||
menuForward.actionId -> {
|
||||
val images = message.parts.filter { it.isImage() }.mapNotNull { it.getUri() }
|
||||
navigator.showCompose(message.body, images)
|
||||
}
|
||||
|
||||
menuDelete.actionId -> deleteMessage.execute(message.id)
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Save draft when the activity goes into the background
|
||||
view.activityVisibleIntent
|
||||
.filter { visible -> !visible }
|
||||
@@ -446,6 +457,21 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Navigate back
|
||||
view.optionsItemIntent
|
||||
.filter { it == android.R.id.home }
|
||||
.map { Unit }
|
||||
.mergeWith(view.backPressedIntent)
|
||||
.withLatestFrom(state, { _, state ->
|
||||
when {
|
||||
state.selectedMessages > 0 -> view.clearSelection()
|
||||
else -> newState { it.copy(hasError = true) }
|
||||
}
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user