mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-22 13:48:52 +01:00
#1010 - Message content search
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package common.util.filter
|
package filter
|
||||||
|
|
||||||
import model.Contact
|
import model.Contact
|
||||||
import util.extensions.removeAccents
|
import util.extensions.removeAccents
|
||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package common.util.filter
|
package filter
|
||||||
|
|
||||||
import model.Conversation
|
import model.Conversation
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package common.util.filter
|
package filter
|
||||||
|
|
||||||
abstract class Filter<in T> {
|
abstract class Filter<in T> {
|
||||||
|
|
||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package common.util.filter
|
package filter
|
||||||
|
|
||||||
import android.telephony.PhoneNumberUtils
|
import android.telephony.PhoneNumberUtils
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package common.util.filter
|
package filter
|
||||||
|
|
||||||
import model.Recipient
|
import model.Recipient
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -32,10 +32,11 @@ import android.telephony.PhoneNumberUtils
|
|||||||
import android.telephony.SmsManager
|
import android.telephony.SmsManager
|
||||||
import com.klinker.android.send_message.BroadcastUtils
|
import com.klinker.android.send_message.BroadcastUtils
|
||||||
import com.klinker.android.send_message.StripAccents
|
import com.klinker.android.send_message.StripAccents
|
||||||
import io.reactivex.Flowable
|
import filter.ConversationFilter
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import io.realm.Case
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
@@ -46,6 +47,7 @@ import model.Contact
|
|||||||
import model.Conversation
|
import model.Conversation
|
||||||
import model.Message
|
import model.Message
|
||||||
import model.MmsPart
|
import model.MmsPart
|
||||||
|
import model.SearchResult
|
||||||
import util.MessageUtils
|
import util.MessageUtils
|
||||||
import util.Preferences
|
import util.Preferences
|
||||||
import util.extensions.anyOf
|
import util.extensions.anyOf
|
||||||
@@ -59,13 +61,13 @@ import javax.inject.Singleton
|
|||||||
class MessageRepositoryImpl @Inject constructor(
|
class MessageRepositoryImpl @Inject constructor(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val messageIds: KeyManager,
|
private val messageIds: KeyManager,
|
||||||
|
private val conversationFilter: ConversationFilter,
|
||||||
private val cursorToConversation: CursorToConversation,
|
private val cursorToConversation: CursorToConversation,
|
||||||
private val cursorToRecipient: CursorToRecipient,
|
private val cursorToRecipient: CursorToRecipient,
|
||||||
private val prefs: Preferences) : MessageRepository {
|
private val prefs: Preferences) : MessageRepository {
|
||||||
|
|
||||||
override fun getConversations(archived: Boolean): Flowable<List<Conversation>> {
|
override fun getConversations(archived: Boolean): RealmResults<Conversation> {
|
||||||
val realm = Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
return realm
|
|
||||||
.where(Conversation::class.java)
|
.where(Conversation::class.java)
|
||||||
.notEqualTo("id", 0L)
|
.notEqualTo("id", 0L)
|
||||||
.greaterThan("count", 0)
|
.greaterThan("count", 0)
|
||||||
@@ -74,10 +76,6 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
.isNotEmpty("recipients")
|
.isNotEmpty("recipients")
|
||||||
.sort("date", Sort.DESCENDING)
|
.sort("date", Sort.DESCENDING)
|
||||||
.findAllAsync()
|
.findAllAsync()
|
||||||
.asFlowable()
|
|
||||||
.filter { it.isLoaded }
|
|
||||||
.filter { it.isValid }
|
|
||||||
.map { conversations -> realm.copyFromRealm(conversations) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getConversationsSnapshot(): List<Conversation> {
|
override fun getConversationsSnapshot(): List<Conversation> {
|
||||||
@@ -92,6 +90,25 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
.findAll())
|
.findAll())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun searchConversations(query: String): List<SearchResult> {
|
||||||
|
val conversations = getConversationsSnapshot()
|
||||||
|
|
||||||
|
val messagesByConversation = Realm.getDefaultInstance()
|
||||||
|
.where(Message::class.java)
|
||||||
|
.contains("body", query, Case.INSENSITIVE)
|
||||||
|
.findAll()
|
||||||
|
.groupBy { message -> message.threadId }
|
||||||
|
.filter { (threadId, _) -> conversations.firstOrNull { it.id == threadId } != null }
|
||||||
|
.map { (threadId, messages) -> Pair(conversations.first { it.id == threadId }, messages.size) }
|
||||||
|
.map { (conversation, messages) -> SearchResult(query, conversation, messages) }
|
||||||
|
.sortedByDescending { result -> result.messages }
|
||||||
|
|
||||||
|
return conversations
|
||||||
|
.filter { conversation -> conversationFilter.filter(conversation, query) }
|
||||||
|
.map { conversation -> SearchResult(query, conversation, 0) }
|
||||||
|
.plus(messagesByConversation)
|
||||||
|
}
|
||||||
|
|
||||||
override fun getBlockedConversations(): RealmResults<Conversation> {
|
override fun getBlockedConversations(): RealmResults<Conversation> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
.where(Conversation::class.java)
|
.where(Conversation::class.java)
|
||||||
@@ -166,10 +183,11 @@ class MessageRepositoryImpl @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMessages(threadId: Long): RealmResults<Message> {
|
override fun getMessages(threadId: Long, query: String): RealmResults<Message> {
|
||||||
return Realm.getDefaultInstance()
|
return Realm.getDefaultInstance()
|
||||||
.where(Message::class.java)
|
.where(Message::class.java)
|
||||||
.equalTo("threadId", threadId)
|
.equalTo("threadId", threadId)
|
||||||
|
.let { if (query.isEmpty()) it else it.contains("body", query, Case.INSENSITIVE) }
|
||||||
.sort("date")
|
.sort("date")
|
||||||
.findAllAsync()
|
.findAllAsync()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
data class SearchResult(val query: String, val conversation: Conversation, val messages: Int)
|
||||||
@@ -18,19 +18,21 @@
|
|||||||
*/
|
*/
|
||||||
package repository
|
package repository
|
||||||
|
|
||||||
import io.reactivex.Flowable
|
|
||||||
import io.reactivex.Maybe
|
import io.reactivex.Maybe
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
import model.Conversation
|
import model.Conversation
|
||||||
import model.Message
|
import model.Message
|
||||||
import model.MmsPart
|
import model.MmsPart
|
||||||
|
import model.SearchResult
|
||||||
|
|
||||||
interface MessageRepository {
|
interface MessageRepository {
|
||||||
|
|
||||||
fun getConversations(archived: Boolean = false): Flowable<List<Conversation>>
|
fun getConversations(archived: Boolean = false): RealmResults<Conversation>
|
||||||
|
|
||||||
fun getConversationsSnapshot(): List<Conversation>
|
fun getConversationsSnapshot(): List<Conversation>
|
||||||
|
|
||||||
|
fun searchConversations(query: String): List<SearchResult>
|
||||||
|
|
||||||
fun getBlockedConversations(): RealmResults<Conversation>
|
fun getBlockedConversations(): RealmResults<Conversation>
|
||||||
|
|
||||||
fun getConversationAsync(threadId: Long): Conversation
|
fun getConversationAsync(threadId: Long): Conversation
|
||||||
@@ -45,7 +47,7 @@ interface MessageRepository {
|
|||||||
|
|
||||||
fun saveDraft(threadId: Long, draft: String)
|
fun saveDraft(threadId: Long, draft: String)
|
||||||
|
|
||||||
fun getMessages(threadId: Long): RealmResults<Message>
|
fun getMessages(threadId: Long, query: String = ""): RealmResults<Message>
|
||||||
|
|
||||||
fun getMessage(id: Long): Message?
|
fun getMessage(id: Long): Message?
|
||||||
|
|
||||||
|
|||||||
@@ -84,9 +84,10 @@ class Navigator @Inject constructor(private val context: Context, private val no
|
|||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun showConversation(threadId: Long) {
|
fun showConversation(threadId: Long, query: String? = null) {
|
||||||
val intent = Intent(context, ComposeActivity::class.java)
|
val intent = Intent(context, ComposeActivity::class.java)
|
||||||
intent.putExtra("threadId", threadId)
|
.putExtra("threadId", threadId)
|
||||||
|
.putExtra("query", query)
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ package common.base
|
|||||||
|
|
||||||
import android.support.annotation.CallSuper
|
import android.support.annotation.CallSuper
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
import android.view.View
|
|
||||||
import common.util.extensions.setVisible
|
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.disposables.Disposable
|
import io.reactivex.disposables.Disposable
|
||||||
|
|
||||||
@@ -31,16 +29,6 @@ import io.reactivex.disposables.Disposable
|
|||||||
*/
|
*/
|
||||||
abstract class FlowableAdapter<T> : QkAdapter<T>() {
|
abstract class FlowableAdapter<T> : QkAdapter<T>() {
|
||||||
|
|
||||||
/**
|
|
||||||
* This view can be set, and the adapter will automatically control the visibility of this view
|
|
||||||
* based on the data
|
|
||||||
*/
|
|
||||||
var emptyView: View? = null
|
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
value?.setVisible(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
var flowable: Flowable<List<T>>? = null
|
var flowable: Flowable<List<T>>? = null
|
||||||
set(value) {
|
set(value) {
|
||||||
if (field === value) return
|
if (field === value) return
|
||||||
@@ -83,7 +71,6 @@ abstract class FlowableAdapter<T> : QkAdapter<T>() {
|
|||||||
private fun subscribe() {
|
private fun subscribe() {
|
||||||
disposable = flowable?.subscribe {
|
disposable = flowable?.subscribe {
|
||||||
data = it
|
data = it
|
||||||
emptyView?.setVisible(data.isEmpty())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ package common.base
|
|||||||
|
|
||||||
import android.support.v7.util.DiffUtil
|
import android.support.v7.util.DiffUtil
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
|
import android.view.View
|
||||||
|
import common.util.extensions.setVisible
|
||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
|
|
||||||
@@ -36,7 +38,18 @@ abstract class QkAdapter<T> : RecyclerView.Adapter<QkViewHolder>() {
|
|||||||
val diff = DiffUtil.calculateDiff(getDiffUtilCallback(field, value))
|
val diff = DiffUtil.calculateDiff(getDiffUtilCallback(field, value))
|
||||||
field = value
|
field = value
|
||||||
diff.dispatchUpdatesTo(this)
|
diff.dispatchUpdatesTo(this)
|
||||||
onDatasetChanged()
|
|
||||||
|
emptyView?.setVisible(value.isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This view can be set, and the adapter will automatically control the visibility of this view
|
||||||
|
* based on the data
|
||||||
|
*/
|
||||||
|
var emptyView: View? = null
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
value?.setVisible(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
val selectionChanges: Subject<List<Long>> = BehaviorSubject.create()
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ abstract class QkRealmAdapter<T : RealmModel> : RealmRecyclerViewAdapter<T, QkVi
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun updateData(data: OrderedRealmCollection<T>?) {
|
override fun updateData(data: OrderedRealmCollection<T>?) {
|
||||||
|
if (getData() === data) return
|
||||||
|
|
||||||
removeListener(getData())
|
removeListener(getData())
|
||||||
addListener(data)
|
addListener(data)
|
||||||
|
|
||||||
|
|||||||
@@ -82,14 +82,14 @@ abstract class QkThemedActivity : QkActivity() {
|
|||||||
super.onPostCreate(savedInstanceState)
|
super.onPostCreate(savedInstanceState)
|
||||||
|
|
||||||
// Update the colours of the menu items
|
// Update the colours of the menu items
|
||||||
Observables.combineLatest(menu, theme, colors.textTertiary, { menu, theme, textTertiary ->
|
Observables.combineLatest(menu, theme, colors.textSecondary, { menu, theme, text ->
|
||||||
(0 until menu.size())
|
(0 until menu.size())
|
||||||
.map { position -> menu.getItem(position) }
|
.map { position -> menu.getItem(position) }
|
||||||
.forEach { menuItem ->
|
.forEach { menuItem ->
|
||||||
menuItem?.icon?.run {
|
menuItem?.icon?.run {
|
||||||
setTint(when (menuItem.itemId) {
|
setTint(when (menuItem.itemId) {
|
||||||
R.id.info -> textTertiary
|
in getColoredMenuItems() -> theme
|
||||||
else -> theme
|
else -> text
|
||||||
})
|
})
|
||||||
|
|
||||||
menuItem.icon = this
|
menuItem.icon = this
|
||||||
@@ -119,6 +119,10 @@ abstract class QkThemedActivity : QkActivity() {
|
|||||||
.subscribe { color -> toolbar?.setBackgroundTint(color) }
|
.subscribe { color -> toolbar?.setBackgroundTint(color) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open fun getColoredMenuItems(): List<Int> {
|
||||||
|
return listOf()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This can be overridden in case an activity does not want to use the default themes
|
* This can be overridden in case an activity does not want to use the default themes
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -204,6 +204,11 @@ class Colors @Inject constructor(private val context: Context, private val prefs
|
|||||||
.map { res -> getColor(res) }
|
.map { res -> getColor(res) }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
|
|
||||||
|
val highlightColor: Observable<Int> = theme
|
||||||
|
.map { theme -> FloatArray(3).apply { Color.colorToHSV(theme, this) } }
|
||||||
|
.map { hsv -> hsv.apply { set(2, 0.75f) } } // 75% value
|
||||||
|
.map { hsv -> Color.HSVToColor(85, hsv) } // 33% alpha
|
||||||
|
|
||||||
val textPrimaryOnTheme = textPrimaryOnThemeForConversation()
|
val textPrimaryOnTheme = textPrimaryOnThemeForConversation()
|
||||||
|
|
||||||
val textSecondaryOnTheme = textSecondaryOnThemeForConversation()
|
val textSecondaryOnTheme = textSecondaryOnThemeForConversation()
|
||||||
|
|||||||
@@ -171,11 +171,15 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
|
|
||||||
threadId.onNext(state.selectedConversation)
|
threadId.onNext(state.selectedConversation)
|
||||||
|
|
||||||
title = when (state.selectedMessages) {
|
title = when {
|
||||||
0 -> state.conversationtitle
|
state.selectedMessages > 0 -> getString(R.string.compose_title_selected, state.selectedMessages)
|
||||||
else -> 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)
|
toolbarTitle.setVisible(!state.editingMode)
|
||||||
chips.setVisible(state.editingMode)
|
chips.setVisible(state.editingMode)
|
||||||
contacts.setVisible(state.contactsVisible)
|
contacts.setVisible(state.contactsVisible)
|
||||||
@@ -185,11 +189,14 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
if (state.editingMode && chips.adapter == null) chips.adapter = chipsAdapter
|
if (state.editingMode && chips.adapter == null) chips.adapter = chipsAdapter
|
||||||
if (state.editingMode && contacts.adapter == null) contacts.adapter = contactsAdapter
|
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.call)?.isVisible = !state.editingMode && state.selectedMessages == 0 && state.query.isEmpty()
|
||||||
toolbar.menu.findItem(R.id.info)?.isVisible = !state.editingMode && state.selectedMessages == 0
|
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.copy)?.isVisible = !state.editingMode && state.selectedMessages == 1
|
||||||
toolbar.menu.findItem(R.id.delete)?.isVisible = !state.editingMode && state.selectedMessages > 0
|
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.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()) {
|
if (chipsAdapter.data.isEmpty() && state.selectedContacts.isNotEmpty()) {
|
||||||
message.showKeyboard()
|
message.showKeyboard()
|
||||||
@@ -198,6 +205,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
chipsAdapter.data = state.selectedContacts
|
chipsAdapter.data = state.selectedContacts
|
||||||
contactsAdapter.data = state.contacts
|
contactsAdapter.data = state.contacts
|
||||||
messageAdapter.data = state.messages
|
messageAdapter.data = state.messages
|
||||||
|
messageAdapter.highlight = state.searchSelectionId
|
||||||
|
|
||||||
attachments.setVisible(state.attachments.isNotEmpty())
|
attachments.setVisible(state.attachments.isNotEmpty())
|
||||||
attachmentAdapter.data = state.attachments
|
attachmentAdapter.data = state.attachments
|
||||||
@@ -220,6 +228,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
message.setText(draft)
|
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 {
|
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||||
menuInflater.inflate(R.menu.compose, menu)
|
menuInflater.inflate(R.menu.compose, menu)
|
||||||
return super.onCreateOptionsMenu(menu)
|
return super.onCreateOptionsMenu(menu)
|
||||||
@@ -230,6 +245,10 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun getColoredMenuItems(): List<Int> {
|
||||||
|
return super.getColoredMenuItems() + R.id.call
|
||||||
|
}
|
||||||
|
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
backPressedIntent.onNext(Unit)
|
backPressedIntent.onNext(Unit)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ data class ComposeState(
|
|||||||
val selectedConversation: Long = 0,
|
val selectedConversation: Long = 0,
|
||||||
val selectedContacts: List<Contact> = ArrayList(),
|
val selectedContacts: List<Contact> = ArrayList(),
|
||||||
val conversationtitle: String = "",
|
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 messages: Pair<Conversation, RealmResults<Message>>? = null,
|
||||||
val selectedMessages: Int = 0,
|
val selectedMessages: Int = 0,
|
||||||
val attachments: List<Attachment> = ArrayList(),
|
val attachments: List<Attachment> = ArrayList(),
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
|
|
||||||
fun clearSelection()
|
fun clearSelection()
|
||||||
fun setDraft(draft: String)
|
fun setDraft(draft: String)
|
||||||
|
fun scrollToMessage(id: Long)
|
||||||
val backPressedIntent: Observable<Unit>
|
val backPressedIntent: Observable<Unit>
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ import common.Navigator
|
|||||||
import common.base.QkViewModel
|
import common.base.QkViewModel
|
||||||
import common.util.ClipboardUtils
|
import common.util.ClipboardUtils
|
||||||
import common.util.extensions.makeToast
|
import common.util.extensions.makeToast
|
||||||
import common.util.filter.ContactFilter
|
import filter.ContactFilter
|
||||||
import interactor.CancelDelayedMessage
|
import interactor.CancelDelayedMessage
|
||||||
import interactor.ContactSync
|
import interactor.ContactSync
|
||||||
import interactor.DeleteMessages
|
import interactor.DeleteMessages
|
||||||
@@ -64,7 +64,6 @@ import java.net.URLDecoder
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Named
|
|
||||||
|
|
||||||
class ComposeViewModel @Inject constructor(
|
class ComposeViewModel @Inject constructor(
|
||||||
private val intent: Intent,
|
private val intent: Intent,
|
||||||
@@ -79,19 +78,19 @@ class ComposeViewModel @Inject constructor(
|
|||||||
private val retrySending: RetrySending,
|
private val retrySending: RetrySending,
|
||||||
private val markRead: MarkRead,
|
private val markRead: MarkRead,
|
||||||
private val deleteMessages: DeleteMessages
|
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 attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(ArrayList())
|
||||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
|
||||||
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
||||||
private val selectedContacts: Observable<List<Contact>>
|
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 conversation: Subject<Conversation> = BehaviorSubject.create()
|
||||||
private val messages: Subject<List<Message>> = BehaviorSubject.create()
|
private val messages: Subject<List<Message>> = BehaviorSubject.create()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
sharedText = intent.extras?.getString(Intent.EXTRA_TEXT) ?: ""
|
|
||||||
|
|
||||||
// If there are any image attachments, we'll set those as the initial attachments for the
|
// If there are any image attachments, we'll set those as the initial attachments for the
|
||||||
// conversation
|
// conversation
|
||||||
val sharedImages = mutableListOf<Uri>()
|
val sharedImages = mutableListOf<Uri>()
|
||||||
@@ -176,6 +175,25 @@ class ComposeViewModel @Inject constructor(
|
|||||||
disposables += attachments
|
disposables += attachments
|
||||||
.subscribe { attachments -> newState { it.copy(attachments = 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) {
|
if (threadId == 0L) {
|
||||||
syncContacts.execute(Unit)
|
syncContacts.execute(Unit)
|
||||||
}
|
}
|
||||||
@@ -314,6 +332,48 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { view.clearSelection() }
|
.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
|
// Retry sending
|
||||||
view.messageClickIntent
|
view.messageClickIntent
|
||||||
.filter { message -> message.isFailedMessage() }
|
.filter { message -> message.isFailedMessage() }
|
||||||
|
|||||||
@@ -103,6 +103,17 @@ class MessagesAdapter @Inject constructor(
|
|||||||
private val conversation: Conversation?
|
private val conversation: Conversation?
|
||||||
get() = data?.first?.takeIf { it.isValid }
|
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 layoutInflater = LayoutInflater.from(context)
|
||||||
private val contactCache = ContactCache()
|
private val contactCache = ContactCache()
|
||||||
private val expanded = HashMap<Long, Boolean>()
|
private val expanded = HashMap<Long, Boolean>()
|
||||||
@@ -178,7 +189,7 @@ class MessagesAdapter @Inject constructor(
|
|||||||
|
|
||||||
|
|
||||||
// Update the selected state
|
// Update the selected state
|
||||||
view.isSelected = isSelected(message.id)
|
view.isSelected = isSelected(message.id) || highlight == message.id
|
||||||
|
|
||||||
|
|
||||||
// Bind the cancel view
|
// Bind the cancel view
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import com.jakewharton.rxbinding2.view.clicks
|
|||||||
import com.jakewharton.rxbinding2.view.longClicks
|
import com.jakewharton.rxbinding2.view.longClicks
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import common.Navigator
|
import common.Navigator
|
||||||
import common.base.FlowableAdapter
|
import common.base.QkRealmAdapter
|
||||||
import common.base.QkViewHolder
|
import common.base.QkViewHolder
|
||||||
import common.util.Colors
|
import common.util.Colors
|
||||||
import common.util.DateFormatter
|
import common.util.DateFormatter
|
||||||
@@ -42,7 +42,7 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
private val colors: Colors,
|
private val colors: Colors,
|
||||||
private val dateFormatter: DateFormatter,
|
private val dateFormatter: DateFormatter,
|
||||||
private val navigator: Navigator
|
private val navigator: Navigator
|
||||||
) : FlowableAdapter<Conversation>() {
|
) : QkRealmAdapter<Conversation>() {
|
||||||
|
|
||||||
private val disposables = CompositeDisposable()
|
private val disposables = CompositeDisposable()
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
override fun onBindViewHolder(viewHolder: QkViewHolder, position: Int) {
|
||||||
val conversation = getItem(position)
|
val conversation = getItem(position)!!
|
||||||
val view = viewHolder.itemView
|
val view = viewHolder.itemView
|
||||||
|
|
||||||
view.clicks().subscribe {
|
view.clicks().subscribe {
|
||||||
@@ -103,14 +103,10 @@ class ConversationsAdapter @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemId(index: Int): Long {
|
override fun getItemId(index: Int): Long {
|
||||||
return getItem(index).id
|
return getItem(index)!!.id
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
return if (getItem(position).read) 0 else 1
|
return if (getItem(position)!!.read) 0 else 1
|
||||||
}
|
|
||||||
|
|
||||||
override fun areItemsTheSame(old: Conversation, new: Conversation): Boolean {
|
|
||||||
return old.id == new.id
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
|||||||
|
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
@Inject lateinit var conversationsAdapter: ConversationsAdapter
|
@Inject lateinit var conversationsAdapter: ConversationsAdapter
|
||||||
|
@Inject lateinit var searchAdapter: SearchAdapter
|
||||||
@Inject lateinit var itemTouchCallback: ConversationItemTouchCallback
|
@Inject lateinit var itemTouchCallback: ConversationItemTouchCallback
|
||||||
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
|
||||||
|
|
||||||
@@ -202,7 +203,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
|||||||
else -> 0
|
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)
|
toolbarTitle.setVisible(toolbarSearch.visibility != View.VISIBLE)
|
||||||
|
|
||||||
toolbar.menu.findItem(R.id.archive)?.isVisible = state.page is Inbox && selectedConversations != 0
|
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)
|
showBackButton(state.page.showClearButton)
|
||||||
title = getString(R.string.main_title_selected, state.page.selected)
|
title = getString(R.string.main_title_selected, state.page.selected)
|
||||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||||
conversationsAdapter.flowable = state.page.data
|
conversationsAdapter.updateData(state.page.data)
|
||||||
itemTouchHelper.attachToRecyclerView(recyclerView)
|
itemTouchHelper.attachToRecyclerView(recyclerView)
|
||||||
empty.setText(when (state.page.showClearButton) {
|
empty.setText(R.string.inbox_empty_text)
|
||||||
true -> R.string.inbox_search_empty_text
|
}
|
||||||
false -> 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 -> {
|
is Archived -> {
|
||||||
@@ -234,7 +240,7 @@ class MainActivity : QkThemedActivity(), MainView {
|
|||||||
false -> getString(R.string.title_archived)
|
false -> getString(R.string.title_archived)
|
||||||
}
|
}
|
||||||
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
if (recyclerView.adapter !== conversationsAdapter) recyclerView.adapter = conversationsAdapter
|
||||||
conversationsAdapter.flowable = state.page.data
|
conversationsAdapter.updateData(state.page.data)
|
||||||
itemTouchHelper.attachToRecyclerView(null)
|
itemTouchHelper.attachToRecyclerView(null)
|
||||||
empty.setText(R.string.archived_empty_text)
|
empty.setText(R.string.archived_empty_text)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
package feature.main
|
package feature.main
|
||||||
|
|
||||||
import io.reactivex.Flowable
|
import io.realm.RealmResults
|
||||||
import model.Conversation
|
import model.Conversation
|
||||||
|
import model.SearchResult
|
||||||
import repository.SyncRepository
|
import repository.SyncRepository
|
||||||
|
|
||||||
data class MainState(
|
data class MainState(
|
||||||
@@ -37,13 +38,18 @@ sealed class MainPage
|
|||||||
|
|
||||||
data class Inbox(
|
data class Inbox(
|
||||||
val showClearButton: Boolean = false,
|
val showClearButton: Boolean = false,
|
||||||
val data: Flowable<List<Conversation>>? = null,
|
val data: RealmResults<Conversation>? = null,
|
||||||
val selected: Int = 0,
|
val selected: Int = 0,
|
||||||
val showArchivedSnackbar: Boolean = false) : MainPage()
|
val showArchivedSnackbar: Boolean = false) : MainPage()
|
||||||
|
|
||||||
|
data class Searching(
|
||||||
|
val loading: Boolean = false,
|
||||||
|
val data: List<SearchResult>? = null
|
||||||
|
) : MainPage()
|
||||||
|
|
||||||
data class Archived(
|
data class Archived(
|
||||||
val showClearButton: Boolean = false,
|
val showClearButton: Boolean = false,
|
||||||
val data: Flowable<List<Conversation>>? = null,
|
val data: RealmResults<Conversation>? = null,
|
||||||
val selected: Int = 0) : MainPage()
|
val selected: Int = 0) : MainPage()
|
||||||
|
|
||||||
data class Scheduled(
|
data class Scheduled(
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import com.uber.autodispose.android.lifecycle.scope
|
|||||||
import com.uber.autodispose.kotlin.autoDisposable
|
import com.uber.autodispose.kotlin.autoDisposable
|
||||||
import common.Navigator
|
import common.Navigator
|
||||||
import common.base.QkViewModel
|
import common.base.QkViewModel
|
||||||
import common.util.filter.ConversationFilter
|
|
||||||
import interactor.DeleteConversations
|
import interactor.DeleteConversations
|
||||||
import interactor.MarkAllSeen
|
import interactor.MarkAllSeen
|
||||||
import interactor.MarkArchived
|
import interactor.MarkArchived
|
||||||
@@ -32,9 +31,11 @@ import interactor.MarkUnarchived
|
|||||||
import interactor.MigratePreferences
|
import interactor.MigratePreferences
|
||||||
import interactor.SyncMessages
|
import interactor.SyncMessages
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
import io.reactivex.rxkotlin.Observables
|
import io.reactivex.rxkotlin.Observables
|
||||||
import io.reactivex.rxkotlin.plusAssign
|
import io.reactivex.rxkotlin.plusAssign
|
||||||
import io.reactivex.rxkotlin.withLatestFrom
|
import io.reactivex.rxkotlin.withLatestFrom
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
import io.realm.Realm
|
import io.realm.Realm
|
||||||
import manager.PermissionManager
|
import manager.PermissionManager
|
||||||
import manager.RatingManager
|
import manager.RatingManager
|
||||||
@@ -46,7 +47,6 @@ import java.util.concurrent.TimeUnit
|
|||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class MainViewModel @Inject constructor(
|
class MainViewModel @Inject constructor(
|
||||||
private val conversationFilter: ConversationFilter,
|
|
||||||
private val messageRepo: MessageRepository,
|
private val messageRepo: MessageRepository,
|
||||||
private val markAllSeen: MarkAllSeen,
|
private val markAllSeen: MarkAllSeen,
|
||||||
private val deleteConversations: DeleteConversations,
|
private val deleteConversations: DeleteConversations,
|
||||||
@@ -59,9 +59,7 @@ class MainViewModel @Inject constructor(
|
|||||||
private val ratingManager: RatingManager,
|
private val ratingManager: RatingManager,
|
||||||
private val syncMessages: SyncMessages,
|
private val syncMessages: SyncMessages,
|
||||||
private val syncRepository: SyncRepository
|
private val syncRepository: SyncRepository
|
||||||
) : QkViewModel<MainView, MainState>(MainState()) {
|
) : QkViewModel<MainView, MainState>(MainState(page = Inbox(data = messageRepo.getConversations()))) {
|
||||||
|
|
||||||
private val conversations = messageRepo.getConversations()
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
disposables += deleteConversations
|
disposables += deleteConversations
|
||||||
@@ -132,18 +130,25 @@ class MainViewModel @Inject constructor(
|
|||||||
|
|
||||||
view.queryChangedIntent
|
view.queryChangedIntent
|
||||||
.debounce(200, TimeUnit.MILLISECONDS)
|
.debounce(200, TimeUnit.MILLISECONDS)
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
.map { query -> query.removeAccents() }
|
.map { query -> query.removeAccents() }
|
||||||
.withLatestFrom(state, { query, state ->
|
.withLatestFrom(state, { query, state ->
|
||||||
if (state.page is Inbox) {
|
if (query.isEmpty() && state.page is Searching) {
|
||||||
val filteredConversations = if (query.isEmpty()) conversations
|
newState { it.copy(page = Inbox(data = messageRepo.getConversations())) }
|
||||||
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) }
|
|
||||||
}
|
}
|
||||||
|
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())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe { data -> newState { it.copy(page = Searching(loading = false, data = data)) } }
|
||||||
|
|
||||||
view.composeIntent
|
view.composeIntent
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
@@ -152,6 +157,8 @@ class MainViewModel @Inject constructor(
|
|||||||
view.homeIntent
|
view.homeIntent
|
||||||
.withLatestFrom(state, { _, state ->
|
.withLatestFrom(state, { _, state ->
|
||||||
when {
|
when {
|
||||||
|
state.page is Searching -> view.clearSearch()
|
||||||
|
|
||||||
state.page is Inbox && state.page.selected > 0 -> view.clearSelection()
|
state.page is Inbox && state.page.selected > 0 -> view.clearSelection()
|
||||||
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
||||||
|
|
||||||
@@ -175,7 +182,7 @@ class MainViewModel @Inject constructor(
|
|||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.doOnNext {
|
.doOnNext {
|
||||||
when (it) {
|
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.ARCHIVED -> newState { it.copy(page = Archived(data = messageRepo.getConversations(true))) }
|
||||||
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
DrawerItem.SCHEDULED -> newState { it.copy(page = Scheduled()) }
|
||||||
else -> {
|
else -> {
|
||||||
@@ -289,12 +296,14 @@ class MainViewModel @Inject constructor(
|
|||||||
when {
|
when {
|
||||||
state.drawerOpen -> newState { it.copy(drawerOpen = false) }
|
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.selected > 0 -> view.clearSelection()
|
||||||
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
state.page is Inbox && state.page.showClearButton -> view.clearSearch()
|
||||||
|
|
||||||
state.page is Archived && state.page.selected > 0 -> view.clearSelection()
|
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) }
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:viewportHeight="24.0">
|
||||||
|
<path
|
||||||
|
android:fillColor="#FF000000"
|
||||||
|
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:viewportHeight="24.0">
|
||||||
|
<path
|
||||||
|
android:fillColor="#FF000000"
|
||||||
|
android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24.0"
|
||||||
|
android:viewportHeight="24.0">
|
||||||
|
<path
|
||||||
|
android:fillColor="#FF000000"
|
||||||
|
android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
|
||||||
|
</vector>
|
||||||
@@ -224,17 +224,35 @@
|
|||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
tools:background="@color/toolbarLight">
|
tools:background="@color/toolbarLight">
|
||||||
|
|
||||||
<common.widget.QkTextView
|
<LinearLayout
|
||||||
android:id="@+id/toolbarTitle"
|
|
||||||
style="@style/ToolbarText" />
|
|
||||||
|
|
||||||
<android.support.v7.widget.RecyclerView
|
|
||||||
android:id="@+id/chips"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:layout_marginBottom="4dp"
|
android:gravity="center_vertical"
|
||||||
android:layout_marginTop="4dp"
|
android:orientation="vertical">
|
||||||
tools:visibility="gone" />
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/toolbarTitle"
|
||||||
|
style="@style/ToolbarText"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
tools:text="Moez Bhatti" />
|
||||||
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/toolbarSubtitle"
|
||||||
|
style="@style/ToolbarText"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:textColor="tertiary"
|
||||||
|
app:textSize="secondary"
|
||||||
|
tools:text="3 of 9 results" />
|
||||||
|
|
||||||
|
<android.support.v7.widget.RecyclerView
|
||||||
|
android:id="@+id/chips"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="4dp"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
tools:visibility="gone" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</android.support.v7.widget.Toolbar>
|
</android.support.v7.widget.Toolbar>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?><!--
|
||||||
|
~ 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/>.
|
||||||
|
-->
|
||||||
|
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@drawable/ripple"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<android.support.constraint.Group
|
||||||
|
android:id="@+id/resultsHeader"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:constraint_referenced_ids="resultsSeparator, resultsTitle" />
|
||||||
|
|
||||||
|
<common.widget.Separator
|
||||||
|
android:id="@+id/resultsSeparator"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="1dp"
|
||||||
|
android:layout_marginTop="12dp"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/resultsTitle"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:text="@string/main_message_results_title"
|
||||||
|
android:textStyle="bold"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/resultsSeparator"
|
||||||
|
app:textColor="secondary"
|
||||||
|
app:textSize="secondary" />
|
||||||
|
|
||||||
|
<common.widget.GroupAvatarView
|
||||||
|
android:id="@+id/avatars"
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:layout_gravity="top"
|
||||||
|
android:layout_marginBottom="12dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginTop="12dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/resultsTitle"
|
||||||
|
app:layout_constraintVertical_bias="0"
|
||||||
|
app:layout_constraintVertical_chainStyle="packed" />
|
||||||
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/title"
|
||||||
|
style="@style/TextPrimary"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
app:layout_constraintBottom_toTopOf="@id/snippet"
|
||||||
|
app:layout_constraintEnd_toStartOf="@id/date"
|
||||||
|
app:layout_constraintStart_toEndOf="@id/avatars"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/resultsTitle"
|
||||||
|
app:layout_constraintVertical_chainStyle="packed"
|
||||||
|
tools:text="@tools:sample/full_names" />
|
||||||
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/snippet"
|
||||||
|
style="@style/TextSecondary"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:layout_marginTop="2dp"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:maxLines="1"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="@id/title"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/title"
|
||||||
|
tools:text="@tools:sample/lorem/random" />
|
||||||
|
|
||||||
|
<common.widget.QkTextView
|
||||||
|
android:id="@+id/date"
|
||||||
|
style="@style/TextTertiary"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
app:layout_constraintBottom_toBottomOf="@id/title"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintHorizontal_bias="100"
|
||||||
|
tools:text="Oct 11" />
|
||||||
|
|
||||||
|
</android.support.constraint.ConstraintLayout>
|
||||||
@@ -54,5 +54,26 @@
|
|||||||
android:visible="false"
|
android:visible="false"
|
||||||
app:showAsAction="ifRoom" />
|
app:showAsAction="ifRoom" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/previous"
|
||||||
|
android:icon="@drawable/ic_keyboard_arrow_up_black_24dp"
|
||||||
|
android:title="@string/compose_menu_previous"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="always" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/next"
|
||||||
|
android:icon="@drawable/ic_keyboard_arrow_down_black_24dp"
|
||||||
|
android:title="@string/compose_menu_next"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="always" />
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/clear"
|
||||||
|
android:icon="@drawable/ic_clear_black_24dp"
|
||||||
|
android:title="@string/compose_menu_clear"
|
||||||
|
android:visible="false"
|
||||||
|
app:showAsAction="always" />
|
||||||
|
|
||||||
|
|
||||||
</menu>
|
</menu>
|
||||||
@@ -44,6 +44,8 @@
|
|||||||
<string name="main_menu_delete">Delete</string>
|
<string name="main_menu_delete">Delete</string>
|
||||||
<string name="main_syncing">Syncing messages…</string>
|
<string name="main_syncing">Syncing messages…</string>
|
||||||
<string name="main_sender_you">You: %s</string>
|
<string name="main_sender_you">You: %s</string>
|
||||||
|
<string name="main_message_results_title">Results in messages</string>
|
||||||
|
<string name="main_message_results">%d messages</string>
|
||||||
<string name="inbox_empty_text">Your conversations will appear here</string>
|
<string name="inbox_empty_text">Your conversations will appear here</string>
|
||||||
<string name="inbox_search_empty_text">No results</string>
|
<string name="inbox_search_empty_text">No results</string>
|
||||||
<string name="archived_empty_text">Your archived conversations will appear here</string>
|
<string name="archived_empty_text">Your archived conversations will appear here</string>
|
||||||
@@ -80,11 +82,15 @@
|
|||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string name="compose_title_selected">%d selected</string>
|
<string name="compose_title_selected">%d selected</string>
|
||||||
|
<string name="compose_subtitle_results">%1$d of %2$d results</string>
|
||||||
<string name="compose_messages_empty">This is the start of your conversation. Say something nice!</string>
|
<string name="compose_messages_empty">This is the start of your conversation. Say something nice!</string>
|
||||||
<string name="compose_hint">Write a message…</string>
|
<string name="compose_hint">Write a message…</string>
|
||||||
<string name="compose_menu_copy">Copy text</string>
|
<string name="compose_menu_copy">Copy text</string>
|
||||||
<string name="compose_menu_forward">Forward</string>
|
<string name="compose_menu_forward">Forward</string>
|
||||||
<string name="compose_menu_delete">Delete</string>
|
<string name="compose_menu_delete">Delete</string>
|
||||||
|
<string name="compose_menu_previous">Previous</string>
|
||||||
|
<string name="compose_menu_next">Next</string>
|
||||||
|
<string name="compose_menu_clear">Clear</string>
|
||||||
<string name="compose_attach_cd">Add an attachment</string>
|
<string name="compose_attach_cd">Add an attachment</string>
|
||||||
<string name="compose_gallery_cd">Attach a photo</string>
|
<string name="compose_gallery_cd">Attach a photo</string>
|
||||||
<string name="compose_camera_cd">Take a photo</string>
|
<string name="compose_camera_cd">Take a photo</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user