Skip to content

Commit

Permalink
network storage fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed Nov 25, 2023
1 parent 8831167 commit 4f0ef5b
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions lib/network/bloc/network_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,61 @@ class NetworkCubit extends Cubit<NetworkState> {
final (result, err) = await hiveStorage.getValue(StorageKeys.network);
if (err != null) {
loadNetworks();

return;
}

final network = NetworkState.fromJson(jsonDecode(result!) as Map<String, dynamic>);
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));
Expand Down Expand Up @@ -98,49 +143,6 @@ class NetworkCubit extends Cubit<NetworkState> {
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);

Expand Down Expand Up @@ -202,7 +204,7 @@ class NetworkCubit extends Cubit<NetworkState> {
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();
}

Expand Down

0 comments on commit 4f0ef5b

Please sign in to comment.