From 058d12d3bbe817bd5dee158b2a646d1c040d5b61 Mon Sep 17 00:00:00 2001 From: moezbhatti Date: Thu, 31 Oct 2019 02:06:53 -0400 Subject: [PATCH] Create chip model to keep original contact and know which number was selected --- .../QKSMS/feature/compose/ComposeActivity.kt | 10 +-- .../QKSMS/feature/compose/ComposeState.kt | 4 +- .../moez/QKSMS/feature/compose/ComposeView.kt | 4 +- .../QKSMS/feature/compose/ComposeViewModel.kt | 68 +++++++++---------- .../QKSMS/feature/compose/editing/Chip.kt | 26 +++++++ .../feature/compose/editing/ChipsAdapter.kt | 32 +++------ .../feature/compose/editing/ComposeItem.kt | 2 +- .../compose/editing/DetailedChipView.kt | 9 ++- 8 files changed, 82 insertions(+), 73 deletions(-) create mode 100644 presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/Chip.kt diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt index 841a9961..d8aa4bcd 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeActivity.kt @@ -54,11 +54,11 @@ import com.moez.QKSMS.common.util.extensions.setBackgroundTint import com.moez.QKSMS.common.util.extensions.setTint import com.moez.QKSMS.common.util.extensions.setVisible 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.ComposeItem import com.moez.QKSMS.feature.compose.editing.ComposeItemAdapter import com.moez.QKSMS.model.Attachment -import com.moez.QKSMS.model.Contact import com.uber.autodispose.android.lifecycle.scope import com.uber.autodispose.autoDisposable import dagger.android.AndroidInjection @@ -91,7 +91,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { override val queryBackspaceIntent: Observable<*> by lazy { search.backspaces } override val queryEditorActionIntent: Observable by lazy { search.editorActions() } override val chipSelectedIntent: Subject by lazy { contactsAdapter.itemSelected } - override val chipDeletedIntent: Subject by lazy { chipsAdapter.chipDeleted } + override val chipDeletedIntent: Subject by lazy { chipsAdapter.chipDeleted } override val menuReadyIntent: Observable = menu.map { Unit } override val optionsItemIntent: Subject = PublishSubject.create() override val sendAsGroupIntent by lazy { sendAsGroupBackground.clicks() } @@ -212,16 +212,16 @@ class ComposeActivity : QkThemedActivity(), ComposeView { 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.selectedChips.isNotEmpty()) { message.showKeyboard() } - chipsAdapter.data = state.selectedContacts + chipsAdapter.data = state.selectedChips contactsAdapter.data = state.composeItems loading.setVisible(state.loading) - sendAsGroup.setVisible(state.editingMode && state.selectedContacts.size >= 2) + sendAsGroup.setVisible(state.editingMode && state.selectedChips.size >= 2) sendAsGroupSwitch.isChecked = state.sendAsGroup messageList.setVisible(state.sendAsGroup) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeState.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeState.kt index 2502442d..b2f55b0e 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeState.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeState.kt @@ -19,9 +19,9 @@ package com.moez.QKSMS.feature.compose import com.moez.QKSMS.compat.SubscriptionInfoCompat +import com.moez.QKSMS.feature.compose.editing.Chip import com.moez.QKSMS.feature.compose.editing.ComposeItem import com.moez.QKSMS.model.Attachment -import com.moez.QKSMS.model.Contact import com.moez.QKSMS.model.Conversation import com.moez.QKSMS.model.Message import io.realm.RealmResults @@ -32,7 +32,7 @@ data class ComposeState( val searching: Boolean = false, val composeItems: List = ArrayList(), val selectedConversation: Long = 0, - val selectedContacts: List = ArrayList(), + val selectedChips: List = ArrayList(), val sendAsGroup: Boolean = true, val conversationtitle: String = "", val loading: Boolean = false, diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt index 7dd8cc5d..78f1cbe8 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeView.kt @@ -22,9 +22,9 @@ import android.net.Uri import androidx.annotation.StringRes import androidx.core.view.inputmethod.InputContentInfoCompat import com.moez.QKSMS.common.base.QkView +import com.moez.QKSMS.feature.compose.editing.Chip import com.moez.QKSMS.feature.compose.editing.ComposeItem import com.moez.QKSMS.model.Attachment -import com.moez.QKSMS.model.Contact import io.reactivex.Observable import io.reactivex.subjects.Subject @@ -35,7 +35,7 @@ interface ComposeView : QkView { val queryBackspaceIntent: Observable<*> val queryEditorActionIntent: Observable val chipSelectedIntent: Subject - val chipDeletedIntent: Subject + val chipDeletedIntent: Subject val menuReadyIntent: Observable val optionsItemIntent: Observable val sendAsGroupIntent: Observable<*> diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index f96f163f..d28fea7c 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -37,6 +37,7 @@ import com.moez.QKSMS.extensions.isImage import com.moez.QKSMS.extensions.isVideo import com.moez.QKSMS.extensions.mapNotNull 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.filter.ContactFilter @@ -118,11 +119,11 @@ class ComposeViewModel @Inject constructor( private val attachments: Subject> = BehaviorSubject.createDefault(sharedAttachments) private val contactGroups: Observable> by lazy { contactsRepo.getUnmanagedContactGroups() } private val contacts: Observable> by lazy { contactsRepo.getUnmanagedContacts() } - private val contactsReducer: Subject<(List) -> List> = PublishSubject.create() + private val chipsReducer: Subject<(List) -> List> = PublishSubject.create() private val conversation: Subject = BehaviorSubject.create() private val messages: Subject> = BehaviorSubject.create() private val recents: Observable> by lazy { conversationRepo.getUnmanagedConversations() } - private val selectedContacts: Subject> = BehaviorSubject.createDefault(listOf()) + private val selectedChips: Subject> = BehaviorSubject.createDefault(listOf()) private val searchResults: Subject> = BehaviorSubject.create() private val searchSelection: Subject = BehaviorSubject.createDefault(-1) private val starredContacts: Observable> by lazy { contactsRepo.getUnmanagedContacts(true) } @@ -133,9 +134,9 @@ class ComposeViewModel @Inject constructor( ?.asObservable() ?: Observable.empty() - val selectedConversation = selectedContacts + val selectedConversation = selectedChips .skipWhile { it.isEmpty() } - .map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } } + .map { chips -> chips.map { it.address } } .distinctUntilChanged() .doOnNext { newState { copy(loading = true) } } .observeOn(Schedulers.io()) @@ -177,15 +178,15 @@ class ComposeViewModel @Inject constructor( .subscribe(conversation::onNext) if (address.isNotBlank()) { - selectedContacts.onNext(listOf(Contact(numbers = RealmList(PhoneNumber(address))))) + selectedChips.onNext(listOf(Chip(address))) } - disposables += contactsReducer - .scan(listOf()) { previousState, reducer -> reducer(previousState) } - .doOnNext { contacts -> newState { copy(selectedContacts = contacts) } } + disposables += chipsReducer + .scan(listOf()) { previousState, reducer -> reducer(previousState) } + .doOnNext { chips -> newState { copy(selectedChips = chips) } } .skipUntil(state.filter { state -> state.editingMode }) .takeUntil(state.filter { state -> !state.editingMode }) - .subscribe(selectedContacts::onNext) + .subscribe(selectedChips::onNext) // When the conversation changes, mark read, and update the threadId and the messages for the adapter disposables += conversation @@ -256,8 +257,8 @@ class ComposeViewModel @Inject constructor( // that have already been selected Observables .combineLatest( - view.queryChangedIntent, recents, starredContacts, contactGroups, contacts, selectedContacts - ) { query, recents, starredContacts, contactGroups, contacts, selectedContacts -> + view.queryChangedIntent, recents, starredContacts, contactGroups, contacts, selectedChips + ) { query, recents, starredContacts, contactGroups, contacts, selectedChips -> val composeItems = mutableListOf() if (query.isBlank()) { composeItems += recents.map(::Recent) @@ -276,7 +277,7 @@ class ComposeViewModel @Inject constructor( // cache the result instead of doing it for each contact val normalizedQuery = query.removeAccents() composeItems += starredContacts - .filterNot { contact -> selectedContacts.contains(contact) } + .filterNot { contact -> selectedChips.map { it.contact }.contains(contact) } .filter { contact -> contactFilter.filter(contact, normalizedQuery) } .map(::Starred) @@ -285,7 +286,7 @@ class ComposeViewModel @Inject constructor( .map(::Group) composeItems += contacts - .filterNot { contact -> selectedContacts.contains(contact) } + .filterNot { contact -> selectedChips.map { it.contact }.contains(contact) } .filter { contact -> contactFilter.filter(contact, normalizedQuery) } .map(::Person) } @@ -301,47 +302,42 @@ class ComposeViewModel @Inject constructor( // Backspaces should delete the most recent contact if there's no text input // Close the activity if user presses back view.queryBackspaceIntent - .withLatestFrom(selectedContacts, view.queryChangedIntent) { event, contacts, query -> + .withLatestFrom(selectedChips, view.queryChangedIntent) { event, contacts, query -> if (contacts.isNotEmpty() && query.isEmpty()) { - contactsReducer.onNext { it.dropLast(1) } + chipsReducer.onNext { it.dropLast(1) } } } .autoDisposable(view.scope()) .subscribe() - // Enter the first contact suggestion if the enter button is pressed + // Enter the first contact suggestion if the enter button is pressed, and enter contacts that are selected view.queryEditorActionIntent .filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE } .withLatestFrom(state) { _, state -> state } + .mapNotNull { state -> state.composeItems.firstOrNull() } + .mergeWith(view.chipSelectedIntent) .autoDisposable(view.scope()) - .subscribe { state -> + .subscribe { composeItem -> newState { copy(searching = false) } - state.composeItems.firstOrNull()?.let { composeItem -> - contactsReducer.onNext { contacts -> contacts + composeItem.getContacts() } + chipsReducer.onNext { chips -> + chips + composeItem.getContacts().map { Chip(it.numbers.first()?.address.orEmpty(), it) } } } // Update the list of selected contacts when a new contact is selected or an existing one is deselected - Observable.merge( - view.chipDeletedIntent.doOnNext { contact -> - contactsReducer.onNext { contacts -> + view.chipDeletedIntent + .skipUntil(state.filter { state -> state.editingMode }) + .takeUntil(state.filter { state -> !state.editingMode }) + .autoDisposable(view.scope()) + .subscribe { contact -> + chipsReducer.onNext { contacts -> val result = contacts.filterNot { it == contact } if (result.isEmpty()) { newState { copy(searching = true) } } result } - }, - view.chipSelectedIntent.doOnNext { composeItem -> - newState { copy(searching = false) } - contactsReducer.onNext { contacts -> - contacts.toMutableList().apply { addAll(composeItem.getContacts()) } - } - }) - .skipUntil(state.filter { state -> state.editingMode }) - .takeUntil(state.filter { state -> !state.editingMode }) - .autoDisposable(view.scope()) - .subscribe() + } // When the menu is loaded, trigger a new state so that the menu options can be rendered correctly view.menuReadyIntent @@ -667,12 +663,12 @@ class ComposeViewModel @Inject constructor( .filter { permissionManager.hasSendSms().also { if (!it) view.requestSmsPermission() } } .withLatestFrom(view.textChangedIntent) { _, body -> body } .map { body -> body.toString() } - .withLatestFrom(state, attachments, conversation, selectedContacts) { body, state, attachments, - conversation, contacts -> + .withLatestFrom(state, attachments, conversation, selectedChips) { body, state, attachments, + conversation, chips -> val subId = state.subscription?.subscriptionId ?: -1 val addresses = when (conversation.recipients.isNotEmpty()) { true -> conversation.recipients.map { it.address } - false -> contacts.mapNotNull { it.numbers.firstOrNull()?.address } + false -> chips.map { chip -> chip.address } } val delay = when (prefs.sendDelay.get()) { Preferences.SEND_DELAY_SHORT -> 3000 diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/Chip.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/Chip.kt new file mode 100644 index 00000000..31846fbd --- /dev/null +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/Chip.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019 Moez Bhatti + * + * 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 . + */ +package com.moez.QKSMS.feature.compose.editing + +import com.moez.QKSMS.model.Contact + +data class Chip( + val address: String, + val contact: Contact? = null +) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt index ca5b5364..f0dc7935 100755 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ChipsAdapter.kt @@ -27,48 +27,40 @@ import com.moez.QKSMS.R import com.moez.QKSMS.common.base.QkAdapter import com.moez.QKSMS.common.base.QkViewHolder import com.moez.QKSMS.common.util.extensions.dpToPx -import com.moez.QKSMS.model.Contact import io.reactivex.subjects.PublishSubject import kotlinx.android.synthetic.main.contact_chip.view.* import javax.inject.Inject -class ChipsAdapter @Inject constructor() : QkAdapter() { +class ChipsAdapter @Inject constructor() : QkAdapter() { var view: RecyclerView? = null - val chipDeleted: PublishSubject = PublishSubject.create() + val chipDeleted: PublishSubject = PublishSubject.create() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { val inflater = LayoutInflater.from(parent.context) val view = inflater.inflate(R.layout.contact_chip, parent, false) return QkViewHolder(view).apply { view.setOnClickListener { - val contact = getItem(adapterPosition) - showDetailedChip(view.context, contact) + val chip = getItem(adapterPosition) + showDetailedChip(view.context, chip) } } } override fun onBindViewHolder(holder: QkViewHolder, position: Int) { - val contact = getItem(position) + val chip = getItem(position) val view = holder.containerView - view.avatar.setContact(contact) - - // If the contact's name is empty, try to display a phone number instead - // The contacts provided here should only have one number - view.name.text = if (contact.name.isNotBlank()) { - contact.name - } else { - contact.numbers.firstOrNull { it.address.isNotBlank() }?.address ?: "" - } + view.avatar.setContact(chip.contact, chip.address) + view.name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address } /** * The [context] has to come from a view, because we're inflating a view that used themed attrs */ - private fun showDetailedChip(context: Context, contact: Contact) { + private fun showDetailedChip(context: Context, chip: Chip) { val detailedChipView = DetailedChipView(context) - detailedChipView.setContact(contact) + detailedChipView.setChip(chip) val rootView = view?.rootView as ViewGroup @@ -83,12 +75,8 @@ class ChipsAdapter @Inject constructor() : QkAdapter() { detailedChipView.show() detailedChipView.setOnDeleteListener { - chipDeleted.onNext(contact) + chipDeleted.onNext(chip) detailedChipView.hide() } } - - override fun areItemsTheSame(old: Contact, new: Contact): Boolean { - return old.lookupKey == new.lookupKey - } } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ComposeItem.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ComposeItem.kt index 894daa1e..421418bf 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ComposeItem.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/ComposeItem.kt @@ -34,7 +34,7 @@ sealed class ComposeItem { data class Recent(val value: Conversation) : ComposeItem() { override fun getContacts(): List = value.recipients.map { recipient -> - Contact(numbers = RealmList(PhoneNumber(address = recipient.address))) + recipient.contact ?: Contact(numbers = RealmList(PhoneNumber(address = recipient.address))) } } diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt index 731d9c9d..262d0ab6 100755 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/editing/DetailedChipView.kt @@ -27,7 +27,6 @@ import com.moez.QKSMS.common.util.Colors import com.moez.QKSMS.common.util.extensions.setBackgroundTint import com.moez.QKSMS.common.util.extensions.setTint import com.moez.QKSMS.injection.appComponent -import com.moez.QKSMS.model.Contact import kotlinx.android.synthetic.main.contact_chip_detailed.view.* import javax.inject.Inject @@ -54,10 +53,10 @@ class DetailedChipView(context: Context) : RelativeLayout(context) { } } - fun setContact(contact: Contact) { - avatar.setContact(contact) - name.text = contact.name - info.text = contact.numbers.joinToString(", ") { it.address } + fun setChip(chip: Chip) { + avatar.setContact(chip.contact, chip.address) + name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address + info.text = chip.address } fun show() {