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; return false;
} }
bool _succeeded = _connecting == CashFusionStatus.success; bool _succeeded = _connecting.status == CashFusionStatus.success;
_succeeded &= _outputs == CashFusionStatus.success; _succeeded &= _outputs.status == CashFusionStatus.success;
_succeeded &= _peers == CashFusionStatus.success; _succeeded &= _peers.status == CashFusionStatus.success;
_succeeded &= _fusing == CashFusionStatus.success; _succeeded &= _fusing.status == CashFusionStatus.success;
_succeeded &= _complete == CashFusionStatus.success; _succeeded &= _complete.status == CashFusionStatus.success;
_succeeded &= fusionState == CashFusionStatus.success; _succeeded &= fusionState == CashFusionStatus.success;
return _succeeded; return _succeeded;
} }
({CashFusionStatus status, String? info}) _connecting = CashFusionState _connecting =
(status: CashFusionStatus.waiting, info: null); CashFusionState(status: CashFusionStatus.waiting, info: null);
({CashFusionStatus status, String? info}) get connecting => _connecting; CashFusionState get connecting => _connecting;
set connecting(({CashFusionStatus status, String? info}) state) { set connecting(CashFusionState state) {
_connecting = state; _connecting = state;
notifyListeners(); notifyListeners();
} }
CashFusionStatus _outputs = CashFusionStatus.waiting; CashFusionState _outputs =
CashFusionStatus get outputs => _outputs; CashFusionState(status: CashFusionStatus.waiting, info: null);
set outputs(CashFusionStatus state) { CashFusionState get outputs => _outputs;
set outputs(CashFusionState state) {
_outputs = state; _outputs = state;
notifyListeners(); notifyListeners();
} }
CashFusionStatus _peers = CashFusionStatus.waiting; CashFusionState _peers =
CashFusionStatus get peers => _peers; CashFusionState(status: CashFusionStatus.waiting, info: null);
set peers(CashFusionStatus state) { CashFusionState get peers => _peers;
set peers(CashFusionState state) {
_peers = state; _peers = state;
notifyListeners(); notifyListeners();
} }
CashFusionStatus _fusing = CashFusionStatus.waiting; CashFusionState _fusing =
CashFusionStatus get fusing => _fusing; CashFusionState(status: CashFusionStatus.waiting, info: null);
set fusing(CashFusionStatus state) { CashFusionState get fusing => _fusing;
set fusing(CashFusionState state) {
_fusing = state; _fusing = state;
notifyListeners(); notifyListeners();
} }
CashFusionStatus _complete = CashFusionStatus.waiting; CashFusionState _complete =
CashFusionStatus get complete => _complete; CashFusionState(status: CashFusionStatus.waiting, info: null);
set complete(CashFusionStatus state) { CashFusionState get complete => _complete;
set complete(CashFusionState state) {
_complete = state; _complete = state;
notifyListeners(); notifyListeners();
} }
CashFusionStatus _fusionStatus = CashFusionStatus.waiting; CashFusionState _fusionStatus =
CashFusionStatus get fusionState => _fusionStatus; CashFusionState(status: CashFusionStatus.waiting, info: null);
set fusionState(CashFusionStatus fusionStatus) { CashFusionState get fusionState => _fusionStatus;
set fusionState(CashFusionState fusionStatus) {
_fusionStatus = fusionStatus; _fusionStatus = fusionStatus;
notifyListeners(); 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 } enum CashFusionStatus { waiting, running, success, failed }
class CashFusionState {
final CashFusionStatus status;
final String? info;
CashFusionState({required this.status, this.info});
}
class FusionDialog extends StatelessWidget { class FusionDialog extends StatelessWidget {
const FusionDialog({ const FusionDialog({
Key? key, Key? key,

View file

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

View file

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