Skip to content

Commit

Permalink
Further improved loading of images in dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Waboodoo committed Jul 12, 2022
1 parent 36f02a3 commit b9dcbf7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.activity.result.launch
Expand Down Expand Up @@ -727,9 +728,10 @@ class ExecuteActivity : BaseActivity(), Entrypoint {
val finalOutput = (output ?: response?.getContentAsString(context) ?: "")
.ifBlank { getString(R.string.message_blank_response) }
.let {
HTMLUtil.formatWithImageSupport(it, resources, textView::reloadImageSpans, destroyer)
HTMLUtil.formatWithImageSupport(it, context, textView::reloadImageSpans, destroyer)
}
textView.text = finalOutput
textView.movementMethod = LinkMovementMethod.getInstance()
builder.view(textView)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun TextView.reloadImageSpans() {
getSpans<ImageSpan>()
.map { it.drawable }
.forEach { drawable ->
if (drawable.intrinsicWidth > width && drawable.intrinsicWidth != 0) {
if (drawable.intrinsicWidth > width && drawable.intrinsicWidth > 0) {
val aspectRatio = drawable.intrinsicWidth / drawable.intrinsicHeight.toDouble()
val newImageWidth = width
val newImageHeight = (newImageWidth / aspectRatio).toInt()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.rmy.android.http_shortcuts.scripting.actions.types

import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import ch.rmy.android.http_shortcuts.R
import ch.rmy.android.http_shortcuts.databinding.DialogTextBinding
Expand Down Expand Up @@ -27,10 +28,11 @@ class DialogAction(private val message: String, private val title: String) : Bas
val textView = view.text
textView.text = HTMLUtil.formatWithImageSupport(
finalMessage,
executionContext.context.resources,
executionContext.context,
textView::reloadImageSpans,
destroyer,
)
textView.movementMethod = LinkMovementMethod.getInstance()
DialogBuilder(executionContext.context)
.title(title)
.view(view.root)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.rmy.android.http_shortcuts.utils

import android.content.res.Resources
import android.content.Context
import android.os.Build
import android.text.Html
import android.text.Spanned
Expand All @@ -12,11 +12,11 @@ object HTMLUtil {

fun formatWithImageSupport(
string: String,
resources: Resources,
context: Context,
onImageLoaded: () -> Unit,
destroyer: Destroyer,
): Spanned =
fromHTML(string.convertNewlines().normalize(), ImageGetter(resources, onImageLoaded, destroyer))
fromHTML(string.convertNewlines().normalize(), ImageGetter(context, onImageLoaded, destroyer))

private fun String.normalize(): String =
replace("<pre>", "<tt>")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ch.rmy.android.http_shortcuts.utils

import android.content.res.Resources
import android.graphics.Bitmap
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.text.Html.ImageGetter
import android.util.Base64
import androidx.appcompat.graphics.drawable.DrawableWrapper
import androidx.core.graphics.drawable.toDrawable
import ch.rmy.android.framework.extensions.attachTo
import ch.rmy.android.framework.extensions.dimen
import ch.rmy.android.framework.extensions.logException
import ch.rmy.android.framework.utils.Destroyer
import ch.rmy.android.http_shortcuts.R
Expand All @@ -20,17 +20,16 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class ImageGetter(
private val resources: Resources,
private val context: Context,
private val onImageLoaded: () -> Unit,
private val destroyer: Destroyer,
) : ImageGetter {
override fun getDrawable(source: String): Drawable {
val drawableWrapper = DrawableWrapper(
resources.getDrawable(R.drawable.image_placeholder, null),
)

val placeholderSize = dimen(context, R.dimen.html_image_placeholder_size)
val drawableWrapper = DrawableWrapper(context.resources.getDrawable(R.drawable.image_placeholder, null))
drawableWrapper.setBounds(0, 0, placeholderSize, placeholderSize)
Single.fromCallable {
val bitmap: Bitmap = if (source.isBase64EncodedImage()) {
if (source.isBase64EncodedImage()) {
val imageAsBytes = source.getBase64ImageData()
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.size)
} else {
Expand All @@ -40,7 +39,7 @@ class ImageGetter(
.memoryPolicy(MemoryPolicy.NO_CACHE)
.get()
}
bitmap.toDrawable(resources)
.toDrawable(context.resources)
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -51,7 +50,8 @@ class ImageGetter(
},
{ error ->
logException(error)
drawableWrapper.wrappedDrawable = resources.getDrawable(R.drawable.bitsies_cancel, null)
drawableWrapper.wrappedDrawable = context.resources.getDrawable(R.drawable.bitsies_cancel, null)
drawableWrapper.setBounds(0, 0, placeholderSize * 2, placeholderSize * 2)
onImageLoaded()
},
)
Expand Down
1 change: 1 addition & 0 deletions HTTPShortcuts/app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
<dimen name="grid_layout_shortcut_width_wide">180dp</dimen>
<dimen name="grid_layout_custom_icon_width">66dp</dimen>
<dimen name="grid_layout_builtin_icon_width">56dp</dimen>
<dimen name="html_image_placeholder_size">36dp</dimen>
</resources>
6 changes: 1 addition & 5 deletions HTTPShortcuts/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<resources>

<style name="DialogText" parent="@android:style/TextAppearance.Medium">
<item name="android:paddingStart">16dp</item>
<item name="android:paddingEnd">16dp</item>
<item name="android:textSize">16sp</item>
</style>
<style name="DialogText" parent="@style/MD_Dialog_Message" />

<style name="ListName" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">wrap_content</item>
Expand Down

0 comments on commit b9dcbf7

Please sign in to comment.