From 3da6a25454981f10733176bdc47ff42b35e7270a Mon Sep 17 00:00:00 2001 From: James Brown Date: Tue, 30 Mar 2021 15:37:28 +1100 Subject: [PATCH] Tidy up icon loading and add pass-through to check AW icon repo --- .../app/service/AssetDefinitionService.java | 7 +--- .../app/ui/widget/entity/IconItem.java | 11 ++++- .../java/com/alphawallet/app/util/Utils.java | 8 +++- .../com/alphawallet/app/widget/TokenIcon.java | 40 +++++++++---------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java index af6b2e40e..3fc355736 100644 --- a/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java +++ b/app/src/main/java/com/alphawallet/app/service/AssetDefinitionService.java @@ -157,8 +157,6 @@ public class AssetDefinitionService implements ParseResult, AttributeInterface @Nullable private Disposable checkEventDisposable; - private final Map iconCheck = new ConcurrentHashMap<>(); - /* Designed with the assmuption that only a single instance of this class at any given time * ^^ The "service" part of AssetDefinitionService is the keyword here. * This is shorthand in the project to indicate this is a singleton that other classes inject. @@ -2569,10 +2567,7 @@ public IconItem fetchIconForToken(Token token) tURL = Utils.getTokenImageUrl(token.tokenInfo.chainId, correctedAddr); } - boolean onlyTryCache = iconCheck.containsKey(correctedAddr); - iconCheck.put(correctedAddr, true); - - return new IconItem(tURL, onlyTryCache, correctedAddr, token.tokenInfo.chainId); + return new IconItem(tURL, correctedAddr, token.tokenInfo.chainId); } public Single fetchViewHeight(int chainId, String address) diff --git a/app/src/main/java/com/alphawallet/app/ui/widget/entity/IconItem.java b/app/src/main/java/com/alphawallet/app/ui/widget/entity/IconItem.java index bf424f25a..bf5e58c7e 100644 --- a/app/src/main/java/com/alphawallet/app/ui/widget/entity/IconItem.java +++ b/app/src/main/java/com/alphawallet/app/ui/widget/entity/IconItem.java @@ -2,17 +2,24 @@ import com.bumptech.glide.signature.ObjectKey; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + public class IconItem { private final String url; private final boolean fetchFromCache; private final String correctedAddress; private final int chainId; - public IconItem(String url, boolean fetchFromCache, String correctedAddress, int chainId) { + private final static Map iconCheck = new ConcurrentHashMap<>(); + + public IconItem(String url, String correctedAddress, int chainId) { this.url = url; - this.fetchFromCache = fetchFromCache; + this.fetchFromCache = iconCheck.containsKey(correctedAddress); this.correctedAddress = correctedAddress; this.chainId = chainId; + + iconCheck.put(correctedAddress, true); } public String getUrl() { diff --git a/app/src/main/java/com/alphawallet/app/util/Utils.java b/app/src/main/java/com/alphawallet/app/util/Utils.java index f062ce13c..f97e4171b 100644 --- a/app/src/main/java/com/alphawallet/app/util/Utils.java +++ b/app/src/main/java/com/alphawallet/app/util/Utils.java @@ -46,8 +46,9 @@ public class Utils { private static final String ISOLATE_NUMERIC = "(0?x?[0-9a-fA-F]+)"; private static final String ICON_REPO_ADDRESS_TOKEN = "[TOKEN]"; private static final String CHAIN_REPO_ADDRESS_TOKEN = "[CHAIN]"; + public static final String ALPHAWALLET_REPO_NAME = "alphawallet/iconassets"; private static final String TRUST_ICON_REPO = "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/" + CHAIN_REPO_ADDRESS_TOKEN + "/assets/" + ICON_REPO_ADDRESS_TOKEN + "/logo.png"; - private static final String ALPHAWALLET_ICON_REPO = "https://raw.githubusercontent.com/alphawallet/iconassets/master/" + ICON_REPO_ADDRESS_TOKEN + "/logo.png"; + private static final String ALPHAWALLET_ICON_REPO = "https://raw.githubusercontent.com/" + ALPHAWALLET_REPO_NAME + "/master/" + ICON_REPO_ADDRESS_TOKEN + "/logo.png"; public static int dp2px(Context context, int dp) { Resources r = context.getResources(); @@ -769,6 +770,11 @@ public static String getTokenImageUrl(int chainId, String address) return tURL; } + public static String getAWIconRepo(String address) + { + return ALPHAWALLET_ICON_REPO.replace(ICON_REPO_ADDRESS_TOKEN, Keys.toChecksumAddress(address)); + } + public static boolean isContractCall(Context context, String operationName) { return !TextUtils.isEmpty(operationName) && context.getString(R.string.contract_call).equals(operationName); diff --git a/app/src/main/java/com/alphawallet/app/widget/TokenIcon.java b/app/src/main/java/com/alphawallet/app/widget/TokenIcon.java index 1977b696c..9442dd30a 100644 --- a/app/src/main/java/com/alphawallet/app/widget/TokenIcon.java +++ b/app/src/main/java/com/alphawallet/app/widget/TokenIcon.java @@ -2,7 +2,6 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; @@ -23,6 +22,7 @@ import com.alphawallet.app.ui.widget.entity.StatusType; import com.alphawallet.app.util.Utils; import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; @@ -45,12 +45,10 @@ public class TokenIcon extends ConstraintLayout private OnTokenClickListener onTokenClickListener; private Token token; - private CustomViewTarget viewTarget; + private CustomViewTarget viewTarget; private String tokenName; - private AssetDefinitionService assetDefinition; private boolean showStatus = false; private boolean largeIcon = false; - private boolean smallIcon = false; private StatusType currentStatus; public TokenIcon(Context context, AttributeSet attrs) @@ -113,9 +111,10 @@ public void bindData(Token token, AssetDefinitionService assetDefinition) if (token == null) return; this.token = token; this.tokenName = token.getFullName(assetDefinition, token.getTicketCount()); - this.assetDefinition = assetDefinition; - viewTarget = new CustomViewTarget(icon) { + final IconItem iconItem = assetDefinition != null ? assetDefinition.fetchIconForToken(token) : getIconUrl(token); + + viewTarget = new CustomViewTarget(icon) { @Override protected void onResourceCleared(@Nullable Drawable placeholder) { } @@ -126,7 +125,7 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) } @Override - public void onResourceReady(@NotNull BitmapDrawable bitmap, Transition transition) + public void onResourceReady(@NotNull Drawable bitmap, Transition transition) { textIcon.setVisibility(View.GONE); icon.setVisibility(View.VISIBLE); @@ -134,13 +133,13 @@ public void onResourceReady(@NotNull BitmapDrawable bitmap, Transition rb = null; + + //if the main request wasn't checking the AW icon repo, check it if main repo doesn't have an icon + if (!iconItem.getUrl().contains(Utils.ALPHAWALLET_REPO_NAME)) + { + rb = Glide.with(getContext().getApplicationContext()).load(Utils.getAWIconRepo(token.getAddress())); + } Glide.with(getContext().getApplicationContext()) .load(iconItem.getUrl()) .signature(iconItem.getSignature()) .onlyRetrieveFromCache(iconItem.onlyFetchFromCache()) //reduce URL checking, only check once per session + .error(rb) .apply(new RequestOptions().circleCrop()) .apply(new RequestOptions().placeholder(chainIcon)) .listener(requestListener) @@ -175,16 +181,7 @@ private IconItem getIconUrl(Token token) { String correctedAddr = Keys.toChecksumAddress(token.getAddress()); String tURL = Utils.getTokenImageUrl(token.tokenInfo.chainId, correctedAddr); - return new IconItem(tURL, false, correctedAddr, token.tokenInfo.chainId); - } - - /** - * This method is used to set Custom image to the Token Icon - * @param imageResource Drawable resource identifier - */ - public void setTokenImage(int imageResource) - { - icon.setImageResource(imageResource); + return new IconItem(tURL, correctedAddr, token.tokenInfo.chainId); } public void setStatusIcon(StatusType type) @@ -276,7 +273,8 @@ private void performTokenClick(View v) private final RequestListener requestListener = new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - return false; + setupTextIcon(token); + return true; } @Override