forked from upstream-mirrors/quik
Use lists of data instead of Flowables for ChipsAdapter and ContactsAdapter
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.moez.QKSMS.presentation.base
|
||||
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.disposables.Disposable
|
||||
|
||||
/**
|
||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||
* data list handing and item animations
|
||||
*/
|
||||
abstract class FlowableAdapter<T, VH : RecyclerView.ViewHolder>(private val flowable: Flowable<List<T>>) : QkAdapter<T, VH>() {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView?) {
|
||||
super.onAttachedToRecyclerView(recyclerView)
|
||||
disposable = flowable.subscribe { data = it }
|
||||
}
|
||||
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) {
|
||||
super.onDetachedFromRecyclerView(recyclerView)
|
||||
disposable?.dispose()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,39 +3,24 @@ package com.moez.QKSMS.presentation.base
|
||||
import android.support.v7.util.DiffUtil
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import com.moez.QKSMS.common.util.AbstractDiffUtilCallback
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.disposables.Disposable
|
||||
|
||||
/**
|
||||
* Base RecyclerView.Adapter that provides some convenience when creating a new Adapter, such as
|
||||
* data list handing and item animations
|
||||
*
|
||||
* If Sections are required for the RecyclerView UI, please use the BaseSectionAdapter
|
||||
*/
|
||||
abstract class QkAdapter<T, VH : RecyclerView.ViewHolder>(private val flowable: Flowable<List<T>>) : RecyclerView.Adapter<VH>() {
|
||||
abstract class QkAdapter<T, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
private var data: List<T> = ArrayList()
|
||||
var data: List<T> = ArrayList()
|
||||
set(value) {
|
||||
DiffUtil.calculateDiff(provideDiffUtilCallback(field, value)).dispatchUpdatesTo(this)
|
||||
val diff = DiffUtil.calculateDiff(provideDiffUtilCallback(field, value))
|
||||
field = value
|
||||
diff.dispatchUpdatesTo(this)
|
||||
}
|
||||
|
||||
fun getItem(position: Int): T {
|
||||
return data[position]
|
||||
}
|
||||
|
||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView?) {
|
||||
super.onAttachedToRecyclerView(recyclerView)
|
||||
disposable = flowable.subscribe { data = it }
|
||||
}
|
||||
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) {
|
||||
super.onDetachedFromRecyclerView(recyclerView)
|
||||
disposable?.dispose()
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return data.size
|
||||
}
|
||||
|
||||
@@ -19,14 +19,13 @@ import com.moez.QKSMS.common.util.extensions.dpToPx
|
||||
import com.moez.QKSMS.data.model.Contact
|
||||
import com.moez.QKSMS.presentation.base.QkAdapter
|
||||
import com.moez.QKSMS.presentation.base.QkViewHolder
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import kotlinx.android.synthetic.main.contact_chip.view.*
|
||||
|
||||
class ChipsAdapter(
|
||||
private val context: Context,
|
||||
private val view: RecyclerView,
|
||||
data: Flowable<List<Contact>>) : QkAdapter<Contact, QkViewHolder>(data) {
|
||||
private val view: RecyclerView)
|
||||
: QkAdapter<Contact, QkViewHolder>() {
|
||||
|
||||
companion object {
|
||||
private val TYPE_EDIT_TEXT = 0
|
||||
|
||||
@@ -15,6 +15,7 @@ import com.moez.QKSMS.common.util.extensions.setTint
|
||||
import com.moez.QKSMS.data.model.Contact
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.presentation.base.QkActivity
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import io.realm.RealmResults
|
||||
@@ -30,9 +31,9 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
private lateinit var layoutManager: LinearLayoutManager
|
||||
|
||||
override val viewModelClass = ComposeViewModel::class
|
||||
override val queryChangedIntent: Subject<CharSequence> = PublishSubject.create()
|
||||
override val chipSelectedIntent: Subject<Contact> = PublishSubject.create()
|
||||
override val chipDeletedIntent: Subject<Contact> = PublishSubject.create()
|
||||
override val queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val copyTextIntent: Subject<Message> = PublishSubject.create()
|
||||
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
override val deleteMessageIntent: Subject<Message> = PublishSubject.create()
|
||||
@@ -40,6 +41,9 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val attachIntent by lazy { attach.clicks() }
|
||||
override val sendIntent by lazy { send.clicks() }
|
||||
|
||||
private val chipsAdapter by lazy { ChipsAdapter(this, chips) }
|
||||
private val contactsAdapter by lazy { ContactAdapter(this) }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
@@ -48,7 +52,10 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
viewModel.bindView(this)
|
||||
|
||||
chips.layoutManager = FlexboxLayoutManager(this)
|
||||
chips.adapter = chipsAdapter
|
||||
|
||||
contacts.layoutManager = LinearLayoutManager(this)
|
||||
contacts.adapter = contactsAdapter
|
||||
|
||||
window.callback = ComposeWindowCallback(window.callback, this)
|
||||
|
||||
@@ -62,19 +69,12 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
chips.visibility = if (state.editingMode) View.VISIBLE else View.GONE
|
||||
contacts.visibility = if (state.editingMode) View.VISIBLE else View.GONE
|
||||
|
||||
if (chips.adapter == null && state.selectedContacts != null) {
|
||||
val adapter = ChipsAdapter(this, chips, state.selectedContacts)
|
||||
adapter.chipDeleted.subscribe { chipDeletedIntent.onNext(it) }
|
||||
adapter.textChanges.subscribe { queryChangedIntent.onNext(it) }
|
||||
|
||||
chips.adapter = adapter
|
||||
if (chipsAdapter.data !== state.selectedContacts) {
|
||||
chipsAdapter.data = state.selectedContacts
|
||||
}
|
||||
|
||||
if (contacts.adapter == null && state.contacts != null) {
|
||||
val adapter = ContactAdapter(this, state.contacts)
|
||||
adapter.contactSelected.subscribe { chipSelectedIntent.onNext(it) }
|
||||
|
||||
contacts.adapter = adapter
|
||||
if (contactsAdapter.data !== state.contacts) {
|
||||
contactsAdapter.data = state.contacts
|
||||
}
|
||||
|
||||
if (title != state.title) title = state.title
|
||||
|
||||
@@ -2,13 +2,12 @@ package com.moez.QKSMS.presentation.compose
|
||||
|
||||
import com.moez.QKSMS.data.model.Contact
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import io.reactivex.Flowable
|
||||
import io.realm.RealmResults
|
||||
|
||||
data class ComposeState(
|
||||
val editingMode: Boolean = false,
|
||||
val contacts: Flowable<List<Contact>>? = null,
|
||||
val selectedContacts: Flowable<List<Contact>>? = null,
|
||||
val contacts: List<Contact> = ArrayList(),
|
||||
val selectedContacts: List<Contact> = ArrayList(),
|
||||
val title: String = "",
|
||||
val messages: RealmResults<Message>? = null,
|
||||
val draft: String = "",
|
||||
|
||||
@@ -8,7 +8,7 @@ import io.reactivex.subjects.Subject
|
||||
|
||||
interface ComposeView : QkView<ComposeState> {
|
||||
|
||||
val queryChangedIntent: Subject<CharSequence>
|
||||
val queryChangedIntent: Observable<CharSequence>
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val copyTextIntent: Subject<Message>
|
||||
|
||||
@@ -15,12 +15,10 @@ import com.moez.QKSMS.domain.interactor.MarkRead
|
||||
import com.moez.QKSMS.domain.interactor.SendMessage
|
||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||
import io.reactivex.BackpressureStrategy
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import javax.inject.Inject
|
||||
|
||||
class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeState>(ComposeState(editingMode = threadId == 0L)) {
|
||||
@@ -32,9 +30,6 @@ class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeSta
|
||||
@Inject lateinit var markRead: MarkRead
|
||||
@Inject lateinit var deleteMessage: DeleteMessage
|
||||
|
||||
private val contactsSubject: Subject<List<Contact>> = BehaviorSubject.create()
|
||||
private val selectedContactsReducer: Subject<(List<Contact>) -> List<Contact>> = BehaviorSubject.create()
|
||||
|
||||
private val contacts: List<Contact>
|
||||
|
||||
private var conversation: Conversation? = null
|
||||
@@ -60,14 +55,6 @@ class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeSta
|
||||
|
||||
contacts = contactsRepo.getContacts()
|
||||
|
||||
val contactsFlowable = contactsSubject.toFlowable(BackpressureStrategy.BUFFER)
|
||||
|
||||
val selectedContacts: Flowable<List<Contact>> = selectedContactsReducer
|
||||
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
|
||||
.toFlowable(BackpressureStrategy.BUFFER)
|
||||
|
||||
newState { it.copy(contacts = contactsFlowable, selectedContacts = selectedContacts) }
|
||||
|
||||
dataChanged()
|
||||
}
|
||||
|
||||
@@ -84,15 +71,17 @@ class ComposeViewModel(val threadId: Long) : QkViewModel<ComposeView, ComposeSta
|
||||
}
|
||||
.subscribeOn(Schedulers.computation())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe { contacts -> contactsSubject.onNext(contacts) }
|
||||
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
|
||||
|
||||
intents += view.chipSelectedIntent.subscribe { contact ->
|
||||
selectedContactsReducer.onNext { it.toMutableList().apply { add(contact) } }
|
||||
}
|
||||
|
||||
intents += view.chipDeletedIntent.subscribe { contact ->
|
||||
selectedContactsReducer.onNext { it.filterNot { it == contact } }
|
||||
}
|
||||
intents += Observable.merge<(List<Contact>) -> List<Contact>>(
|
||||
view.chipDeletedIntent.map { contact ->
|
||||
{ contacts: List<Contact> -> contacts.filterNot { it == contact } }
|
||||
},
|
||||
view.chipSelectedIntent.map { contact ->
|
||||
{ contacts: List<Contact> -> contacts.toMutableList().apply { add(contact) } }
|
||||
})
|
||||
.scan(listOf<Contact>(), { previousState, reducer -> reducer(previousState) })
|
||||
.subscribe { contacts -> newState { it.copy(selectedContacts = contacts) } }
|
||||
|
||||
intents += view.textChangedIntent.subscribe { text ->
|
||||
newState { it.copy(draft = text.toString(), canSend = text.isNotEmpty()) }
|
||||
|
||||
@@ -8,12 +8,11 @@ import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.data.model.Contact
|
||||
import com.moez.QKSMS.presentation.base.QkAdapter
|
||||
import com.moez.QKSMS.presentation.base.QkViewHolder
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.contact_list_item.view.*
|
||||
|
||||
class ContactAdapter(private val context: Context, data: Flowable<List<Contact>>) : QkAdapter<Contact, QkViewHolder>(data) {
|
||||
class ContactAdapter(private val context: Context) : QkAdapter<Contact, QkViewHolder>() {
|
||||
|
||||
val contactSelected: Subject<Contact> = PublishSubject.create()
|
||||
|
||||
|
||||
@@ -11,13 +11,13 @@ import com.moez.QKSMS.common.util.ThemeManager
|
||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
import com.moez.QKSMS.presentation.base.QkAdapter
|
||||
import com.moez.QKSMS.presentation.base.FlowableAdapter
|
||||
import com.moez.QKSMS.presentation.base.QkViewHolder
|
||||
import io.reactivex.Flowable
|
||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : QkAdapter<ConversationMessagePair, QkViewHolder>(flowable) {
|
||||
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : FlowableAdapter<ConversationMessagePair, QkViewHolder>(flowable) {
|
||||
|
||||
@Inject lateinit var context: Context
|
||||
@Inject lateinit var navigator: Navigator
|
||||
|
||||
Reference in New Issue
Block a user