From 4ab8f037f2b3da61a11b46f547615be3d2aab77f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 30 Oct 2021 21:20:48 +0800 Subject: Start migrating to Material Design 3. Try optimizing image loading in PostRecyclerViewAdapter. --- .../infinityforreddit/activities/BaseActivity.java | 26 +- .../infinityforreddit/activities/MainActivity.java | 10 +- .../activities/ViewSubredditDetailActivity.java | 20 +- .../activities/ViewUserDetailActivity.java | 15 +- .../adapters/PostDetailRecyclerViewAdapter.java | 6 +- .../adapters/PostRecyclerViewAdapter.java | 308 ++++++++++++--------- .../infinityforreddit/post/Post.java | 2 +- 7 files changed, 231 insertions(+), 156 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java index 12e565c4..cea711ae 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java @@ -1,5 +1,10 @@ package ml.docilealligator.infinityforreddit.activities; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; +import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; + import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.content.res.ColorStateList; @@ -44,11 +49,6 @@ import ml.docilealligator.infinityforreddit.font.TitleFontStyle; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO; -import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES; - public abstract class BaseActivity extends AppCompatActivity { private boolean immersiveInterface; private boolean changeStatusBarIconColor; @@ -275,6 +275,15 @@ public abstract class BaseActivity extends AppCompatActivity { return 0; } + public int getStatusBarHeight() { + int result = 0; + int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (resourceId > 0) { + result = getResources().getDimensionPixelSize(resourceId); + } + return result; + } + protected void setTransparentStatusBarAfterToolbarCollapsed() { this.transparentStatusBarAfterToolbarCollapsed = true; } @@ -288,7 +297,14 @@ public abstract class BaseActivity extends AppCompatActivity { } protected void applyAppBarLayoutAndToolbarTheme(AppBarLayout appBarLayout, Toolbar toolbar) { + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar, true); + } + + protected void applyAppBarLayoutAndToolbarTheme(AppBarLayout appBarLayout, Toolbar toolbar, boolean setToolbarBackgroundColor) { appBarLayout.setBackgroundColor(customThemeWrapper.getColorPrimary()); + if (setToolbarBackgroundColor) { + toolbar.setBackgroundColor(customThemeWrapper.getColorPrimary()); + } toolbar.setTitleTextColor(customThemeWrapper.getToolbarPrimaryTextAndIconColor()); toolbar.setSubtitleTextColor(customThemeWrapper.getToolbarSecondaryTextColor()); if (toolbar.getNavigationIcon() != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 0697ec04..6b5231e2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -31,7 +31,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatDelegate; -import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.app.ActivityCompat; import androidx.core.view.GravityCompat; @@ -52,6 +51,7 @@ import androidx.work.WorkManager; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.google.android.material.appbar.MaterialToolbar; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -160,7 +160,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @BindView(R.id.collapsing_toolbar_layout_main_activity) CollapsingToolbarLayout collapsingToolbarLayout; @BindView(R.id.toolbar) - Toolbar toolbar; + MaterialToolbar toolbar; @BindView(R.id.nav_drawer_recycler_view_main_activity) RecyclerView navDrawerRecyclerView; @BindView(R.id.tab_layout_main_activity) @@ -269,9 +269,10 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb drawer.setStatusBarBackgroundColor(Color.TRANSPARENT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { drawer.setFitsSystemWindows(false); - drawer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + getWindow().setDecorFitsSystemWindows(false); + /*drawer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);*/ } else { window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } @@ -356,6 +357,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb option4BottomAppBar.setColorFilter(bottomAppBarIconColor, android.graphics.PorterDuff.Mode.SRC_IN); navigationView.setBackgroundColor(backgroundColor); applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + collapsingToolbarLayout.setContentScrimColor(mCustomThemeWrapper.getColorPrimary()); applyTabLayoutTheme(tabLayout); bottomAppBar.setBackgroundTint(ColorStateList.valueOf(mCustomThemeWrapper.getBottomAppBarBackgroundColor())); applyFABTheme(fab); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 41fd582d..a94ecbcf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -16,6 +16,7 @@ import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; @@ -27,7 +28,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; @@ -42,6 +42,7 @@ import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.google.android.material.appbar.MaterialToolbar; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.chip.Chip; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -149,7 +150,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @BindView(R.id.toolbar_linear_layout_view_subreddit_detail_activity) LinearLayout linearLayout; @BindView(R.id.toolbar) - Toolbar toolbar; + MaterialToolbar toolbar; @BindView(R.id.tab_layout_view_subreddit_detail_activity) TabLayout tabLayout; @BindView(R.id.banner_image_view_view_subreddit_detail_activity) @@ -268,9 +269,10 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp if (isImmersiveInterface()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); - coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + /*coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);*/ + getWindow().setDecorFitsSystemWindows(false); } else { window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } @@ -515,7 +517,14 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp protected void applyCustomTheme() { coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); collapsingToolbarLayout.setContentScrimColor(mCustomThemeWrapper.getColorPrimary()); - applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + appBarLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + collapsingToolbarLayout.setScrimVisibleHeightTrigger(toolbar.getHeight() + tabLayout.getHeight() + getStatusBarHeight() * 2); + appBarLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar, false); expandedTabTextColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTextColor(); expandedTabIndicatorColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTabIndicator(); expandedTabBackgroundColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTabBackground(); @@ -793,6 +802,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp option1BottomAppBar.setOnClickListener(view -> { bottomAppBarOptionAction(option1); + //Toast.makeText(this, "s " + collapsingToolbarLayout.getScrimVisibleHeightTrigger(), Toast.LENGTH_SHORT).show(); }); option2BottomAppBar.setOnClickListener(view -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 4884ca40..51a96234 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -6,7 +6,6 @@ import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.content.res.Resources; import android.net.Uri; -import android.opengl.Visibility; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; @@ -18,6 +17,7 @@ import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.view.inputmethod.EditorInfo; @@ -29,7 +29,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; @@ -44,6 +43,7 @@ import com.bumptech.glide.RequestManager; import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.google.android.material.appbar.MaterialToolbar; import com.google.android.material.bottomappbar.BottomAppBar; import com.google.android.material.chip.Chip; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -141,7 +141,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele @BindView(R.id.appbar_layout_view_user_detail) AppBarLayout appBarLayout; @BindView(R.id.toolbar_view_user_detail_activity) - Toolbar toolbar; + MaterialToolbar toolbar; @BindView(R.id.toolbar_linear_layout_view_user_detail_activity) LinearLayout linearLayout; @BindView(R.id.tab_layout_view_user_detail_activity) @@ -564,7 +564,14 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele protected void applyCustomTheme() { coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); collapsingToolbarLayout.setContentScrimColor(mCustomThemeWrapper.getColorPrimary()); - applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + appBarLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + collapsingToolbarLayout.setScrimVisibleHeightTrigger(toolbar.getHeight() + tabLayout.getHeight() + getStatusBarHeight() * 2); + appBarLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + }); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar, false); expandedTabTextColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTextColor(); expandedTabIndicatorColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTabIndicator(); expandedTabBackgroundColor = mCustomThemeWrapper.getTabLayoutWithExpandedCollapsingToolbarTabBackground(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index d083a0bc..789ce798 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -680,7 +680,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter previews = post.getPreviews(); - if (previews != null && !previews.isEmpty()) { + if (post.getPreviews() != null && !post.getPreviews().isEmpty()) { if (post.getPostType() != Post.GIF_TYPE && post.getPostType() != Post.VIDEO_TYPE) { ((PostCompactBaseViewHolder) holder).imageView.setVisibility(View.VISIBLE); ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.VISIBLE); } - loadImage(holder, post, previews.get(0)); + loadImage(holder); } } @@ -1269,6 +1272,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostWithPreviewTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostWithPreviewTypeViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostWithPreviewTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post, preview); - }); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostWithPreviewTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostWithPreviewTypeViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; + Post post = ((PostWithPreviewTypeViewHolder) holder).post; + Post.Preview preview = ((PostWithPreviewTypeViewHolder) holder).preview; + if (preview != null) { + String url; + boolean blurImage = (post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler; + if (post.getPostType() == Post.GIF_TYPE && mAutoplay && !blurImage) { + url = post.getUrl(); + } else { + url = preview.getPreviewUrl(); } - }); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(((PostWithPreviewTypeViewHolder) holder).glideRequestListener); - if (blurImage) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostWithPreviewTypeViewHolder) holder).imageView); - } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostWithPreviewTypeViewHolder) holder).imageView); + if (blurImage) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostWithPreviewTypeViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostWithPreviewTypeViewHolder) holder).imageView); + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostWithPreviewTypeViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostWithPreviewTypeViewHolder) holder).imageView); + } } } } else if (holder instanceof PostCompactBaseViewHolder) { + Post post = ((PostCompactBaseViewHolder) holder).post; String postCompactThumbnailPreviewUrl; ArrayList previews = post.getPreviews(); if (previews != null && !previews.isEmpty()) { if (previews.size() >= 2) { postCompactThumbnailPreviewUrl = previews.get(1).getPreviewUrl(); } else { - postCompactThumbnailPreviewUrl = preview.getPreviewUrl(); + postCompactThumbnailPreviewUrl = previews.get(0).getPreviewUrl(); } RequestBuilder imageRequestBuilder = mGlide.load(postCompactThumbnailPreviewUrl) - .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostCompactBaseViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; - } - }); + .error(R.drawable.ic_error_outline_black_24dp).listener(((PostCompactBaseViewHolder) holder).requestListener); if ((post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit())) || post.isSpoiler() && mNeedBlurSpoiler) { imageRequestBuilder .transform(new BlurTransformation(50, 2)).into(((PostCompactBaseViewHolder) holder).imageView); @@ -1537,81 +1520,51 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostGalleryViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostGalleryViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostGalleryViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostGalleryViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostGalleryViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post, preview); - }); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostGalleryViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostGalleryViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; + Post post = ((PostGalleryViewHolder) holder).post; + Post.Preview preview = ((PostGalleryViewHolder) holder).preview; + if (preview != null) { + String url; + boolean blurImage = (post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler; + if (post.getPostType() == Post.GIF_TYPE && mAutoplay && !blurImage) { + url = post.getUrl(); + } else { + url = preview.getPreviewUrl(); } - }); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(((PostGalleryViewHolder) holder).requestListener); - if (blurImage) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostGalleryViewHolder) holder).imageView); - } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostGalleryViewHolder) holder).imageView); + if (blurImage) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostGalleryViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostGalleryViewHolder) holder).imageView); + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostGalleryViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostGalleryViewHolder) holder).imageView); + } } } } else if (holder instanceof PostCard2WithPreviewViewHolder) { - String url; - boolean blurImage = (post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler; - if (post.getPostType() == Post.GIF_TYPE && mAutoplay && !blurImage) { - url = post.getUrl(); - } else { - url = preview.getPreviewUrl(); - } - RequestBuilder imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - loadImage(holder, post, preview); - }); - return false; - } - - @Override - public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - ((PostCard2WithPreviewViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostCard2WithPreviewViewHolder) holder).progressBar.setVisibility(View.GONE); - return false; + Post post = ((PostCard2WithPreviewViewHolder) holder).post; + Post.Preview preview = ((PostCard2WithPreviewViewHolder) holder).preview; + if (preview != null) { + String url; + boolean blurImage = (post.isNSFW() && mNeedBlurNsfw && !(mDoNotBlurNsfwInNsfwSubreddits && mFragment != null && mFragment.getIsNsfwSubreddit()) && !(post.getPostType() == Post.GIF_TYPE && mAutoplayNsfwVideos)) || post.isSpoiler() && mNeedBlurSpoiler; + if (post.getPostType() == Post.GIF_TYPE && mAutoplay && !blurImage) { + url = post.getUrl(); + } else { + url = preview.getPreviewUrl(); } - }); + RequestBuilder imageRequestBuilder = mGlide.load(url).listener(((PostCard2WithPreviewViewHolder) holder).requestListener); - if (blurImage) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostCard2WithPreviewViewHolder) holder).imageView); - } else { - if (mImageViewWidth > preview.getPreviewWidth()) { - imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostCard2WithPreviewViewHolder) holder).imageView); + if (blurImage) { + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostCard2WithPreviewViewHolder) holder).imageView); } else { - imageRequestBuilder.into(((PostCard2WithPreviewViewHolder) holder).imageView); + if (mImageViewWidth > preview.getPreviewWidth()) { + imageRequestBuilder.override(preview.getPreviewWidth(), preview.getPreviewHeight()).into(((PostCard2WithPreviewViewHolder) holder).imageView); + } else { + imageRequestBuilder.into(((PostCard2WithPreviewViewHolder) holder).imageView); + } } } } @@ -1801,7 +1754,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter glideRequestListener; PostWithPreviewTypeViewHolder(View itemView) { super(itemView); @@ -2943,9 +2897,31 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + progressBar.setVisibility(View.VISIBLE); + errorTextView.setVisibility(View.GONE); + loadImage(this); + }); + noPreviewLinkImageView.setOnClickListener(view -> { imageView.performClick(); }); + + glideRequestListener = new RequestListener<>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorTextView.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + errorTextView.setVisibility(View.GONE); + progressBar.setVisibility(View.GONE); + return false; + } + }; } } @@ -3056,6 +3032,8 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter requestListener; + Post post; int currentPosition; @@ -3552,6 +3530,20 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + return false; + } + }; } void markPostRead(Post post, boolean changePostItemColor) { @@ -3736,6 +3728,9 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter requestListener; + Post post; + Post.Preview preview; int currentPosition; @@ -3788,9 +3783,31 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + progressBar.setVisibility(View.VISIBLE); + errorRelativeLayout.setVisibility(View.GONE); + loadImage(this); + }); + noPreviewImageView.setOnClickListener(view -> { itemView.performClick(); }); + + requestListener = new RequestListener<>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorRelativeLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + errorRelativeLayout.setVisibility(View.GONE); + progressBar.setVisibility(View.GONE); + return false; + } + }; } void markPostRead(Post post, boolean changePostItemColor) { @@ -3931,14 +3948,14 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter requestListener; PostCard2WithPreviewViewHolder(@NonNull View itemView) { super(itemView); @@ -4170,9 +4188,31 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + progressBar.setVisibility(View.VISIBLE); + errorRelativeLayout.setVisibility(View.GONE); + loadImage(this); + }); + noPreviewImageView.setOnClickListener(view -> { imageView.performClick(); }); + + requestListener = new RequestListener<>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + progressBar.setVisibility(View.GONE); + errorRelativeLayout.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + errorRelativeLayout.setVisibility(View.GONE); + progressBar.setVisibility(View.GONE); + return false; + } + }; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java index d814fb86..6a0173c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/Post.java @@ -337,7 +337,7 @@ public class Post implements Parcelable { this.isRedgifs = isRedgifs; } - public boolean isLoadGfyOrRedgifsVideoSuccess() { + public boolean isLoadGfycatOrRedgifsVideoSuccess() { return loadGfyOrRedgifsVideoSuccess; } -- cgit v1.2.3