Skip to content

Commit

Permalink
Load images in main view asynchronously
Browse files Browse the repository at this point in the history
This ensures scrolling stays smooth on slow devices
  • Loading branch information
TheLastProject committed Jun 15, 2024
1 parent ae4c44a commit eeda041
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseBooleanArray;
import android.util.TypedValue;
import android.view.HapticFeedbackConstants;
Expand Down Expand Up @@ -88,9 +90,13 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
inputHolder.mDivider.setVisibility(View.GONE);

LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor);
Bitmap icon = Utils.getThumbnailWithFallback(mContext, loyaltyCard.id).first;
inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);

// Default header at first, real icon will be retrieved later
Utils.setIconOrTextWithBackground(mContext, loyaltyCard, null, inputHolder.mCardIcon, inputHolder.mCardText);

if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && icon != null) {
ImageLocationType thumbnailImageLocationType = Utils.getThumbnailImageLocationType(mContext, loyaltyCard.id);
if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && thumbnailImageLocationType != null) {
showDivider = true;
inputHolder.setStoreField(loyaltyCard.store);
} else {
Expand Down Expand Up @@ -122,15 +128,28 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
inputHolder.setExtraField(inputHolder.mExpiryField, null, null, false);
}

inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);

inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));

inputHolder.itemView.setActivated(mSelectedItems.get(inputCursor.getPosition(), false));
applyIconAnimation(inputHolder, inputCursor.getPosition());
applyClickEvents(inputHolder, inputCursor.getPosition());

if (thumbnailImageLocationType != null) {
new Thread() {
@Override
public void run() {
Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, thumbnailImageLocationType);

new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));
}
});
}
}.start();
}

// Force redraw to fix size not shrinking after data change
inputHolder.mRow.requestLayout();
}
Expand Down
33 changes: 32 additions & 1 deletion app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,16 @@ public static File retrieveCardImageAsFile(Context context, int loyaltyCardId, I
return retrieveCardImageAsFile(context, getCardImageFileName(loyaltyCardId, type));
}

static public boolean hasCardImage(Context context, String fileName) {
try {
context.openFileInput(fileName);
} catch (FileNotFoundException e) {
return false;
}

return true;
}

static public Bitmap retrieveCardImage(Context context, String fileName) {
FileInputStream in;
try {
Expand All @@ -631,6 +641,10 @@ static public Bitmap retrieveCardImage(Context context, String fileName) {
return BitmapFactory.decodeStream(in);
}

static public boolean hasCardImage(Context context, int loyaltyCardId, ImageLocationType type) {
return hasCardImage(context, getCardImageFileName(loyaltyCardId, type));
}

static public Bitmap retrieveCardImage(Context context, int loyaltyCardId, ImageLocationType type) {
return retrieveCardImage(context, getCardImageFileName(loyaltyCardId, type));
}
Expand Down Expand Up @@ -1027,12 +1041,29 @@ public static boolean deviceHasCamera(Context context) {
}
}

/**
* Check if the card has an image thumbnail and if yes return what type it is
*
* @param context Android context
* @param loyaltyCardId Id of Loyalty Card
* @return ImageLocationType The location type of the thumbnail if it exists, or null if no thumbnail
*/
public static ImageLocationType getThumbnailImageLocationType(Context context, int loyaltyCardId) {
for (ImageLocationType imageLocationType : new ImageLocationType[]{ImageLocationType.icon, ImageLocationType.front, ImageLocationType.back}) {
if (Utils.hasCardImage(context, loyaltyCardId, imageLocationType)) {
return imageLocationType;
}
}

return null;
}

/**
* Retrieve the card thumbnail, falling back to the front and back card image if no thumbnail is set
*
* @param context Android context
* @param loyaltyCardId Id of Loyalty Card
* @return Bitmap thumbnail or None if no set
* @return Pair<Bitmap, ImageLocationType> thumbnail and imageLocationType or both null if not set
*/
public static Pair<Bitmap, ImageLocationType> getThumbnailWithFallback(Context context, int loyaltyCardId) {
Bitmap icon;
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/loyalty_card_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
android:layout_height="match_parent"
android:contentDescription="@string/thumbnailDescription"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down

0 comments on commit eeda041

Please sign in to comment.