Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat: Update network if already added
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Nov 8, 2023
1 parent 76f4a45 commit ee55275
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,26 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
final networks = _chainConfigurationUseCase.networks.value;
final foundChainIdIndex =
networks.indexWhere((element) => element.chainId == chainId);
// user can add a network again meaning It will override the old network
final alreadyExists = foundChainIdIndex != -1;
final alreadyEnabled =
alreadyExists ? networks[foundChainIdIndex].enabled : false;

// Add network
final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId);

final res = await showAddNetworkDialog(
context!,
network: newNetwork,
approveFunction: (network) => alreadyExists
? updateNetwork(network, foundChainIdIndex)
: addNewNetwork(network),
);

// TODO:
if (foundChainIdIndex == -1) {
// Add network
final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId);

final res = await showAddNetworkDialog(
context!,
network: newNetwork,
approveFunction: addNewNetwork,
);

if (!(res ?? false)) {
cancelRequest(id);
} else {
if (!(res ?? false)) {
cancelRequest(id);
} else {
if (!alreadyEnabled) {
final res = await showSwitchNetworkDialog(context!,
fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl,
toNetwork: newNetwork.label ?? newNetwork.web3RpcHttpUrl,
Expand All @@ -340,13 +345,14 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
cancelRequest(id);
}
}
} else {
// TODO:
addError(translate('Network already exists!'));
cancelRequest(id);
}
}

Network? updateNetwork(Network network, int index) {
_chainConfigurationUseCase.updateItem(network, index);
return network;
}

Network? addNewNetwork(Network newNetwork) {
_chainConfigurationUseCase.addItem(newNetwork);
return newNetwork;
Expand Down

0 comments on commit ee55275

Please sign in to comment.