forked from upstream-mirrors/quik
Swipe to archive (#6)
* Added support for moving conversations to archived inbox * Optimized query for loading conversations * Upgrade RxKotlin * Explicitly define type for archivedConversationIntent * Ensure that swipe functionality is only for Inbox
This commit is contained in:
+1
-1
@@ -48,7 +48,7 @@ dependencies {
|
||||
compile 'com.karumi:dexter:4.1.0'
|
||||
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
|
||||
compile "io.reactivex.rxjava2:rxjava:2.1.4"
|
||||
compile 'io.reactivex.rxjava2:rxkotlin:2.0.3'
|
||||
compile 'io.reactivex.rxjava2:rxkotlin:2.1.0'
|
||||
compile 'io.realm:android-adapters:2.1.0'
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
|
||||
kapt "android.arch.lifecycle:compiler:1.0.0-alpha9-1"
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.moez.QKSMS.common.util
|
||||
|
||||
import android.support.v7.util.DiffUtil
|
||||
|
||||
/**
|
||||
* Abstract implementation of DiffUtil.Callback, which can be used when we don't need
|
||||
* a detailed change payload
|
||||
*
|
||||
* If we need a detailed change payload, this class can be extended and #areContentsTheSame
|
||||
* and #getChangePayload should overridden
|
||||
*/
|
||||
class AbstractDiffUtilCallback<T>(
|
||||
private val oldList: List<T>,
|
||||
private val newList: List<T>) :
|
||||
DiffUtil.Callback() {
|
||||
|
||||
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
||||
return oldList[oldItemPosition] == newList[newItemPosition]
|
||||
}
|
||||
|
||||
override fun getOldListSize(): Int {
|
||||
return oldList.size
|
||||
}
|
||||
|
||||
override fun getNewListSize(): Int {
|
||||
return newList.size
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
|
||||
return oldList[oldItemPosition] == newList[newItemPosition]
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.moez.QKSMS.data.model
|
||||
|
||||
/**
|
||||
* Groups a conversation with its latest message into a single model
|
||||
*/
|
||||
data class ConversationMessagePair(val conversation: Conversation, val message: Message)
|
||||
@@ -10,8 +10,10 @@ import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessageFlowable
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.functions.BiFunction
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
@@ -25,12 +27,29 @@ class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToMessageFlowable: CursorToMessageFlowable) {
|
||||
|
||||
fun getConversationMessagesAsync(): RealmResults<Message> {
|
||||
return Realm.getDefaultInstance()
|
||||
.where(Message::class.java)
|
||||
fun getConversations(archived: Boolean = false): Flowable<List<ConversationMessagePair>> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
|
||||
val conversations = realm.where(Conversation::class.java)
|
||||
.equalTo("archived", archived)
|
||||
.findAllAsync()
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
.map { conversations -> conversations.associateBy { conversation -> conversation.id } }
|
||||
|
||||
val messages = realm.where(Message::class.java)
|
||||
.findAllSortedAsync("date", Sort.DESCENDING)
|
||||
.where()
|
||||
.distinctAsync("threadId")
|
||||
.asFlowable()
|
||||
.filter { it.isLoaded }
|
||||
|
||||
return Flowable.combineLatest(conversations, messages, BiFunction { conversations, messages ->
|
||||
messages.mapNotNull { message ->
|
||||
val conversation = conversations[message.threadId]
|
||||
if (conversation == null) null else ConversationMessagePair(conversation, message)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun getConversationAsync(threadId: Long): Conversation {
|
||||
@@ -69,7 +88,8 @@ class MessageRepository @Inject constructor(
|
||||
.equalTo("archived", false)
|
||||
.findFirst()
|
||||
|
||||
conversation?.archived = true
|
||||
realm.executeTransaction { conversation?.archived = true }
|
||||
|
||||
realm.close()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
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>() {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
private var data: List<T> = ArrayList()
|
||||
set(value) {
|
||||
DiffUtil.calculateDiff(provideDiffUtilCallback(field, value)).dispatchUpdatesTo(this)
|
||||
field = value
|
||||
}
|
||||
|
||||
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 final fun getItemCount(): Int {
|
||||
return data.size
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the adapter implementation to provide a custom DiffUtil.Callback
|
||||
* If not, then the abstract implementation will be used
|
||||
*/
|
||||
open fun provideDiffUtilCallback(oldData: List<T>, newData: List<T>): DiffUtil.Callback {
|
||||
return AbstractDiffUtilCallback(oldData, newData)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.moez.QKSMS.presentation.main
|
||||
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.support.v7.widget.helper.ItemTouchHelper
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationItemTouchCallback @Inject constructor() : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
|
||||
|
||||
val swipes: PublishSubject<Int> = PublishSubject.create()
|
||||
|
||||
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
||||
swipes.onNext(viewHolder.adapterPosition)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,17 +8,16 @@ import com.moez.QKSMS.R
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.common.util.DateFormatter
|
||||
import com.moez.QKSMS.common.util.ThemeManager
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
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.QkViewHolder
|
||||
import io.realm.OrderedRealmCollection
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmRecyclerViewAdapter
|
||||
import io.reactivex.Flowable
|
||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationsAdapter(data: OrderedRealmCollection<Message>?) : RealmRecyclerViewAdapter<Message, QkViewHolder>(data, true) {
|
||||
class ConversationsAdapter(flowable: Flowable<List<ConversationMessagePair>>) : QkAdapter<ConversationMessagePair, QkViewHolder>(flowable) {
|
||||
|
||||
@Inject lateinit var context: Context
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@@ -47,19 +46,19 @@ class ConversationsAdapter(data: OrderedRealmCollection<Message>?) : RealmRecycl
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
val message = getItem(position)!!
|
||||
val conversation = messageRepo.getConversation(message.threadId)
|
||||
val conversation = getItem(position).conversation
|
||||
val message = getItem(position).message
|
||||
val view = viewHolder.itemView
|
||||
|
||||
RxView.clicks(view).subscribe { navigator.showConversation(message.threadId) }
|
||||
|
||||
view.avatars.contacts = conversation?.contacts ?: RealmList()
|
||||
view.title.text = conversation?.getTitle()
|
||||
view.avatars.contacts = conversation.contacts
|
||||
view.title.text = conversation.getTitle()
|
||||
view.date.text = dateFormatter.getConversationTimestamp(message.date)
|
||||
view.snippet.text = message.body
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (getItem(position)!!.read) 0 else 1
|
||||
return if (getItem(position).message.read) 0 else 1
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.ActionBarDrawerToggle
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.support.v7.widget.helper.ItemTouchHelper
|
||||
import android.view.Gravity
|
||||
import com.jakewharton.rxbinding2.support.v4.widget.drawerOpen
|
||||
@@ -21,7 +20,6 @@ import com.moez.QKSMS.common.util.ThemeManager
|
||||
import com.moez.QKSMS.common.util.extensions.setTint
|
||||
import com.moez.QKSMS.presentation.Navigator
|
||||
import com.moez.QKSMS.presentation.base.QkActivity
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import kotlinx.android.synthetic.main.drawer_view.*
|
||||
import kotlinx.android.synthetic.main.main_activity.*
|
||||
import kotlinx.android.synthetic.main.toolbar.*
|
||||
@@ -32,6 +30,7 @@ class MainActivity : QkActivity<MainViewModel, MainState>(), MainView {
|
||||
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@Inject lateinit var themeManager: ThemeManager
|
||||
@Inject lateinit var itemTouchCallback: ConversationItemTouchCallback
|
||||
|
||||
override val viewModelClass = MainViewModel::class
|
||||
override val composeIntent by lazy { compose.clicks() }
|
||||
@@ -41,7 +40,9 @@ class MainActivity : QkActivity<MainViewModel, MainState>(), MainView {
|
||||
override val scheduledIntent by lazy { scheduled.clicks() }
|
||||
override val blockedIntent by lazy { blocked.clicks() }
|
||||
override val settingsIntent by lazy { settings.clicks() }
|
||||
override val archivedConversationIntent = PublishSubject.create<Long>()
|
||||
override val conversationSwipedIntent by lazy { itemTouchCallback.swipes }
|
||||
|
||||
private val itemTouchHelper by lazy { ItemTouchHelper(itemTouchCallback) }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -67,9 +68,7 @@ class MainActivity : QkActivity<MainViewModel, MainState>(), MainView {
|
||||
override fun render(state: MainState) {
|
||||
if (conversationList.adapter != state.adapter) {
|
||||
conversationList.adapter = state.adapter
|
||||
if (state.adapter is ConversationsAdapter) {
|
||||
setConversationListItemTouchListener(state.adapter)
|
||||
}
|
||||
itemTouchHelper.attachToRecyclerView(if (state.page == MainPage.INBOX) conversationList else null)
|
||||
}
|
||||
|
||||
inbox.setBackgroundResource(getRowBackground(state.page == MainPage.INBOX))
|
||||
@@ -108,18 +107,4 @@ class MainActivity : QkActivity<MainViewModel, MainState>(), MainView {
|
||||
.check()
|
||||
}
|
||||
|
||||
private fun setConversationListItemTouchListener(adapter: ConversationsAdapter) {
|
||||
val itemTouchCallBack = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
|
||||
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder?): Boolean {
|
||||
return false
|
||||
}
|
||||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
||||
adapter.getItem(viewHolder.adapterPosition)?.threadId?.let { threadId ->
|
||||
archivedConversationIntent.onNext(threadId)
|
||||
}
|
||||
}
|
||||
}
|
||||
val itemTouchHelper = ItemTouchHelper(itemTouchCallBack)
|
||||
itemTouchHelper.attachToRecyclerView(conversationList)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ interface MainView : QkView<MainState> {
|
||||
val scheduledIntent: Observable<Unit>
|
||||
val blockedIntent: Observable<Unit>
|
||||
val settingsIntent: Observable<Unit>
|
||||
val archivedConversationIntent: PublishSubject<Long>
|
||||
val conversationSwipedIntent: PublishSubject<Int>
|
||||
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
|
||||
disposables += markAllSeen
|
||||
|
||||
newState { it.copy(page = MainPage.INBOX, adapter = ConversationsAdapter(messageRepo.getConversationMessagesAsync())) }
|
||||
newState { it.copy(page = MainPage.INBOX, adapter = ConversationsAdapter(messageRepo.getConversations())) }
|
||||
|
||||
markAllSeen.execute(Unit)
|
||||
}
|
||||
@@ -38,12 +38,13 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
}
|
||||
|
||||
intents += view.inboxIntent.subscribe {
|
||||
val adapter = ConversationsAdapter(messageRepo.getConversationMessagesAsync())
|
||||
val adapter = ConversationsAdapter(messageRepo.getConversations())
|
||||
newState { it.copy(page = MainPage.INBOX, adapter = adapter, drawerOpen = false) }
|
||||
}
|
||||
|
||||
intents += view.archivedIntent.subscribe {
|
||||
newState { it.copy(page = MainPage.ARCHIVED, adapter = null, drawerOpen = false) }
|
||||
val adapter = ConversationsAdapter(messageRepo.getConversations(true))
|
||||
newState { it.copy(page = MainPage.ARCHIVED, adapter = adapter, drawerOpen = false) }
|
||||
}
|
||||
|
||||
intents += view.scheduledIntent.subscribe {
|
||||
@@ -59,8 +60,15 @@ class MainViewModel : QkViewModel<MainView, MainState>(MainState()) {
|
||||
newState { it.copy(drawerOpen = false) }
|
||||
}
|
||||
|
||||
intents += view.archivedConversationIntent.subscribe { threadId ->
|
||||
markArchived.execute(threadId)
|
||||
intents += view.conversationSwipedIntent.subscribe { adapterPosition ->
|
||||
state.value
|
||||
?.takeIf { state -> state.page == MainPage.INBOX }
|
||||
?.takeIf { state -> state.adapter is ConversationsAdapter }
|
||||
?.let { state ->
|
||||
val adapter = state.adapter as ConversationsAdapter
|
||||
val conversation = adapter.getItem(adapterPosition).conversation
|
||||
markArchived.execute(conversation.id)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user