fmassive/lib/edit_plan.dart

176 lines
4.9 KiB
Dart
Raw Normal View History

2023-04-13 18:58:26 +12:00
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as material;
2023-11-28 20:04:20 +13:00
import 'package:fmassive/constants.dart';
2023-04-13 18:58:26 +12:00
import 'package:fmassive/database.dart';
import 'package:fmassive/main.dart';
2023-11-30 13:01:42 +13:00
import 'package:fmassive/start_plan.dart';
2023-04-13 18:58:26 +12:00
import 'package:moor_flutter/moor_flutter.dart';
class EditPlanPage extends StatefulWidget {
final PlansCompanion plan;
const EditPlanPage({required this.plan, super.key});
@override
createState() => _EditPlanPageState();
}
class _EditPlanPageState extends State<EditPlanPage> {
2023-11-10 23:43:06 +13:00
List<String?> names = [];
2023-11-28 20:04:20 +13:00
List<bool>? daySelections;
List<bool>? exerciseSelections;
2023-11-10 23:43:06 +13:00
Future<List<String?>> getDistinctNames() async {
final names = await (db.gymSets.selectOnly(distinct: true)
..addColumns([db.gymSets.name]))
.get();
return names.map((name) => name.read(db.gymSets.name)).toList();
}
2023-04-13 18:58:26 +12:00
@override
void initState() {
super.initState();
2023-11-28 20:04:20 +13:00
final dayList = widget.plan.days.value.split(',');
daySelections = weekdayNames.map((day) => dayList.contains(day)).toList();
2023-11-10 23:43:06 +13:00
getDistinctNames().then((value) {
setState(() {
names = value;
2023-11-28 20:04:20 +13:00
final exercises = widget.plan.exercises.value.split(',');
exerciseSelections =
names.map((name) => exercises.contains(name)).toList();
2023-11-10 23:43:06 +13:00
});
});
2023-04-13 18:58:26 +12:00
}
@override
dispose() {
super.dispose();
}
Future<bool?> _showConfirmationDialog(BuildContext context) {
return showDialog<bool>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Confirm Delete'),
content: const Text('Are you sure you want to delete this plan?'),
actions: <Widget>[
ElevatedButton(
child: const Text('Yes'),
onPressed: () {
Navigator.pop(context, true); // showDialog() returns true
},
),
ElevatedButton(
child: const Text('No'),
onPressed: () {
Navigator.pop(context, false); // showDialog() returns false
},
),
],
);
},
);
}
2023-04-13 18:58:26 +12:00
@override
Widget build(BuildContext context) {
List<Widget> actions = [];
if (widget.plan.id.present)
actions.add(IconButton(
onPressed: () async {
bool? confirm = await _showConfirmationDialog(context);
if (!confirm!) return;
2023-04-13 18:58:26 +12:00
await db.plans.deleteOne(widget.plan);
if (!mounted) return;
Navigator.pop(context);
},
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,
2023-11-10 23:43:06 +13:00
children: getChildren,
2023-04-13 18:58:26 +12:00
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
2023-11-29 16:58:40 +13:00
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]);
}
2023-11-30 13:01:42 +13:00
var newPlan = widget.plan.copyWith(
2023-11-29 16:58:40 +13:00
days: Value(days.join(',')),
exercises: Value(exercises.join(',')));
2023-11-30 13:01:42 +13:00
if (widget.plan.id.present)
2023-11-29 16:58:40 +13:00
await db.update(db.plans).replace(newPlan);
2023-11-30 13:01:42 +13:00
else {
final id = await db.into(db.plans).insert(newPlan);
newPlan = newPlan.copyWith(id: Value(id));
}
2023-04-13 18:58:26 +12:00
if (!mounted) return;
Navigator.pop(context);
},
child: const Icon(Icons.check),
),
));
}
2023-11-10 23:43:06 +13:00
List<Widget> get getChildren {
2023-11-28 20:04:20 +13:00
final List<Widget> children = [
Text('Days', style: Theme.of(context).textTheme.headlineSmall),
];
final days = List.generate(7, (index) {
return SwitchListTile(
title: Text(weekdayNames[index]),
value: daySelections![index],
onChanged: (value) {
setState(() {
daySelections![index] = value;
});
},
);
});
final exercises = List.generate(names.length, (index) {
return SwitchListTile(
title: Text(names[index] ?? ''),
value: exerciseSelections![index],
onChanged: (value) {
setState(() {
exerciseSelections![index] = value;
});
},
);
});
children.addAll(days);
children.add(
Text('Exercises', style: Theme.of(context).textTheme.headlineSmall),
);
children.addAll(exercises);
2023-11-10 23:43:06 +13:00
return [
Expanded(
2023-11-28 20:04:20 +13:00
child: ListView(
children: children,
))
2023-11-10 23:43:06 +13:00
];
}
2023-04-13 18:58:26 +12:00
}