forked from upstream-mirrors/quik
Don't provide flowable to ConversationsAdapter
This commit is contained in:
@@ -1,25 +0,0 @@
|
|||||||
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>(private val flowable: Flowable<List<T>>) : QkAdapter<T>() {
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -12,15 +12,14 @@ import com.moez.QKSMS.common.util.DateFormatter
|
|||||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||||
import com.moez.QKSMS.data.repository.MessageRepository
|
import com.moez.QKSMS.data.repository.MessageRepository
|
||||||
import com.moez.QKSMS.presentation.Navigator
|
import com.moez.QKSMS.presentation.Navigator
|
||||||
import com.moez.QKSMS.presentation.base.FlowableAdapter
|
import com.moez.QKSMS.presentation.base.QkAdapter
|
||||||
import com.moez.QKSMS.presentation.base.QkViewHolder
|
import com.moez.QKSMS.presentation.base.QkViewHolder
|
||||||
import io.reactivex.Flowable
|
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : FlowableAdapter<ConversationMessagePair>(flowable) {
|
class ConversationsAdapter : QkAdapter<ConversationMessagePair>() {
|
||||||
|
|
||||||
@Inject lateinit var context: Context
|
@Inject lateinit var context: Context
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
|
|||||||
@@ -19,9 +19,12 @@ import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
|||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.moez.QKSMS.common.di.appComponent
|
import com.moez.QKSMS.common.di.appComponent
|
||||||
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
import com.moez.QKSMS.common.util.extensions.setBackgroundTint
|
||||||
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.presentation.Navigator
|
import com.moez.QKSMS.presentation.Navigator
|
||||||
import com.moez.QKSMS.presentation.base.QkActivity
|
import com.moez.QKSMS.presentation.base.QkActivity
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
|
import io.reactivex.subjects.Subject
|
||||||
import kotlinx.android.synthetic.main.drawer_view.*
|
import kotlinx.android.synthetic.main.drawer_view.*
|
||||||
import kotlinx.android.synthetic.main.main_activity.*
|
import kotlinx.android.synthetic.main.main_activity.*
|
||||||
import kotlinx.android.synthetic.main.toolbar.*
|
import kotlinx.android.synthetic.main.toolbar.*
|
||||||
@@ -41,7 +44,8 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
|||||||
override val scheduledIntent by lazy { scheduled.clicks() }
|
override val scheduledIntent by lazy { scheduled.clicks() }
|
||||||
override val blockedIntent by lazy { blocked.clicks() }
|
override val blockedIntent by lazy { blocked.clicks() }
|
||||||
override val settingsIntent by lazy { settings.clicks() }
|
override val settingsIntent by lazy { settings.clicks() }
|
||||||
override val conversationSwipedIntent by lazy { itemTouchCallback.swipes }
|
override val deleteConversationIntent: Subject<Conversation> = PublishSubject.create()
|
||||||
|
override val archiveConversationIntent by lazy { itemTouchCallback.swipes }
|
||||||
|
|
||||||
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
|
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
|
||||||
|
|
||||||
@@ -56,7 +60,7 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
|||||||
requestPermissions()
|
requestPermissions()
|
||||||
viewModel.bindView(this)
|
viewModel.bindView(this)
|
||||||
|
|
||||||
conversationList.layoutManager = LinearLayoutManager(this)
|
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||||
|
|
||||||
// Don't allow clicks to pass through the drawer layout
|
// Don't allow clicks to pass through the drawer layout
|
||||||
drawer.clicks().subscribe()
|
drawer.clicks().subscribe()
|
||||||
@@ -96,9 +100,30 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun render(state: MainState) {
|
override fun render(state: MainState) {
|
||||||
if (conversationList.adapter != state.adapter) {
|
when (state.page) {
|
||||||
conversationList.adapter = state.adapter
|
MainPage.INBOX -> {
|
||||||
itemTouchHelper.attachToRecyclerView(if (state.page == MainPage.INBOX) conversationList else null)
|
if (recyclerView.adapter !is ConversationsAdapter) recyclerView.adapter = ConversationsAdapter()
|
||||||
|
val adapter = recyclerView.adapter as ConversationsAdapter
|
||||||
|
if (adapter.data != state.conversations) adapter.data = state.conversations
|
||||||
|
itemTouchHelper.attachToRecyclerView(recyclerView)
|
||||||
|
}
|
||||||
|
|
||||||
|
MainPage.ARCHIVED -> {
|
||||||
|
if (recyclerView.adapter !is ConversationsAdapter) recyclerView.adapter = ConversationsAdapter()
|
||||||
|
val adapter = recyclerView.adapter as ConversationsAdapter
|
||||||
|
if (adapter.data != state.archivedConversations) adapter.data = state.archivedConversations
|
||||||
|
itemTouchHelper.attachToRecyclerView(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
MainPage.SCHEDULED -> {
|
||||||
|
recyclerView.adapter = null
|
||||||
|
itemTouchHelper.attachToRecyclerView(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
MainPage.BLOCKED -> {
|
||||||
|
recyclerView.adapter = null
|
||||||
|
itemTouchHelper.attachToRecyclerView(null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inbox.isSelected = state.page == MainPage.INBOX
|
inbox.isSelected = state.page == MainPage.INBOX
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package com.moez.QKSMS.presentation.main
|
package com.moez.QKSMS.presentation.main
|
||||||
|
|
||||||
import android.support.v7.widget.RecyclerView
|
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||||
|
|
||||||
data class MainState(
|
data class MainState(
|
||||||
val page: MainPage = MainPage.INBOX,
|
val page: MainPage = MainPage.INBOX,
|
||||||
val adapter: RecyclerView.Adapter<*>? = null,
|
val conversations: List<ConversationMessagePair> = ArrayList(),
|
||||||
|
val archivedConversations: List<ConversationMessagePair> = ArrayList(),
|
||||||
val refreshing: Boolean = false,
|
val refreshing: Boolean = false,
|
||||||
val drawerOpen: Boolean = false
|
val drawerOpen: Boolean = false
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package com.moez.QKSMS.presentation.main
|
package com.moez.QKSMS.presentation.main
|
||||||
|
|
||||||
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.presentation.base.QkView
|
import com.moez.QKSMS.presentation.base.QkView
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.Subject
|
||||||
|
|
||||||
interface MainView : QkView<MainState> {
|
interface MainView : QkView<MainState> {
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ interface MainView : QkView<MainState> {
|
|||||||
val scheduledIntent: Observable<Unit>
|
val scheduledIntent: Observable<Unit>
|
||||||
val blockedIntent: Observable<Unit>
|
val blockedIntent: Observable<Unit>
|
||||||
val settingsIntent: Observable<Unit>
|
val settingsIntent: Observable<Unit>
|
||||||
val conversationSwipedIntent: PublishSubject<Int>
|
val deleteConversationIntent: Subject<Conversation>
|
||||||
|
val archiveConversationIntent: Subject<Int>
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,9 +10,10 @@ import com.moez.QKSMS.domain.interactor.PartialSync
|
|||||||
import com.moez.QKSMS.presentation.Navigator
|
import com.moez.QKSMS.presentation.Navigator
|
||||||
import com.moez.QKSMS.presentation.base.QkViewModel
|
import com.moez.QKSMS.presentation.base.QkViewModel
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
class MainViewModel : QkViewModel<MainView, MainState>(MainState(page = MainPage.INBOX)) {
|
||||||
|
|
||||||
@Inject lateinit var context: Context
|
@Inject lateinit var context: Context
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
@@ -26,7 +27,13 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
|
|
||||||
disposables += markAllSeen
|
disposables += markAllSeen
|
||||||
|
|
||||||
newState { it.copy(page = MainPage.INBOX, adapter = ConversationsAdapter(messageRepo.getConversations())) }
|
disposables += messageRepo.getConversations().subscribe { conversations ->
|
||||||
|
newState { it.copy(conversations = conversations) }
|
||||||
|
}
|
||||||
|
|
||||||
|
disposables += messageRepo.getConversations(true).subscribe { conversations ->
|
||||||
|
newState { it.copy(archivedConversations = conversations) }
|
||||||
|
}
|
||||||
|
|
||||||
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
|
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
|
||||||
partialSync.execute(Unit)
|
partialSync.execute(Unit)
|
||||||
@@ -48,21 +55,19 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intents += view.inboxIntent.subscribe {
|
intents += view.inboxIntent.subscribe {
|
||||||
val adapter = ConversationsAdapter(messageRepo.getConversations())
|
newState { it.copy(page = MainPage.INBOX, drawerOpen = false) }
|
||||||
newState { it.copy(page = MainPage.INBOX, adapter = adapter, drawerOpen = false) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intents += view.archivedIntent.subscribe {
|
intents += view.archivedIntent.subscribe {
|
||||||
val adapter = ConversationsAdapter(messageRepo.getConversations(true))
|
newState { it.copy(page = MainPage.ARCHIVED, drawerOpen = false) }
|
||||||
newState { it.copy(page = MainPage.ARCHIVED, adapter = adapter, drawerOpen = false) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intents += view.scheduledIntent.subscribe {
|
intents += view.scheduledIntent.subscribe {
|
||||||
newState { it.copy(page = MainPage.SCHEDULED, adapter = null, drawerOpen = false) }
|
newState { it.copy(page = MainPage.SCHEDULED, drawerOpen = false) }
|
||||||
}
|
}
|
||||||
|
|
||||||
intents += view.blockedIntent.subscribe {
|
intents += view.blockedIntent.subscribe {
|
||||||
newState { it.copy(page = MainPage.BLOCKED, adapter = null, drawerOpen = false) }
|
newState { it.copy(page = MainPage.BLOCKED, drawerOpen = false) }
|
||||||
}
|
}
|
||||||
|
|
||||||
intents += view.settingsIntent.subscribe {
|
intents += view.settingsIntent.subscribe {
|
||||||
@@ -70,17 +75,10 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
|||||||
newState { it.copy(drawerOpen = false) }
|
newState { it.copy(drawerOpen = false) }
|
||||||
}
|
}
|
||||||
|
|
||||||
intents += view.conversationSwipedIntent.subscribe { adapterPosition ->
|
intents += view.archiveConversationIntent
|
||||||
state.value
|
.withLatestFrom(state, { position, state -> state.conversations[position] })
|
||||||
?.takeIf { state -> state.page == MainPage.INBOX }
|
.map { pair -> pair.conversation.id }
|
||||||
?.takeIf { state -> state.adapter is ConversationsAdapter }
|
.subscribe { threadId -> markArchived.execute(threadId)}
|
||||||
?.let { state ->
|
|
||||||
val adapter = state.adapter as ConversationsAdapter
|
|
||||||
val conversation = adapter.getItem(adapterPosition).conversation
|
|
||||||
markArchived.execute(conversation.id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
android:layout_weight="1">
|
android:layout_weight="1">
|
||||||
|
|
||||||
<android.support.v7.widget.RecyclerView
|
<android.support.v7.widget.RecyclerView
|
||||||
android:id="@+id/conversationList"
|
android:id="@+id/recyclerView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:clipChildren="false"
|
android:clipChildren="false"
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
android:layout_margin="16dp"
|
android:layout_margin="16dp"
|
||||||
android:src="@drawable/ic_add_black_24dp"
|
android:src="@drawable/ic_add_black_24dp"
|
||||||
android:tint="@color/white"
|
android:tint="@color/white"
|
||||||
app:layout_anchor="@+id/conversationList"
|
app:layout_anchor="@+id/recyclerView"
|
||||||
app:layout_anchorGravity="bottom|right|end"
|
app:layout_anchorGravity="bottom|right|end"
|
||||||
tools:backgroundTint="@color/tools_theme" />
|
tools:backgroundTint="@color/tools_theme" />
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user