forked from upstream-mirrors/quik
#1010 - Message content search
This commit is contained in:
@@ -171,11 +171,15 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
|
||||
threadId.onNext(state.selectedConversation)
|
||||
|
||||
title = when (state.selectedMessages) {
|
||||
0 -> state.conversationtitle
|
||||
else -> getString(R.string.compose_title_selected, state.selectedMessages)
|
||||
title = when {
|
||||
state.selectedMessages > 0 -> getString(R.string.compose_title_selected, state.selectedMessages)
|
||||
state.query.isNotEmpty() -> state.query
|
||||
else -> state.conversationtitle
|
||||
}
|
||||
|
||||
toolbarSubtitle.setVisible(state.query.isNotEmpty())
|
||||
toolbarSubtitle.text = getString(R.string.compose_subtitle_results, state.searchSelectionPosition, state.searchResults)
|
||||
|
||||
toolbarTitle.setVisible(!state.editingMode)
|
||||
chips.setVisible(state.editingMode)
|
||||
contacts.setVisible(state.contactsVisible)
|
||||
@@ -185,11 +189,14 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
if (state.editingMode && chips.adapter == null) chips.adapter = chipsAdapter
|
||||
if (state.editingMode && contacts.adapter == null) contacts.adapter = contactsAdapter
|
||||
|
||||
toolbar.menu.findItem(R.id.call)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
||||
toolbar.menu.findItem(R.id.call)?.isVisible = !state.editingMode && state.selectedMessages == 0 && state.query.isEmpty()
|
||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode && state.selectedMessages == 0 && state.query.isEmpty()
|
||||
toolbar.menu.findItem(R.id.copy)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
toolbar.menu.findItem(R.id.delete)?.isVisible = !state.editingMode && state.selectedMessages > 0
|
||||
toolbar.menu.findItem(R.id.forward)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||
toolbar.menu.findItem(R.id.previous)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty()
|
||||
toolbar.menu.findItem(R.id.next)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty()
|
||||
toolbar.menu.findItem(R.id.clear)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty()
|
||||
|
||||
if (chipsAdapter.data.isEmpty() && state.selectedContacts.isNotEmpty()) {
|
||||
message.showKeyboard()
|
||||
@@ -198,6 +205,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
chipsAdapter.data = state.selectedContacts
|
||||
contactsAdapter.data = state.contacts
|
||||
messageAdapter.data = state.messages
|
||||
messageAdapter.highlight = state.searchSelectionId
|
||||
|
||||
attachments.setVisible(state.attachments.isNotEmpty())
|
||||
attachmentAdapter.data = state.attachments
|
||||
@@ -220,6 +228,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
message.setText(draft)
|
||||
}
|
||||
|
||||
override fun scrollToMessage(id: Long) {
|
||||
messageAdapter.data?.second
|
||||
?.indexOfLast { message -> message.id == id }
|
||||
?.takeIf { position -> position != -1 }
|
||||
?.let(messageList::scrollToPosition)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.compose, menu)
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
@@ -230,6 +245,10 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun getColoredMenuItems(): List<Int> {
|
||||
return super.getColoredMenuItems() + R.id.call
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
backPressedIntent.onNext(Unit)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,10 @@ data class ComposeState(
|
||||
val selectedConversation: Long = 0,
|
||||
val selectedContacts: List<Contact> = ArrayList(),
|
||||
val conversationtitle: String = "",
|
||||
val query: String = "",
|
||||
val searchSelectionId: Long = -1,
|
||||
val searchSelectionPosition: Int = 0,
|
||||
val searchResults: Int = 0,
|
||||
val messages: Pair<Conversation, RealmResults<Message>>? = null,
|
||||
val selectedMessages: Int = 0,
|
||||
val attachments: List<Attachment> = ArrayList(),
|
||||
|
||||
@@ -48,6 +48,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
|
||||
fun clearSelection()
|
||||
fun setDraft(draft: String)
|
||||
fun scrollToMessage(id: Long)
|
||||
val backPressedIntent: Observable<Unit>
|
||||
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import common.Navigator
|
||||
import common.base.QkViewModel
|
||||
import common.util.ClipboardUtils
|
||||
import common.util.extensions.makeToast
|
||||
import common.util.filter.ContactFilter
|
||||
import filter.ContactFilter
|
||||
import interactor.CancelDelayedMessage
|
||||
import interactor.ContactSync
|
||||
import interactor.DeleteMessages
|
||||
@@ -64,7 +64,6 @@ import java.net.URLDecoder
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
class ComposeViewModel @Inject constructor(
|
||||
private val intent: Intent,
|
||||
@@ -79,19 +78,19 @@ class ComposeViewModel @Inject constructor(
|
||||
private val retrySending: RetrySending,
|
||||
private val markRead: MarkRead,
|
||||
private val deleteMessages: DeleteMessages
|
||||
) : QkViewModel<ComposeView, ComposeState>(ComposeState()) {
|
||||
) : QkViewModel<ComposeView, ComposeState>(ComposeState(query = intent.extras?.getString("query") ?: "")) {
|
||||
|
||||
private var sharedText: String = ""
|
||||
private var sharedText: String = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
|
||||
private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(ArrayList())
|
||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
||||
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
||||
private val selectedContacts: Observable<List<Contact>>
|
||||
private val searchResults: Subject<List<Message>> = BehaviorSubject.create()
|
||||
private val searchSelection: Subject<Long> = BehaviorSubject.createDefault(-1)
|
||||
private val conversation: Subject<Conversation> = BehaviorSubject.create()
|
||||
private val messages: Subject<List<Message>> = BehaviorSubject.create()
|
||||
|
||||
init {
|
||||
sharedText = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
|
||||
|
||||
// If there are any image attachments, we'll set those as the initial attachments for the
|
||||
// conversation
|
||||
val sharedImages = mutableListOf<Uri>()
|
||||
@@ -176,6 +175,25 @@ class ComposeViewModel @Inject constructor(
|
||||
disposables += attachments
|
||||
.subscribe { attachments -> newState { it.copy(attachments = attachments) } }
|
||||
|
||||
disposables += conversation
|
||||
.map { conversation -> conversation.id }
|
||||
.distinctUntilChanged()
|
||||
.withLatestFrom(state) { id, state -> messageRepo.getMessages(id, state.query) }
|
||||
.switchMap { messages -> messages.asObservable() }
|
||||
.takeUntil(state.map { it.query }.filter { it.isEmpty() })
|
||||
.filter { messages -> messages.isLoaded }
|
||||
.filter { messages -> messages.isValid }
|
||||
.subscribe(searchResults::onNext)
|
||||
|
||||
disposables += Observables.combineLatest(searchSelection, searchResults) { selected, messages ->
|
||||
if (selected == -1L) {
|
||||
messages.lastOrNull()?.let { message -> searchSelection.onNext(message.id) }
|
||||
} else {
|
||||
val position = messages.indexOfFirst { it.id == selected } + 1
|
||||
newState { it.copy(searchSelectionPosition = position, searchResults = messages.size) }
|
||||
}
|
||||
}.subscribe()
|
||||
|
||||
if (threadId == 0L) {
|
||||
syncContacts.execute(Unit)
|
||||
}
|
||||
@@ -314,6 +332,48 @@ class ComposeViewModel @Inject constructor(
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { view.clearSelection() }
|
||||
|
||||
|
||||
// Show the previous search result
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.previous }
|
||||
.withLatestFrom(searchSelection, searchResults) { _, selection, messages ->
|
||||
val currentPosition = messages.indexOfFirst { it.id == selection }
|
||||
if (currentPosition <= 0L) messages.lastOrNull()?.id ?: -1
|
||||
else messages.getOrNull(currentPosition - 1)?.id ?: -1
|
||||
}
|
||||
.filter { id -> id != -1L }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe(searchSelection)
|
||||
|
||||
|
||||
// Show the next search result
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.next }
|
||||
.withLatestFrom(searchSelection, searchResults) { _, selection, messages ->
|
||||
val currentPosition = messages.indexOfFirst { it.id == selection }
|
||||
if (currentPosition >= messages.size - 1) messages.firstOrNull()?.id ?: -1
|
||||
else messages.getOrNull(currentPosition + 1)?.id ?: -1
|
||||
}
|
||||
.filter { id -> id != -1L }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe(searchSelection)
|
||||
|
||||
|
||||
// Clear the search
|
||||
view.optionsItemIntent
|
||||
.filter { it == R.id.clear }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { newState { it.copy(query = "", searchSelectionId = -1) } }
|
||||
|
||||
|
||||
// Scroll to search position
|
||||
searchSelection
|
||||
.filter { id -> id != -1L }
|
||||
.doOnNext { id -> newState { it.copy(searchSelectionId = id) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe(view::scrollToMessage)
|
||||
|
||||
|
||||
// Retry sending
|
||||
view.messageClickIntent
|
||||
.filter { message -> message.isFailedMessage() }
|
||||
|
||||
@@ -103,6 +103,17 @@ class MessagesAdapter @Inject constructor(
|
||||
private val conversation: Conversation?
|
||||
get() = data?.first?.takeIf { it.isValid }
|
||||
|
||||
/**
|
||||
* Mark this message as highlighted
|
||||
*/
|
||||
var highlight: Long = -1L
|
||||
set(value) {
|
||||
if (field == value) return
|
||||
|
||||
field = value
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
private val layoutInflater = LayoutInflater.from(context)
|
||||
private val contactCache = ContactCache()
|
||||
private val expanded = HashMap<Long, Boolean>()
|
||||
@@ -178,7 +189,7 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
|
||||
// Update the selected state
|
||||
view.isSelected = isSelected(message.id)
|
||||
view.isSelected = isSelected(message.id) || highlight == message.id
|
||||
|
||||
|
||||
// Bind the cancel view
|
||||
|
||||
@@ -27,7 +27,7 @@ import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.jakewharton.rxbinding2.view.longClicks
|
||||
import com.moez.QKSMS.R
|
||||
import common.Navigator
|
||||
import common.base.FlowableAdapter
|
||||
import common.base.QkRealmAdapter
|
||||
import common.base.QkViewHolder
|
||||
import common.util.Colors
|
||||
import common.util.DateFormatter
|
||||
@@ -42,7 +42,7 @@ class ConversationsAdapter @Inject constructor(
|
||||
private val colors: Colors,
|
||||
private val dateFormatter: DateFormatter,
|
||||
private val navigator: Navigator
|
||||
) : FlowableAdapter<Conversation>() {
|
||||
) : QkRealmAdapter<Conversation>() {
|
||||
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
@@ -72,7 +72,7 @@ class ConversationsAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
val conversation = getItem(position)
|
||||
val conversation = getItem(position)!!
|
||||
val view = viewHolder.itemView
|
||||
|
||||
view.clicks().subscribe {
|
||||
@@ -103,14 +103,10 @@ class ConversationsAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
override fun getItemId(index: Int): Long {
|
||||
return getItem(index).id
|
||||
return getItem(index)!!.id
|
||||
}
|
||||
|
||||
override fun getItemViewType(position: Int): Int {
|
||||
return if (getItem(position).read) 0 else 1
|
||||
}
|
||||
|
||||
override fun areItemsTheSame(old: Conversation, new: Conversation): Boolean {
|
||||
return old.id == new.id
|
||||
return if (getItem(position)!!.read) 0 else 1
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
||||
|
||||
@Inject lateinit var navigator: Navigator
|
||||
@Inject lateinit var conversationsAdapter: ConversationsAdapter
|
||||
@Inject lateinit var searchAdapter: SearchAdapter
|
||||
@Inject lateinit var itemTouchCallback: ConversationItemTouchCallback
|
||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||
|
||||
@@ -202,7 +203,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
||||
else -> 0
|
||||
}
|
||||
|
||||
toolbarSearch.setVisible(state.page is Inbox && state.page.selected == 0)
|
||||
toolbarSearch.setVisible(state.page is Inbox && state.page.selected == 0 || state.page is Searching)
|
||||
toolbarTitle.setVisible(toolbarSearch.visibility != View.VISIBLE)
|
||||
|
||||
toolbar.menu.findItem(R.id.archive)?.isVisible = state.page is Inbox && selectedConversations != 0
|
||||
@@ -219,12 +220,17 @@ class MainActivity : QkThemedActivity(), MainView {
|
||||
showBackButton(state.page.showClearButton)
|
||||
title = getString(R.string.main_title_selected, state.page.selected)
|
||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||
conversationsAdapter.flowable = state.page.data
|
||||
conversationsAdapter.updateData(state.page.data)
|
||||
itemTouchHelper.attachToRecyclerView(recyclerView)
|
||||
empty.setText(when (state.page.showClearButton) {
|
||||
true -> R.string.inbox_search_empty_text
|
||||
false -> R.string.inbox_empty_text
|
||||
})
|
||||
empty.setText(R.string.inbox_empty_text)
|
||||
}
|
||||
|
||||
is Searching -> {
|
||||
showBackButton(true)
|
||||
if (recyclerView.adapter !== searchAdapter) recyclerView.adapter = searchAdapter
|
||||
searchAdapter.data = state.page.data ?: listOf()
|
||||
itemTouchHelper.attachToRecyclerView(null)
|
||||
empty.setText(R.string.inbox_search_empty_text)
|
||||
}
|
||||
|
||||
is Archived -> {
|
||||
@@ -234,7 +240,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
||||
false -> getString(R.string.title_archived)
|
||||
}
|
||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||
conversationsAdapter.flowable = state.page.data
|
||||
conversationsAdapter.updateData(state.page.data)
|
||||
itemTouchHelper.attachToRecyclerView(null)
|
||||
empty.setText(R.string.archived_empty_text)
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@
|
||||
*/
|
||||
package feature.main
|
||||
|
||||
import io.reactivex.Flowable
|
||||
import io.realm.RealmResults
|
||||
import model.Conversation
|
||||
import model.SearchResult
|
||||
import repository.SyncRepository
|
||||
|
||||
data class MainState(
|
||||
@@ -37,13 +38,18 @@ sealed class MainPage
|
||||
|
||||
data class Inbox(
|
||||
val showClearButton: Boolean = false,
|
||||
val data: Flowable<List<Conversation>>? = null,
|
||||
val data: RealmResults<Conversation>? = null,
|
||||
val selected: Int = 0,
|
||||
val showArchivedSnackbar: Boolean = false) : MainPage()
|
||||
|
||||
data class Searching(
|
||||
val loading: Boolean = false,
|
||||
val data: List<SearchResult>? = null
|
||||
) : MainPage()
|
||||
|
||||
data class Archived(
|
||||
val showClearButton: Boolean = false,
|
||||
val data: Flowable<List<Conversation>>? = null,
|
||||
val data: RealmResults<Conversation>? = null,
|
||||
val selected: Int = 0) : MainPage()
|
||||
|
||||
data class Scheduled(
|
||||
|
||||
@@ -23,7 +23,6 @@ import com.uber.autodispose.android.lifecycle.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import common.Navigator
|
||||
import common.base.QkViewModel
|
||||
import common.util.filter.ConversationFilter
|
||||
import interactor.DeleteConversations
|
||||
import interactor.MarkAllSeen
|
||||
import interactor.MarkArchived
|
||||
@@ -32,9 +31,11 @@ import interactor.MarkUnarchived
|
||||
import interactor.MigratePreferences
|
||||
import interactor.SyncMessages
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxkotlin.Observables
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.rxkotlin.withLatestFrom
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import manager.PermissionManager
|
||||
import manager.RatingManager
|
||||
@@ -46,7 +47,6 @@ import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
class MainViewModel @Inject constructor(
|
||||
private val conversationFilter: ConversationFilter,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val markAllSeen: MarkAllSeen,
|
||||
private val deleteConversations: DeleteConversations,
|
||||
@@ -59,9 +59,7 @@ class MainViewModel @Inject constructor(
|
||||
private val ratingManager: RatingManager,
|
||||
private val syncMessages: SyncMessages,
|
||||
private val syncRepository: SyncRepository
|
||||
) : QkViewModel<MainView, MainState>(MainState()) {
|
||||
|
||||
private val conversations = messageRepo.getConversations()
|
||||
) : QkViewModel<MainView, MainState>(MainState(page = Inbox(data = messageRepo.getConversations()))) {
|
||||
|
||||
init {
|
||||
disposables += deleteConversations
|
||||
@@ -132,18 +130,25 @@ class MainViewModel @Inject constructor(
|
||||
|
||||
view.queryChangedIntent
|
||||
.debounce(200, TimeUnit.MILLISECONDS)
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.map { query -> query.removeAccents() }
|
||||
.withLatestFrom(state, { query, state ->
|
||||
if (state.page is Inbox) {
|
||||
val filteredConversations = if (query.isEmpty()) conversations
|
||||
else conversations.map { conversations -> conversations.filter { conversationFilter.filter(it, query) } }
|
||||
|
||||
val page = state.page.copy(showClearButton = query.isNotEmpty(), data = filteredConversations)
|
||||
newState { it.copy(page = page) }
|
||||
if (query.isEmpty() && state.page is Searching) {
|
||||
newState { it.copy(page = Inbox(data = messageRepo.getConversations())) }
|
||||
}
|
||||
query
|
||||
})
|
||||
.filter { query -> query.length >= 2 }
|
||||
.doOnNext {
|
||||
newState { state ->
|
||||
val page = (state.page as? Searching) ?: Searching()
|
||||
state.copy(page = page.copy(loading = true))
|
||||
}
|
||||
}
|
||||
.observeOn(Schedulers.io())
|
||||
.switchMap { query -> Observable.just(query).map { messageRepo.searchConversations(it) } }
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
.subscribe { data -> newState { it.copy(page = Searching(loading = false, data = data)) } }
|
||||
|
||||
view.composeIntent
|
||||
.autoDisposable(view.scope())
|
||||
@@ -152,6 +157,8 @@ class MainViewModel @Inject constructor(
|
||||
view.homeIntent
|
||||
.withLatestFrom(state, { _, state ->
|
||||
when {
|
||||
state.page is Searching -> view.clearSearch()
|
||||
|
||||
state.page is Inbox && state.page.selected > 0 -> view.clearSelection()
|
||||
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
||||
|
||||
@@ -175,7 +182,7 @@ class MainViewModel @Inject constructor(
|
||||
.distinctUntilChanged()
|
||||
.doOnNext {
|
||||
when (it) {
|
||||
DrawerItem.INBOX -> newState { it.copy(page = Inbox(data = conversations)) }
|
||||
DrawerItem.INBOX -> newState { it.copy(page = Inbox(data = messageRepo.getConversations())) }
|
||||
DrawerItem.ARCHIVED -> newState { it.copy(page = Archived(data = messageRepo.getConversations(true))) }
|
||||
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
||||
else -> {
|
||||
@@ -289,12 +296,14 @@ class MainViewModel @Inject constructor(
|
||||
when {
|
||||
state.drawerOpen -> newState { it.copy(drawerOpen = false) }
|
||||
|
||||
state.page is Searching -> view.clearSearch()
|
||||
|
||||
state.page is Inbox && state.page.selected > 0 -> view.clearSelection()
|
||||
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
||||
|
||||
state.page is Archived && state.page.selected > 0 -> view.clearSelection()
|
||||
|
||||
state.page !is Inbox -> newState { it.copy(page = Inbox(data = conversations)) }
|
||||
state.page !is Inbox -> newState { it.copy(page = Inbox(data = messageRepo.getConversations())) }
|
||||
|
||||
else -> newState { it.copy(hasError = true) }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package feature.main
|
||||
|
||||
import android.content.Context
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import android.text.style.BackgroundColorSpan
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import common.Navigator
|
||||
import common.base.QkAdapter
|
||||
import common.base.QkViewHolder
|
||||
import common.util.Colors
|
||||
import common.util.DateFormatter
|
||||
import common.util.extensions.setVisible
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import kotlinx.android.synthetic.main.search_list_item.view.*
|
||||
import model.SearchResult
|
||||
import javax.inject.Inject
|
||||
|
||||
class SearchAdapter @Inject constructor(
|
||||
private val context: Context,
|
||||
private val colors: Colors,
|
||||
private val dateFormatter: DateFormatter,
|
||||
private val navigator: Navigator
|
||||
) : QkAdapter<SearchResult>() {
|
||||
|
||||
private var highlightColor: Int = 0
|
||||
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
|
||||
val layoutInflater = LayoutInflater.from(context)
|
||||
val view = layoutInflater.inflate(R.layout.search_list_item, parent, false)
|
||||
return QkViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||
val previous = data.getOrNull(position - 1)
|
||||
val result = getItem(position)
|
||||
val view = viewHolder.itemView
|
||||
|
||||
view.clicks().subscribe {
|
||||
navigator.showConversation(result.conversation.id, result.query.takeIf { result.messages > 0 })
|
||||
}
|
||||
|
||||
view.resultsHeader.setVisible(result.messages > 0 && previous?.messages == 0)
|
||||
|
||||
val query = result.query
|
||||
val title = SpannableString(result.conversation.getTitle())
|
||||
var index = title.indexOf(query, ignoreCase = true)
|
||||
|
||||
while (index >= 0) {
|
||||
title.setSpan(BackgroundColorSpan(highlightColor), index, index + query.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
index = title.indexOf(query, index + query.length, true)
|
||||
}
|
||||
view.title.text = title
|
||||
|
||||
view.avatars.contacts = result.conversation.recipients
|
||||
|
||||
when (result.messages == 0) {
|
||||
true -> {
|
||||
view.date.setVisible(true)
|
||||
view.date.text = dateFormatter.getConversationTimestamp(result.conversation.date)
|
||||
view.snippet.text = when (result.conversation.me) {
|
||||
true -> context.getString(R.string.main_sender_you, result.conversation.snippet)
|
||||
false -> result.conversation.snippet
|
||||
}
|
||||
}
|
||||
|
||||
false -> {
|
||||
view.date.setVisible(false)
|
||||
view.snippet.text = context.getString(R.string.main_message_results, result.messages)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun areItemsTheSame(old: SearchResult, new: SearchResult): Boolean {
|
||||
return old.conversation.id == new.conversation.id && old.messages > 0 == new.messages > 0
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(old: SearchResult, new: SearchResult): Boolean {
|
||||
return old.query == new.query && // Queries are the same
|
||||
old.conversation.id == new.conversation.id // Conversation id is the same
|
||||
&& old.messages == new.messages // Result count is the same
|
||||
}
|
||||
|
||||
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
|
||||
disposables += colors.highlightColor
|
||||
.doOnNext { color -> highlightColor = color }
|
||||
.subscribe { notifyDataSetChanged() }
|
||||
}
|
||||
|
||||
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
|
||||
disposables.dispose()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user