Add ability to attach and send photos

This commit is contained in:
Moez Bhatti
2018-01-12 18:46:47 -05:00
parent 432f71ee95
commit 161f673914
11 changed files with 230 additions and 42 deletions
+1
View File
@@ -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'
@@ -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
}
}
@@ -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<SendMessage.Params, Unit>() {
data class Params(val threadId: Long, val addresses: List<String>, val body: String)
data class Params(val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Bitmap> = listOf())
override fun buildObservable(params: Params): Flowable<Unit> {
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<String>, body: String) {
private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Bitmap>) {
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)
@@ -0,0 +1,79 @@
/*
* 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/>.
*/
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<Bitmap>() {
val attachmentDeleted: Subject<Bitmap> = 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)
}
}
@@ -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<ComposeViewModel>(), ComposeView {
override val copyTextIntent: Subject<Message> = PublishSubject.create()
override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
override val deleteMessageIntent: Subject<Message> = PublishSubject.create()
override val attachmentDeletedIntent: Subject<Bitmap> 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<ComposeViewModel>(), 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<ComposeViewModel>(), 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<ComposeViewModel>(), 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
@@ -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<Message>? = null,
val attachments: List<Bitmap> = ArrayList(),
val canSend: Boolean = false
)
@@ -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<ComposeState> {
val copyTextIntent: Subject<Message>
val forwardMessageIntent: Subject<Message>
val deleteMessageIntent: Subject<Message>
val attachmentDeletedIntent: Subject<Bitmap>
val textChangedIntent: Observable<CharSequence>
val attachIntent: Observable<Unit>
val sendIntent: Observable<Unit>
@@ -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<ComposeView, ComposeState>(
@Inject lateinit var deleteMessage: DeleteMessage
private var draft: String = ""
private val attachments: Subject<List<Bitmap>> = BehaviorSubject.createDefault(ArrayList())
private val contacts: List<Contact> by lazy { contactsRepo.getContacts() }
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>>
@@ -149,6 +155,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
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<ComposeView, ComposeState>(
})
.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) {
@@ -0,0 +1,55 @@
<?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/>.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:background="@color/backgroundLight">
<FrameLayout
android:id="@+id/thumbnailBounds"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/rounded_rectangle_2dp">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:src="@tools:sample/avatars" />
</FrameLayout>
<ImageView
android:id="@+id/detach"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="top|end"
android:layout_margin="2dp"
android:background="@drawable/circle"
android:src="@drawable/ic_cancel_black_24dp"
android:tint="@color/textSecondary"
tools:backgroundTint="@color/backgroundLight" />
</FrameLayout>
+45 -28
View File
@@ -76,36 +76,53 @@
android:layout_weight="1"
android:background="@drawable/rounded_rectangle_shadow"
android:gravity="center_vertical"
android:orientation="horizontal">
android:orientation="vertical">
<ImageView
android:id="@+id/attach"
android:layout_width="44dp"
android:layout_height="44dp"
android:background="@drawable/ripple"
android:padding="10dp"
android:src="@drawable/ic_insert_photo_black_24dp"
tools:tint="@color/textSecondary" />
<presentation.common.widget.Separator
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" />
<presentation.common.widget.QkEditText
android:id="@+id/message"
style="@style/TextPrimary"
android:layout_width="0dp"
<android.support.v7.widget.RecyclerView
android:id="@+id/attachments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/compose_hint"
android:inputType="textLongMessage|textCapSentences|textMultiLine"
android:minHeight="44dp"
android:padding="12dp"
app:textColorHint="tertiary" />
android:clipChildren="false"
android:clipToPadding="false"
android:paddingEnd="8dp"
android:paddingStart="8dp"
android:paddingTop="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/attach"
android:layout_width="44dp"
android:layout_height="44dp"
android:background="@drawable/ripple"
android:padding="10dp"
android:src="@drawable/ic_insert_photo_black_24dp"
tools:tint="@color/textSecondary" />
<presentation.common.widget.Separator
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp" />
<presentation.common.widget.QkEditText
android:id="@+id/message"
style="@style/TextPrimary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:hint="@string/compose_hint"
android:inputType="textLongMessage|textCapSentences|textMultiLine"
android:minHeight="44dp"
android:padding="12dp"
app:textColorHint="tertiary" />
</LinearLayout>
</LinearLayout>
-1
View File
@@ -40,7 +40,6 @@
<string name="main_syncing">Optimizing database, this might take a minute…</string>
<string name="compose_attach_unsupported">MMS support will be added shortly!</string>
<string name="compose_hint">Write a message…</string>
<string name="message_status_sending">Sending…</string>