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
+22 -4
View File
@@ -21,16 +21,19 @@ package interactor
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.telephony.SmsManager
import com.klinker.android.send_message.Message
import com.klinker.android.send_message.Settings
import com.klinker.android.send_message.Transaction
import com.mlsdev.rximagepicker.RxImageConverters
import common.util.Preferences
import data.repository.MessageRepository
import id.zelory.compressor.Compressor
import io.reactivex.Flowable
import presentation.receiver.MessageDeliveredReceiver
import presentation.receiver.MessageSentReceiver
import java.io.File
import javax.inject.Inject
class SendMessage @Inject constructor(
@@ -39,7 +42,7 @@ 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, 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> {
return Flowable.just(Unit)
@@ -73,12 +76,27 @@ class SendMessage @Inject constructor(
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 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)
transaction.sendNewMessage(message, threadId)
@@ -19,10 +19,11 @@
package presentation.feature.compose
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.jakewharton.rxbinding2.view.clicks
import com.moez.QKSMS.R
import common.util.Colors
@@ -40,9 +41,9 @@ import javax.inject.Inject
class AttachmentAdapter @Inject constructor(
private val context: Context,
private val colors: Colors
) : QkAdapter<Bitmap>() {
) : QkAdapter<Uri>() {
val attachmentDeleted: Subject<Bitmap> = PublishSubject.create()
val attachmentDeleted: Subject<Uri> = PublishSubject.create()
private val disposables = CompositeDisposable()
@@ -66,14 +67,14 @@ class AttachmentAdapter @Inject constructor(
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val bitmap = getItem(position)
val uri = getItem(position)
val view = holder.itemView
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.graphics.Bitmap
import android.graphics.PorterDuff
import android.net.Uri
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
@@ -64,7 +65,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 attachmentDeletedIntent: Subject<Uri> 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() }
@@ -18,7 +18,7 @@
*/
package presentation.feature.compose
import android.graphics.Bitmap
import android.net.Uri
import data.model.Contact
import data.model.Message
import io.realm.RealmResults
@@ -32,6 +32,6 @@ data class ComposeState(
val title: String = "",
val archived: Boolean = false,
val messages: RealmResults<Message>? = null,
val attachments: List<Bitmap> = ArrayList(),
val attachments: List<Uri> = ArrayList(),
val canSend: Boolean = false
)
@@ -19,6 +19,7 @@
package presentation.feature.compose
import android.graphics.Bitmap
import android.net.Uri
import data.model.Contact
import data.model.Message
import io.reactivex.Observable
@@ -38,7 +39,7 @@ interface ComposeView : QkView<ComposeState> {
val copyTextIntent: Subject<Message>
val forwardMessageIntent: Subject<Message>
val deleteMessageIntent: Subject<Message>
val attachmentDeletedIntent: Subject<Bitmap>
val attachmentDeletedIntent: Subject<Uri>
val textChangedIntent: Observable<CharSequence>
val attachIntent: Observable<Unit>
val sendIntent: Observable<Unit>
@@ -20,8 +20,7 @@ package presentation.feature.compose
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import com.mlsdev.rximagepicker.RxImageConverters
import android.net.Uri
import com.mlsdev.rximagepicker.RxImagePicker
import com.mlsdev.rximagepicker.Sources
import com.moez.QKSMS.R
@@ -68,7 +67,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 attachments: Subject<List<Uri>> = BehaviorSubject.createDefault(ArrayList())
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>>
@@ -252,7 +251,6 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
// Attach a photo
view.attachIntent
.flatMap { RxImagePicker.with(context).requestImage(Sources.GALLERY) }
.flatMap { uri -> RxImageConverters.uriToBitmap(context, uri) }
.withLatestFrom(attachments, { attachment, attachments -> attachments + attachment })
.autoDisposable(view.scope())
.subscribe { attachments.onNext(it) }