From f6e07d9bc32e901b57dd38d2549d7097dafc9e5f Mon Sep 17 00:00:00 2001 From: moezbhatti Date: Tue, 25 Dec 2018 19:46:07 -0500 Subject: [PATCH] Add button to select contact, parse response as vCard string --- .../QKSMS/feature/compose/ComposeActivity.kt | 35 ++++++++++-------- .../moez/QKSMS/feature/compose/ComposeView.kt | 3 ++ .../QKSMS/feature/compose/ComposeViewModel.kt | 32 ++++++++++++++++ .../src/main/res/layout/compose_activity.xml | 37 ++++++++++++++++++- presentation/src/main/res/values/strings.xml | 2 + 5 files changed, 93 insertions(+), 16 deletions(-) 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 9c11d55b..16a046dd 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 @@ -27,6 +27,7 @@ import android.content.Intent import android.net.Uri import android.os.Build import android.os.Bundle +import android.provider.ContactsContract import android.provider.MediaStore import android.text.format.DateFormat import android.view.Menu @@ -68,8 +69,9 @@ import javax.inject.Inject class ComposeActivity : QkThemedActivity(), ComposeView { companion object { - const val CAMERA_REQUEST_CODE = 0 - const val GALLERY_REQUEST_CODE = 1 + private const val CAMERA_REQUEST_CODE = 0 + private const val GALLERY_REQUEST_CODE = 1 + private const val CONTACT_REQUEST_CODE = 2 } @Inject lateinit var attachmentAdapter: AttachmentAdapter @@ -97,7 +99,9 @@ class ComposeActivity : QkThemedActivity(), ComposeView { override val cameraIntent by lazy { Observable.merge(camera.clicks(), cameraLabel.clicks()) } override val galleryIntent by lazy { Observable.merge(gallery.clicks(), galleryLabel.clicks()) } override val scheduleIntent by lazy { Observable.merge(schedule.clicks(), scheduleLabel.clicks()) } + override val attachContactIntent by lazy { Observable.merge(contact.clicks(), contactLabel.clicks()) } override val attachmentSelectedIntent: Subject = PublishSubject.create() + override val contactSelectedIntent: Subject = PublishSubject.create() override val inputContentIntent by lazy { message.inputContentSelected } override val scheduleSelectedIntent: Subject = PublishSubject.create() override val changeSimIntent by lazy { sim.clicks() } @@ -232,9 +236,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { send.imageAlpha = if (state.canSend) 255 else 128 } - override fun clearSelection() { - messageAdapter.clearSelection() - } + override fun clearSelection() = messageAdapter.clearSelection() override fun showDetails(details: String) { AlertDialog.Builder(this) @@ -268,6 +270,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView { }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show() } + override fun requestContact() { + val intent = Intent(Intent.ACTION_PICK) + .setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE) + + startActivityForResult(Intent.createChooser(intent, null), CONTACT_REQUEST_CODE) + } + override fun requestCamera() { cameraDestination = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) .let { timestamp -> ContentValues().apply { put(MediaStore.Images.Media.TITLE, timestamp) } } @@ -288,9 +297,7 @@ class ComposeActivity : QkThemedActivity(), ComposeView { startActivityForResult(Intent.createChooser(intent, null), GALLERY_REQUEST_CODE) } - override fun setDraft(draft: String) { - message.setText(draft) - } + override fun setDraft(draft: String) = message.setText(draft) override fun scrollToMessage(id: Long) { messageAdapter.data?.second @@ -323,15 +330,13 @@ class ComposeActivity : QkThemedActivity(), ComposeView { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (resultCode == Activity.RESULT_OK) { when (requestCode) { - CAMERA_REQUEST_CODE -> cameraDestination - GALLERY_REQUEST_CODE -> data?.data - else -> null - }?.let(attachmentSelectedIntent::onNext) + CAMERA_REQUEST_CODE -> cameraDestination?.let(attachmentSelectedIntent::onNext) + GALLERY_REQUEST_CODE -> data?.data?.let(attachmentSelectedIntent::onNext) + CONTACT_REQUEST_CODE -> data?.data?.let(contactSelectedIntent::onNext) + } } } - override fun onBackPressed() { - backPressedIntent.onNext(Unit) - } + override fun onBackPressed() = backPressedIntent.onNext(Unit) } \ No newline at end of file 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 a75b3178..e15b9d00 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 @@ -48,7 +48,9 @@ interface ComposeView : QkView { val cameraIntent: Observable<*> val galleryIntent: Observable<*> val scheduleIntent: Observable<*> + val attachContactIntent: Observable<*> val attachmentSelectedIntent: Observable + val contactSelectedIntent: Observable val inputContentIntent: Observable val scheduleSelectedIntent: Observable val scheduleCancelIntent: Observable<*> @@ -64,6 +66,7 @@ interface ComposeView : QkView { fun requestCamera() fun requestGallery() fun requestDatePicker() + fun requestContact() fun setDraft(draft: String) fun scrollToMessage(id: Long) fun showQksmsPlusSnackbar(@StringRes message: Int) 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 76fd3681..4b06796c 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 @@ -19,6 +19,8 @@ package com.moez.QKSMS.feature.compose import android.content.Context +import android.net.Uri +import android.provider.ContactsContract import android.telephony.PhoneNumberUtils import android.telephony.SmsMessage import android.view.inputmethod.EditorInfo @@ -68,10 +70,12 @@ import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import io.realm.RealmList +import timber.log.Timber import java.util.* import javax.inject.Inject import javax.inject.Named + class ComposeViewModel @Inject constructor( @Named("query") private val query: String, @Named("threadId") private val threadId: Long, @@ -519,6 +523,21 @@ class ComposeViewModel @Inject constructor( .autoDisposable(view.scope()) .subscribe { scheduled -> newState { copy(scheduled = scheduled) } } + // Attach a contact + view.attachContactIntent + .doOnNext { newState { copy(attaching = false) } } + .autoDisposable(view.scope()) + .subscribe { view.requestContact() } + + // Contact was selected for attachment + view.contactSelectedIntent + .map(::vCard) + .autoDisposable(view.scope()) + .subscribe({}, { error -> + context.makeToast(R.string.compose_contact_error) + Timber.w(error) + }) + // Detach a photo view.attachmentDeletedIntent .withLatestFrom(attachments) { bitmap, attachments -> attachments.filter { it !== bitmap } } @@ -678,4 +697,17 @@ class ComposeViewModel @Inject constructor( } + private fun vCard(contactData: Uri): String? { + val lookupKey = context.contentResolver.query(contactData, null, null, null, null)?.use { cursor -> + cursor.moveToFirst() + cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)) + } + + val vCardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey) + return context.contentResolver.openAssetFileDescriptor(vCardUri, "r") + ?.createInputStream() + ?.readBytes() + ?.let { bytes -> String(bytes) } + } + } \ No newline at end of file diff --git a/presentation/src/main/res/layout/compose_activity.xml b/presentation/src/main/res/layout/compose_activity.xml index 86ea1246..f31dcacd 100644 --- a/presentation/src/main/res/layout/compose_activity.xml +++ b/presentation/src/main/res/layout/compose_activity.xml @@ -397,7 +397,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" - app:constraint_referenced_ids="schedule, scheduleLabel, gallery, galleryLabel, camera, cameraLabel, attachingBackground" /> + app:constraint_referenced_ids="contact, contactLabel, schedule, scheduleLabel, gallery, galleryLabel, camera, + cameraLabel, attachingBackground" /> + + + + Attach a photo Take a photo Schedule message + Attach a contact + Error reading contact %s selected, change SIM card Send message