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

Commit e848ced

Browse files
authored
Merge pull request #116 from MXCzkEVM/dapp_add_network_feat
feat: Update network if already added
2 parents 738bf6b + ee55275 commit e848ced

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,26 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
315315
final networks = _chainConfigurationUseCase.networks.value;
316316
final foundChainIdIndex =
317317
networks.indexWhere((element) => element.chainId == chainId);
318+
// user can add a network again meaning It will override the old network
319+
final alreadyExists = foundChainIdIndex != -1;
320+
final alreadyEnabled =
321+
alreadyExists ? networks[foundChainIdIndex].enabled : false;
322+
323+
// Add network
324+
final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId);
325+
326+
final res = await showAddNetworkDialog(
327+
context!,
328+
network: newNetwork,
329+
approveFunction: (network) => alreadyExists
330+
? updateNetwork(network, foundChainIdIndex)
331+
: addNewNetwork(network),
332+
);
318333

319-
// TODO:
320-
if (foundChainIdIndex == -1) {
321-
// Add network
322-
final newNetwork = Network.fromAddEthereumChain(networkDetails, chainId);
323-
324-
final res = await showAddNetworkDialog(
325-
context!,
326-
network: newNetwork,
327-
approveFunction: addNewNetwork,
328-
);
329-
330-
if (!(res ?? false)) {
331-
cancelRequest(id);
332-
} else {
334+
if (!(res ?? false)) {
335+
cancelRequest(id);
336+
} else {
337+
if (!alreadyEnabled) {
333338
final res = await showSwitchNetworkDialog(context!,
334339
fromNetwork: state.network!.label ?? state.network!.web3RpcHttpUrl,
335340
toNetwork: newNetwork.label ?? newNetwork.web3RpcHttpUrl,
@@ -340,13 +345,14 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
340345
cancelRequest(id);
341346
}
342347
}
343-
} else {
344-
// TODO:
345-
addError(translate('Network already exists!'));
346-
cancelRequest(id);
347348
}
348349
}
349350

351+
Network? updateNetwork(Network network, int index) {
352+
_chainConfigurationUseCase.updateItem(network, index);
353+
return network;
354+
}
355+
350356
Network? addNewNetwork(Network newNetwork) {
351357
_chainConfigurationUseCase.addItem(newNetwork);
352358
return newNetwork;

0 commit comments

Comments
 (0)