#42 - Multi selection for messages

This commit is contained in:
Moez Bhatti
2018-05-06 17:12:56 -04:00
parent 7c7e40d90a
commit 7c2e6a2d2c
15 changed files with 203 additions and 98 deletions
@@ -3,6 +3,8 @@ package common.base
import android.support.v7.widget.RecyclerView
import android.view.View
import common.util.extensions.setVisible
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.Subject
import io.realm.OrderedRealmCollection
import io.realm.RealmList
import io.realm.RealmModel
@@ -21,6 +23,38 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
emptyView?.setVisible(data.isLoaded && data.isEmpty())
}
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
private val selection = mutableListOf<Long>()
/**
* Toggles the selected state for a particular view
*
* If we are currently in selection mode (we have an active selection), then the state will
* toggle. If we are not in selection mode, then we will only toggle if [force]
*/
protected fun toggleSelection(id: Long, force: Boolean = true): Boolean {
if (!force && selection.isEmpty()) return false
when (selection.contains(id)) {
true -> selection.remove(id)
false -> selection.add(id)
}
selectionChanges.onNext(selection)
return true
}
protected fun isSelected(id: Long): Boolean {
return selection.contains(id)
}
fun clearSelection() {
selection.clear()
selectionChanges.onNext(selection)
notifyDataSetChanged()
}
override fun updateData(data: OrderedRealmCollection<T>?) {
removeListener(getData())
addListener(data)