Some refactors

This commit is contained in:
Kazimierz Ciołek
2026-02-19 14:16:03 +01:00
parent 782986a632
commit f943e89430
21 changed files with 490 additions and 7378 deletions

View File

@@ -10,9 +10,9 @@ part 'calendar_controller.g.dart';
@riverpod
class CalendarController extends _$CalendarController {
late final ProgramRepository _programRepo;
late final TrainingPlanRepository _planRepo;
late final ExerciseRepository _exerciseRepo;
late ProgramRepository _programRepo;
late TrainingPlanRepository _planRepo;
late ExerciseRepository _exerciseRepo;
@override
Future<CalendarState> build() async {

View File

@@ -1,9 +1,14 @@
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 {
@@ -14,103 +19,111 @@ class CalendarPage extends ConsumerWidget {
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,
);
},
),
),
],
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),
);
},
error: (e, s) => Center(child: Text('Error: $e')),
loading: () => const Center(child: CircularProgressIndicator()),
}
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()),
);
}
void _showCreateProgramDialog(
Future<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'),
),
],
),
) async {
final name = await TextInputDialog.show(
context,
title: 'New Program',
hintText: 'e.g. 12 Week Strength',
);
if (name != null && name.isNotEmpty) {
controller.createProgram(name);
}
}
}