Skip to content

Commit

Permalink
Refactor Sentinel stepper with Pillar impl. (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin authored Mar 18, 2024
1 parent 5bfa152 commit a409712
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 376 deletions.
15 changes: 11 additions & 4 deletions lib/blocs/sentinels/sentinel_qsr_info_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/model/sentinels_qsr_info.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class SentinelsQsrInfoBloc extends BaseBloc<BigInt?> {
Future<void> getQsrDepositedAmount(String address) async {
class SentinelsQsrInfoBloc extends BaseBloc<SentinelsQsrInfo?> {
Future<void> getQsrManagementInfo(String address) async {
try {
addEvent(null);
BigInt response = await zenon!.embedded.sentinel.getDepositedQsr(
BigInt deposit = (await zenon!.embedded.sentinel.getDepositedQsr(
Address.parse(address),
));
BigInt cost = sentinelRegisterQsrAmount;
addEvent(
SentinelsQsrInfo(
deposit: deposit,
cost: cost,
),
);
addEvent(response);
} catch (e, stackTrace) {
addError(e, stackTrace);
}
Expand Down
1 change: 1 addition & 0 deletions lib/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export 'pillars_qsr_info.dart';
export 'plasma_info_wrapper.dart';
export 'p2p_swap/p2p_swap.dart';
export 'p2p_swap/htlc_swap.dart';
export 'sentinels_qsr_info.dart';
9 changes: 9 additions & 0 deletions lib/model/sentinels_qsr_info.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SentinelsQsrInfo {
final BigInt cost;
final BigInt deposit;

SentinelsQsrInfo({
required this.cost,
required this.deposit,
});
}
2 changes: 1 addition & 1 deletion lib/widgets/modular_widgets/modular_widgets.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export 'accelerator_widgets/accelerator_widgets.dart';
export 'dashboard_widgets/dashboard_widgets.dart';
export 'help_widgets/help_widgets.dart';
export 'pillars_widgets/pillars_widgets.dart';
export 'pillar_widgets/pillar_widgets.dart';
export 'plasma_widgets/plasma_widgets.dart';
export 'sentinel_widgets/sentinel_widgets.dart';
export 'settings_widgets/settings_widgets.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class _CreatePillarState extends State<CreatePillar> {
context,
MaterialPageRoute(
builder: (context) => StepperScreen(
stepper: const PillarsStepperContainer(),
stepper: const PillarStepperContainer(),
onStepperNotificationSeeMorePressed:
widget.onStepperNotificationSeeMorePressed,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class _PillarCollectState extends State<PillarCollect> {
);
} catch (e) {
await NotificationUtils.sendNotificationError(
e, 'Error while collecting rewards');
e, 'Error while collecting Pillar rewards');
} finally {
_collectButtonKey.currentState?.animateReverse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ import 'package:zenon_syrius_wallet_flutter/utils/zts_utils.dart';
import 'package:zenon_syrius_wallet_flutter/widgets/widgets.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class PillarsListWidget extends StatefulWidget {
class PillarListWidget extends StatefulWidget {
final String? title;

const PillarsListWidget({Key? key, this.title}) : super(key: key);
const PillarListWidget({Key? key, this.title}) : super(key: key);

@override
State<PillarsListWidget> createState() => _PillarsListWidgetState();
State<PillarListWidget> createState() => _PillarListWidgetState();
}

class _PillarsListWidgetState extends State<PillarsListWidget> {
class _PillarListWidgetState extends State<PillarListWidget> {
final ScrollController _scrollController = ScrollController();

final PagingController<int, PillarInfo> _pagingController = PagingController(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ enum PillarType {
regularPillar,
}

enum PillarsStepperStep {
enum PillarStepperStep {
checkPlasma,
qsrManagement,
znnManagement,
deployPillar,
}

class PillarsStepperContainer extends StatefulWidget {
const PillarsStepperContainer({Key? key}) : super(key: key);
class PillarStepperContainer extends StatefulWidget {
const PillarStepperContainer({Key? key}) : super(key: key);

@override
State createState() {
return _MainPillarsState();
return _MainPillarState();
}
}

class _MainPillarsState extends State<PillarsStepperContainer> {
late PillarsStepperStep _currentStep;
PillarsStepperStep? _lastCompletedStep;
class _MainPillarState extends State<PillarStepperContainer> {
late PillarStepperStep _currentStep;
PillarStepperStep? _lastCompletedStep;

final int _numSteps = PillarsStepperStep.values.length;
final int _numSteps = PillarStepperStep.values.length;

PillarType? _selectedPillarType = PillarType.regularPillar;

Expand All @@ -58,7 +58,6 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
TextEditingController();
final TextEditingController _znnAmountController = TextEditingController();
final TextEditingController _addressController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();

final FocusNode _pillarNameNode = FocusNode();
final FocusNode _pillarRewardNode = FocusNode();
Expand Down Expand Up @@ -220,9 +219,11 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
_qsrAmountController.text,
),
controller: _qsrAmountController,
validator: (value) => _qsrAmountValidator(
validator: (value) => InputValidators.correctValue(
value,
qsrInfo,
_maxQsrAmount,
kQsrCoin.decimals,
BigInt.zero,
),
suffixIcon: _getAmountSuffix(accountInfo),
suffixIconConstraints:
Expand Down Expand Up @@ -251,7 +252,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
children: [
Visibility(
visible: qsrInfo.deposit < qsrInfo.cost,
child: _getDepositQsrViewModel(qsrInfo),
child: _getDepositQsrViewModel(accountInfo, qsrInfo),
),
Visibility(
visible: qsrInfo.deposit >= qsrInfo.cost,
Expand Down Expand Up @@ -352,7 +353,8 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
);
}

Widget _getDepositQsrViewModel(PillarsQsrInfo qsrInfo) {
Widget _getDepositQsrViewModel(
AccountInfo accountInfo, PillarsQsrInfo qsrInfo) {
return ViewModelBuilder<PillarsDepositQsrBloc>.reactive(
onViewModelReady: (model) {
model.stream.listen(
Expand All @@ -378,19 +380,22 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
},
);
},
builder: (_, model, __) => _getDepositQsrButton(model, qsrInfo),
builder: (_, model, __) =>
_getDepositQsrButton(model, accountInfo, qsrInfo),
viewModelBuilder: () => PillarsDepositQsrBloc(),
);
}

Widget _getDepositQsrButton(
PillarsDepositQsrBloc model,
AccountInfo accountInfo,
PillarsQsrInfo qsrInfo,
) {
return LoadingButton.stepper(
key: _depositQsrButtonKey,
text: 'Deposit',
onPressed: _qsrAmountValidator(_qsrAmountController.text, qsrInfo) == null
onPressed: _hasQsrBalance(accountInfo) &&
_qsrAmountValidator(_qsrAmountController.text, qsrInfo) == null
? () => _onDepositButtonPressed(model, qsrInfo)
: null,
outlineColor: AppColors.qsrColor,
Expand All @@ -407,7 +412,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
if (event != null) {
_withdrawButtonKey.currentState?.animateReverse();
_saveProgressAndNavigateToNextStep(
PillarsStepperStep.checkPlasma,
PillarStepperStep.checkPlasma,
);
_pillarsQsrInfoViewModel.getQsrManagementInfo(
_selectedPillarType,
Expand Down Expand Up @@ -460,17 +465,17 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
stepContent: _getPlasmaCheckFutureBuilder(),
stepSubtitle: 'Sufficient Plasma',
stepState: StepperUtils.getStepState(
PillarsStepperStep.checkPlasma.index,
PillarStepperStep.checkPlasma.index,
_lastCompletedStep?.index,
),
context: context,
),
StepperUtils.getMaterialStep(
stepTitle: '${kQsrCoin.symbol} management',
stepContent: _getQsrManagementStep(context, accountInfo),
stepSubtitle: '${kQsrCoin.symbol} Deposited',
stepSubtitle: '${kQsrCoin.symbol} deposited',
stepState: StepperUtils.getStepState(
PillarsStepperStep.qsrManagement.index,
PillarStepperStep.qsrManagement.index,
_lastCompletedStep?.index,
),
context: context,
Expand All @@ -481,7 +486,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
stepContent: _getZnnManagementStepBody(context, accountInfo),
stepSubtitle: '${kZnnCoin.symbol} locked',
stepState: StepperUtils.getStepState(
PillarsStepperStep.znnManagement.index,
PillarStepperStep.znnManagement.index,
_lastCompletedStep?.index,
),
context: context,
Expand All @@ -491,7 +496,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
stepContent: _getDeployPillarStepBody(context),
stepSubtitle: 'Pillar registered',
stepState: StepperUtils.getStepState(
PillarsStepperStep.deployPillar.index,
PillarStepperStep.deployPillar.index,
_lastCompletedStep?.index,
),
context: context,
Expand Down Expand Up @@ -632,7 +637,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
if (response != null) {
_registerButtonKey.currentState?.animateReverse();
_saveProgressAndNavigateToNextStep(
PillarsStepperStep.deployPillar,
PillarStepperStep.deployPillar,
);
setState(() {});
} else {
Expand Down Expand Up @@ -675,7 +680,9 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
),
],
),
kVerticalSpacing,
StepperUtils.getBalanceWidget(kZnnCoin, accountInfo),
kVerticalSpacing,
Row(
children: [
Expanded(
Expand Down Expand Up @@ -722,21 +729,21 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
}

void _onNextPressed() {
if (_lastCompletedStep == PillarsStepperStep.qsrManagement) {
_saveProgressAndNavigateToNextStep(PillarsStepperStep.znnManagement);
if (_lastCompletedStep == PillarStepperStep.qsrManagement) {
_saveProgressAndNavigateToNextStep(PillarStepperStep.znnManagement);
} else if (StepperUtils.getStepState(
PillarsStepperStep.qsrManagement.index,
PillarStepperStep.qsrManagement.index,
_lastCompletedStep?.index,
) ==
custom_material_stepper.StepState.complete) {
setState(() {
_currentStep = PillarsStepperStep.values[_currentStep.index + 1];
_currentStep = PillarStepperStep.values[_currentStep.index + 1];
});
}
}

void _onDeployPressed(PillarsDeployBloc model) {
if (_lastCompletedStep == PillarsStepperStep.znnManagement) {
if (_lastCompletedStep == PillarStepperStep.znnManagement) {
if (_pillarFormKeys
.every((element) => element.currentState!.validate())) {
_registerButtonKey.currentState?.animateForward();
Expand Down Expand Up @@ -908,18 +915,18 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
_iniStepperControllers();
});
}
void _saveProgressAndNavigateToNextStep(PillarsStepperStep completedStep) {

void _saveProgressAndNavigateToNextStep(PillarStepperStep completedStep) {
setState(() {
_lastCompletedStep = completedStep;
if (_lastCompletedStep!.index + 1 < _numSteps) {
_currentStep = PillarsStepperStep.values[completedStep.index + 1];
_currentStep = PillarStepperStep.values[completedStep.index + 1];
}
});
}

void _iniStepperControllers() {
_currentStep = PillarsStepperStep.values.first;
_currentStep = PillarStepperStep.values.first;
_selectedPillarType = PillarType.values.first;
}

Expand All @@ -943,6 +950,9 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
) ==
null;

bool _hasQsrBalance(AccountInfo accountInfo) =>
accountInfo.qsr()! > BigInt.zero;

String? _qsrAmountValidator(String? value, PillarsQsrInfo qsrInfo) =>
InputValidators.correctValue(
value,
Expand All @@ -954,7 +964,7 @@ class _MainPillarsState extends State<PillarsStepperContainer> {

void _onQsrNextPressed() {
setState(() {
_saveProgressAndNavigateToNextStep(PillarsStepperStep.qsrManagement);
_saveProgressAndNavigateToNextStep(PillarStepperStep.qsrManagement);
});
}

Expand Down Expand Up @@ -1013,14 +1023,14 @@ class _MainPillarsState extends State<PillarsStepperContainer> {

void _onPlasmaCheckNextPressed() {
if (_lastCompletedStep == null) {
_saveProgressAndNavigateToNextStep(PillarsStepperStep.checkPlasma);
_saveProgressAndNavigateToNextStep(PillarStepperStep.checkPlasma);
} else if (StepperUtils.getStepState(
PillarsStepperStep.checkPlasma.index,
PillarStepperStep.checkPlasma.index,
_lastCompletedStep?.index,
) ==
custom_material_stepper.StepState.complete) {
setState(() {
_currentStep = PillarsStepperStep.values[_currentStep.index + 1];
_currentStep = PillarStepperStep.values[_currentStep.index + 1];
});
}
}
Expand Down Expand Up @@ -1109,7 +1119,6 @@ class _MainPillarsState extends State<PillarsStepperContainer> {
_pillarMomentumController.dispose();
_znnAmountController.dispose();
_addressController.dispose();
_passwordController.dispose();
_pillarNameNode.dispose();
_pillarRewardNode.dispose();
_pillarMomentumNode.dispose;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export 'create_pillar.dart';
export 'pillar_collect.dart';
export 'pillar_list_widget.dart';
export 'pillar_rewards.dart';
export 'pillar_stepper_container.dart';
export 'pillar_update_stepper.dart';
export 'pillars_list_widget.dart';
export 'pillars_stepper_container.dart';
Loading

0 comments on commit a409712

Please sign in to comment.