forked from upstream-mirrors/quik
Only store necessary strings in AvatarView, instead of realm model (contact or recipient)
This commit is contained in:
@@ -36,6 +36,7 @@ import common.util.extensions.setTint
|
||||
import injection.appComponent
|
||||
import kotlinx.android.synthetic.main.avatar_view.view.*
|
||||
import model.Contact
|
||||
import model.Recipient
|
||||
import javax.inject.Inject
|
||||
|
||||
class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) {
|
||||
@@ -48,11 +49,9 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
||||
*/
|
||||
var threadId: Long = 0
|
||||
|
||||
var contact: Contact? = null
|
||||
set(value) {
|
||||
field = value
|
||||
updateView()
|
||||
}
|
||||
private var lookupKey: String? = null
|
||||
private var name: String? = null
|
||||
private var address: String? = null
|
||||
|
||||
init {
|
||||
if (!isInEditMode) {
|
||||
@@ -65,6 +64,26 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
||||
clipToOutline = true
|
||||
}
|
||||
|
||||
fun setContact(recipient: Recipient?) {
|
||||
// If the recipient has a contact, just use that and return
|
||||
recipient?.contact?.let { contact ->
|
||||
setContact(contact)
|
||||
return
|
||||
}
|
||||
|
||||
lookupKey = null
|
||||
name = null
|
||||
address = recipient?.address
|
||||
updateView()
|
||||
}
|
||||
|
||||
fun setContact(contact: Contact?) {
|
||||
lookupKey = contact?.lookupKey
|
||||
name = contact?.name
|
||||
address = contact?.numbers?.firstOrNull()?.address
|
||||
updateView()
|
||||
}
|
||||
|
||||
override fun onAttachedToWindow() {
|
||||
super.onAttachedToWindow()
|
||||
|
||||
@@ -84,13 +103,10 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
||||
clicks()
|
||||
.autoDisposable(scope())
|
||||
.subscribe {
|
||||
if (contact?.lookupKey.isNullOrEmpty()) {
|
||||
contact?.numbers?.firstOrNull()?.let { number ->
|
||||
navigator.addContact(number.address)
|
||||
}
|
||||
if (lookupKey.isNullOrEmpty()) {
|
||||
address?.let { address -> navigator.addContact(address) }
|
||||
} else {
|
||||
val key = contact?.lookupKey
|
||||
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, key)
|
||||
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
|
||||
ContactsContract.QuickContact.showQuickContact(context, this@AvatarView, uri,
|
||||
ContactsContract.QuickContact.MODE_MEDIUM, null)
|
||||
}
|
||||
@@ -107,8 +123,8 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
||||
}
|
||||
|
||||
private fun updateView() {
|
||||
if (contact?.name.orEmpty().isNotEmpty()) {
|
||||
initial.text = contact?.name?.substring(0, 1)
|
||||
if (name?.isNotEmpty() == true) {
|
||||
initial.text = name?.substring(0, 1)
|
||||
icon.visibility = GONE
|
||||
} else {
|
||||
initial.text = null
|
||||
@@ -116,7 +132,7 @@ class AvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
||||
}
|
||||
|
||||
photo.setImageDrawable(null)
|
||||
contact?.numbers?.firstOrNull()?.address?.let { address ->
|
||||
address?.let { address ->
|
||||
GlideApp.with(photo).load(PhoneNumberUtils.stripSeparators(address)).into(photo)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,11 +24,11 @@ import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import com.moez.QKSMS.R
|
||||
import kotlinx.android.synthetic.main.group_avatar_view.view.*
|
||||
import model.Contact
|
||||
import model.Recipient
|
||||
|
||||
class GroupAvatarView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs) {
|
||||
|
||||
var contacts: List<Contact> = ArrayList()
|
||||
var contacts: List<Recipient> = ArrayList()
|
||||
set(value) {
|
||||
field = value
|
||||
updateView()
|
||||
@@ -57,7 +57,7 @@ class GroupAvatarView @JvmOverloads constructor(context: Context, attrs: Attribu
|
||||
private fun updateView() {
|
||||
avatars.forEachIndexed { index, avatar ->
|
||||
avatar.visibility = if (contacts.size > index) View.VISIBLE else View.GONE
|
||||
avatar.contact = if (contacts.size > index) contacts[index] else null
|
||||
avatar.setContact(contacts.getOrNull(index))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,9 +26,7 @@ import common.base.QkRealmAdapter
|
||||
import common.base.QkViewHolder
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import kotlinx.android.synthetic.main.blocked_list_item.view.*
|
||||
import model.Contact
|
||||
import model.Conversation
|
||||
import model.PhoneNumber
|
||||
import javax.inject.Inject
|
||||
|
||||
class BlockedAdapter @Inject constructor(private val context: Context) : QkRealmAdapter<Conversation>() {
|
||||
@@ -45,10 +43,7 @@ class BlockedAdapter @Inject constructor(private val context: Context) : QkRealm
|
||||
|
||||
view.setOnClickListener { unblock.onNext(conversation.id) }
|
||||
|
||||
view.avatars.contacts = conversation.recipients.map { recipient ->
|
||||
recipient.contact ?: Contact().apply { numbers.add(PhoneNumber().apply { address = recipient.address }) }
|
||||
}
|
||||
|
||||
view.avatars.contacts = conversation.recipients
|
||||
view.title.text = conversation.getTitle()
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ class ChipsAdapter @Inject constructor(private val context: Context, private val
|
||||
val contact = getItem(position)
|
||||
val view = holder.itemView
|
||||
|
||||
view.avatar.contact = contact
|
||||
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
|
||||
|
||||
@@ -24,13 +24,13 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.jakewharton.rxbinding2.view.clicks
|
||||
import com.moez.QKSMS.R
|
||||
import common.base.QkAdapter
|
||||
import common.base.QkViewHolder
|
||||
import common.util.extensions.setVisible
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.contact_list_item.view.*
|
||||
import model.Contact
|
||||
import common.base.QkAdapter
|
||||
import common.base.QkViewHolder
|
||||
import javax.inject.Inject
|
||||
|
||||
class ContactAdapter @Inject constructor(private val context: Context) : QkAdapter<Contact>() {
|
||||
@@ -50,7 +50,7 @@ class ContactAdapter @Inject constructor(private val context: Context) : QkAdapt
|
||||
|
||||
view.primary.clicks().subscribe { contactSelected.onNext(copyContact(contact, 0)) }
|
||||
|
||||
view.avatar.contact = contact
|
||||
view.avatar.setContact(contact)
|
||||
view.name.text = contact.name
|
||||
view.name.setVisible(view.name.text.isNotEmpty())
|
||||
view.address.text = contact.numbers.first()?.address ?: ""
|
||||
|
||||
@@ -25,9 +25,9 @@ import android.widget.RelativeLayout
|
||||
import com.moez.QKSMS.R
|
||||
import com.uber.autodispose.android.scope
|
||||
import com.uber.autodispose.kotlin.autoDisposable
|
||||
import injection.appComponent
|
||||
import common.util.Colors
|
||||
import common.util.extensions.setBackgroundTint
|
||||
import injection.appComponent
|
||||
import kotlinx.android.synthetic.main.contact_chip_detailed.view.*
|
||||
import model.Contact
|
||||
import javax.inject.Inject
|
||||
@@ -58,7 +58,7 @@ class DetailedChipView(context: Context) : RelativeLayout(context) {
|
||||
}
|
||||
|
||||
fun setContact(contact: Contact) {
|
||||
avatar.contact = contact
|
||||
avatar.setContact(contact)
|
||||
name.text = contact.name
|
||||
info.text = contact.numbers.map { it.address }.toString()
|
||||
}
|
||||
|
||||
@@ -37,14 +37,12 @@ import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.rxkotlin.plusAssign
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmRecyclerViewAdapter
|
||||
import io.realm.RealmResults
|
||||
import kotlinx.android.synthetic.main.message_list_item_in.view.*
|
||||
import model.Contact
|
||||
import model.Conversation
|
||||
import model.Message
|
||||
import model.PhoneNumber
|
||||
import model.Recipient
|
||||
import util.extensions.isImage
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
@@ -79,7 +77,7 @@ class MessagesAdapter @Inject constructor(
|
||||
updateData(value?.second)
|
||||
}
|
||||
|
||||
private val contactMap = HashMap<String, Contact>()
|
||||
private val contactMap = HashMap<String, Recipient?>()
|
||||
private val selected = HashMap<Long, Boolean>()
|
||||
private val disposables = CompositeDisposable()
|
||||
|
||||
@@ -149,13 +147,12 @@ class MessagesAdapter @Inject constructor(
|
||||
|
||||
val address = message.address
|
||||
if (contactMap[address]?.isValid != true) {
|
||||
contactMap[address] = data?.first?.recipients?.mapNotNull { it.contact } // Map the conversation to its contacts
|
||||
?.firstOrNull { it.numbers.any { PhoneNumberUtils.compare(it.address, address) } } // See if any of the phone numbers match
|
||||
?: Contact(numbers = RealmList(PhoneNumber(address = address))) // Fallback to a fake contact
|
||||
contactMap[address] = data?.first?.recipients
|
||||
?.firstOrNull { PhoneNumberUtils.compare(it.address, address) } // See if any of the phone numbers match
|
||||
}
|
||||
|
||||
view.avatar.threadId = data?.first?.id ?: 0
|
||||
view.avatar.contact = contactMap[address]
|
||||
view.avatar.setContact(contactMap[address])
|
||||
}
|
||||
|
||||
private fun bindStatus(view: View, position: Int) {
|
||||
@@ -169,7 +166,7 @@ class MessagesAdapter @Inject constructor(
|
||||
message.isSending() -> context.getString(R.string.message_status_sending)
|
||||
message.isDelivered() -> context.getString(R.string.message_status_delivered, timestamp)
|
||||
message.isFailedMessage() -> context.getString(R.string.message_status_failed)
|
||||
!message.isMe() && data?.first?.recipients?.size ?: 0 > 1 -> "${contactMap[message.address]?.name} • $timestamp"
|
||||
!message.isMe() && data?.first?.recipients?.size ?: 0 > 1 -> "${contactMap[message.address]?.getDisplayName()} • $timestamp"
|
||||
else -> timestamp
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ class ConversationRecipientAdapter @Inject constructor(
|
||||
}
|
||||
|
||||
view.avatar.threadId = threadId
|
||||
view.avatar.contact = recipient.contact
|
||||
view.avatar.setContact(recipient)
|
||||
|
||||
view.name.text = recipient.contact?.name ?: recipient.address
|
||||
|
||||
|
||||
@@ -32,9 +32,7 @@ import common.util.DateFormatter
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.Subject
|
||||
import kotlinx.android.synthetic.main.conversation_list_item.view.*
|
||||
import model.Contact
|
||||
import model.Conversation
|
||||
import model.PhoneNumber
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationsAdapter @Inject constructor(
|
||||
@@ -75,9 +73,7 @@ class ConversationsAdapter @Inject constructor(
|
||||
view.clicks().subscribe { clicks.onNext(conversation.id) }
|
||||
view.longClicks().subscribe { longClicks.onNext(conversation.id) }
|
||||
|
||||
view.avatars.contacts = conversation.recipients.map { recipient ->
|
||||
recipient.contact ?: Contact().apply { numbers.add(PhoneNumber().apply { address = recipient.address }) }
|
||||
}
|
||||
view.avatars.contacts = conversation.recipients
|
||||
view.title.text = conversation.getTitle()
|
||||
view.date.text = dateFormatter.getConversationTimestamp(conversation.date)
|
||||
view.snippet.text = if (conversation.me) "You: ${conversation.snippet}" else conversation.snippet
|
||||
|
||||
Reference in New Issue
Block a user