Skip to content

Commit

Permalink
Refactor Token Card Handling (#3324)
Browse files Browse the repository at this point in the history
* Refactor TSTokenView handling
  • Loading branch information
JamesSmartCell authored Oct 7, 2023
1 parent 7a9d88c commit 8c7c7e1
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 285 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ dependencies {
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
implementation 'com.google.zxing:core:3.5.2'

implementation 'com.gu.android:toolargetool:0.3.0'

// Sugar
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.alphawallet.app.web3.entity.Web3Transaction;

import java.math.BigInteger;

/**
* Created by JB on 16/01/2021.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1051,14 +1051,27 @@ public static String getNodeURLByNetworkId(long networkId)
/**
* This is used so as not to leak API credentials to web3; XInfuraAPI is the backup API key checked into github
*
* @param networkId
* @param chainId
* @return
*/
public static String getDefaultNodeURL(long networkId)
public static String getDefaultNodeURL(long chainId)
{
NetworkInfo info = networkMap.get(networkId);
if (info != null) return info.rpcServerUrl;
else return "";
NetworkInfo info = networkMap.get(chainId);

if (info == null)
{
return "";
}

int index = info.rpcServerUrl.indexOf(INFURA_ENDPOINT);
if (index > 0)
{
return info.rpcServerUrl.substring(0, index + INFURA_ENDPOINT.length()) + keyProvider.getTertiaryInfuraKey();
}
else
{
return info.backupNodeUrl != null ? info.backupNodeUrl : info.rpcServerUrl;
}
}

public static long getNetworkIdFromName(String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.alphawallet.token.entity.SigReturnType;
import com.alphawallet.token.entity.TSAction;
import com.alphawallet.token.entity.TSSelection;
import com.alphawallet.token.entity.TSTokenView;
import com.alphawallet.token.entity.TokenScriptResult;
import com.alphawallet.token.entity.TokenscriptContext;
import com.alphawallet.token.entity.TokenscriptElement;
Expand Down Expand Up @@ -688,12 +689,15 @@ public Single<Boolean> refreshAllAttributes(Token token)

private void updateAttributeResult(Token token, TokenDefinition td, Attribute attr, BigInteger tokenId)
{
ContractAddress useAddress = new ContractAddress(attr.function); //always use the function attribute's address
tokenscriptUtility.fetchResultFromEthereum(token, useAddress, attr, tokenId, td, this) // Fetch function result from blockchain
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(txResult -> storeAuxData(getWalletAddr(), txResult))
.isDisposed();
if (attr != null && attr.function != null)
{
ContractAddress useAddress = new ContractAddress(attr.function); //always use the function attribute's address
tokenscriptUtility.fetchResultFromEthereum(token, useAddress, attr, tokenId, td, this) // Fetch function result from blockchain
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(txResult -> storeAuxData(getWalletAddr(), txResult))
.isDisposed();
}
}

public void addLocalRefs(Map<String, String> refs)
Expand Down Expand Up @@ -2136,6 +2140,19 @@ public boolean hasTokenView(Token token, String type)
}
}

public TSTokenView getTSTokenView(Token token, String type)
{
TokenDefinition td = getAssetDefinition(token);
if (td != null)
{
return td.getTSTokenView(type);
}
else
{
return null;
}
}

public String getTokenView(Token token, String type)
{
String viewHTML = "";
Expand Down Expand Up @@ -2652,14 +2669,14 @@ private void addOpenSeaAttributes(StringBuilder attrs, Token token, BigInteger t
if (tokenAsset.getBackgroundColor() != null)
TokenScriptResult.addPair(attrs, "background_colour", URLEncoder.encode(tokenAsset.getBackgroundColor(), "utf-8"));
if (tokenAsset.getThumbnail() != null)
TokenScriptResult.addPair(attrs, "image_preview_url", URLEncoder.encode(tokenAsset.getThumbnail(), "utf-8"));
TokenScriptResult.addPair(attrs, "image_preview_url", tokenAsset.getThumbnail());
if (tokenAsset.getDescription() != null)
TokenScriptResult.addPair(attrs, "description", URLEncoder.encode(tokenAsset.getDescription(), "utf-8"));
if (tokenAsset.getExternalLink() != null)
TokenScriptResult.addPair(attrs, "external_link", URLEncoder.encode(tokenAsset.getExternalLink(), "utf-8"));
TokenScriptResult.addPair(attrs, "external_link", tokenAsset.getExternalLink());
//if (tokenAsset.getTraits() != null) TokenScriptResult.addPair(attrs, "traits", tokenAsset.getTraits());
if (tokenAsset.getName() != null)
TokenScriptResult.addPair(attrs, "metadata_name", URLEncoder.encode(tokenAsset.getName(), "utf-8"));
TokenScriptResult.addPair(attrs, "metadata_name", tokenAsset.getName());
}
catch (UnsupportedEncodingException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.alphawallet.token.entity.TicketRange;
import com.alphawallet.token.entity.ViewType;
import com.alphawallet.token.entity.XMLDsigDescriptor;
import com.alphawallet.token.tools.TokenDefinition;

import java.math.BigInteger;
import java.util.List;
Expand Down Expand Up @@ -192,10 +193,10 @@ private void viewHeight(int fetchedViewHeight)
}
}

private void onNewScript(Boolean aBoolean)
private void onNewScript(TokenDefinition td)
{
//need to reload tokens, now we have an updated/new script
if (viewModel.getAssetDefinitionService().hasDefinition(token))
if (td != null)
{
initWebViewCheck();
handler.postDelayed(this, TOKEN_SIZING_DELAY);
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/com/alphawallet/app/ui/FunctionActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import com.gu.toolargetool.TooLargeTool;

/**
* Created by James on 4/04/2019.
Expand Down Expand Up @@ -282,6 +283,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_script_view);
setupViews();

TooLargeTool.startLogging(getApplication());
}

private void setupViews()
Expand Down Expand Up @@ -406,13 +409,15 @@ public void unresolvedSymbolError(String value)

private void checkTokenScriptElement(CalcJsValueCallback cb, TSAction action, TokenscriptElement e)
{
if (e.ref != null && e.ref.length() > 0 && action.attributes != null)
String refName = !TextUtils.isEmpty(e.localRef) ? e.localRef : e.ref; //favour the localRef, then ref

if (!TextUtils.isEmpty(refName) && action.attributes != null)
{
Attribute attr = action.attributes.get(e.ref);
Attribute attr = action.attributes.get(refName);
if (attr != null && attr.userInput)
{
resolveInputCheckCount++;
evaluateJavaScript(cb, e.ref, e, attr);
evaluateJavaScript(cb, refName, e, attr);
}
}
}
Expand Down Expand Up @@ -461,6 +466,12 @@ else if (confirmationDialog == null || !confirmationDialog.isShowing())
}
}

@Override
public BigInteger getTokenId()
{
return tokenId;
}

private void calculateEstimateDialog()
{
if (alertDialog != null && alertDialog.isShowing()) alertDialog.dismiss();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/alphawallet/app/ui/HomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
import com.walletconnect.android.CoreClient;
import com.gu.toolargetool.TooLargeTool;

import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;

Expand Down Expand Up @@ -277,6 +278,8 @@ public void onPageScrollStateChanged(int state)
viewModel.defaultWallet().observe(this, this::onDefaultWallet);
viewModel.updateAvailable().observe(this, this::onUpdateAvailable);

TooLargeTool.startLogging(getApplication());

if (CustomViewSettings.hideDappBrowser())
{
removeDappBrowser();
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/alphawallet/app/ui/NFTActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@
public class NFTActivity extends BaseActivity implements StandardFunctionInterface
{
private NFTViewModel viewModel;

private Wallet wallet;
private Token token;
private FunctionButtonBar functionBar;
private boolean isGridView;

private MenuItem sendMultipleTokensMenuItem;
private MenuItem switchToGridViewMenuItem;
private MenuItem switchToListViewMenuItem;

private NFTAssetsFragment assetsFragment;

private final ActivityResultLauncher<Intent> handleTransactionSuccess = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
Expand All @@ -84,7 +81,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState)
initViewModel();
getIntentData();
setTitle(token.tokenInfo.name);
isGridView = !hasTokenScriptOverride(token);
isGridView = !hasTokenScriptOverride(token) && token.isERC875();
setupViewPager();

//check NFT events, expedite balance update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public void onResume()
@Override
protected void onDestroy()
{
viewModel.onDestroy();
super.onDestroy();
viewModel.onDestroy();
tokenImage.onDestroy();
}

Expand All @@ -184,6 +184,12 @@ public void onPause()
tokenImage.onPause();
}

@Override
public void onSaveInstanceState(Bundle bundle)
{
super.onSaveInstanceState(bundle);
}

@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu)
{
Expand Down Expand Up @@ -291,7 +297,7 @@ private void setup()

if (asset != null && asset.isAttestation())
{
setupAttestation();
setupAttestation(viewModel.getAssetDefinitionService().getAssetDefinition(token));
}
else
{
Expand All @@ -315,11 +321,11 @@ private void initViewModel()
viewModel.walletUpdate().observe(this, this::setupFunctionBar);
}

private void newScriptFound(Boolean status)
private void newScriptFound(TokenDefinition td)
{
CertifiedToolbarView certificateToolbar = findViewById(R.id.certified_toolbar);
//determinate signature
if (token != null && status)
if (token != null && td != null)
{
certificateToolbar.stopDownload();
certificateToolbar.setVisibility(View.VISIBLE);
Expand All @@ -330,12 +336,19 @@ private void newScriptFound(Boolean status)
//now re-load the verbs if already called. If wallet is null this won't complete
setupFunctionBar(viewModel.getWallet());

setupAttestation();
if (token.getInterfaceSpec() == ContractType.ATTESTATION)
{
setupAttestation(td);
}
else
{
displayTokenView(td, tokenId);
}
}
else
{
certificateToolbar.stopDownload();
setupAttestation();
setupAttestation(null);
}
}

Expand Down Expand Up @@ -417,7 +430,10 @@ private void clearRefreshAnimation()

private void onNftAsset(NFTAsset asset)
{
loadAssetFromMetadata(asset);
if (token != null)
{
loadAssetFromMetadata(asset);
}
}

private void updateDefaultTokenData()
Expand Down Expand Up @@ -587,15 +603,9 @@ private void loadFromOpenSeaData(OpenSeaAsset openSeaAsset)
clearRefreshAnimation();
}

private void onOpenSeaAsset(OpenSeaAsset openSeaAsset)
{
loadFromOpenSeaData(openSeaAsset);
}

private void setupAttestation()
private void setupAttestation(TokenDefinition td)
{
NFTAsset attnAsset = new NFTAsset();
TokenDefinition td = viewModel.getAssetDefinitionService().getAssetDefinition(token);
if (token.getInterfaceSpec() != ContractType.ATTESTATION)
{
return;
Expand All @@ -604,7 +614,7 @@ else if (td != null)
{
attnAsset.setupScriptElements(td);
attnAsset.setupScriptAttributes(td, token);
if (!displayTokenView(td))
if (!displayTokenView(td, BigInteger.ONE))
{
tokenImage.setupTokenImage(attnAsset);
}
Expand Down Expand Up @@ -821,7 +831,7 @@ public WalletType getWalletType()
/***
* TokenScript view handling
*/
private boolean displayTokenView(TokenDefinition td)
private boolean displayTokenView(TokenDefinition td, BigInteger tokenId)
{
if (!td.hasTokenView())
{
Expand All @@ -837,7 +847,7 @@ private boolean displayTokenView(TokenDefinition td)
scriptView.setChainId(token.tokenInfo.chainId);
scriptView.setWalletAddress(new Address(token.getWallet()));

scriptView.renderTokenScriptView(token, new TicketRange(BigInteger.ONE, token.getAddress()), viewModel.getAssetDefinitionService(), ViewType.VIEW);
scriptView.renderTokenScriptView(token, new TicketRange(tokenId, token.getAddress()), viewModel.getAssetDefinitionService(), ViewType.VIEW);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

gridItemDecoration = new ItemOffsetDecoration(requireContext(), R.dimen.grid_divider_offset);

if (hasTokenScriptOverride(token))
if (hasTokenScriptOverride(token) && token.isERC875())
{
showListView();
}
Expand Down Expand Up @@ -196,7 +196,7 @@ public void showListView()

private void initAndAttachAdapter(boolean isGridView)
{
if (hasTokenScriptOverride(token))
if (hasTokenScriptOverride(token) && token.isERC875())
{
searchLayout.setVisibility(View.GONE);
adapter = new NonFungibleTokenAdapter(this, token, viewModel.getAssetDefinitionService(), viewModel.getOpenseaService(), isGridView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.alphawallet.app.web3.entity.Web3Transaction;
import com.alphawallet.token.entity.Signable;

import java.math.BigInteger;

/**
* Created by JB on 27/11/2020.
*/
Expand Down Expand Up @@ -63,4 +65,9 @@ default void signingFailed(Throwable error, Signable message)
}

WalletType getWalletType();

default BigInteger getTokenId()
{
return BigInteger.ZERO;
}
}
Loading

0 comments on commit 8c7c7e1

Please sign in to comment.