forked from upstream-mirrors/quik
#42 - Multi selection for messages
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user