From 8678e935df8c0341bbc3ef5dacf0ba9b66822385 Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Thu, 30 Nov 2023 13:10:28 +1300 Subject: [PATCH] Ensure days + exercises are selected for new plan --- lib/edit_plan.dart | 100 ++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/lib/edit_plan.dart b/lib/edit_plan.dart index 696e84a..d3cace9 100644 --- a/lib/edit_plan.dart +++ b/lib/edit_plan.dart @@ -78,7 +78,8 @@ class _EditPlanPageState extends State { Widget build(BuildContext context) { List actions = []; if (widget.plan.id.present) - actions.add(IconButton( + actions.add( + IconButton( onPressed: () async { bool? confirm = await _showConfirmationDialog(context); if (!confirm!) return; @@ -86,47 +87,63 @@ class _EditPlanPageState extends State { if (!mounted) return; Navigator.pop(context); }, - icon: const Icon(Icons.delete),),); + icon: const Icon(Icons.delete), + ), + ); return SafeArea( - child: Scaffold( - appBar: AppBar(title: const Text('Edit Plan'), actions: actions), - body: Padding( - padding: const EdgeInsets.all(16.0), - child: material.Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: getChildren, + child: Scaffold( + appBar: AppBar(title: const Text('Edit Plan'), actions: actions), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: material.Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: getChildren, + ), + ), + floatingActionButton: FloatingActionButton( + onPressed: () async { + final days = []; + for (int i = 0; i < daySelections!.length; i++) { + if (daySelections![i]) days.add(weekdayNames[i]); + } + if (days.isEmpty) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Select days first')), + ); + return; + } + + final exercises = []; + for (int i = 0; i < exerciseSelections!.length; i++) { + if (exerciseSelections![i]) exercises.add(names[i]); + } + if (exercises.isEmpty) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('Select exercises first')), + ); + return; + } + + var newPlan = widget.plan.copyWith( + days: Value(days.join(',')), + exercises: Value(exercises.join(',')), + ); + + if (widget.plan.id.present) + await db.update(db.plans).replace(newPlan); + else { + final id = await db.into(db.plans).insert(newPlan); + newPlan = newPlan.copyWith(id: Value(id)); + } + + if (!mounted) return; + Navigator.pop(context); + }, + child: const Icon(Icons.check), ), ), - floatingActionButton: FloatingActionButton( - onPressed: () async { - final days = []; - for (int i = 0; i < daySelections!.length; i++) { - if (daySelections![i]) days.add(weekdayNames[i]); - } - - final exercises = []; - for (int i = 0; i < exerciseSelections!.length; i++) { - if (exerciseSelections![i]) exercises.add(names[i]); - } - - var newPlan = widget.plan.copyWith( - days: Value(days.join(',')), - exercises: Value(exercises.join(',')),); - - if (widget.plan.id.present) - await db.update(db.plans).replace(newPlan); - else { - final id = await db.into(db.plans).insert(newPlan); - newPlan = newPlan.copyWith(id: Value(id)); - } - - if (!mounted) return; - Navigator.pop(context); - }, - child: const Icon(Icons.check), - ), - ),); + ); } List get getChildren { @@ -166,9 +183,10 @@ class _EditPlanPageState extends State { return [ Expanded( - child: ListView( - children: children, - ),), + child: ListView( + children: children, + ), + ), ]; } }