130 lines
5.0 KiB
Dart
130 lines
5.0 KiB
Dart
import 'package:auto_route/auto_route.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:trainhub_flutter/core/theme/app_colors.dart';
|
|
import 'package:trainhub_flutter/core/constants/ui_constants.dart';
|
|
import 'package:trainhub_flutter/presentation/calendar/calendar_controller.dart';
|
|
import 'package:trainhub_flutter/presentation/calendar/widgets/program_selector.dart';
|
|
import 'package:trainhub_flutter/presentation/calendar/widgets/program_week_view.dart';
|
|
import 'package:trainhub_flutter/presentation/common/widgets/app_empty_state.dart';
|
|
import 'package:trainhub_flutter/presentation/common/dialogs/text_input_dialog.dart';
|
|
import 'package:trainhub_flutter/presentation/common/dialogs/confirm_dialog.dart';
|
|
|
|
@RoutePage()
|
|
class CalendarPage extends ConsumerWidget {
|
|
const CalendarPage({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final state = ref.watch(calendarControllerProvider);
|
|
final controller = ref.read(calendarControllerProvider.notifier);
|
|
|
|
return state.when(
|
|
data: (data) {
|
|
if (data.programs.isEmpty) {
|
|
return AppEmptyState(
|
|
icon: Icons.calendar_month_outlined,
|
|
title: 'No programs yet',
|
|
subtitle: 'Create your first training program to start scheduling workouts',
|
|
actionLabel: 'Create Program',
|
|
onAction: () => _showCreateProgramDialog(context, controller),
|
|
);
|
|
}
|
|
|
|
return Column(
|
|
children: [
|
|
ProgramSelector(
|
|
programs: data.programs,
|
|
activeProgram: data.activeProgram,
|
|
onProgramSelected: (p) => controller.loadProgram(p.id),
|
|
onCreateProgram: () =>
|
|
_showCreateProgramDialog(context, controller),
|
|
onDuplicateProgram: () {
|
|
if (data.activeProgram != null) {
|
|
controller.duplicateProgram(data.activeProgram!.id);
|
|
}
|
|
},
|
|
onDeleteProgram: () async {
|
|
if (data.activeProgram != null) {
|
|
final confirmed = await ConfirmDialog.show(
|
|
context,
|
|
title: 'Delete Program?',
|
|
message:
|
|
'Are you sure you want to delete "${data.activeProgram!.name}"? This cannot be undone.',
|
|
);
|
|
if (confirmed == true) {
|
|
controller.deleteProgram(data.activeProgram!.id);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
Expanded(
|
|
child: data.activeProgram == null
|
|
? const Center(
|
|
child: Text(
|
|
'Select a program to view its schedule',
|
|
style: TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 14,
|
|
),
|
|
),
|
|
)
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.all(UIConstants.spacing24),
|
|
itemCount: data.weeks.length + 1,
|
|
itemBuilder: (context, index) {
|
|
if (index == data.weeks.length) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: UIConstants.spacing24),
|
|
child: Center(
|
|
child: OutlinedButton.icon(
|
|
onPressed: controller.addWeek,
|
|
icon: const Icon(Icons.add, size: 18),
|
|
label: const Text('Add Week'),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final week = data.weeks[index];
|
|
final weekWorkouts = data.workouts
|
|
.where((w) => w.weekId == week.id)
|
|
.toList();
|
|
return ProgramWeekView(
|
|
week: week,
|
|
workouts: weekWorkouts,
|
|
availablePlans: data.plans,
|
|
onAddWorkout: (workout) =>
|
|
controller.addWorkout(workout),
|
|
onDeleteWorkout: controller.deleteWorkout,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
error: (e, s) => Center(
|
|
child: Text(
|
|
'Error: $e',
|
|
style: const TextStyle(color: AppColors.destructive),
|
|
),
|
|
),
|
|
loading: () => const Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
Future<void> _showCreateProgramDialog(
|
|
BuildContext context,
|
|
CalendarController controller,
|
|
) async {
|
|
final name = await TextInputDialog.show(
|
|
context,
|
|
title: 'New Program',
|
|
hintText: 'e.g. 12 Week Strength',
|
|
);
|
|
if (name != null && name.isNotEmpty) {
|
|
controller.createProgram(name);
|
|
}
|
|
}
|
|
}
|