Compress images before sending

This commit is contained in:
Moez Bhatti
2018-01-15 13:36:36 -05:00
parent a834266bf3
commit 20487ea6fc
7 changed files with 38 additions and 18 deletions
+1
View File
@@ -69,6 +69,7 @@ dependencies {
compile 'com.uber.autodispose:autodispose-android-archcomponents-test-kotlin:0.5.1' compile 'com.uber.autodispose:autodispose-android-archcomponents-test-kotlin:0.5.1'
compile 'com.uber.autodispose:autodispose-android-kotlin:0.5.1' compile 'com.uber.autodispose:autodispose-android-kotlin:0.5.1'
compile 'com.uber.autodispose:autodispose-kotlin:0.5.1' compile 'com.uber.autodispose:autodispose-kotlin:0.5.1'
compile 'id.zelory:compressor:2.1.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile "io.reactivex.rxjava2:rxjava:2.1.4" compile "io.reactivex.rxjava2:rxjava:2.1.4"
compile 'io.reactivex.rxjava2:rxkotlin:2.1.0' compile 'io.reactivex.rxjava2:rxkotlin:2.1.0'
+22 -4
View File
@@ -21,16 +21,19 @@ package interactor
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.net.Uri
import android.telephony.SmsManager import android.telephony.SmsManager
import com.klinker.android.send_message.Message import com.klinker.android.send_message.Message
import com.klinker.android.send_message.Settings import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction import com.klinker.android.send_message.Transaction
import com.mlsdev.rximagepicker.RxImageConverters
import common.util.Preferences import common.util.Preferences
import data.repository.MessageRepository import data.repository.MessageRepository
import id.zelory.compressor.Compressor
import io.reactivex.Flowable import io.reactivex.Flowable
import presentation.receiver.MessageDeliveredReceiver import presentation.receiver.MessageDeliveredReceiver
import presentation.receiver.MessageSentReceiver import presentation.receiver.MessageSentReceiver
import java.io.File
import javax.inject.Inject import javax.inject.Inject
class SendMessage @Inject constructor( class SendMessage @Inject constructor(
@@ -39,7 +42,7 @@ class SendMessage @Inject constructor(
private val messageRepo: MessageRepository private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params, Unit>() { ) : Interactor<SendMessage.Params, Unit>() {
data class Params(val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Bitmap> = listOf()) data class Params(val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Uri> = listOf())
override fun buildObservable(params: Params): Flowable<Unit> { override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(Unit) return Flowable.just(Unit)
@@ -73,12 +76,27 @@ class SendMessage @Inject constructor(
smsManager.sendMultipartTextMessage(address, null, parts, ArrayList(sentIntents), ArrayList(deliveredIntents)) smsManager.sendMultipartTextMessage(address, null, parts, ArrayList(sentIntents), ArrayList(deliveredIntents))
} }
private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Bitmap>) { private fun sendMms(threadId: Long, addresses: List<String>, body: String, attachments: List<Uri>) {
val settings = Settings().apply { val settings = Settings().apply {
} }
val message = Message(body, addresses.toTypedArray()) val message = Message(body, addresses.toTypedArray())
attachments.forEach { message.addImage(it) } attachments
.map { uri ->
val file = File(context.cacheDir, "${System.currentTimeMillis()}.jpeg")
RxImageConverters.uriToFile(context, uri, file).blockingFirst()
}
.map { file ->
Compressor(context)
.setMaxHeight(1920)
.setMaxHeight(1920)
.setQuality(60)
.compressToBitmap(file)
}
.forEach { bitmap ->
message.addImage(bitmap)
}
val transaction = Transaction(context, settings) val transaction = Transaction(context, settings)
transaction.sendNewMessage(message, threadId) transaction.sendNewMessage(message, threadId)
@@ -19,10 +19,11 @@
package presentation.feature.compose package presentation.feature.compose
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.net.Uri
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.jakewharton.rxbinding2.view.clicks import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R import com.moez.QKSMS.R
import common.util.Colors import common.util.Colors
@@ -40,9 +41,9 @@ import javax.inject.Inject
class AttachmentAdapter @Inject constructor( class AttachmentAdapter @Inject constructor(
private val context: Context, private val context: Context,
private val colors: Colors private val colors: Colors
) : QkAdapter<Bitmap>() { ) : QkAdapter<Uri>() {
val attachmentDeleted: Subject<Bitmap> = PublishSubject.create() val attachmentDeleted: Subject<Uri> = PublishSubject.create()
private val disposables = CompositeDisposable() private val disposables = CompositeDisposable()
@@ -66,14 +67,14 @@ class AttachmentAdapter @Inject constructor(
} }
override fun onBindViewHolder(holder: QkViewHolder, position: Int) { override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val bitmap = getItem(position) val uri = getItem(position)
val view = holder.itemView val view = holder.itemView
view.clicks().subscribe { view.clicks().subscribe {
attachmentDeleted.onNext(bitmap) attachmentDeleted.onNext(uri)
} }
view.thumbnail.setImageBitmap(bitmap) Glide.with(context).load(uri).into(view.thumbnail)
} }
} }
@@ -22,6 +22,7 @@ import android.app.AlertDialog
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.PorterDuff import android.graphics.PorterDuff
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
@@ -64,7 +65,7 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
override val copyTextIntent: Subject<Message> = PublishSubject.create() override val copyTextIntent: Subject<Message> = PublishSubject.create()
override val forwardMessageIntent: Subject<Message> = PublishSubject.create() override val forwardMessageIntent: Subject<Message> = PublishSubject.create()
override val deleteMessageIntent: Subject<Message> = PublishSubject.create() override val deleteMessageIntent: Subject<Message> = PublishSubject.create()
override val attachmentDeletedIntent: Subject<Bitmap> by lazy { attachmentAdapter.attachmentDeleted } override val attachmentDeletedIntent: Subject<Uri> by lazy { attachmentAdapter.attachmentDeleted }
override val textChangedIntent by lazy { message.textChanges() } override val textChangedIntent by lazy { message.textChanges() }
override val attachIntent by lazy { attach.clicks() } override val attachIntent by lazy { attach.clicks() }
override val sendIntent by lazy { send.clicks() } override val sendIntent by lazy { send.clicks() }
@@ -18,7 +18,7 @@
*/ */
package presentation.feature.compose package presentation.feature.compose
import android.graphics.Bitmap import android.net.Uri
import data.model.Contact import data.model.Contact
import data.model.Message import data.model.Message
import io.realm.RealmResults import io.realm.RealmResults
@@ -32,6 +32,6 @@ data class ComposeState(
val title: String = "", val title: String = "",
val archived: Boolean = false, val archived: Boolean = false,
val messages: RealmResults<Message>? = null, val messages: RealmResults<Message>? = null,
val attachments: List<Bitmap> = ArrayList(), val attachments: List<Uri> = ArrayList(),
val canSend: Boolean = false val canSend: Boolean = false
) )
@@ -19,6 +19,7 @@
package presentation.feature.compose package presentation.feature.compose
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri
import data.model.Contact import data.model.Contact
import data.model.Message import data.model.Message
import io.reactivex.Observable import io.reactivex.Observable
@@ -38,7 +39,7 @@ interface ComposeView : QkView<ComposeState> {
val copyTextIntent: Subject<Message> val copyTextIntent: Subject<Message>
val forwardMessageIntent: Subject<Message> val forwardMessageIntent: Subject<Message>
val deleteMessageIntent: Subject<Message> val deleteMessageIntent: Subject<Message>
val attachmentDeletedIntent: Subject<Bitmap> val attachmentDeletedIntent: Subject<Uri>
val textChangedIntent: Observable<CharSequence> val textChangedIntent: Observable<CharSequence>
val attachIntent: Observable<Unit> val attachIntent: Observable<Unit>
val sendIntent: Observable<Unit> val sendIntent: Observable<Unit>
@@ -20,8 +20,7 @@ package presentation.feature.compose
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.net.Uri
import com.mlsdev.rximagepicker.RxImageConverters
import com.mlsdev.rximagepicker.RxImagePicker import com.mlsdev.rximagepicker.RxImagePicker
import com.mlsdev.rximagepicker.Sources import com.mlsdev.rximagepicker.Sources
import com.moez.QKSMS.R import com.moez.QKSMS.R
@@ -68,7 +67,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
@Inject lateinit var deleteMessage: DeleteMessage @Inject lateinit var deleteMessage: DeleteMessage
private var draft: String = "" private var draft: String = ""
private val attachments: Subject<List<Bitmap>> = BehaviorSubject.createDefault(ArrayList()) private val attachments: Subject<List<Uri>> = BehaviorSubject.createDefault(ArrayList())
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() } private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create() private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>> private val selectedContacts: Observable<List<Contact>>
@@ -252,7 +251,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
// Attach a photo // Attach a photo
view.attachIntent view.attachIntent
.flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) } .flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) }
.flatMap { uri -> RxImageConverters.uriToBitmap(context, uri) }
.withLatestFrom(attachments, { attachment, attachments -> attachments + attachment }) .withLatestFrom(attachments, { attachment, attachments -> attachments + attachment })
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { attachments.onNext(it) } .subscribe { attachments.onNext(it) }