From cc6dcafa88776b94e9547f8f7d53d45a8ec5aa5f Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 12 Apr 2020 19:30:03 +0200 Subject: [PATCH] add handling for multiple attachments in one mms --- .../smsmessenger/adapters/ThreadAdapter.kt | 67 +++++++++++-------- .../main/res/layout/item_attachment_image.xml | 7 ++ .../main/res/layout/item_received_message.xml | 8 +-- app/src/main/res/layout/item_sent_message.xml | 13 ++-- 4 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 app/src/main/res/layout/item_attachment_image.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt index 605d4b38..c88238a6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ThreadAdapter.kt @@ -30,6 +30,7 @@ import com.simplemobiletools.smsmessenger.models.Message import com.simplemobiletools.smsmessenger.models.ThreadDateTime import com.simplemobiletools.smsmessenger.models.ThreadError import com.simplemobiletools.smsmessenger.models.ThreadItem +import kotlinx.android.synthetic.main.item_attachment_image.view.* import kotlinx.android.synthetic.main.item_received_message.view.* import kotlinx.android.synthetic.main.item_thread_date_time.view.* @@ -187,41 +188,49 @@ class ThreadAdapter( thread_message_body.setTextColor(background.getContrastColor()) } + thread_mesage_attachments_holder.removeAllViews() if (message.attachment?.attachments?.isNotEmpty() == true) { - val attachment = message.attachment.attachments.first() - val type = attachment.type - if (type.startsWith("image/") || type.startsWith("video/")) { - val uri = attachment.uri - val options = RequestOptions() - .diskCacheStrategy(DiskCacheStrategy.NONE) - .transform(FitCenter(), RoundedCorners(roundedCornersRadius)) + message.attachment.attachments.forEach { + val attachment = it + val imageView = layoutInflater.inflate(R.layout.item_attachment_image, null) + thread_mesage_attachments_holder.addView(imageView) - Glide.with(context) - .load(uri) - .transition(DrawableTransitionOptions.withCrossFade()) - .apply(options) - .listener(object : RequestListener { - override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { - thread_message_play_outline.beGone() - return false + val type = attachment.type + if (type.startsWith("image/") || type.startsWith("video/")) { + val uri = attachment.uri + val options = RequestOptions() + .diskCacheStrategy(DiskCacheStrategy.NONE) + .transform(FitCenter(), RoundedCorners(roundedCornersRadius)) + + Glide.with(context) + .load(uri) + .transition(DrawableTransitionOptions.withCrossFade()) + .apply(options) + .listener(object : RequestListener { + override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { + thread_message_play_outline.beGone() + thread_mesage_attachments_holder.removeView(imageView) + return false + } + + override fun onResourceReady(dr: Drawable?, a: Any?, t: Target?, d: DataSource?, i: Boolean) = + false + + }) + .into(imageView.attachment_image) + + attachment_image.setOnClickListener { + Intent().apply { + action = Intent.ACTION_VIEW + setDataAndType(uri, type) + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + activity.startActivity(this) } - - override fun onResourceReady(dr: Drawable?, a: Any?, t: Target?, d: DataSource?, i: Boolean) = false - - }) - .into(thread_message_image) - - thread_message_image.setOnClickListener { - Intent().apply { - action = Intent.ACTION_VIEW - setDataAndType(uri, type) - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - activity.startActivity(this) } } - } - thread_message_play_outline.beVisibleIf(type.startsWith("video/")) + thread_message_play_outline.beVisibleIf(type.startsWith("video/")) + } } } } diff --git a/app/src/main/res/layout/item_attachment_image.xml b/app/src/main/res/layout/item_attachment_image.xml new file mode 100644 index 00000000..fc9d5f2b --- /dev/null +++ b/app/src/main/res/layout/item_attachment_image.xml @@ -0,0 +1,7 @@ + + diff --git a/app/src/main/res/layout/item_received_message.xml b/app/src/main/res/layout/item_received_message.xml index d4bfdf5a..a3b9e4d7 100644 --- a/app/src/main/res/layout/item_received_message.xml +++ b/app/src/main/res/layout/item_received_message.xml @@ -26,13 +26,11 @@ android:layout_marginEnd="@dimen/medium_margin" android:visibility="gone" /> - + android:orientation="vertical" /> - + android:orientation="vertical" /> @@ -38,7 +37,7 @@ android:id="@+id/thread_message_body" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_below="@+id/thread_message_image" + android:layout_below="@+id/thread_mesage_attachments_holder" android:layout_alignParentEnd="true" android:background="@drawable/item_sent_background" android:padding="@dimen/normal_margin"