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/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) { 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(