diff --git a/lib/network/bloc/network_cubit.dart b/lib/network/bloc/network_cubit.dart index 66fc8157..e254f5b0 100644 --- a/lib/network/bloc/network_cubit.dart +++ b/lib/network/bloc/network_cubit.dart @@ -37,16 +37,61 @@ class NetworkCubit extends Cubit { final (result, err) = await hiveStorage.getValue(StorageKeys.network); if (err != null) { loadNetworks(); - return; } final network = NetworkState.fromJson(jsonDecode(result!) as Map); emit(network); - await Future.delayed(const Duration(milliseconds: 50)); + await Future.delayed(const Duration(milliseconds: 100)); loadNetworks(); } + Future loadNetworks() async { + if (state.loadingNetworks) return; + emit(state.copyWith(loadingNetworks: true)); + + final networks = state.networks; + + if (networks.isNotEmpty) { + final selectedNetwork = networks.firstWhere((_) => _.type == state.selectedNetwork); + + emit( + state.copyWith( + loadingNetworks: false, + tempNetworkDetails: selectedNetwork, + tempNetwork: selectedNetwork.type, + ), + ); + await Future.delayed(const Duration(milliseconds: 100)); + setupBlockchain(); + return; + } + + final newNetworks = [ + const ElectrumNetwork.defaultElectrum(), + const ElectrumNetwork.bullbitcoin(), + const ElectrumNetwork.custom( + mainnet: 'ssl://$bbelectrum:50002', + testnet: 'ssl://$bbelectrum:60002', + ), + ]; + + final selectedNetwork = newNetworks.firstWhere((_) => _.type == state.selectedNetwork); + + emit( + state.copyWith( + loadingNetworks: false, + networks: newNetworks, + tempNetworkDetails: selectedNetwork, + tempNetwork: selectedNetwork.type, + ), + ); + + await Future.delayed(const Duration(milliseconds: 50)); + + await setupBlockchain(); + } + void toggleTestnet() async { final isTestnet = state.testnet; emit(state.copyWith(testnet: !isTestnet)); @@ -98,49 +143,6 @@ class NetworkCubit extends Cubit { emit(state.copyWith(blockchain: blockchain, networkConnected: true)); } - Future loadNetworks() async { - if (state.loadingNetworks) return; - emit(state.copyWith(loadingNetworks: true)); - - final networks = state.networks; - - if (networks.isNotEmpty) { - final selectedNetwork = networks.firstWhere((_) => _.type == state.selectedNetwork); - - emit( - state.copyWith( - loadingNetworks: false, - tempNetworkDetails: selectedNetwork, - ), - ); - setupBlockchain(); - return; - } - - final newNetworks = [ - const ElectrumNetwork.defaultElectrum(), - const ElectrumNetwork.bullbitcoin(), - const ElectrumNetwork.custom( - mainnet: 'ssl://$bbelectrum:50002', - testnet: 'ssl://$bbelectrum:60002', - ), - ]; - - final selectedNetwork = newNetworks.firstWhere((_) => _.type == state.selectedNetwork); - - emit( - state.copyWith( - loadingNetworks: false, - networks: newNetworks, - tempNetworkDetails: selectedNetwork, - ), - ); - - await Future.delayed(const Duration(milliseconds: 50)); - - await setupBlockchain(); - } - void networkTypeTempChanged(ElectrumTypes type) { final network = state.networks.firstWhere((_) => _.type == type); @@ -202,7 +204,7 @@ class NetworkCubit extends Cubit { networks.removeAt(index); networks.insert(index, state.tempNetworkDetails!); emit(state.copyWith(networks: networks, selectedNetwork: tempNetwork.type)); - await Future.delayed(const Duration(milliseconds: 50)); + await Future.delayed(const Duration(milliseconds: 100)); setupBlockchain(); }