2023-04-13 18:58:26 +12:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/material.dart' as material;
|
|
|
|
import 'package:fmassive/database.dart';
|
2023-06-25 15:47:45 +12:00
|
|
|
import 'package:fmassive/days.dart';
|
2023-11-10 23:43:06 +13:00
|
|
|
import 'package:fmassive/exercises.dart';
|
2023-04-13 18:58:26 +12:00
|
|
|
import 'package:fmassive/main.dart';
|
|
|
|
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> {
|
|
|
|
late PlansCompanion plan;
|
2023-11-10 23:43:06 +13:00
|
|
|
List<String?> names = [];
|
|
|
|
|
|
|
|
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-10 23:43:06 +13:00
|
|
|
|
|
|
|
getDistinctNames().then((value) {
|
|
|
|
setState(() {
|
|
|
|
names = value;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-04-13 18:58:26 +12:00
|
|
|
plan = widget.plan;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-11-11 15:18:23 +13:00
|
|
|
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 {
|
2023-11-11 15:18:23 +13:00
|
|
|
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 {
|
|
|
|
if (plan.id.present)
|
2023-11-10 17:09:27 +13:00
|
|
|
await db.update(db.plans).replace(plan);
|
2023-04-13 18:58:26 +12:00
|
|
|
else
|
|
|
|
await db.into(db.plans).insert(plan);
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.check),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
2023-11-10 23:43:06 +13:00
|
|
|
|
|
|
|
List<Widget> get getChildren {
|
|
|
|
return [
|
|
|
|
Expanded(
|
|
|
|
child: Days(
|
|
|
|
onChanged: (days) {
|
|
|
|
setState(() {
|
|
|
|
plan = plan.copyWith(days: Value(days));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
days: plan.days.value,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
names.isNotEmpty
|
|
|
|
? Expanded(
|
|
|
|
child: Exercises(
|
|
|
|
onChanged: (exercises) {
|
|
|
|
setState(() {
|
|
|
|
plan = plan.copyWith(exercises: Value(exercises));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
exercises: plan.exercises.value,
|
|
|
|
names: names,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: const CircularProgressIndicator(),
|
|
|
|
];
|
|
|
|
}
|
2023-04-13 18:58:26 +12:00
|
|
|
}
|