Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ public class AssetDefinitionService implements ParseResult, AttributeInterface
@Nullable
private Disposable checkEventDisposable;

private final Map<String, Boolean> 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.
Expand Down Expand Up @@ -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<Integer> fetchViewHeight(int chainId, String address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Boolean> 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() {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/alphawallet/app/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 19 additions & 21 deletions app/src/main/java/com/alphawallet/app/widget/TokenIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -45,12 +45,10 @@ public class TokenIcon extends ConstraintLayout

private OnTokenClickListener onTokenClickListener;
private Token token;
private CustomViewTarget viewTarget;
private CustomViewTarget<ImageView, Drawable> 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)
Expand Down Expand Up @@ -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<ImageView, BitmapDrawable>(icon) {
final IconItem iconItem = assetDefinition != null ? assetDefinition.fetchIconForToken(token) : getIconUrl(token);

viewTarget = new CustomViewTarget<ImageView, Drawable>(icon) {
@Override
protected void onResourceCleared(@Nullable Drawable placeholder) { }

Expand All @@ -126,21 +125,21 @@ public void onLoadFailed(@Nullable Drawable errorDrawable)
}

@Override
public void onResourceReady(@NotNull BitmapDrawable bitmap, Transition<? super BitmapDrawable> transition)
public void onResourceReady(@NotNull Drawable bitmap, Transition<? super Drawable> transition)
{
textIcon.setVisibility(View.GONE);
icon.setVisibility(View.VISIBLE);
icon.setImageDrawable(bitmap);
}
};

displayTokenIcon();
displayTokenIcon(iconItem);
}

/**
* Try to fetch Token Icon from the Token URL.
*/
private void displayTokenIcon()
private void displayTokenIcon(IconItem iconItem)
{
int chainIcon = EthereumNetworkRepository.getChainLogo(token.tokenInfo.chainId);

Expand All @@ -158,12 +157,19 @@ private void displayTokenIcon()
else
{
setupTextIcon(token);
IconItem iconItem = assetDefinition != null ? assetDefinition.fetchIconForToken(token) : getIconUrl(token);
RequestBuilder<Drawable> 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)
Expand All @@ -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)
Expand Down Expand Up @@ -276,7 +273,8 @@ private void performTokenClick(View v)
private final RequestListener<Drawable> requestListener = new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
setupTextIcon(token);
return true;
}

@Override
Expand Down