Files
trainhub-flutter/lib/presentation/calendar/calendar_page.dart
Kazimierz Ciołek 782986a632 Initial commit
2026-02-19 02:49:29 +01:00

117 lines
4.3 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/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';
@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 Scaffold(
appBar: AppBar(title: const Text('Program & Schedule')),
body: state.when(
data: (data) {
if (data.programs.isEmpty) {
return Center(
child: ElevatedButton(
onPressed: () => _showCreateProgramDialog(context, controller),
child: const Text('Create First Program'),
),
);
}
return Column(
children: [
ProgramSelector(
programs: data.programs,
activeProgram: data.activeProgram,
onProgramSelected: (p) => controller.loadProgram(p.id),
onCreateProgram: () =>
_showCreateProgramDialog(context, controller),
),
Expanded(
child: data.activeProgram == null
? const Center(child: Text("Select a program"))
: ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: data.weeks.length + 1,
itemBuilder: (context, index) {
if (index == data.weeks.length) {
return Padding(
padding: const EdgeInsets.symmetric(
vertical: 24.0,
),
child: Center(
child: OutlinedButton.icon(
onPressed: controller.addWeek,
icon: const Icon(Icons.add),
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,
), // logic needs refined params
onDeleteWorkout: controller.deleteWorkout,
);
},
),
),
],
);
},
error: (e, s) => Center(child: Text('Error: $e')),
loading: () => const Center(child: CircularProgressIndicator()),
),
);
}
void _showCreateProgramDialog(
BuildContext context,
CalendarController controller,
) {
final textController = TextEditingController();
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('New Program'),
content: TextField(
controller: textController,
decoration: const InputDecoration(labelText: 'Program Name'),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () {
if (textController.text.isNotEmpty) {
controller.createProgram(textController.text);
Navigator.pop(context);
}
},
child: const Text('Create'),
),
],
),
);
}
}