Add multi-select options for blocked conversations

This commit is contained in:
moezbhatti
2019-08-29 22:52:36 +02:00
parent cdfd75fc01
commit b14cc53d7d
7 changed files with 172 additions and 4 deletions
@@ -48,7 +48,7 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
emptyView?.setVisible(data.isLoaded && data.isEmpty())
}
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
val selectionChanges: Subject<List<Long>> = BehaviorSubject.createDefault(listOf())
private var selection = listOf<Long>()
@@ -37,7 +37,16 @@ class BlockedMessagesAdapter @Inject constructor() : QkRealmAdapter<Conversation
return QkViewHolder(view).apply {
view.setOnClickListener {
val conversation = getItem(adapterPosition) ?: return@setOnClickListener
clicks.onNext(conversation.id)
when (toggleSelection(conversation.id, false)) {
true -> view.isActivated = isSelected(conversation.id)
false -> clicks.onNext(conversation.id)
}
}
view.setOnLongClickListener {
val conversation = getItem(adapterPosition) ?: return@setOnLongClickListener true
toggleSelection(conversation.id)
view.isActivated = isSelected(conversation.id)
true
}
}
}
@@ -46,6 +55,8 @@ class BlockedMessagesAdapter @Inject constructor() : QkRealmAdapter<Conversation
val conversation = getItem(position) ?: return
val view = holder.containerView
view.isActivated = isSelected(conversation.id)
view.avatars.contacts = conversation.recipients
view.title.collapseEnabled = conversation.recipients.size > 1
view.title.text = conversation.getTitle()
@@ -18,21 +18,37 @@
*/
package com.moez.QKSMS.feature.blocking.messages
import android.app.AlertDialog
import android.content.Context
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkController
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.feature.blocking.BlockingDialog
import com.moez.QKSMS.injection.appComponent
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.blocked_messages_controller.*
import kotlinx.android.synthetic.main.container_activity.*
import javax.inject.Inject
class BlockedMessagesController : QkController<BlockedMessagesView, BlockedMessagesState, BlockedMessagesPresenter>(),
BlockedMessagesView {
override val menuReadyIntent: Subject<Unit> = PublishSubject.create()
override val optionsItemIntent: Subject<Int> = PublishSubject.create()
override val conversationClicks by lazy { blockedMessagesAdapter.clicks }
override val selectionChanges by lazy { blockedMessagesAdapter.selectionChanges }
override val confirmDeleteIntent: Subject<List<Long>> = PublishSubject.create()
override val backClicked: Subject<Unit> = PublishSubject.create()
@Inject lateinit var blockedMessagesAdapter: BlockedMessagesAdapter
@Inject lateinit var blockingDialog: BlockingDialog
@Inject lateinit var colors: Colors
@Inject lateinit var context: Context
@Inject override lateinit var presenter: BlockedMessagesPresenter
init {
@@ -43,7 +59,6 @@ class BlockedMessagesController : QkController<BlockedMessagesView, BlockedMessa
override fun onViewCreated() {
super.onViewCreated()
blockedMessagesAdapter.emptyView = empty
conversations.adapter = blockedMessagesAdapter
}
@@ -53,10 +68,55 @@ class BlockedMessagesController : QkController<BlockedMessagesView, BlockedMessa
presenter.bindIntents(this)
setTitle(R.string.blocked_messages_title)
showBackButton(true)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.blocked_messages, menu)
menuReadyIntent.onNext(Unit)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
optionsItemIntent.onNext(item.itemId)
return true
}
override fun handleBack(): Boolean {
backClicked.onNext(Unit)
return true
}
override fun render(state: BlockedMessagesState) {
blockedMessagesAdapter.updateData(state.data)
themedActivity?.toolbar?.menu?.findItem(R.id.block)?.isVisible = state.selected > 0
themedActivity?.toolbar?.menu?.findItem(R.id.delete)?.isVisible = state.selected > 0
setTitle(when (state.selected) {
0 -> context.getString(R.string.blocked_messages_title)
else -> context.getString(R.string.main_title_selected, state.selected)
})
}
override fun clearSelection() = blockedMessagesAdapter.clearSelection()
override fun showBlockingDialog(conversations: List<Long>, block: Boolean) {
blockingDialog.show(activity!!, conversations, block)
}
override fun showDeleteDialog(conversations: List<Long>) {
val count = conversations.size
AlertDialog.Builder(activity)
.setTitle(R.string.dialog_delete_title)
.setMessage(resources?.getQuantityString(R.plurals.dialog_delete_message, count, count))
.setPositiveButton(R.string.button_delete) { _, _ -> confirmDeleteIntent.onNext(conversations) }
.setNegativeButton(R.string.button_cancel, null)
.show()
}
override fun goBack() {
router.popCurrentController()
}
}
@@ -18,15 +18,21 @@
*/
package com.moez.QKSMS.feature.blocking.messages
import com.moez.QKSMS.R
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.interactor.DeleteConversations
import com.moez.QKSMS.repository.ConversationRepository
import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.rxkotlin.withLatestFrom
import javax.inject.Inject
class BlockedMessagesPresenter @Inject constructor(
conversationRepo: ConversationRepository,
private val blockingClient: BlockingClient,
private val deleteConversations: DeleteConversations,
private val navigator: Navigator
) : QkPresenter<BlockedMessagesView, BlockedMessagesState>(BlockedMessagesState(
data = conversationRepo.getBlockedConversations()
@@ -35,9 +41,50 @@ class BlockedMessagesPresenter @Inject constructor(
override fun bindIntents(view: BlockedMessagesView) {
super.bindIntents(view)
view.menuReadyIntent
.autoDisposable(view.scope())
.subscribe { newState { copy() } }
view.optionsItemIntent
.withLatestFrom(view.selectionChanges) { itemId, conversations ->
when (itemId) {
R.id.block -> {
view.showBlockingDialog(conversations, false)
view.clearSelection()
}
R.id.delete -> {
view.showDeleteDialog(conversations)
}
}
}
.autoDisposable(view.scope())
.subscribe()
view.confirmDeleteIntent
.autoDisposable(view.scope())
.subscribe { conversations ->
deleteConversations.execute(conversations)
view.clearSelection()
}
view.conversationClicks
.autoDisposable(view.scope())
.subscribe { threadId -> navigator.showConversation(threadId) }
view.selectionChanges
.autoDisposable(view.scope())
.subscribe { selection -> newState { copy(selected = selection.size) } }
view.backClicked
.withLatestFrom(view.selectionChanges) { _, selection ->
when (selection.size) {
0 -> view.goBack()
else -> view.clearSelection()
}
}
.autoDisposable(view.scope())
.subscribe()
}
}
@@ -22,5 +22,6 @@ import com.moez.QKSMS.model.Conversation
import io.realm.RealmResults
data class BlockedMessagesState(
val data: RealmResults<Conversation>? = null
val data: RealmResults<Conversation>? = null,
val selected: Int = 0
)
@@ -22,5 +22,17 @@ import com.moez.QKSMS.common.base.QkViewContract
import io.reactivex.Observable
interface BlockedMessagesView : QkViewContract<BlockedMessagesState> {
val menuReadyIntent: Observable<Unit>
val optionsItemIntent: Observable<Int>
val conversationClicks: Observable<Long>
val selectionChanges: Observable<List<Long>>
val confirmDeleteIntent: Observable<List<Long>>
val backClicked: Observable<*>
fun clearSelection()
fun showBlockingDialog(conversations: List<Long>, block: Boolean)
fun showDeleteDialog(conversations: List<Long>)
fun goBack()
}
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2019 Moez Bhatti <moez.bhatti@gmail.com>
~
~ This file is part of QKSMS.
~
~ QKSMS is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ QKSMS is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/delete"
android:icon="@drawable/ic_delete_white_24dp"
android:title="@string/main_menu_delete"
android:visible="false"
app:showAsAction="ifRoom" />
<item
android:id="@+id/block"
android:icon="@drawable/ic_block_black_24dp"
android:title="@string/main_menu_block"
android:visible="false"
app:showAsAction="ifRoom" />
</menu>