refactor fusion state to remove instance variables for each step

This commit is contained in:
sneurlax 2023-10-17 11:15:31 -05:00
parent 74dbc718be
commit 974687e412
4 changed files with 86 additions and 101 deletions

View file

@ -31,101 +31,62 @@ class FusionProgressUIState extends ChangeNotifier {
return false;
}
bool _succeeded = _connecting == CashFusionStatus.success;
_succeeded &= _outputs == CashFusionStatus.success;
_succeeded &= _peers == CashFusionStatus.success;
_succeeded &= _fusing == CashFusionStatus.success;
_succeeded &= _complete == CashFusionStatus.success;
bool _succeeded = _connecting.status == CashFusionStatus.success;
_succeeded &= _outputs.status == CashFusionStatus.success;
_succeeded &= _peers.status == CashFusionStatus.success;
_succeeded &= _fusing.status == CashFusionStatus.success;
_succeeded &= _complete.status == CashFusionStatus.success;
_succeeded &= fusionState == CashFusionStatus.success;
return _succeeded;
}
({CashFusionStatus status, String? info}) _connecting =
(status: CashFusionStatus.waiting, info: null);
({CashFusionStatus status, String? info}) get connecting => _connecting;
set connecting(({CashFusionStatus status, String? info}) state) {
CashFusionState _connecting =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get connecting => _connecting;
set connecting(CashFusionState state) {
_connecting = state;
notifyListeners();
}
CashFusionStatus _outputs = CashFusionStatus.waiting;
CashFusionStatus get outputs => _outputs;
set outputs(CashFusionStatus state) {
CashFusionState _outputs =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get outputs => _outputs;
set outputs(CashFusionState state) {
_outputs = state;
notifyListeners();
}
CashFusionStatus _peers = CashFusionStatus.waiting;
CashFusionStatus get peers => _peers;
set peers(CashFusionStatus state) {
CashFusionState _peers =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get peers => _peers;
set peers(CashFusionState state) {
_peers = state;
notifyListeners();
}
CashFusionStatus _fusing = CashFusionStatus.waiting;
CashFusionStatus get fusing => _fusing;
set fusing(CashFusionStatus state) {
CashFusionState _fusing =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get fusing => _fusing;
set fusing(CashFusionState state) {
_fusing = state;
notifyListeners();
}
CashFusionStatus _complete = CashFusionStatus.waiting;
CashFusionStatus get complete => _complete;
set complete(CashFusionStatus state) {
CashFusionState _complete =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get complete => _complete;
set complete(CashFusionState state) {
_complete = state;
notifyListeners();
}
CashFusionStatus _fusionStatus = CashFusionStatus.waiting;
CashFusionStatus get fusionState => _fusionStatus;
set fusionState(CashFusionStatus fusionStatus) {
CashFusionState _fusionStatus =
CashFusionState(status: CashFusionStatus.waiting, info: null);
CashFusionState get fusionState => _fusionStatus;
set fusionState(CashFusionState fusionStatus) {
_fusionStatus = fusionStatus;
notifyListeners();
}
// Instance variables for info labels on fusion progress steps.
//
// "Connecting to server"
String? _connectionInfo;
String? get connectionInfo => _connectionInfo;
set connectionInfo(String? fusionInfo) {
_connectionInfo = fusionInfo;
notifyListeners();
}
// "Allocating outputs"
String? _outputsInfo;
String? get outputsInfo => _outputsInfo;
set outputsInfo(String? fusionInfo) {
_outputsInfo = fusionInfo;
notifyListeners();
}
// "Waiting for peers"
String? _peersInfo;
String? get peersInfo => _peersInfo;
set peersInfo(String? fusionInfo) {
_peersInfo = fusionInfo;
notifyListeners();
}
// "Fusing"
String? _fusingInfo;
String? get fusingInfo => _fusingInfo;
set fusingInfo(String? fusionInfo) {
_fusingInfo = fusionInfo;
notifyListeners();
}
// "Complete"
//
// Should show txId if successful.
String? _completeInfo;
String? get completeInfo => _completeInfo;
set completeInfo(String? fusionInfo) {
_completeInfo = fusionInfo;
notifyListeners();
}
}

View file

@ -7,6 +7,13 @@ import 'package:stackwallet/widgets/desktop/secondary_button.dart';
enum CashFusionStatus { waiting, running, success, failed }
class CashFusionState {
final CashFusionStatus status;
final String? info;
CashFusionState({required this.status, this.info});
}
class FusionDialog extends StatelessWidget {
const FusionDialog({
Key? key,

View file

@ -95,7 +95,7 @@ class _ProgressItem extends StatelessWidget {
final String iconAsset;
final String label;
final ({CashFusionStatus status, String? info}) state;
final CashFusionState state;
Widget _getIconForState(CashFusionStatus state, BuildContext context) {
switch (state) {

View file

@ -156,68 +156,85 @@ mixin FusionWalletInterface {
void _updateStatus({required fusion.FusionStatus status, String? info}) {
switch (status) {
case fusion.FusionStatus.connecting:
_uiState?.connecting = CashFusionStatus.running;
_uiState?.connecting =
CashFusionState(status: CashFusionStatus.running, info: info);
break;
case fusion.FusionStatus.setup:
_uiState?.connecting = CashFusionStatus.success;
_uiState?.outputs = CashFusionStatus.running;
_uiState?.connecting =
CashFusionState(status: CashFusionStatus.success, info: info);
_uiState?.outputs =
CashFusionState(status: CashFusionStatus.running, info: info);
break;
case fusion.FusionStatus.waiting:
_uiState?.outputs = CashFusionStatus.success;
_uiState?.peers = CashFusionStatus.running;
_uiState?.outputs =
CashFusionState(status: CashFusionStatus.success, info: info);
_uiState?.peers =
CashFusionState(status: CashFusionStatus.running, info: info);
break;
case fusion.FusionStatus.running:
_uiState?.peers = CashFusionStatus.success;
_uiState?.fusing = CashFusionStatus.running;
_uiState?.peers =
CashFusionState(status: CashFusionStatus.success, info: info);
_uiState?.fusing =
CashFusionState(status: CashFusionStatus.running, info: info);
break;
case fusion.FusionStatus.complete:
_uiState?.fusing = CashFusionStatus.success;
_uiState?.complete = CashFusionStatus.success;
_uiState?.fusing =
CashFusionState(status: CashFusionStatus.success, info: info);
_uiState?.complete =
CashFusionState(status: CashFusionStatus.success, info: info);
break;
case fusion.FusionStatus.failed:
// _uiState?.fusing = CashFusionStatus.failed;
_uiState?.complete = CashFusionStatus.failed;
_uiState?.complete =
CashFusionState(status: CashFusionStatus.failed, info: info);
failCurrentUiState(info);
break;
case fusion.FusionStatus.exception:
_uiState?.complete = CashFusionStatus.failed;
_uiState?.complete =
CashFusionState(status: CashFusionStatus.failed, info: info);
failCurrentUiState(info);
break;
case fusion.FusionStatus.reset:
_uiState?.outputs = CashFusionStatus.waiting;
_uiState?.peers = CashFusionStatus.waiting;
_uiState?.connecting = CashFusionStatus.waiting;
_uiState?.fusing = CashFusionStatus.waiting;
_uiState?.complete = CashFusionStatus.waiting;
_uiState?.fusionState = CashFusionStatus.waiting;
_uiState?.outputs =
CashFusionState(status: CashFusionStatus.waiting, info: info);
_uiState?.peers =
CashFusionState(status: CashFusionStatus.waiting, info: info);
_uiState?.connecting =
CashFusionState(status: CashFusionStatus.waiting, info: info);
_uiState?.fusing =
CashFusionState(status: CashFusionStatus.waiting, info: info);
_uiState?.complete =
CashFusionState(status: CashFusionStatus.waiting, info: info);
_uiState?.fusionState =
CashFusionState(status: CashFusionStatus.waiting, info: info);
break;
}
}
void failCurrentUiState(String? info) {
// Check each _uiState value to see if it is running. If so, set it to failed.
if (_uiState?.connecting == CashFusionStatus.running) {
_uiState?.connecting = CashFusionStatus.failed;
_uiState?.connectionInfo = info;
if (_uiState?.connecting.status == CashFusionStatus.running) {
_uiState?.connecting =
CashFusionState(status: CashFusionStatus.failed, info: info);
}
if (_uiState?.outputs == CashFusionStatus.running) {
_uiState?.outputs = CashFusionStatus.failed;
_uiState?.outputsInfo = info;
if (_uiState?.outputs.status == CashFusionStatus.running) {
_uiState?.outputs =
CashFusionState(status: CashFusionStatus.failed, info: info);
}
if (_uiState?.peers == CashFusionStatus.running) {
_uiState?.peers = CashFusionStatus.failed;
_uiState?.connectionInfo = info;
if (_uiState?.peers.status == CashFusionStatus.running) {
_uiState?.peers =
CashFusionState(status: CashFusionStatus.failed, info: info);
}
if (_uiState?.fusing == CashFusionStatus.running) {
_uiState?.fusing = CashFusionStatus.failed;
_uiState?.fusingInfo = info;
if (_uiState?.fusing.status == CashFusionStatus.running) {
_uiState?.fusing =
CashFusionState(status: CashFusionStatus.failed, info: info);
}
if (_uiState?.complete == CashFusionStatus.running) {
_uiState?.complete = CashFusionStatus.failed;
_uiState?.completeInfo = info;
if (_uiState?.complete.status == CashFusionStatus.running) {
_uiState?.complete =
CashFusionState(status: CashFusionStatus.failed, info: info);
}
}