From 7c24c3013786c8dfc5d2af5fc39b803628ff4081 Mon Sep 17 00:00:00 2001 From: reasje Date: Mon, 23 Oct 2023 15:21:20 +0330 Subject: [PATCH 1/2] feat: Removed fixed networks update --- .../domain/chain_configuration_use_case.dart | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart b/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart index 891f4fa5..05c78cfc 100644 --- a/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart +++ b/lib/features/settings/subfeatures/chain_configuration/domain/chain_configuration_use_case.dart @@ -9,9 +9,7 @@ import 'chain_configuration_repository.dart'; class ChainConfigurationUseCase extends ReactiveUseCase { ChainConfigurationUseCase( this._repository, - ) { - updateFixedNetworks(); - } + ); final ChainConfigurationRepository _repository; @@ -51,38 +49,50 @@ class ChainConfigurationUseCase extends ReactiveUseCase { update(networks, _repository.items); } - void updateFixedNetworks() { - final fixedList = Network.fixedNetworks(); + /// If return true then the enabled network props changed + Network? updateNetworks(List newNetworkList) { + Network? selectedNetwork; + for (int i = 0; i < _repository.items.length; i++) { final repoItem = _repository.items[i]; - final index = Network.fixedNetworks().indexWhere( + final index = newNetworkList.indexWhere( (element) => element.chainId == repoItem.chainId, ); if (index != -1) { // matches - final fixedItem = fixedList.elementAt(index); - if (!repoItem.compareWithOther(fixedItem)) { - _repository.updateItem(repoItem.copyWithOther(fixedItem), i); + final toCompareItem = newNetworkList.elementAt(index); + if (!repoItem.compareWithOther(toCompareItem)) { + final mergedItem = repoItem.copyWithOther(toCompareItem); + if (repoItem.enabled) { + updateSelectedNetwork(mergedItem); + selectedNetwork = mergedItem; + } + _repository.updateItem(mergedItem, i); } } else { // Fixed network does't contain repo Item It means It's deleted - _repository.removeItem(repoItem); + // But we check If It's not a custom network + if (repoItem.networkType != NetworkType.custom) { + _repository.removeItem(repoItem); + } } } // Adding new networks If available - for (Network network in fixedList) { + for (Network network in newNetworkList) { final foundIndex = _repository.items.indexWhere((e) => e.chainId == network.chainId); if (foundIndex == -1) { - _repository.addItem(network); + // Disable the network, Just in case + _repository.addItem(network.copyWith(enabled: false)); } } update(networks, _repository.items); + return selectedNetwork; } void changeIpfsGateWay(String newIpfsGateWay) { From 09ea830e7414ed17c85593eb1ab638cf49667959 Mon Sep 17 00:00:00 2001 From: reasje Date: Mon, 23 Oct 2023 15:48:40 +0330 Subject: [PATCH 2/2] feat: Detecting the change & updating chains --- .../common/contract/chains_use_case.dart | 32 +++++-------------- lib/main.dart | 2 +- packages/shared | 2 +- 3 files changed, 10 insertions(+), 26 deletions(-) diff --git a/lib/features/common/contract/chains_use_case.dart b/lib/features/common/contract/chains_use_case.dart index 20adca9f..927066d5 100644 --- a/lib/features/common/contract/chains_use_case.dart +++ b/lib/features/common/contract/chains_use_case.dart @@ -12,41 +12,25 @@ class ChainsUseCase extends ReactiveUseCase { final ChainConfigurationUseCase _chainConfigurationUseCase; final AuthUseCase _authUseCase; - Future getChainsRpcUrls() async { + Future getChainsRpcUrls() async { return await _repository.chainsRepository.getChainsRpcUrls(); } - void updateChainsRPCUrls() async { + void updateChains() async { try { final chainsRpcUrls = await getChainsRpcUrls(); - final networks = _chainConfigurationUseCase.networks.value; - for (ChainRpcUrl chainRpcUrl in chainsRpcUrls.chainList ?? []) { - final foundIndex = networks - .indexWhere((element) => element.chainId == chainRpcUrl.chainId); + if (chainsRpcUrls.networks?.isNotEmpty ?? false) { + final selectedNetwork = + _chainConfigurationUseCase.updateNetworks(chainsRpcUrls.networks!); - if (foundIndex != -1) { - final network = networks.elementAt(foundIndex); - - // If any change is detected - if (network.web3RpcHttpUrl != chainRpcUrl.httpUrl || - network.web3RpcWebsocketUrl != chainRpcUrl.wssUrl) { - final updatedNetwork = network.copyWith( - web3RpcHttpUrl: chainRpcUrl.httpUrl, - web3RpcWebsocketUrl: chainRpcUrl.wssUrl); - // Update in DB - _chainConfigurationUseCase.updateItem(updatedNetwork, foundIndex); - - if (network.enabled) { - _chainConfigurationUseCase.updateSelectedNetwork(updatedNetwork); - _authUseCase.resetNetwork(updatedNetwork); - } - } + if (selectedNetwork != null) { + _authUseCase.resetNetwork(selectedNetwork); } } } catch (e) { // This update necessary since, RPC change might be essential. - updateChainsRPCUrls(); + updateChains(); } } diff --git a/lib/main.dart b/lib/main.dart index 4987822d..b65d5068 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -34,7 +34,7 @@ void main() { await appVersionUseCase.checkLatestVersion(); final initializationUseCase = container.read(chainsUseCaseProvider); - initializationUseCase.updateChainsRPCUrls(); + initializationUseCase.updateChains(); runApp( UncontrolledProviderScope( diff --git a/packages/shared b/packages/shared index a3e46009..73336956 160000 --- a/packages/shared +++ b/packages/shared @@ -1 +1 @@ -Subproject commit a3e460096a0242eff5fd7fa5c51d88a2ea6705d5 +Subproject commit 733369560b552ca65b70ae47893166343126daaa