Use Recipient instead of Chip

This commit is contained in:
Moez Bhatti
2020-01-12 02:02:39 -05:00
parent 7d9cd35c7c
commit abd198db81
11 changed files with 55 additions and 71 deletions
@@ -37,11 +37,10 @@ class CursorToRecipientImpl @Inject constructor(
const val COLUMN_ADDRESS = 1 const val COLUMN_ADDRESS = 1
} }
override fun map(from: Cursor) = Recipient().apply { override fun map(from: Cursor) = Recipient(
id = from.getLong(COLUMN_ID) id = from.getLong(COLUMN_ID),
address = from.getString(COLUMN_ADDRESS) address = from.getString(COLUMN_ADDRESS),
lastUpdate = System.currentTimeMillis() lastUpdate = System.currentTimeMillis())
}
override fun getRecipientCursor(): Cursor? { override fun getRecipientCursor(): Cursor? {
return when (permissionManager.hasReadSms()) { return when (permissionManager.hasReadSms()) {
@@ -220,6 +220,12 @@ class ConversationRepositoryImpl @Inject constructor(
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
} }
override fun getRecipients(): RealmResults<Recipient> {
val realm = Realm.getDefaultInstance()
return realm.where(Recipient::class.java)
.findAll()
}
override fun getUnmanagedRecipients(): Observable<List<Recipient>> { override fun getUnmanagedRecipients(): Observable<List<Recipient>> {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
return realm.where(Recipient::class.java) return realm.where(Recipient::class.java)
@@ -54,6 +54,8 @@ interface ConversationRepository {
fun getUnmanagedConversations(): Observable<List<Conversation>> fun getUnmanagedConversations(): Observable<List<Conversation>>
fun getRecipients(): RealmResults<Recipient>
fun getUnmanagedRecipients(): Observable<List<Recipient>> fun getUnmanagedRecipients(): Observable<List<Recipient>>
fun getRecipient(recipientId: Long): Recipient? fun getRecipient(recipientId: Long): Recipient?
@@ -54,10 +54,10 @@ import com.moez.QKSMS.common.util.extensions.setBackgroundTint
import com.moez.QKSMS.common.util.extensions.setTint import com.moez.QKSMS.common.util.extensions.setTint
import com.moez.QKSMS.common.util.extensions.setVisible import com.moez.QKSMS.common.util.extensions.setVisible
import com.moez.QKSMS.common.util.extensions.showKeyboard import com.moez.QKSMS.common.util.extensions.showKeyboard
import com.moez.QKSMS.feature.compose.editing.Chip
import com.moez.QKSMS.feature.compose.editing.ChipsAdapter import com.moez.QKSMS.feature.compose.editing.ChipsAdapter
import com.moez.QKSMS.feature.contacts.ContactsActivity import com.moez.QKSMS.feature.contacts.ContactsActivity
import com.moez.QKSMS.model.Attachment import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Recipient
import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable import com.uber.autodispose.autoDisposable
import dagger.android.AndroidInjection import dagger.android.AndroidInjection
@@ -90,7 +90,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
override val activityVisibleIntent: Subject<Boolean> = PublishSubject.create() override val activityVisibleIntent: Subject<Boolean> = PublishSubject.create()
override val chipsSelectedIntent: Subject<HashMap<String, String?>> = PublishSubject.create() override val chipsSelectedIntent: Subject<HashMap<String, String?>> = PublishSubject.create()
override val chipDeletedIntent: Subject<Chip> by lazy { chipsAdapter.chipDeleted } override val chipDeletedIntent: Subject<Recipient> by lazy { chipsAdapter.chipDeleted }
override val menuReadyIntent: Observable<Unit> = menu.map { Unit } override val menuReadyIntent: Observable<Unit> = menu.map { Unit }
override val optionsItemIntent: Subject<Int> = PublishSubject.create() override val optionsItemIntent: Subject<Int> = PublishSubject.create()
override val sendAsGroupIntent by lazy { sendAsGroupBackground.clicks() } override val sendAsGroupIntent by lazy { sendAsGroupBackground.clicks() }
@@ -286,7 +286,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
startActivityForResult(Intent.createChooser(intent, null), AttachContactRequestCode) startActivityForResult(Intent.createChooser(intent, null), AttachContactRequestCode)
} }
override fun showContacts(sharing: Boolean, chips: List<Chip>) { override fun showContacts(sharing: Boolean, chips: List<Recipient>) {
message.hideKeyboard() message.hideKeyboard()
val serialized = HashMap(chips.associate { chip -> chip.address to chip.contact?.lookupKey }) val serialized = HashMap(chips.associate { chip -> chip.address to chip.contact?.lookupKey })
val intent = Intent(this, ContactsActivity::class.java) val intent = Intent(this, ContactsActivity::class.java)
@@ -19,17 +19,17 @@
package com.moez.QKSMS.feature.compose package com.moez.QKSMS.feature.compose
import com.moez.QKSMS.compat.SubscriptionInfoCompat import com.moez.QKSMS.compat.SubscriptionInfoCompat
import com.moez.QKSMS.feature.compose.editing.Chip
import com.moez.QKSMS.model.Attachment import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.Recipient
import io.realm.RealmResults import io.realm.RealmResults
data class ComposeState( data class ComposeState(
val hasError: Boolean = false, val hasError: Boolean = false,
val editingMode: Boolean = false, val editingMode: Boolean = false,
val threadId: Long = 0, val threadId: Long = 0,
val selectedChips: List<Chip> = ArrayList(), val selectedChips: List<Recipient> = ArrayList(),
val sendAsGroup: Boolean = true, val sendAsGroup: Boolean = true,
val conversationtitle: String = "", val conversationtitle: String = "",
val loading: Boolean = false, val loading: Boolean = false,
@@ -22,8 +22,8 @@ import android.net.Uri
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.view.inputmethod.InputContentInfoCompat import androidx.core.view.inputmethod.InputContentInfoCompat
import com.moez.QKSMS.common.base.QkView import com.moez.QKSMS.common.base.QkView
import com.moez.QKSMS.feature.compose.editing.Chip
import com.moez.QKSMS.model.Attachment import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Recipient
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.subjects.Subject import io.reactivex.subjects.Subject
@@ -31,7 +31,7 @@ interface ComposeView : QkView<ComposeState> {
val activityVisibleIntent: Observable<Boolean> val activityVisibleIntent: Observable<Boolean>
val chipsSelectedIntent: Subject<HashMap<String, String?>> val chipsSelectedIntent: Subject<HashMap<String, String?>>
val chipDeletedIntent: Subject<Chip> val chipDeletedIntent: Subject<Recipient>
val menuReadyIntent: Observable<Unit> val menuReadyIntent: Observable<Unit>
val optionsItemIntent: Observable<Int> val optionsItemIntent: Observable<Int>
val sendAsGroupIntent: Observable<*> val sendAsGroupIntent: Observable<*>
@@ -61,7 +61,7 @@ interface ComposeView : QkView<ComposeState> {
fun requestDefaultSms() fun requestDefaultSms()
fun requestStoragePermission() fun requestStoragePermission()
fun requestSmsPermission() fun requestSmsPermission()
fun showContacts(sharing: Boolean, chips: List<Chip>) fun showContacts(sharing: Boolean, chips: List<Recipient>)
fun themeChanged() fun themeChanged()
fun showKeyboard() fun showKeyboard()
fun requestCamera() fun requestCamera()
@@ -36,7 +36,6 @@ import com.moez.QKSMS.extensions.asObservable
import com.moez.QKSMS.extensions.isImage import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isVideo import com.moez.QKSMS.extensions.isVideo
import com.moez.QKSMS.extensions.mapNotNull import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.feature.compose.editing.Chip
import com.moez.QKSMS.interactor.AddScheduledMessage import com.moez.QKSMS.interactor.AddScheduledMessage
import com.moez.QKSMS.interactor.CancelDelayedMessage import com.moez.QKSMS.interactor.CancelDelayedMessage
import com.moez.QKSMS.interactor.DeleteMessages import com.moez.QKSMS.interactor.DeleteMessages
@@ -49,6 +48,7 @@ import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments import com.moez.QKSMS.model.Attachments
import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message import com.moez.QKSMS.model.Message
import com.moez.QKSMS.model.Recipient
import com.moez.QKSMS.repository.ContactRepository import com.moez.QKSMS.repository.ContactRepository
import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository import com.moez.QKSMS.repository.MessageRepository
@@ -104,10 +104,10 @@ class ComposeViewModel @Inject constructor(
) { ) {
private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(sharedAttachments) private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(sharedAttachments)
private val chipsReducer: Subject<(List<Chip>) -> List<Chip>> = PublishSubject.create() private val chipsReducer: Subject<(List<Recipient>) -> List<Recipient>> = PublishSubject.create()
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()
private val selectedChips: Subject<List<Chip>> = BehaviorSubject.createDefault(listOf()) private val selectedChips: Subject<List<Recipient>> = BehaviorSubject.createDefault(listOf())
private val searchResults: Subject<List<Message>> = BehaviorSubject.create() private val searchResults: Subject<List<Message>> = BehaviorSubject.create()
private val searchSelection: Subject<Long> = BehaviorSubject.createDefault(-1) private val searchSelection: Subject<Long> = BehaviorSubject.createDefault(-1)
@@ -163,11 +163,11 @@ class ComposeViewModel @Inject constructor(
.subscribe(conversation::onNext) .subscribe(conversation::onNext)
if (addresses.isNotEmpty()) { if (addresses.isNotEmpty()) {
selectedChips.onNext(addresses.map { address -> Chip(address) }) selectedChips.onNext(addresses.map { address -> Recipient(address = address) })
} }
disposables += chipsReducer disposables += chipsReducer
.scan(listOf<Chip>()) { previousState, reducer -> reducer(previousState) } .scan(listOf<Recipient>()) { previousState, reducer -> reducer(previousState) }
.doOnNext { chips -> newState { copy(selectedChips = chips) } } .doOnNext { chips -> newState { copy(selectedChips = chips) } }
.skipUntil(state.filter { state -> state.editingMode }) .skipUntil(state.filter { state -> state.editingMode })
.takeUntil(state.filter { state -> !state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode })
@@ -241,13 +241,19 @@ class ComposeViewModel @Inject constructor(
} }
// Filter out any numbers that are already selected // Filter out any numbers that are already selected
hashmap.filter { (address) -> hashmap.filter { (address) ->
chips.none { chip -> phoneNumberUtils.compare(address, chip.address) } chips.none { recipient -> phoneNumberUtils.compare(address, recipient.address) }
} }
} }
.filter { hashmap -> hashmap.isNotEmpty() } .filter { hashmap -> hashmap.isNotEmpty() }
.map { hashmap -> .map { hashmap ->
hashmap.map { (address, lookupKey) -> hashmap.map { (address, lookupKey) ->
Chip(address, lookupKey?.let(contactRepo::getUnmanagedContact)) conversationRepo.getRecipients()
.asSequence()
.filter { recipient -> recipient.contact?.lookupKey == lookupKey }
.first { recipient -> phoneNumberUtils.compare(recipient.address, address) }
?: Recipient(
address = address,
contact = lookupKey?.let(contactRepo::getUnmanagedContact))
} }
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -1,26 +0,0 @@
/*
* Copyright (C) 2019 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 com.moez.QKSMS.feature.compose.editing
import com.moez.QKSMS.model.Contact
data class Chip(
val address: String,
val contact: Contact? = null
)
@@ -35,10 +35,10 @@ import io.reactivex.subjects.PublishSubject
import kotlinx.android.synthetic.main.contact_chip.* import kotlinx.android.synthetic.main.contact_chip.*
import javax.inject.Inject import javax.inject.Inject
class ChipsAdapter @Inject constructor() : QkAdapter<Chip>() { class ChipsAdapter @Inject constructor() : QkAdapter<Recipient>() {
var view: RecyclerView? = null var view: RecyclerView? = null
val chipDeleted: PublishSubject<Chip> = PublishSubject.create<Chip>() val chipDeleted: PublishSubject<Recipient> = PublishSubject.create<Recipient>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val inflater = LayoutInflater.from(parent.context) val inflater = LayoutInflater.from(parent.context)
@@ -57,18 +57,18 @@ class ChipsAdapter @Inject constructor() : QkAdapter<Chip>() {
} }
override fun onBindViewHolder(holder: QkViewHolder, position: Int) { override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val chip = getItem(position) val recipient = getItem(position)
holder.avatar.setRecipient(Recipient(id = -1, contact = chip.contact)) holder.avatar.setRecipient(recipient)
holder.name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address holder.name.text = recipient.contact?.name?.takeIf { it.isNotBlank() } ?: recipient.address
} }
/** /**
* The [context] has to come from a view, because we're inflating a view that used themed attrs * The [context] has to come from a view, because we're inflating a view that used themed attrs
*/ */
private fun showDetailedChip(context: Context, chip: Chip) { private fun showDetailedChip(context: Context, recipient: Recipient) {
val detailedChipView = DetailedChipView(context) val detailedChipView = DetailedChipView(context)
detailedChipView.setChip(chip) detailedChipView.setRecipient(recipient)
val rootView = view?.rootView as ViewGroup val rootView = view?.rootView as ViewGroup
@@ -83,7 +83,7 @@ class ChipsAdapter @Inject constructor() : QkAdapter<Chip>() {
detailedChipView.show() detailedChipView.show()
detailedChipView.setOnDeleteListener { detailedChipView.setOnDeleteListener {
chipDeleted.onNext(chip) chipDeleted.onNext(recipient)
detailedChipView.hide() detailedChipView.hide()
} }
} }
@@ -45,8 +45,14 @@ class DetailedChipView(context: Context) : RelativeLayout(context) {
isFocusable = true isFocusable = true
isFocusableInTouchMode = true isFocusableInTouchMode = true
}
colors.theme().let { theme -> fun setRecipient(recipient: Recipient) {
avatar.setRecipient(recipient)
name.text = recipient.contact?.name?.takeIf { it.isNotBlank() } ?: recipient.address
info.text = recipient.address
colors.theme(recipient).let { theme ->
card.setBackgroundTint(theme.theme) card.setBackgroundTint(theme.theme)
name.setTextColor(theme.textPrimary) name.setTextColor(theme.textPrimary)
info.setTextColor(theme.textTertiary) info.setTextColor(theme.textTertiary)
@@ -54,12 +60,6 @@ class DetailedChipView(context: Context) : RelativeLayout(context) {
} }
} }
fun setChip(chip: Chip) {
avatar.setRecipient(Recipient(id = -1, contact = chip.contact))
name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address
info.text = chip.address
}
fun show() { fun show() {
startAnimation(AlphaAnimation(0f, 1f).apply { duration = 200 }) startAnimation(AlphaAnimation(0f, 1f).apply { duration = 200 })
@@ -22,7 +22,6 @@ import android.view.inputmethod.EditorInfo
import com.moez.QKSMS.common.base.QkViewModel import com.moez.QKSMS.common.base.QkViewModel
import com.moez.QKSMS.extensions.mapNotNull import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.extensions.removeAccents import com.moez.QKSMS.extensions.removeAccents
import com.moez.QKSMS.feature.compose.editing.Chip
import com.moez.QKSMS.feature.compose.editing.ComposeItem import com.moez.QKSMS.feature.compose.editing.ComposeItem
import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction import com.moez.QKSMS.feature.compose.editing.PhoneNumberAction
import com.moez.QKSMS.filter.ContactFilter import com.moez.QKSMS.filter.ContactFilter
@@ -32,6 +31,7 @@ import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.ContactGroup import com.moez.QKSMS.model.ContactGroup
import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.PhoneNumber import com.moez.QKSMS.model.PhoneNumber
import com.moez.QKSMS.model.Recipient
import com.moez.QKSMS.repository.ContactRepository import com.moez.QKSMS.repository.ContactRepository
import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.PhoneNumberUtils import com.moez.QKSMS.util.PhoneNumberUtils
@@ -68,7 +68,7 @@ class ContactsViewModel @Inject constructor(
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.map { hashmap -> .map { hashmap ->
hashmap.map { (address, lookupKey) -> hashmap.map { (address, lookupKey) ->
Chip(address, lookupKey?.let(contactsRepo::getUnmanagedContact)) Recipient(address = address, contact = lookupKey?.let(contactsRepo::getUnmanagedContact))
} }
} }
@@ -181,11 +181,10 @@ class ContactsViewModel @Inject constructor(
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { (composeItem, force) -> .subscribe { (composeItem, force) ->
val contacts = composeItem.getContacts() view.finish(HashMap(composeItem.getContacts().associate { contact ->
val newChips = contacts.map { contact ->
if (contact.numbers.size == 1 || contact.getDefaultNumber() != null && !force) { if (contact.numbers.size == 1 || contact.getDefaultNumber() != null && !force) {
val number = contact.getDefaultNumber() ?: contact.numbers[0]!! val address = contact.getDefaultNumber()?.address ?: contact.numbers[0]!!.address
Chip(number.address, contact) address to contact.lookupKey
} else { } else {
runBlocking { runBlocking {
newState { copy(selectedContact = contact) } newState { copy(selectedContact = contact) }
@@ -203,12 +202,10 @@ class ContactsViewModel @Inject constructor(
setDefaultPhoneNumber.execute(params) setDefaultPhoneNumber.execute(params)
} }
Chip(number.address, contact) number.address to contact.lookupKey
} ?: return@subscribe } ?: return@subscribe
} }
} }))
view.finish(HashMap(newChips.associate { chip -> chip.address to chip.contact?.lookupKey }))
} }
} }