forked from upstream-mirrors/quik
Create chip model to keep original contact and know which number was selected
This commit is contained in:
@@ -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.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.compose.editing.ComposeItem
|
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||||
import com.moez.QKSMS.feature.compose.editing.ComposeItemAdapter
|
import com.moez.QKSMS.feature.compose.editing.ComposeItemAdapter
|
||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
import com.moez.QKSMS.model.Contact
|
|
||||||
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
|
||||||
@@ -91,7 +91,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView {
|
|||||||
override val queryBackspaceIntent: Observable<*> by lazy { search.backspaces }
|
override val queryBackspaceIntent: Observable<*> by lazy { search.backspaces }
|
||||||
override val queryEditorActionIntent: Observable<Int> by lazy { search.editorActions() }
|
override val queryEditorActionIntent: Observable<Int> by lazy { search.editorActions() }
|
||||||
override val chipSelectedIntent: Subject<ComposeItem> by lazy { contactsAdapter.itemSelected }
|
override val chipSelectedIntent: Subject<ComposeItem> by lazy { contactsAdapter.itemSelected }
|
||||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
override val chipDeletedIntent: Subject<Chip> 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() }
|
||||||
@@ -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.next)?.isVisible = state.selectedMessages == 0 && state.query.isNotEmpty()
|
||||||
toolbar.menu.findItem(R.id.clear)?.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()
|
message.showKeyboard()
|
||||||
}
|
}
|
||||||
|
|
||||||
chipsAdapter.data = state.selectedContacts
|
chipsAdapter.data = state.selectedChips
|
||||||
contactsAdapter.data = state.composeItems
|
contactsAdapter.data = state.composeItems
|
||||||
|
|
||||||
loading.setVisible(state.loading)
|
loading.setVisible(state.loading)
|
||||||
|
|
||||||
sendAsGroup.setVisible(state.editingMode && state.selectedContacts.size >= 2)
|
sendAsGroup.setVisible(state.editingMode && state.selectedChips.size >= 2)
|
||||||
sendAsGroupSwitch.isChecked = state.sendAsGroup
|
sendAsGroupSwitch.isChecked = state.sendAsGroup
|
||||||
|
|
||||||
messageList.setVisible(state.sendAsGroup)
|
messageList.setVisible(state.sendAsGroup)
|
||||||
|
|||||||
@@ -19,9 +19,9 @@
|
|||||||
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.feature.compose.editing.ComposeItem
|
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
import com.moez.QKSMS.model.Contact
|
|
||||||
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 io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
@@ -32,7 +32,7 @@ data class ComposeState(
|
|||||||
val searching: Boolean = false,
|
val searching: Boolean = false,
|
||||||
val composeItems: List<ComposeItem> = ArrayList(),
|
val composeItems: List<ComposeItem> = ArrayList(),
|
||||||
val selectedConversation: Long = 0,
|
val selectedConversation: Long = 0,
|
||||||
val selectedContacts: List<Contact> = ArrayList(),
|
val selectedChips: List<Chip> = ArrayList(),
|
||||||
val sendAsGroup: Boolean = true,
|
val sendAsGroup: Boolean = true,
|
||||||
val conversationtitle: String = "",
|
val conversationtitle: String = "",
|
||||||
val loading: Boolean = false,
|
val loading: Boolean = false,
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ 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.feature.compose.editing.ComposeItem
|
import com.moez.QKSMS.feature.compose.editing.ComposeItem
|
||||||
import com.moez.QKSMS.model.Attachment
|
import com.moez.QKSMS.model.Attachment
|
||||||
import com.moez.QKSMS.model.Contact
|
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
val queryBackspaceIntent: Observable<*>
|
val queryBackspaceIntent: Observable<*>
|
||||||
val queryEditorActionIntent: Observable<Int>
|
val queryEditorActionIntent: Observable<Int>
|
||||||
val chipSelectedIntent: Subject<ComposeItem>
|
val chipSelectedIntent: Subject<ComposeItem>
|
||||||
val chipDeletedIntent: Subject<Contact>
|
val chipDeletedIntent: Subject<Chip>
|
||||||
val menuReadyIntent: Observable<Unit>
|
val menuReadyIntent: Observable<Unit>
|
||||||
val optionsItemIntent: Observable<Int>
|
val optionsItemIntent: Observable<Int>
|
||||||
val sendAsGroupIntent: Observable<*>
|
val sendAsGroupIntent: Observable<*>
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ 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.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.ComposeItem.*
|
import com.moez.QKSMS.feature.compose.editing.ComposeItem.*
|
||||||
import com.moez.QKSMS.filter.ContactFilter
|
import com.moez.QKSMS.filter.ContactFilter
|
||||||
@@ -118,11 +119,11 @@ class ComposeViewModel @Inject constructor(
|
|||||||
private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(sharedAttachments)
|
private val attachments: Subject<List<Attachment>> = BehaviorSubject.createDefault(sharedAttachments)
|
||||||
private val contactGroups: Observable<List<ContactGroup>> by lazy { contactsRepo.getUnmanagedContactGroups() }
|
private val contactGroups: Observable<List<ContactGroup>> by lazy { contactsRepo.getUnmanagedContactGroups() }
|
||||||
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts() }
|
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts() }
|
||||||
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
|
private val chipsReducer: Subject<(List<Chip>) -> List<Chip>> = 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 recents: Observable<List<Conversation>> by lazy { conversationRepo.getUnmanagedConversations() }
|
private val recents: Observable<List<Conversation>> by lazy { conversationRepo.getUnmanagedConversations() }
|
||||||
private val selectedContacts: Subject<List<Contact>> = BehaviorSubject.createDefault(listOf())
|
private val selectedChips: Subject<List<Chip>> = 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)
|
||||||
private val starredContacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts(true) }
|
private val starredContacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts(true) }
|
||||||
@@ -133,9 +134,9 @@ class ComposeViewModel @Inject constructor(
|
|||||||
?.asObservable()
|
?.asObservable()
|
||||||
?: Observable.empty()
|
?: Observable.empty()
|
||||||
|
|
||||||
val selectedConversation = selectedContacts
|
val selectedConversation = selectedChips
|
||||||
.skipWhile { it.isEmpty() }
|
.skipWhile { it.isEmpty() }
|
||||||
.map { contacts -> contacts.map { it.numbers.firstOrNull()?.address ?: "" } }
|
.map { chips -> chips.map { it.address } }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.doOnNext { newState { copy(loading = true) } }
|
.doOnNext { newState { copy(loading = true) } }
|
||||||
.observeOn(Schedulers.io())
|
.observeOn(Schedulers.io())
|
||||||
@@ -177,15 +178,15 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.subscribe(conversation::onNext)
|
.subscribe(conversation::onNext)
|
||||||
|
|
||||||
if (address.isNotBlank()) {
|
if (address.isNotBlank()) {
|
||||||
selectedContacts.onNext(listOf(Contact(numbers = RealmList(PhoneNumber(address)))))
|
selectedChips.onNext(listOf(Chip(address)))
|
||||||
}
|
}
|
||||||
|
|
||||||
disposables += contactsReducer
|
disposables += chipsReducer
|
||||||
.scan(listOf<Contact>()) { previousState, reducer -> reducer(previousState) }
|
.scan(listOf<Chip>()) { previousState, reducer -> reducer(previousState) }
|
||||||
.doOnNext { contacts -> newState { copy(selectedContacts = contacts) } }
|
.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 })
|
||||||
.subscribe(selectedContacts::onNext)
|
.subscribe(selectedChips::onNext)
|
||||||
|
|
||||||
// When the conversation changes, mark read, and update the threadId and the messages for the adapter
|
// When the conversation changes, mark read, and update the threadId and the messages for the adapter
|
||||||
disposables += conversation
|
disposables += conversation
|
||||||
@@ -256,8 +257,8 @@ class ComposeViewModel @Inject constructor(
|
|||||||
// that have already been selected
|
// that have already been selected
|
||||||
Observables
|
Observables
|
||||||
.combineLatest(
|
.combineLatest(
|
||||||
view.queryChangedIntent, recents, starredContacts, contactGroups, contacts, selectedContacts
|
view.queryChangedIntent, recents, starredContacts, contactGroups, contacts, selectedChips
|
||||||
) { query, recents, starredContacts, contactGroups, contacts, selectedContacts ->
|
) { query, recents, starredContacts, contactGroups, contacts, selectedChips ->
|
||||||
val composeItems = mutableListOf<ComposeItem>()
|
val composeItems = mutableListOf<ComposeItem>()
|
||||||
if (query.isBlank()) {
|
if (query.isBlank()) {
|
||||||
composeItems += recents.map(::Recent)
|
composeItems += recents.map(::Recent)
|
||||||
@@ -276,7 +277,7 @@ class ComposeViewModel @Inject constructor(
|
|||||||
// cache the result instead of doing it for each contact
|
// cache the result instead of doing it for each contact
|
||||||
val normalizedQuery = query.removeAccents()
|
val normalizedQuery = query.removeAccents()
|
||||||
composeItems += starredContacts
|
composeItems += starredContacts
|
||||||
.filterNot { contact -> selectedContacts.contains(contact) }
|
.filterNot { contact -> selectedChips.map { it.contact }.contains(contact) }
|
||||||
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
||||||
.map(::Starred)
|
.map(::Starred)
|
||||||
|
|
||||||
@@ -285,7 +286,7 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.map(::Group)
|
.map(::Group)
|
||||||
|
|
||||||
composeItems += contacts
|
composeItems += contacts
|
||||||
.filterNot { contact -> selectedContacts.contains(contact) }
|
.filterNot { contact -> selectedChips.map { it.contact }.contains(contact) }
|
||||||
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
.filter { contact -> contactFilter.filter(contact, normalizedQuery) }
|
||||||
.map(::Person)
|
.map(::Person)
|
||||||
}
|
}
|
||||||
@@ -301,47 +302,42 @@ class ComposeViewModel @Inject constructor(
|
|||||||
// Backspaces should delete the most recent contact if there's no text input
|
// Backspaces should delete the most recent contact if there's no text input
|
||||||
// Close the activity if user presses back
|
// Close the activity if user presses back
|
||||||
view.queryBackspaceIntent
|
view.queryBackspaceIntent
|
||||||
.withLatestFrom(selectedContacts, view.queryChangedIntent) { event, contacts, query ->
|
.withLatestFrom(selectedChips, view.queryChangedIntent) { event, contacts, query ->
|
||||||
if (contacts.isNotEmpty() && query.isEmpty()) {
|
if (contacts.isNotEmpty() && query.isEmpty()) {
|
||||||
contactsReducer.onNext { it.dropLast(1) }
|
chipsReducer.onNext { it.dropLast(1) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.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
|
view.queryEditorActionIntent
|
||||||
.filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE }
|
.filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE }
|
||||||
.withLatestFrom(state) { _, state -> state }
|
.withLatestFrom(state) { _, state -> state }
|
||||||
|
.mapNotNull { state -> state.composeItems.firstOrNull() }
|
||||||
|
.mergeWith(view.chipSelectedIntent)
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe { state ->
|
.subscribe { composeItem ->
|
||||||
newState { copy(searching = false) }
|
newState { copy(searching = false) }
|
||||||
state.composeItems.firstOrNull()?.let { composeItem ->
|
chipsReducer.onNext { chips ->
|
||||||
contactsReducer.onNext { contacts -> contacts + composeItem.getContacts() }
|
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
|
// Update the list of selected contacts when a new contact is selected or an existing one is deselected
|
||||||
Observable.merge(
|
view.chipDeletedIntent
|
||||||
view.chipDeletedIntent.doOnNext { contact ->
|
.skipUntil(state.filter { state -> state.editingMode })
|
||||||
contactsReducer.onNext { contacts ->
|
.takeUntil(state.filter { state -> !state.editingMode })
|
||||||
|
.autoDisposable(view.scope())
|
||||||
|
.subscribe { contact ->
|
||||||
|
chipsReducer.onNext { contacts ->
|
||||||
val result = contacts.filterNot { it == contact }
|
val result = contacts.filterNot { it == contact }
|
||||||
if (result.isEmpty()) {
|
if (result.isEmpty()) {
|
||||||
newState { copy(searching = true) }
|
newState { copy(searching = true) }
|
||||||
}
|
}
|
||||||
result
|
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
|
// When the menu is loaded, trigger a new state so that the menu options can be rendered correctly
|
||||||
view.menuReadyIntent
|
view.menuReadyIntent
|
||||||
@@ -667,12 +663,12 @@ class ComposeViewModel @Inject constructor(
|
|||||||
.filter { permissionManager.hasSendSms().also { if (!it) view.requestSmsPermission() } }
|
.filter { permissionManager.hasSendSms().also { if (!it) view.requestSmsPermission() } }
|
||||||
.withLatestFrom(view.textChangedIntent) { _, body -> body }
|
.withLatestFrom(view.textChangedIntent) { _, body -> body }
|
||||||
.map { body -> body.toString() }
|
.map { body -> body.toString() }
|
||||||
.withLatestFrom(state, attachments, conversation, selectedContacts) { body, state, attachments,
|
.withLatestFrom(state, attachments, conversation, selectedChips) { body, state, attachments,
|
||||||
conversation, contacts ->
|
conversation, chips ->
|
||||||
val subId = state.subscription?.subscriptionId ?: -1
|
val subId = state.subscription?.subscriptionId ?: -1
|
||||||
val addresses = when (conversation.recipients.isNotEmpty()) {
|
val addresses = when (conversation.recipients.isNotEmpty()) {
|
||||||
true -> conversation.recipients.map { it.address }
|
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()) {
|
val delay = when (prefs.sendDelay.get()) {
|
||||||
Preferences.SEND_DELAY_SHORT -> 3000
|
Preferences.SEND_DELAY_SHORT -> 3000
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
)
|
||||||
@@ -27,48 +27,40 @@ import com.moez.QKSMS.R
|
|||||||
import com.moez.QKSMS.common.base.QkAdapter
|
import com.moez.QKSMS.common.base.QkAdapter
|
||||||
import com.moez.QKSMS.common.base.QkViewHolder
|
import com.moez.QKSMS.common.base.QkViewHolder
|
||||||
import com.moez.QKSMS.common.util.extensions.dpToPx
|
import com.moez.QKSMS.common.util.extensions.dpToPx
|
||||||
import com.moez.QKSMS.model.Contact
|
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import kotlinx.android.synthetic.main.contact_chip.view.*
|
import kotlinx.android.synthetic.main.contact_chip.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ChipsAdapter @Inject constructor() : QkAdapter<Contact>() {
|
class ChipsAdapter @Inject constructor() : QkAdapter<Chip>() {
|
||||||
|
|
||||||
var view: RecyclerView? = null
|
var view: RecyclerView? = null
|
||||||
val chipDeleted: PublishSubject<Contact> = PublishSubject.create<Contact>()
|
val chipDeleted: PublishSubject<Chip> = PublishSubject.create<Chip>()
|
||||||
|
|
||||||
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)
|
||||||
val view = inflater.inflate(R.layout.contact_chip, parent, false)
|
val view = inflater.inflate(R.layout.contact_chip, parent, false)
|
||||||
return QkViewHolder(view).apply {
|
return QkViewHolder(view).apply {
|
||||||
view.setOnClickListener {
|
view.setOnClickListener {
|
||||||
val contact = getItem(adapterPosition)
|
val chip = getItem(adapterPosition)
|
||||||
showDetailedChip(view.context, contact)
|
showDetailedChip(view.context, chip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
|
||||||
val contact = getItem(position)
|
val chip = getItem(position)
|
||||||
val view = holder.containerView
|
val view = holder.containerView
|
||||||
|
|
||||||
view.avatar.setContact(contact)
|
view.avatar.setContact(chip.contact, chip.address)
|
||||||
|
view.name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address
|
||||||
// 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 ?: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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, contact: Contact) {
|
private fun showDetailedChip(context: Context, chip: Chip) {
|
||||||
val detailedChipView = DetailedChipView(context)
|
val detailedChipView = DetailedChipView(context)
|
||||||
detailedChipView.setContact(contact)
|
detailedChipView.setChip(chip)
|
||||||
|
|
||||||
val rootView = view?.rootView as ViewGroup
|
val rootView = view?.rootView as ViewGroup
|
||||||
|
|
||||||
@@ -83,12 +75,8 @@ class ChipsAdapter @Inject constructor() : QkAdapter<Contact>() {
|
|||||||
detailedChipView.show()
|
detailedChipView.show()
|
||||||
|
|
||||||
detailedChipView.setOnDeleteListener {
|
detailedChipView.setOnDeleteListener {
|
||||||
chipDeleted.onNext(contact)
|
chipDeleted.onNext(chip)
|
||||||
detailedChipView.hide()
|
detailedChipView.hide()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun areItemsTheSame(old: Contact, new: Contact): Boolean {
|
|
||||||
return old.lookupKey == new.lookupKey
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ sealed class ComposeItem {
|
|||||||
|
|
||||||
data class Recent(val value: Conversation) : ComposeItem() {
|
data class Recent(val value: Conversation) : ComposeItem() {
|
||||||
override fun getContacts(): List<Contact> = value.recipients.map { recipient ->
|
override fun getContacts(): List<Contact> = value.recipients.map { recipient ->
|
||||||
Contact(numbers = RealmList(PhoneNumber(address = recipient.address)))
|
recipient.contact ?: Contact(numbers = RealmList(PhoneNumber(address = recipient.address)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -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.setBackgroundTint
|
||||||
import com.moez.QKSMS.common.util.extensions.setTint
|
import com.moez.QKSMS.common.util.extensions.setTint
|
||||||
import com.moez.QKSMS.injection.appComponent
|
import com.moez.QKSMS.injection.appComponent
|
||||||
import com.moez.QKSMS.model.Contact
|
|
||||||
import kotlinx.android.synthetic.main.contact_chip_detailed.view.*
|
import kotlinx.android.synthetic.main.contact_chip_detailed.view.*
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@@ -54,10 +53,10 @@ class DetailedChipView(context: Context) : RelativeLayout(context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setContact(contact: Contact) {
|
fun setChip(chip: Chip) {
|
||||||
avatar.setContact(contact)
|
avatar.setContact(chip.contact, chip.address)
|
||||||
name.text = contact.name
|
name.text = chip.contact?.name?.takeIf { it.isNotBlank() } ?: chip.address
|
||||||
info.text = contact.numbers.joinToString(", ") { it.address }
|
info.text = chip.address
|
||||||
}
|
}
|
||||||
|
|
||||||
fun show() {
|
fun show() {
|
||||||
|
|||||||
Reference in New Issue
Block a user