From 161f6739148d6c1f96a6f0359da96ca267f7e9cf Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Fri, 12 Jan 2018 18:46:47 -0500 Subject: [PATCH] Add ability to attach and send photos --- qksms/build.gradle | 1 + .../main/java/data/mapper/CursorToMessage.kt | 3 +- qksms/src/main/java/interactor/SendMessage.kt | 10 ++- .../feature/compose/AttachmentAdapter.kt | 79 +++++++++++++++++++ .../feature/compose/ComposeActivity.kt | 11 +++ .../feature/compose/ComposeState.kt | 2 + .../feature/compose/ComposeView.kt | 2 + .../feature/compose/ComposeViewModel.kt | 35 ++++++-- .../main/res/layout/attachment_list_item.xml | 55 +++++++++++++ .../src/main/res/layout/compose_activity.xml | 73 ++++++++++------- qksms/src/main/res/values/strings.xml | 1 - 11 files changed, 230 insertions(+), 42 deletions(-) create mode 100644 qksms/src/main/java/presentation/feature/compose/AttachmentAdapter.kt create mode 100644 qksms/src/main/res/layout/attachment_list_item.xml diff --git a/qksms/build.gradle b/qksms/build.gradle index 9b67191f..39d2d7e6 100644 --- a/qksms/build.gradle +++ b/qksms/build.gradle @@ -57,6 +57,7 @@ dependencies { compile 'com.jakewharton.timber:timber:4.5.1' compile 'com.klinkerapps:android-smsmms:4.3.0' compile 'com.mixpanel.android:mixpanel-android:5.2.1' + compile 'com.mlsdev.rximagepicker:library:2.0.2' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile "io.reactivex.rxjava2:rxjava:2.1.4" compile 'io.reactivex.rxjava2:rxkotlin:2.1.0' diff --git a/qksms/src/main/java/data/mapper/CursorToMessage.kt b/qksms/src/main/java/data/mapper/CursorToMessage.kt index 898b949f..a0d1f805 100644 --- a/qksms/src/main/java/data/mapper/CursorToMessage.kt +++ b/qksms/src/main/java/data/mapper/CursorToMessage.kt @@ -25,6 +25,7 @@ import android.provider.Telephony.* import common.util.Keys import data.model.Message import timber.log.Timber +import java.util.* import javax.inject.Inject class CursorToMessage @Inject constructor( @@ -169,7 +170,7 @@ class CursorToMessage @Inject constructor( private fun getColumnIndex(columnsName: String) = try { cursor.getColumnIndexOrThrow(columnsName) } catch (e: Exception) { - Timber.w(e) + Timber.e("Couldn't find column \'$columnsName\' in ${Arrays.toString(cursor.columnNames)}") -1 } } diff --git a/qksms/src/main/java/interactor/SendMessage.kt b/qksms/src/main/java/interactor/SendMessage.kt index 1391e8eb..a4b8868b 100644 --- a/qksms/src/main/java/interactor/SendMessage.kt +++ b/qksms/src/main/java/interactor/SendMessage.kt @@ -21,6 +21,7 @@ package interactor import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.graphics.Bitmap import android.telephony.SmsManager import com.klinker.android.send_message.Message import com.klinker.android.send_message.Settings @@ -38,16 +39,16 @@ class SendMessage @Inject constructor( private val messageRepo: MessageRepository ) : Interactor() { - data class Params(val threadId: Long, val addresses: List, val body: String) + data class Params(val threadId: Long, val addresses: List, val body: String, val attachments: List = listOf()) override fun buildObservable(params: Params): Flowable { return Flowable.just(Unit) .filter { params.addresses.isNotEmpty() } .doOnNext { - if (params.addresses.size == 1) { + if (params.addresses.size == 1 && params.attachments.isEmpty()) { sendSms(params.threadId, params.addresses.first(), params.body) } else { - sendMms(params.threadId, params.addresses, params.body) + sendMms(params.threadId, params.addresses, params.body, params.attachments) } } } @@ -72,13 +73,14 @@ class SendMessage @Inject constructor( smsManager.sendMultipartTextMessage(address, null, parts, ArrayList(sentIntents), ArrayList(deliveredIntents)) } - private fun sendMms(threadId: Long, addresses: List, body: String) { + private fun sendMms(threadId: Long, addresses: List, body: String, attachments: List) { val settings = Settings().apply { useSystemSending = true group = true } val message = Message(body, addresses.toTypedArray()) + attachments.forEach { message.addImage(it) } val transaction = Transaction(context, settings) transaction.sendNewMessage(message, threadId) diff --git a/qksms/src/main/java/presentation/feature/compose/AttachmentAdapter.kt b/qksms/src/main/java/presentation/feature/compose/AttachmentAdapter.kt new file mode 100644 index 00000000..85b93f86 --- /dev/null +++ b/qksms/src/main/java/presentation/feature/compose/AttachmentAdapter.kt @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +package presentation.feature.compose + +import android.content.Context +import android.graphics.Bitmap +import android.support.v7.widget.RecyclerView +import android.view.LayoutInflater +import android.view.ViewGroup +import com.jakewharton.rxbinding2.view.clicks +import com.moez.QKSMS.R +import common.util.Colors +import common.util.extensions.setBackgroundTint +import common.util.extensions.setTint +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.rxkotlin.plusAssign +import io.reactivex.subjects.PublishSubject +import io.reactivex.subjects.Subject +import kotlinx.android.synthetic.main.attachment_list_item.view.* +import presentation.common.base.QkAdapter +import presentation.common.base.QkViewHolder +import javax.inject.Inject + +class AttachmentAdapter @Inject constructor( + private val context: Context, + private val colors: Colors +) : QkAdapter() { + + val attachmentDeleted: Subject = PublishSubject.create() + + private val disposables = CompositeDisposable() + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { + val view = LayoutInflater.from(context).inflate(R.layout.attachment_list_item, parent, false) + + view.thumbnailBounds.clipToOutline = true + + disposables += colors.bubble + .subscribe { color -> view.detach.setBackgroundTint(color) } + + disposables += colors.textSecondary + .subscribe { color -> view.detach.setTint(color) } + + return QkViewHolder(view) + } + + override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) { + super.onDetachedFromRecyclerView(recyclerView) + disposables.clear() + } + + override fun onBindViewHolder(holder: QkViewHolder, position: Int) { + val bitmap = getItem(position) + val view = holder.itemView + + view.clicks().subscribe { + attachmentDeleted.onNext(bitmap) + } + + view.thumbnail.setImageBitmap(bitmap) + } + +} \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt b/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt index 8bf13eaf..2c877594 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeActivity.kt @@ -20,6 +20,7 @@ package presentation.feature.compose import android.app.AlertDialog import android.content.res.ColorStateList +import android.graphics.Bitmap import android.graphics.PorterDuff import android.os.Bundle import android.support.v7.widget.LinearLayoutManager @@ -62,6 +63,7 @@ class ComposeActivity : QkActivity(), ComposeView { override val copyTextIntent: Subject = PublishSubject.create() override val forwardMessageIntent: Subject = PublishSubject.create() override val deleteMessageIntent: Subject = PublishSubject.create() + override val attachmentDeletedIntent: Subject by lazy { attachmentAdapter.attachmentDeleted } override val textChangedIntent by lazy { message.textChanges() } override val attachIntent by lazy { attach.clicks() } override val sendIntent by lazy { send.clicks() } @@ -69,6 +71,7 @@ class ComposeActivity : QkActivity(), ComposeView { @Inject lateinit var chipsAdapter: ChipsAdapter @Inject lateinit var contactsAdapter: ContactAdapter @Inject lateinit var messageAdapter: MessagesAdapter + @Inject lateinit var attachmentAdapter: AttachmentAdapter init { appComponent.inject(this) @@ -120,6 +123,9 @@ class ComposeActivity : QkActivity(), ComposeView { messageList.layoutManager = layoutManager messageList.adapter = messageAdapter + attachments.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) + attachments.adapter = attachmentAdapter + messageBackground.backgroundTintMode = PorterDuff.Mode.MULTIPLY val states = arrayOf( @@ -206,6 +212,11 @@ class ComposeActivity : QkActivity(), ComposeView { messageAdapter.updateData(state.messages) } + attachments.setVisible(state.attachments.isNotEmpty()) + if (attachmentAdapter.data !== state.attachments) { + attachmentAdapter.data = state.attachments + } + if (title != state.title) title = state.title send.isEnabled = state.canSend diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeState.kt b/qksms/src/main/java/presentation/feature/compose/ComposeState.kt index b8c55948..3e003029 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeState.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeState.kt @@ -18,6 +18,7 @@ */ package presentation.feature.compose +import android.graphics.Bitmap import data.model.Contact import data.model.Message import io.realm.RealmResults @@ -31,5 +32,6 @@ data class ComposeState( val title: String = "", val archived: Boolean = false, val messages: RealmResults? = null, + val attachments: List = ArrayList(), val canSend: Boolean = false ) \ No newline at end of file diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeView.kt b/qksms/src/main/java/presentation/feature/compose/ComposeView.kt index f93b506a..a6b6138a 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeView.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeView.kt @@ -18,6 +18,7 @@ */ package presentation.feature.compose +import android.graphics.Bitmap import data.model.Contact import data.model.Message import io.reactivex.Observable @@ -37,6 +38,7 @@ interface ComposeView : QkView { val copyTextIntent: Subject val forwardMessageIntent: Subject val deleteMessageIntent: Subject + val attachmentDeletedIntent: Subject val textChangedIntent: Observable val attachIntent: Observable val sendIntent: Observable diff --git a/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt b/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt index e29f42ad..7e75673d 100644 --- a/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt +++ b/qksms/src/main/java/presentation/feature/compose/ComposeViewModel.kt @@ -20,6 +20,10 @@ package presentation.feature.compose import android.content.Context import android.content.Intent +import android.graphics.Bitmap +import com.mlsdev.rximagepicker.RxImageConverters +import com.mlsdev.rximagepicker.RxImagePicker +import com.mlsdev.rximagepicker.Sources import com.moez.QKSMS.R import common.di.appComponent import common.util.ClipboardUtils @@ -38,6 +42,7 @@ import io.reactivex.rxkotlin.Observables import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.withLatestFrom import io.reactivex.schedulers.Schedulers +import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.Subject import presentation.common.Navigator @@ -61,6 +66,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( @Inject lateinit var deleteMessage: DeleteMessage private var draft: String = "" + private val attachments: Subject> = BehaviorSubject.createDefault(ArrayList()) private val contacts: List by lazy { contactsRepo.getContacts() } private val contactsReducer: Subject<(List) -> List> = PublishSubject.create() private val selectedContacts: Observable> @@ -149,6 +155,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel( disposables += deleteConversation disposables += conversation.subscribe() disposables += messages.subscribe() + disposables += attachments.subscribe { attachments -> newState { it.copy(attachments = attachments) } } if (threadId == 0L) { syncContacts.execute(Unit) @@ -233,24 +240,36 @@ class ComposeViewModel(intent: Intent) : QkViewModel( }) .subscribe() - // Add an attachment + // Attach a photo intents += view.attachIntent - .subscribe { context.makeToast(R.string.compose_attach_unsupported) } + .flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) } + .flatMap { uri -> RxImageConverters.uriToBitmap(context, uri) } + .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) + .subscribe { attachments.onNext(it) } - // Enable the send button when there is text input into the new message body, disable otherwise - intents += view.textChangedIntent.subscribe { text -> - newState { it.copy(canSend = text.isNotEmpty()) } - } + // Detach a photo + intents += view.attachmentDeletedIntent + .withLatestFrom(attachments, { bitmap, attachments -> attachments.filter { it !== bitmap } }) + .subscribe { attachments.onNext(it) } + + // Enable the send button when there is text input into the new message body or there's + // an attachment, disable otherwise + intents += Observables + .combineLatest(view.textChangedIntent, attachments, { text, attachments -> + text.isNotBlank() || attachments.isNotEmpty() + }) + .subscribe { canSend -> newState { it.copy(canSend = canSend) } } // Send a message when the send button is clicked, and disable editing mode if it's enabled intents += view.sendIntent .withLatestFrom(view.textChangedIntent, { _, body -> body }) .map { body -> body.toString() } - .withLatestFrom(conversation, { body, conversation -> + .withLatestFrom(attachments, conversation, { body, attachments, conversation -> val threadId = conversation.id val addresses = conversation.recipients.map { it.address } - sendMessage.execute(SendMessage.Params(threadId, addresses, body)) + sendMessage.execute(SendMessage.Params(threadId, addresses, body, attachments)) view.setDraft("") + newState { it.copy(attachments = ArrayList()) } }) .withLatestFrom(state, { _, state -> if (state.editingMode) { diff --git a/qksms/src/main/res/layout/attachment_list_item.xml b/qksms/src/main/res/layout/attachment_list_item.xml new file mode 100644 index 00000000..505fd3a1 --- /dev/null +++ b/qksms/src/main/res/layout/attachment_list_item.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/qksms/src/main/res/layout/compose_activity.xml b/qksms/src/main/res/layout/compose_activity.xml index 7753e0ad..edb2007f 100644 --- a/qksms/src/main/res/layout/compose_activity.xml +++ b/qksms/src/main/res/layout/compose_activity.xml @@ -76,36 +76,53 @@ android:layout_weight="1" android:background="@drawable/rounded_rectangle_shadow" android:gravity="center_vertical" - android:orientation="horizontal"> + android:orientation="vertical"> - - - - - + android:clipChildren="false" + android:clipToPadding="false" + android:paddingEnd="8dp" + android:paddingStart="8dp" + android:paddingTop="8dp" /> + + + + + + + + + + diff --git a/qksms/src/main/res/values/strings.xml b/qksms/src/main/res/values/strings.xml index a50e7805..9d9ac780 100644 --- a/qksms/src/main/res/values/strings.xml +++ b/qksms/src/main/res/values/strings.xml @@ -40,7 +40,6 @@ Optimizing database, this might take a minute… - MMS support will be added shortly! Write a message… Sending…