Skip to content

Commit

Permalink
fix goal error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NeXTormer committed Feb 14, 2024
1 parent 30c4b89 commit 7e0a374
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions lib/screens/edit_goal_data_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
child: FredericButton(
widget.isNewGoal ? 'Create' : 'Save',
onPressed: () {
saveData();
Navigator.of(context).pop();
final success = saveData();
if (success) {
Navigator.of(context).pop();
}
})),
],
),
Expand Down Expand Up @@ -193,40 +195,35 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
return FormError.Success;
}

void saveData() {
Future<void> _showErrorMessage(String text) async {
await showDialog<bool>(
context: context,
builder: (ctx) {
return Container();
},
);
await showDialog(
context: context,
builder: (ctx) {
return FredericActionDialog(
title: text,
infoOnly: true,
onConfirm: () => Navigator.of(ctx).pop());
},
);
}
Future<void> _showErrorMessage(String text) async {
await showDialog(
context: context,
builder: (ctx) {
return FredericActionDialog(
title: text,
infoOnly: true,
onConfirm: () {},
);
},
);
}

bool saveData() {
if (widget.isNewGoal) {
final formStatus = _checkFormInputs();
switch (formStatus) {
case FormError.CurrentStateEqualsEndStart:
_showErrorMessage('The current state must not match the end state!');
return;
return false;
case FormError.StartStateEqualsEndState:
_showErrorMessage('The start state must not match the end state!');
return;
return false;
case FormError.StartDateEqualsEndDate:
_showErrorMessage('The start date must not match the end date!');
break;
return false;
case FormError.EndDateSmallerThenStartDate:
_showErrorMessage('The end date must not be before the start date!');
break;
return false;
case FormError.Success:
widget.goal.updateData(
activityID: dummyActivity == null ? '' : dummyActivity!.id,
Expand All @@ -247,10 +244,10 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
.add(FredericGoalCreateEvent(widget.goal));

FredericBackend.instance.userManager.state.goalsCount += 1;
return;
return true;
default:
_showErrorMessage('Some error happened');
return;
return false;
}
} else {
widget.goal.updateData(
Expand All @@ -266,6 +263,8 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
FredericBackend.instance.goalManager
.add(FredericGoalUpdateEvent(widget.goal));
}

return true;
}

void addNewActivityTracker(BuildContext context) {
Expand Down Expand Up @@ -338,8 +337,10 @@ class _EditGoalDataScreenState extends State<EditGoalDataScreen> {
Expanded(child: Container()),
GestureDetector(
onTap: () {
saveData();
Navigator.of(context).pop();
final success = saveData();
if (success) {
Navigator.of(context).pop();
}
},
child: Text(
widget.isNewGoal ? 'Create' : 'Save',
Expand Down

0 comments on commit 7e0a374

Please sign in to comment.