Better image loading in Markdown

This commit potentially solves issues with slow scrolling on posts and some crashes due to too large files
This commit is contained in:
Balazs Toldi 2024-07-19 20:55:08 +02:00
parent 6708b72fd8
commit 5fdf1208a7
12 changed files with 48 additions and 19 deletions

View File

@ -205,7 +205,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
}
};
Markwon postBodyMarkwon = MarkdownUtils.createFullRedditMarkwon(this,
miscPlugin, parentTextColor, parentSpoilerBackgroundColor, null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, parentTextColor, parentSpoilerBackgroundColor, mGlide, null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter();
binding.commentContentMarkdownView.setLayoutManager(new LinearLayoutManagerBugFixed(this));
binding.commentContentMarkdownView.setAdapter(markwonAdapter);

View File

@ -18,6 +18,7 @@ import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
@ -139,7 +140,7 @@ public class FullMarkdownActivity extends BaseActivity {
}
};
Markwon markwon = MarkdownUtils.createFullRedditMarkwon(this,
miscPlugin, markdownColor, spoilerBackgroundColor, null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, markdownColor, spoilerBackgroundColor, Glide.with(getApplication()), null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
MarkwonAdapter markwonAdapter = MarkdownUtils.createTablesAdapter();
LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() {

View File

@ -23,6 +23,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.snackbar.Snackbar;
@ -190,7 +191,7 @@ public class InstanceInfoActivity extends BaseActivity {
};
mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(this,
miscPlugin, markdownColor, postSpoilerBackgroundColor, null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, markdownColor, postSpoilerBackgroundColor, Glide.with(getApplication()), null, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
mMarkwonAdapter = MarkdownUtils.createTablesAdapter();
mContentMarkdownView.setAdapter(mMarkwonAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

View File

@ -176,7 +176,7 @@ public class WikiActivity extends BaseActivity {
return true;
};
markwon = MarkdownUtils.createFullRedditMarkwon(this,
miscPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, markdownColor, spoilerBackgroundColor, mGlide, onLinkLongClickListener, mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
markwonAdapter = MarkdownUtils.createTablesAdapter();
LinearLayoutManagerBugFixed linearLayoutManager = new SwipeLockLinearLayoutManager(this, new SwipeLockInterface() {

View File

@ -24,6 +24,7 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.google.android.material.button.MaterialButton;
import java.util.Locale;
@ -170,7 +171,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
return true;
};
mMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity,
miscPlugin, mCommentColor, commentSpoilerBackgroundColor, onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, mCommentColor, commentSpoilerBackgroundColor, Glide.with(mActivity.getApplication()), onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
recycledViewPool = new RecyclerView.RecycledViewPool();
}

View File

@ -221,7 +221,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
return true;
};
mCommentMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity,
miscPlugin, mCommentTextColor, commentSpoilerBackgroundColor, onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, mCommentTextColor, commentSpoilerBackgroundColor, mGlide, onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
recycledViewPool = new RecyclerView.RecycledViewPool();
mAccessToken = accessToken;
mAccountQualifiedName = accountName;

View File

@ -295,7 +295,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
return true;
};
mPostDetailMarkwon = MarkdownUtils.createFullRedditMarkwon(mActivity,
miscPlugin, markdownColor, postSpoilerBackgroundColor, onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
miscPlugin, markdownColor, postSpoilerBackgroundColor, mGlide, onLinkLongClickListener, sharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_IMAGE_PREVIEW, false));
mMarkwonAdapter = MarkdownUtils.createTablesAdapter();
mSeparatePostAndComments = separatePostAndComments;
@ -677,7 +677,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
((PostDetailBaseVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE);
Post.Preview preview = getSuitablePreview(mPost.getPreviews());
if (preview != null) {
mGlide.load(preview.getPreviewUrl()).centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget<Drawable>() {
mGlide.load(preview.getPreviewUrl()).centerInside().thumbnail(0.1f).downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
int width = resource.getIntrinsicWidth();
@ -896,7 +896,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
});
if (blurImage) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(new CustomTarget<Drawable>() {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).thumbnail(0.1f).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
int width = resource.getIntrinsicWidth();
@ -916,7 +916,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
}
});
} else {
imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).into(new CustomTarget<Drawable>() {
imageRequestBuilder.centerInside().downsample(mSaveMemoryCenterInsideDownsampleStrategy).thumbnail(0.1f).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
int width = resource.getIntrinsicWidth();

View File

@ -18,17 +18,18 @@ import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import java.util.ArrayList;
import io.noties.markwon.Markwon;
import jp.wasabeef.glide.transformations.BlurTransformation;
import eu.toldi.infinityforlemmy.SaveMemoryCenterInisdeDownsampleStrategy;
import eu.toldi.infinityforlemmy.databinding.ItemGalleryImageInPostFeedBinding;
import eu.toldi.infinityforlemmy.post.Post;
import io.noties.markwon.Markwon;
import jp.wasabeef.glide.transformations.BlurTransformation;
public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapter<PostGalleryTypeImageRecyclerViewAdapter.ImageViewHolder> {
private RequestManager glide;
@ -128,7 +129,7 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte
return;
}
RequestBuilder<Drawable> imageRequestBuilder = glide.load(galleryImages.get(index).url).listener(new RequestListener<>() {
RequestBuilder<Drawable> imageRequestBuilder = glide.load(galleryImages.get(index).url).override(1024).listener(new RequestListener<>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder.binding.progressBarItemGalleryImageInPostFeed.setVisibility(View.GONE);
@ -144,10 +145,10 @@ public class PostGalleryTypeImageRecyclerViewAdapter extends RecyclerView.Adapte
}
});
if (blurImage) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10)))
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).thumbnail(0.1f).downsample(DownsampleStrategy.CENTER_INSIDE).override(1024)
.into(holder.binding.imageViewItemGalleryImageInPostFeed);
} else {
imageRequestBuilder.centerInside().downsample(saveMemoryCenterInisdeDownsampleStrategy).into(holder.binding.imageViewItemGalleryImageInPostFeed);
imageRequestBuilder.centerInside().downsample(saveMemoryCenterInisdeDownsampleStrategy).override(1024).thumbnail(0.1f).downsample(DownsampleStrategy.CENTER_INSIDE).into(holder.binding.imageViewItemGalleryImageInPostFeed);
}
}

View File

@ -12,6 +12,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import butterknife.BindView;
@ -84,7 +86,7 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
return true;
};
markwon = MarkdownUtils.createFullRedditMarkwon(activity,
miscPlugin, mPrimaryTextColor, spoilerBackgroundColor, onLinkLongClickListener, mDisableImagePreview);
miscPlugin, mPrimaryTextColor, spoilerBackgroundColor, Glide.with(activity.getApplication()), onLinkLongClickListener, mDisableImagePreview);
}
@NonNull

View File

@ -21,6 +21,7 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.bumptech.glide.Glide;
import com.evernote.android.state.State;
import com.google.android.material.card.MaterialCardView;
@ -232,7 +233,7 @@ public class SidebarFragment extends Fragment {
return true;
};
markwon = MarkdownUtils.createFullRedditMarkwon(activity,
miscPlugin, markdownColor, spoilerBackgroundColor, onLinkLongClickListener, mDisableImagePreview);
miscPlugin, markdownColor, spoilerBackgroundColor, Glide.with(activity.getApplication()), onLinkLongClickListener, mDisableImagePreview);
markwonAdapter = MarkdownUtils.createTablesAdapter();
linearLayoutManager = new LinearLayoutManagerBugFixed(activity);

View File

@ -7,6 +7,8 @@ import android.text.util.Linkify;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.RequestManager;
import org.commonmark.ext.gfm.tables.TableBlock;
import java.util.regex.Pattern;
@ -37,7 +39,7 @@ public class MarkdownUtils {
public static Markwon createFullRedditMarkwon(@NonNull Activity context,
@NonNull MarkwonPlugin miscPlugin,
int markdownColor,
int spoilerBackgroundColor,
int spoilerBackgroundColor, RequestManager mGlide,
@Nullable BetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener, boolean dataSaverEnabled) {
Markwon result;
if (dataSaverEnabled) {
@ -58,7 +60,7 @@ public class MarkdownUtils {
.build();
} else {
result = Markwon.builder(context)
.usePlugin(GlideImagesPlugin.create(context.getApplicationContext()))
.usePlugin(GlideImagesPlugin.create(new GlideMarkdownLoader(mGlide)))
.usePlugin(MarkwonInlineParserPlugin.create(plugin -> {
plugin.excludeInlineProcessor(HtmlInlineProcessor.class);
}))

View File

@ -0,0 +1,20 @@
package eu.toldi.infinityforlemmy.markdown
import android.graphics.drawable.Drawable
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.request.target.Target
import io.noties.markwon.image.AsyncDrawable
import io.noties.markwon.image.glide.GlideImagesPlugin.GlideStore
class GlideMarkdownLoader(val requestManager: RequestManager) : GlideStore {
override fun load(drawable: AsyncDrawable): RequestBuilder<Drawable> {
return requestManager.load(drawable.destination).override(2048)
.downsample(DownsampleStrategy.CENTER_INSIDE)
}
override fun cancel(target: Target<*>) {
requestManager.clear(target);
}
}