forked from upstream-mirrors/quik
#42 - Multi selection for messages
This commit is contained in:
@@ -30,7 +30,6 @@ import com.jakewharton.rxbinding2.widget.textChanges
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.QkDialog
|
||||
import common.base.QkThemedActivity
|
||||
import common.util.extensions.autoScrollToStart
|
||||
import common.util.extensions.setBackgroundTint
|
||||
@@ -57,12 +56,10 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val menuReadyIntent: Observable<Unit> = menu.map { Unit }
|
||||
override val callIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val infoIntent: Subject<Unit> = PublishSubject.create()
|
||||
override val optionsItemIntent: Subject<Int> = PublishSubject.create()
|
||||
override val messageClickIntent: Subject<Message> by lazy { messageAdapter.clicks }
|
||||
override val messageLongClickIntent: Subject<Message> by lazy { messageAdapter.longClicks }
|
||||
override val messagesSelectedIntent by lazy { messageAdapter.selectionChanges }
|
||||
override val cancelSendingIntent: Subject<Message> by lazy { messageAdapter.cancelSending }
|
||||
override val menuItemIntent: Subject<Int> by lazy { dialog.adapter.menuItemClicks }
|
||||
override val attachmentDeletedIntent: Subject<Attachment> by lazy { attachmentAdapter.attachmentDeleted }
|
||||
override val textChangedIntent by lazy { message.textChanges() }
|
||||
override val attachIntent by lazy { attach.clicks() }
|
||||
@@ -70,10 +67,10 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
override val galleryIntent by lazy { gallery.clicks() }
|
||||
override val inputContentIntent by lazy { message.inputContentSelected }
|
||||
override val sendIntent by lazy { send.clicks() }
|
||||
override val backPressedIntent: Subject<Unit> = PublishSubject.create()
|
||||
|
||||
@Inject lateinit var chipsAdapter: ChipsAdapter
|
||||
@Inject lateinit var contactsAdapter: ContactAdapter
|
||||
@Inject lateinit var dialog: QkDialog
|
||||
@Inject lateinit var messageAdapter: MessagesAdapter
|
||||
@Inject lateinit var attachmentAdapter: AttachmentAdapter
|
||||
|
||||
@@ -173,6 +170,11 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
|
||||
threadId.onNext(state.selectedConversation)
|
||||
|
||||
title = when (state.selectedMessages) {
|
||||
0 -> state.conversationtitle
|
||||
else -> getString(R.string.compose_title_selected, state.selectedMessages)
|
||||
}
|
||||
|
||||
toolbarTitle.setVisible(!state.editingMode)
|
||||
chips.setVisible(state.editingMode)
|
||||
contacts.setVisible(state.contactsVisible)
|
||||
@@ -182,8 +184,11 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
if (state.editingMode && chips.adapter == null) chips.adapter = chipsAdapter
|
||||
if (state.editingMode && contacts.adapter == null) contacts.adapter = contactsAdapter
|
||||
|
||||
toolbar.menu.findItem(R.id.call)?.isVisible = !state.editingMode
|
||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode
|
||||
toolbar.menu.findItem(R.id.call)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
||||
toolbar.menu.findItem(R.id.copy)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
toolbar.menu.findItem(R.id.delete)?.isVisible = !state.editingMode && state.selectedMessages > 0
|
||||
toolbar.menu.findItem(R.id.forward)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
|
||||
if (chipsAdapter.data.isEmpty() && state.selectedContacts.isNotEmpty()) {
|
||||
message.showKeyboard()
|
||||
@@ -200,21 +205,18 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
camera.setVisible(state.attaching)
|
||||
gallery.setVisible(state.attaching)
|
||||
|
||||
if (title != state.title) title = state.title
|
||||
|
||||
counter.text = state.remaining
|
||||
counter.setVisible(counter.text.isNotBlank())
|
||||
|
||||
send.isEnabled = state.canSend
|
||||
}
|
||||
|
||||
override fun setDraft(draft: String) {
|
||||
message.setText(draft)
|
||||
override fun clearSelection() {
|
||||
messageAdapter.clearSelection()
|
||||
}
|
||||
|
||||
override fun showMenu(menuItems: List<common.MenuItem>) {
|
||||
dialog.adapter.data = menuItems
|
||||
dialog.show(this)
|
||||
override fun setDraft(draft: String) {
|
||||
message.setText(draft)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
@@ -223,13 +225,12 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.call -> callIntent.onNext(Unit)
|
||||
R.id.info -> infoIntent.onNext(Unit)
|
||||
else -> return super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
optionsItemIntent.onNext(item.itemId)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
backPressedIntent.onNext(Unit)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,8 +30,9 @@ data class ComposeState(
|
||||
val contactsVisible: Boolean = false,
|
||||
val selectedConversation: Long = 0,
|
||||
val selectedContacts: List<Contact> = ArrayList(),
|
||||
val title: String = "",
|
||||
val conversationtitle: String = "",
|
||||
val messages: Pair<Conversation, RealmResults<Message>>? = null,
|
||||
val selectedMessages: Int = 0,
|
||||
val attachments: List<Attachment> = ArrayList(),
|
||||
val attaching: Boolean = false,
|
||||
val remaining: String = "",
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
package feature.compose
|
||||
|
||||
import android.support.v13.view.inputmethod.InputContentInfoCompat
|
||||
import common.MenuItem
|
||||
import common.base.QkView
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.Subject
|
||||
@@ -35,12 +34,10 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val menuReadyIntent: Observable<Unit>
|
||||
val callIntent: Subject<Unit>
|
||||
val infoIntent: Subject<Unit>
|
||||
val optionsItemIntent: Observable<Int>
|
||||
val messageClickIntent: Subject<Message>
|
||||
val messageLongClickIntent: Subject<Message>
|
||||
val messagesSelectedIntent: Observable<List<Long>>
|
||||
val cancelSendingIntent: Subject<Message>
|
||||
val menuItemIntent: Subject<Int>
|
||||
val attachmentDeletedIntent: Subject<Attachment>
|
||||
val textChangedIntent: Observable<CharSequence>
|
||||
val attachIntent: Observable<Unit>
|
||||
@@ -49,7 +46,8 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val inputContentIntent: Observable<InputContentInfoCompat>
|
||||
val sendIntent: Observable<Unit>
|
||||
|
||||
fun showMenu(menuItems: List<MenuItem>)
|
||||
fun clearSelection()
|
||||
fun setDraft(draft: String)
|
||||
val backPressedIntent: Observable<Unit>
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,8 +32,8 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.ProgressBar
|
||||
import com.jakewharton.rxbinding2.view.RxView
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.jakewharton.rxbinding2.view.longClicks
|
||||
import com.moez.QKSMS.R
|
||||
import common.Navigator
|
||||
import common.base.QkRealmAdapter
|
||||
@@ -80,7 +80,6 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
val clicks: Subject<Message> = PublishSubject.create<Message>()
|
||||
val longClicks: Subject<Message> = PublishSubject.create<Message>()
|
||||
val cancelSending: Subject<Message> = PublishSubject.create<Message>()
|
||||
|
||||
var data: Pair<Conversation, RealmResults<Message>>? = null
|
||||
@@ -106,7 +105,7 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
private val layoutInflater = LayoutInflater.from(context)
|
||||
private val contactCache = ContactCache()
|
||||
private val selected = HashMap<Long, Boolean>()
|
||||
private val expanded = HashMap<Long, Boolean>()
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
private var theme = colors.theme
|
||||
@@ -143,6 +142,9 @@ class MessagesAdapter @Inject constructor(
|
||||
.subscribe { color -> view.body.setBackgroundTint(color) }
|
||||
}
|
||||
|
||||
disposables += colors.ripple
|
||||
.subscribe { res -> view.setBackgroundResource(res) }
|
||||
|
||||
view.body.forwardTouches(view)
|
||||
|
||||
return QkViewHolder(view)
|
||||
@@ -159,12 +161,24 @@ class MessagesAdapter @Inject constructor(
|
||||
val next = if (position == itemCount - 1) null else getItem(position + 1)
|
||||
val view = viewHolder.itemView
|
||||
|
||||
RxView.clicks(view).subscribe {
|
||||
clicks.onNext(message)
|
||||
selected[message.id] = view.status.visibility != View.VISIBLE
|
||||
bindStatus(viewHolder, position)
|
||||
view.clicks().subscribe {
|
||||
when (toggleSelection(message.id, false)) {
|
||||
true -> view.isSelected = isSelected(message.id)
|
||||
false -> {
|
||||
clicks.onNext(message)
|
||||
expanded[message.id] = view.status.visibility != View.VISIBLE
|
||||
bindStatus(viewHolder, message, next)
|
||||
}
|
||||
}
|
||||
}
|
||||
RxView.longClicks(view).subscribe { longClicks.onNext(message) }
|
||||
view.longClicks().subscribe {
|
||||
toggleSelection(message.id)
|
||||
view.isSelected = isSelected(message.id)
|
||||
}
|
||||
|
||||
|
||||
// Update the selected state
|
||||
view.isSelected = isSelected(message.id)
|
||||
|
||||
|
||||
// Bind the cancel view
|
||||
@@ -191,7 +205,7 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
|
||||
// Bind the message status
|
||||
bindStatus(viewHolder, position)
|
||||
bindStatus(viewHolder, message, next)
|
||||
|
||||
|
||||
// Bind the timestamp
|
||||
@@ -284,9 +298,7 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindStatus(viewHolder: QkViewHolder, position: Int) {
|
||||
val message = getItem(position)!!
|
||||
val next = if (position == itemCount - 1) null else getItem(position + 1)
|
||||
private fun bindStatus(viewHolder: QkViewHolder, message: Message, next: Message?) {
|
||||
val view = viewHolder.itemView
|
||||
|
||||
val age = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - message.date)
|
||||
@@ -301,10 +313,10 @@ class MessagesAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
view.status.setVisible(when {
|
||||
selected[message.id] == true -> true
|
||||
expanded[message.id] == true -> true
|
||||
message.isSending() -> true
|
||||
message.isFailedMessage() -> true
|
||||
selected[message.id] == false -> false
|
||||
expanded[message.id] == false -> false
|
||||
conversation?.recipients?.size ?: 0 > 1 && !message.isMe() && next?.compareSender(message) != true -> true
|
||||
message.isDelivered() && next?.isDelivered() != true && age <= TIMESTAMP_THRESHOLD -> true
|
||||
else -> false
|
||||
|
||||
Reference in New Issue
Block a user