Add vcard to message

This commit is contained in:
moezbhatti
2018-12-25 22:44:47 -05:00
parent f6e07d9bc3
commit 14b2d2ef8b
11 changed files with 177 additions and 36 deletions
@@ -25,22 +25,39 @@ import com.bumptech.glide.Glide
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.model.Attachment
import ezvcard.Ezvcard
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.android.synthetic.main.attachment_list_item.view.*
import kotlinx.android.synthetic.main.attachment_contact_list_item.view.*
import kotlinx.android.synthetic.main.attachment_image_list_item.view.*
import javax.inject.Inject
class AttachmentAdapter @Inject constructor(
private val context: Context
) : QkAdapter<Attachment>() {
companion object {
private const val VIEW_TYPE_IMAGE = 0
private const val VIEW_TYPE_CONTACT = 1
}
val attachmentDeleted: Subject<Attachment> = PublishSubject.create()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.attachment_list_item, parent, false)
val inflater = LayoutInflater.from(parent.context)
val view = when (viewType) {
VIEW_TYPE_IMAGE -> inflater.inflate(R.layout.attachment_image_list_item, parent, false)
.apply { thumbnailBounds.clipToOutline = true }
view.thumbnailBounds.clipToOutline = true
VIEW_TYPE_CONTACT -> inflater.inflate(R.layout.attachment_contact_list_item, parent, false)
else -> null!! // Impossible
}
return QkViewHolder(view).apply {
view.setOnClickListener {
@@ -54,7 +71,22 @@ class AttachmentAdapter @Inject constructor(
val attachment = getItem(position)
val view = holder.itemView
Glide.with(context).load(attachment.getUri()).into(view.thumbnail)
when (attachment) {
is Attachment.Image -> Glide.with(context)
.load(attachment.getUri())
.into(view.thumbnail)
is Attachment.Contact -> Observable.just(attachment.vCard)
.mapNotNull { vCard -> Ezvcard.parse(vCard).first() }
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { vcard -> view.name?.text = vcard.formattedName.value }
}
}
override fun getItemViewType(position: Int) = when (getItem(position)) {
is Attachment.Image -> VIEW_TYPE_IMAGE
is Attachment.Contact -> VIEW_TYPE_CONTACT
}
}
@@ -65,7 +65,6 @@ import java.text.SimpleDateFormat
import java.util.*
import javax.inject.Inject
class ComposeActivity : QkThemedActivity(), ComposeView {
companion object {
@@ -23,6 +23,7 @@ import android.net.Uri
import androidx.lifecycle.ViewModel
import com.moez.QKSMS.injection.ViewModelKey
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
@@ -70,11 +71,11 @@ class ComposeActivityModule {
@Provides
@Named("attachments")
fun provideSharedAttachments(activity: ComposeActivity): List<Attachment> {
fun provideSharedAttachments(activity: ComposeActivity): Attachments {
val sharedImages = mutableListOf<Uri>()
activity.intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::add)
activity.intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)?.run(sharedImages::addAll)
return sharedImages.map { Attachment(it) }
return Attachments(sharedImages.map { Attachment.Image(it) })
}
@Provides
@@ -49,6 +49,7 @@ import com.moez.QKSMS.interactor.SendMessage
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.model.Attachments
import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Message
@@ -75,13 +76,12 @@ 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,
@Named("address") private val address: String,
@Named("text") private val sharedText: String,
@Named("attachments") private val sharedAttachments: List<Attachment>,
@Named("attachments") private val sharedAttachments: Attachments,
private val context: Context,
private val activeConversationManager: ActiveConversationManager,
private val addScheduledMessage: AddScheduledMessage,
@@ -506,8 +506,8 @@ class ComposeViewModel @Inject constructor(
// A photo was selected
Observable.merge(
view.attachmentSelectedIntent.map { uri -> Attachment(uri) },
view.inputContentIntent.map { inputContent -> Attachment(inputContent = inputContent) })
view.attachmentSelectedIntent.map { uri -> Attachment.Image(uri) },
view.inputContentIntent.map { inputContent -> Attachment.Image(inputContent = inputContent) })
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment }
.doOnNext { attachments.onNext(it) }
.autoDisposable(view.scope())
@@ -531,12 +531,14 @@ class ComposeViewModel @Inject constructor(
// Contact was selected for attachment
view.contactSelectedIntent
.map(::vCard)
.map { uri -> Attachment.Contact(getVCard(uri)!!) }
.withLatestFrom(attachments) { attachment, attachments -> attachments + attachment}
.subscribeOn(Schedulers.io())
.autoDisposable(view.scope())
.subscribe({}, { error ->
.subscribe(attachments::onNext) { error ->
context.makeToast(R.string.compose_contact_error)
Timber.w(error)
})
}
// Detach a photo
view.attachmentDeletedIntent
@@ -634,7 +636,7 @@ class ComposeViewModel @Inject constructor(
// Scheduling a message
state.scheduled != 0L -> {
newState { copy(scheduled = 0) }
val uris = attachments.map { it.getUri() }.map { it.toString() }
val uris = attachments.mapNotNull { it as? Attachment.Image }.map { it.getUri() }.map { it.toString() }
val params = AddScheduledMessage.Params(state.scheduled, subId, addresses, state.sendAsGroup, body, uris)
addScheduledMessage.execute(params)
context.makeToast(R.string.compose_scheduled_toast)
@@ -697,7 +699,7 @@ class ComposeViewModel @Inject constructor(
}
private fun vCard(contactData: Uri): String? {
private fun getVCard(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))