Skip to content

Commit 4b5a95b

Browse files
Load thumbnails on main screen asynchronously
Decoding a bitmap can be slow. On slow devices, loading all the images as soon as the card should be shown can lead to UI freezes. By loading the thumbnail asynchronously, scrolling quickly remains smooth even on slow devices.
1 parent a8d92c1 commit 4b5a95b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.graphics.Bitmap;
77
import android.graphics.Color;
88
import android.graphics.drawable.Drawable;
9+
import android.os.Handler;
10+
import android.os.Looper;
911
import android.util.SparseBooleanArray;
1012
import android.util.TypedValue;
1113
import android.view.HapticFeedbackConstants;
@@ -88,9 +90,29 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
8890
inputHolder.mDivider.setVisibility(View.GONE);
8991

9092
LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor);
91-
Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, ImageLocationType.icon);
9293

93-
if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && icon != null) {
94+
inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);
95+
96+
// Default header at first, real icon will be retrieved asynchronously if it exists to ensure
97+
// smooth scrolling even on slower devices
98+
Utils.setIconOrTextWithBackground(mContext, loyaltyCard, null, inputHolder.mCardIcon, inputHolder.mCardText);
99+
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));
100+
boolean hasIcon = Utils.retrieveCardImageAsFile(mContext, loyaltyCard.id, ImageLocationType.icon).exists();
101+
if (hasIcon) {
102+
new Thread() {
103+
@Override
104+
public void run() {
105+
Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, ImageLocationType.icon);
106+
107+
new Handler(Looper.getMainLooper()).post(() -> {
108+
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);
109+
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputHolder.getAdapterPosition()));
110+
});
111+
}
112+
}.start();
113+
}
114+
115+
if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && hasIcon) {
94116
showDivider = true;
95117
inputHolder.setStoreField(loyaltyCard.store);
96118
} else {
@@ -122,11 +144,6 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
122144
inputHolder.setExtraField(inputHolder.mExpiryField, null, null, false);
123145
}
124146

125-
inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);
126-
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);
127-
128-
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));
129-
130147
inputHolder.itemView.setActivated(mSelectedItems.get(inputCursor.getPosition(), false));
131148
applyIconAnimation(inputHolder, inputCursor.getPosition());
132149
applyClickEvents(inputHolder, inputCursor.getPosition());

0 commit comments

Comments
 (0)