feat(wallet): implement auto asset discovery (#16768)
This commit is contained in:
@@ -24,6 +24,8 @@ import org.chromium.brave_wallet.mojom.KeyringService;
|
||||
import org.chromium.brave_wallet.mojom.NetworkInfo;
|
||||
import org.chromium.brave_wallet.mojom.SolanaTxManagerProxy;
|
||||
import org.chromium.brave_wallet.mojom.TxService;
|
||||
import org.chromium.chrome.browser.crypto_wallet.observers.BraveWalletServiceObserverImpl;
|
||||
import org.chromium.chrome.browser.crypto_wallet.observers.BraveWalletServiceObserverImpl.BraveWalletServiceObserverImplDelegate;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.AssetUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.AsyncUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.JavaUtils;
|
||||
@@ -34,7 +36,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PortfolioModel {
|
||||
public class PortfolioModel implements BraveWalletServiceObserverImplDelegate {
|
||||
public final LiveData<List<NftDataModel>> mNftModels;
|
||||
private final CryptoSharedData mSharedData;
|
||||
private final MutableLiveData<List<NftDataModel>> _mNftModels;
|
||||
@@ -49,6 +51,8 @@ public class PortfolioModel {
|
||||
private BraveWalletService mBraveWalletService;
|
||||
private AssetRatioService mAssetRatioService;
|
||||
private Context mContext;
|
||||
private MutableLiveData<Boolean> _mIsDiscoveringUserAssets;
|
||||
public LiveData<Boolean> mIsDiscoveringUserAssets;
|
||||
|
||||
public PortfolioModel(Context context, TxService txService, KeyringService keyringService,
|
||||
BlockchainRegistry blockchainRegistry, JsonRpcService jsonRpcService,
|
||||
@@ -67,6 +71,9 @@ public class PortfolioModel {
|
||||
mSharedData = sharedData;
|
||||
_mNftModels = new MutableLiveData<>(Collections.emptyList());
|
||||
mNftModels = _mNftModels;
|
||||
_mIsDiscoveringUserAssets = new MutableLiveData<>(false);
|
||||
mIsDiscoveringUserAssets = _mIsDiscoveringUserAssets;
|
||||
addServiceObservers();
|
||||
}
|
||||
|
||||
// TODO(pav): We should fetch and process all portfolio list here
|
||||
@@ -101,6 +108,20 @@ public class PortfolioModel {
|
||||
});
|
||||
}
|
||||
|
||||
public void discoverAssetsOnAllSupportedChains() {
|
||||
if (mBraveWalletService == null) return;
|
||||
_mIsDiscoveringUserAssets.postValue(true);
|
||||
mBraveWalletService.discoverAssetsOnAllSupportedChains();
|
||||
}
|
||||
|
||||
private void addServiceObservers() {
|
||||
if (mBraveWalletService != null) {
|
||||
BraveWalletServiceObserverImpl walletServiceObserver =
|
||||
new BraveWalletServiceObserverImpl(this);
|
||||
mBraveWalletService.addObserver(walletServiceObserver);
|
||||
}
|
||||
}
|
||||
|
||||
void resetServices(Context context, TxService mTxService, KeyringService mKeyringService,
|
||||
BlockchainRegistry mBlockchainRegistry, JsonRpcService mJsonRpcService,
|
||||
EthTxManagerProxy mEthTxManagerProxy, SolanaTxManagerProxy mSolanaTxManagerProxy,
|
||||
@@ -115,6 +136,7 @@ public class PortfolioModel {
|
||||
this.mSolanaTxManagerProxy = mSolanaTxManagerProxy;
|
||||
this.mBraveWalletService = mBraveWalletService;
|
||||
this.mAssetRatioService = mAssetRatioService;
|
||||
addServiceObservers();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,4 +186,9 @@ public class PortfolioModel {
|
||||
return AssetUtils.httpifyIpfsUrl(url);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDiscoverAssetsCompleted(BlockchainToken[] discoveredAssets) {
|
||||
_mIsDiscoveringUserAssets.postValue(false);
|
||||
}
|
||||
}
|
||||
|
||||
+18
-5
@@ -13,6 +13,7 @@ import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
@@ -92,6 +93,7 @@ public class PortfolioFragment
|
||||
private TextView mTvNftTitle;
|
||||
private SmoothLineChartEquallySpaced mChartES;
|
||||
private PortfolioModel mPortfolioModel;
|
||||
private ProgressBar mPbAssetDiscovery;
|
||||
|
||||
public static PortfolioFragment newInstance() {
|
||||
return new PortfolioFragment();
|
||||
@@ -119,20 +121,22 @@ public class PortfolioFragment
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setHasOptionsMenu(true);
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity != null) {
|
||||
mWalletModel = activity.getWalletModel();
|
||||
mPortfolioModel = mWalletModel.getCryptoModel().getPortfolioModel();
|
||||
mWalletModel.getCryptoModel().getPortfolioModel().discoverAssetsOnAllSupportedChains();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity != null) {
|
||||
mWalletModel = activity.getWalletModel();
|
||||
mPortfolioModel = mWalletModel.getCryptoModel().getPortfolioModel();
|
||||
}
|
||||
View view = inflater.inflate(R.layout.fragment_portfolio, container, false);
|
||||
mRvCoins = view.findViewById(R.id.rvCoins);
|
||||
mChartES = view.findViewById(R.id.line_chart);
|
||||
mPbAssetDiscovery = view.findViewById(R.id.frag_port_pb_asset_discovery);
|
||||
mRvCoins.addItemDecoration(
|
||||
new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
|
||||
|
||||
@@ -210,6 +214,15 @@ public class PortfolioFragment
|
||||
}
|
||||
updatePendingTxNotification();
|
||||
});
|
||||
mWalletModel.getCryptoModel().getPortfolioModel().mIsDiscoveringUserAssets.observe(
|
||||
getViewLifecycleOwner(), isDiscoveringUserAssets -> {
|
||||
if (isDiscoveringUserAssets) {
|
||||
AndroidUtils.show(mPbAssetDiscovery);
|
||||
} else {
|
||||
AndroidUtils.gone(mPbAssetDiscovery);
|
||||
updatePortfolioGetPendingTx();
|
||||
}
|
||||
});
|
||||
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mNeedToCreateAccountForNetwork.observe(
|
||||
getViewLifecycleOwner(), networkInfo -> {
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.chromium.chrome.browser.crypto_wallet.observers;
|
||||
|
||||
import org.chromium.brave_wallet.mojom.BlockchainToken;
|
||||
import org.chromium.brave_wallet.mojom.BraveWalletServiceObserver;
|
||||
import org.chromium.brave_wallet.mojom.OriginInfo;
|
||||
import org.chromium.mojo.system.MojoException;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class BraveWalletServiceObserverImpl implements BraveWalletServiceObserver {
|
||||
public interface BraveWalletServiceObserverImplDelegate {
|
||||
default void onActiveOriginChanged(OriginInfo originInfo) {}
|
||||
|
||||
default void onDefaultEthereumWalletChanged(int wallet) {}
|
||||
|
||||
default void onDefaultSolanaWalletChanged(int wallet) {}
|
||||
|
||||
default void onDefaultBaseCurrencyChanged(String currency) {}
|
||||
|
||||
default void onDefaultBaseCryptocurrencyChanged(String cryptocurrency) {}
|
||||
|
||||
default void onNetworkListChanged() {}
|
||||
|
||||
default void onDiscoverAssetsCompleted(BlockchainToken[] discoveredAssets) {}
|
||||
}
|
||||
|
||||
private WeakReference<BraveWalletServiceObserverImplDelegate> mDelegate;
|
||||
|
||||
public BraveWalletServiceObserverImpl(BraveWalletServiceObserverImplDelegate delegate) {
|
||||
mDelegate = new WeakReference<>(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActiveOriginChanged(OriginInfo originInfo) {
|
||||
if (isActive()) getRef().onActiveOriginChanged(originInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDefaultEthereumWalletChanged(int wallet) {
|
||||
if (isActive()) getRef().onDefaultEthereumWalletChanged(wallet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDefaultSolanaWalletChanged(int wallet) {
|
||||
if (isActive()) getRef().onDefaultSolanaWalletChanged(wallet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDefaultBaseCurrencyChanged(String currency) {
|
||||
if (isActive()) getRef().onDefaultBaseCurrencyChanged(currency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDefaultBaseCryptocurrencyChanged(String cryptocurrency) {
|
||||
if (isActive()) getRef().onDefaultBaseCryptocurrencyChanged(cryptocurrency);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNetworkListChanged() {
|
||||
if (isActive()) getRef().onNetworkListChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDiscoverAssetsCompleted(BlockchainToken[] discoveredAssets) {
|
||||
if (isActive()) getRef().onDiscoverAssetsCompleted(discoveredAssets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
mDelegate.clear();
|
||||
mDelegate = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionError(MojoException e) {}
|
||||
|
||||
public void destroy() {
|
||||
close();
|
||||
}
|
||||
|
||||
private BraveWalletServiceObserverImplDelegate getRef() {
|
||||
return mDelegate.get();
|
||||
}
|
||||
|
||||
private boolean isActive() {
|
||||
return mDelegate != null && mDelegate.get() != null;
|
||||
}
|
||||
}
|
||||
@@ -244,13 +244,26 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<TextView
|
||||
style="@style/BraveWalletTextView"
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_marginStart="10dp"
|
||||
android:text="@string/assets"
|
||||
android:textColor="@color/brave_theme_hint_text" />
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
<TextView
|
||||
style="@style/BraveWalletTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginEnd="6dp"
|
||||
android:text="@string/assets"
|
||||
android:textColor="@color/brave_theme_hint_text" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/frag_port_pb_asset_discovery"
|
||||
android:indeterminateDrawable="@drawable/progress_indeterminate_orange"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:visibility="gone"/>
|
||||
</LinearLayout>
|
||||
|
||||
<androidx.cardview.widget.CardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
||||
Reference in New Issue
Block a user