Allow selection of any of contact's phone numbers

This commit is contained in:
Moez Bhatti
2018-01-12 22:59:45 -05:00
parent 050981faff
commit 634c7c707b
6 changed files with 159 additions and 48 deletions
@@ -18,30 +18,32 @@
*/ */
package data.mapper package data.mapper
import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds.Phone.*
import data.model.Contact import data.model.Contact
import data.model.PhoneNumber import data.model.PhoneNumber
import javax.inject.Inject import javax.inject.Inject
class CursorToContact @Inject constructor() : Mapper<Cursor, Contact> { class CursorToContact @Inject constructor(private val context: Context) : Mapper<Cursor, Contact> {
companion object { companion object {
val URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI val URI = CONTENT_URI
val PROJECTION = arrayOf( val PROJECTION = arrayOf(LOOKUP_KEY, NUMBER, TYPE, DISPLAY_NAME)
ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val LOOKUP_KEY = 0 val COLUMN_LOOKUP_KEY = 0
val NUMBER = 1 val COLUMN_NUMBER = 1
val DISPLAY_NAME = 2 val COLUMN_TYPE = 2
val COLUMN_DISPLAY_NAME = 3
} }
override fun map(from: Cursor) = Contact().apply { override fun map(from: Cursor) = Contact().apply {
lookupKey = from.getString(LOOKUP_KEY) lookupKey = from.getString(COLUMN_LOOKUP_KEY)
name = from.getString(DISPLAY_NAME) name = from.getString(COLUMN_DISPLAY_NAME)
numbers.add(PhoneNumber().apply { address = from.getString(NUMBER) }) numbers.add(PhoneNumber().apply {
address = from.getString(COLUMN_NUMBER)
type = context.getString(getTypeLabelResource(from.getInt(COLUMN_TYPE)))
})
lastUpdate = System.currentTimeMillis() lastUpdate = System.currentTimeMillis()
} }
@@ -23,6 +23,6 @@ import io.realm.RealmObject
open class PhoneNumber : RealmObject() { open class PhoneNumber : RealmObject() {
var address: String = "" var address: String = ""
var label: String = "" var type: String = ""
} }
@@ -44,11 +44,11 @@ open class ContactSync @Inject constructor(
.groupBy { contact -> contact.lookupKey } .groupBy { contact -> contact.lookupKey }
.flatMap { group -> group.toList().toFlowable() } .flatMap { group -> group.toList().toFlowable() }
.map { contacts -> .map { contacts ->
val numbers = contacts.map { it.numbers }.flatten() val allNumbers = contacts.map { it.numbers }.flatten()
val contact = contacts.first() contacts.first().apply {
contact.numbers.clear() numbers.clear()
contact.numbers.addAll(numbers) numbers.addAll(allNumbers)
contact }
} }
.toList().toFlowable() .toList().toFlowable()
.doOnNext { contacts -> .doOnNext { contacts ->
@@ -20,15 +20,16 @@ package presentation.feature.compose
import android.content.Context import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R import com.moez.QKSMS.R
import data.model.Contact import data.model.Contact
import presentation.common.base.QkAdapter
import presentation.common.base.QkViewHolder
import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.contact_list_item.view.* import kotlinx.android.synthetic.main.contact_list_item.view.*
import presentation.common.base.QkAdapter
import presentation.common.base.QkViewHolder
import javax.inject.Inject import javax.inject.Inject
class ContactAdapter @Inject constructor(private val context: Context) : QkAdapter<Contact>() { class ContactAdapter @Inject constructor(private val context: Context) : QkAdapter<Contact>() {
@@ -46,11 +47,34 @@ class ContactAdapter @Inject constructor(private val context: Context) : QkAdapt
val contact = getItem(position) val contact = getItem(position)
val view = holder.itemView val view = holder.itemView
view.clicks().subscribe { contactSelected.onNext(contact) } view.primary.clicks().subscribe { contactSelected.onNext(copyContact(contact, 0)) }
view.avatar.contact = contact view.avatar.contact = contact
view.name.text = contact.name view.name.text = contact.name
view.address.text = contact.numbers.map { it.address }.toString() view.address.text = contact.numbers.first()?.address ?: ""
view.type.text = contact.numbers.first()?.type ?: ""
view.addresses.removeAllViews()
contact.numbers.forEachIndexed { index, number ->
if (index != 0) {
val numberView = View.inflate(context, R.layout.contact_number_list_item, null)
numberView.clicks().subscribe { contactSelected.onNext(copyContact(contact, index)) }
numberView.address.text = number.address
numberView.type.text = number.type
view.addresses.addView(numberView)
}
}
}
/**
* Creates a copy of the contact with only one phone number, so that the chips
* view can still display the name/photo, and not get confused about which phone number to use
*/
private fun copyContact(contact: Contact, numberIndex: Int) = Contact().apply {
lookupKey = contact.lookupKey
name = contact.name
numbers.add(contact.numbers[numberIndex])
} }
override fun areItemsTheSame(old: Contact, new: Contact): Boolean { override fun areItemsTheSame(old: Contact, new: Contact): Boolean {
+60 -25
View File
@@ -18,41 +18,76 @@
~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>. ~ along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
--> -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout 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" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/ripple" android:orientation="vertical">
android:gravity="center_vertical"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<presentation.common.widget.AvatarView
android:id="@+id/avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="16dp" />
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:id="@+id/primary"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:background="@drawable/ripple"
android:gravity="center_vertical"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<presentation.common.widget.QkTextView <presentation.common.widget.AvatarView
android:id="@+id/name" android:id="@+id/avatar"
style="@style/TextPrimary" android:layout_width="40dp"
android:layout_width="wrap_content" android:layout_height="40dp"
android:layout_height="wrap_content" android:layout_marginRight="16dp" />
tools:text="Moez Bhatti" />
<presentation.common.widget.QkTextView <LinearLayout
android:id="@+id/address" android:layout_width="match_parent"
style="@style/TextSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:text="(123) 456-7890" /> android:orientation="vertical">
<presentation.common.widget.QkTextView
android:id="@+id/name"
style="@style/TextPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Moez Bhatti" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<presentation.common.widget.QkTextView
android:id="@+id/address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="14sp"
app:textColor="secondary"
tools:text="(123) 456-7890"
tools:textColor="@color/textSecondary" />
<presentation.common.widget.QkTextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="14sp"
app:textColor="tertiary"
tools:text="Mobile"
tools:textColor="@color/textTertiary" />
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/addresses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout> </LinearLayout>
@@ -0,0 +1,50 @@
<?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/>.
-->
<LinearLayout 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">
<presentation.common.widget.QkTextView
android:id="@+id/address"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginStart="72dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="14sp"
app:textColor="secondary"
tools:text="(123) 456-7890"
tools:textColor="@color/textSecondary" />
<presentation.common.widget.QkTextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginEnd="16dp"
android:gravity="center_vertical"
android:textSize="14sp"
app:textColor="tertiary"
tools:text="Mobile"
tools:textColor="@color/textTertiary" />
</LinearLayout>