forked from upstream-mirrors/quik
Add ability to attach and send photos
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user