Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 858ed347 authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

#1018 Use Glide for image loading

parent 51a920fb
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -25,8 +25,7 @@ import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
import io.noties.markwon.ext.tables.TableAwareMovementMethod;
import io.noties.markwon.ext.tables.TablePlugin;
import io.noties.markwon.ext.tasklist.TaskListPlugin;
import io.noties.markwon.image.DefaultDownScalingMediaDecoder;
import io.noties.markwon.image.ImagesPlugin;
import io.noties.markwon.image.glide.GlideImagesPlugin;
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.movement.MovementMethodPlugin;
@@ -39,6 +38,7 @@ import io.noties.prism4j.Prism4j;
import io.noties.prism4j.annotations.PrismBundle;
import it.niedermann.android.markdown.MarkdownEditor;
import it.niedermann.android.markdown.MarkdownUtil;
import it.niedermann.android.markdown.markwon.plugins.CustomGlideStore;
import it.niedermann.android.markdown.markwon.plugins.LinkClickInterceptorPlugin;
import it.niedermann.android.markdown.markwon.plugins.NextcloudMentionsPlugin;
import it.niedermann.android.markdown.markwon.plugins.SearchHighlightPlugin;
@@ -90,7 +90,7 @@ public class MarkwonMarkdownViewer extends AppCompatTextView implements Markdown
                .usePlugin(LinkifyPlugin.create(true))
                .usePlugin(MovementMethodPlugin.create(TableAwareMovementMethod.create()))
                .usePlugin(LinkClickInterceptorPlugin.create())
                .usePlugin(ImagesPlugin.create(plugin -> plugin.defaultMediaDecoder(DefaultDownScalingMediaDecoder.create(context.getResources().getDisplayMetrics().widthPixels, 0))))
                .usePlugin(GlideImagesPlugin.create(new CustomGlideStore(context)))
                .usePlugin(SoftBreakAddsNewLinePlugin.create())
                .usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme))
                .usePlugin(new ToggleableTaskListPlugin((toggledCheckboxPosition, newCheckedState) -> {
+33 −0
Original line number Diff line number Diff line
package it.niedermann.android.markdown.markwon.glide;

import androidx.annotation.Px;

import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy;

/**
 * @see <a href="https://github.com/noties/Markwon/issues/329#issuecomment-855220315">Source</a>
 */
public class DownsampleWithMaxWidth extends DownsampleStrategy {

    @Px
    private final int maxWidth;

    public DownsampleWithMaxWidth(@Px int maxWidth) {
        this.maxWidth = maxWidth;
    }

    @Override
    public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) {
        // do not scale down if fits requested dimension
        if (sourceWidth < maxWidth) {
            return 1F;
        }
        return (float) maxWidth / sourceWidth;
    }

    @Override
    public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight, int requestedWidth, int requestedHeight) {
        // go figure
        return SampleSizeRounding.MEMORY;
    }
}
 No newline at end of file
+48 −0
Original line number Diff line number Diff line
package it.niedermann.android.markdown.markwon.plugins;

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.target.Target;

import io.noties.markwon.image.AsyncDrawable;
import io.noties.markwon.image.glide.GlideImagesPlugin;
import it.niedermann.android.markdown.R;
import it.niedermann.android.markdown.markwon.glide.DownsampleWithMaxWidth;

/**
 * <ul>
 *      <li>Applies downscaling via {@link DownsampleWithMaxWidth} to avoid <a href="https://github.com/stefan-niedermann/nextcloud-notes/issues/1034">issues with large images</a></li>
 *      <li>Adds a placeholder while loading an image</li>
 *      <li>Adds a "broken image" placeholder in case of an error</li>
 *  </ul>
 */
public class CustomGlideStore implements GlideImagesPlugin.GlideStore {
    private final RequestManager requestManager;
    private final DownsampleWithMaxWidth downsampleWithMaxWidth;

    public CustomGlideStore(@NonNull Context context) {
        this.requestManager = Glide.with(context);
        downsampleWithMaxWidth = new DownsampleWithMaxWidth(context.getResources().getDisplayMetrics().widthPixels);
    }

    @NonNull
    @Override
    public RequestBuilder<Drawable> load(@NonNull AsyncDrawable drawable) {
        return requestManager
                .load(drawable.getDestination())
                .downsample(downsampleWithMaxWidth)
                .placeholder(R.drawable.ic_baseline_image_24)
                .error(R.drawable.ic_baseline_broken_image_24);
    }

    @Override
    public void cancel(@NonNull Target<?> target) {
        requestManager.clear(target);
    }
}
+5 −0
Original line number Diff line number Diff line
<vector android:height="24dp" android:tint="#757575"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M21,5v6.59l-3,-3.01 -4,4.01 -4,-4 -4,4 -3,-3.01L3,5c0,-1.1 0.9,-2 2,-2h14c1.1,0 2,0.9 2,2zM18,11.42l3,3.01L21,19c0,1.1 -0.9,2 -2,2L5,21c-1.1,0 -2,-0.9 -2,-2v-6.58l3,2.99 4,-4 4,4 4,-3.99z"/>
</vector>
+5 −0
Original line number Diff line number Diff line
<vector android:height="24dp" android:tint="#757575"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>
</vector>