Initial commit
This commit is contained in:
125
lib/presentation/calendar/calendar_controller.dart
Normal file
125
lib/presentation/calendar/calendar_controller.dart
Normal file
@@ -0,0 +1,125 @@
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:trainhub_flutter/injection.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program_workout.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/program_repository.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/training_plan_repository.dart';
|
||||
import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart';
|
||||
import 'package:trainhub_flutter/presentation/calendar/calendar_state.dart';
|
||||
|
||||
part 'calendar_controller.g.dart';
|
||||
|
||||
@riverpod
|
||||
class CalendarController extends _$CalendarController {
|
||||
late final ProgramRepository _programRepo;
|
||||
late final TrainingPlanRepository _planRepo;
|
||||
late final ExerciseRepository _exerciseRepo;
|
||||
|
||||
@override
|
||||
Future<CalendarState> build() async {
|
||||
_programRepo = getIt<ProgramRepository>();
|
||||
_planRepo = getIt<TrainingPlanRepository>();
|
||||
_exerciseRepo = getIt<ExerciseRepository>();
|
||||
final programs = await _programRepo.getAllPrograms();
|
||||
final plans = await _planRepo.getAll();
|
||||
final exercises = await _exerciseRepo.getAll();
|
||||
if (programs.isEmpty) {
|
||||
return CalendarState(plans: plans, exercises: exercises);
|
||||
}
|
||||
final activeProgram = programs.first;
|
||||
final weeks = await _programRepo.getWeeks(activeProgram.id);
|
||||
final workouts = await _programRepo.getWorkouts(activeProgram.id);
|
||||
return CalendarState(
|
||||
programs: programs,
|
||||
activeProgram: activeProgram,
|
||||
weeks: weeks,
|
||||
workouts: workouts,
|
||||
plans: plans,
|
||||
exercises: exercises,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> loadProgram(String id) async {
|
||||
final program = await _programRepo.getProgram(id);
|
||||
if (program == null) return;
|
||||
final weeks = await _programRepo.getWeeks(id);
|
||||
final workouts = await _programRepo.getWorkouts(id);
|
||||
final current = state.valueOrNull ?? const CalendarState();
|
||||
state = AsyncValue.data(
|
||||
current.copyWith(
|
||||
activeProgram: program,
|
||||
weeks: weeks,
|
||||
workouts: workouts,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> createProgram(String name) async {
|
||||
final program = await _programRepo.createProgram(name);
|
||||
await _reloadFull();
|
||||
await loadProgram(program.id);
|
||||
}
|
||||
|
||||
Future<void> deleteProgram(String id) async {
|
||||
await _programRepo.deleteProgram(id);
|
||||
state = await AsyncValue.guard(() => build());
|
||||
}
|
||||
|
||||
Future<void> duplicateProgram(String sourceId) async {
|
||||
await _programRepo.duplicateProgram(sourceId);
|
||||
await _reloadFull();
|
||||
}
|
||||
|
||||
Future<void> addWeek() async {
|
||||
final current = state.valueOrNull;
|
||||
if (current?.activeProgram == null) return;
|
||||
final nextPosition =
|
||||
current!.weeks.isEmpty ? 1 : current.weeks.last.position + 1;
|
||||
await _programRepo.addWeek(current.activeProgram!.id, nextPosition);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> deleteWeek(String id) async {
|
||||
await _programRepo.deleteWeek(id);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> updateWeekNote(String weekId, String note) async {
|
||||
await _programRepo.updateWeekNote(weekId, note);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> addWorkout(ProgramWorkoutEntity workout) async {
|
||||
await _programRepo.addWorkout(workout);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> updateWorkout(ProgramWorkoutEntity workout) async {
|
||||
await _programRepo.updateWorkout(workout);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> deleteWorkout(String id) async {
|
||||
await _programRepo.deleteWorkout(id);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> toggleWorkoutComplete(String id, bool currentStatus) async {
|
||||
await _programRepo.toggleWorkoutComplete(id, currentStatus);
|
||||
await _reloadProgramDetails();
|
||||
}
|
||||
|
||||
Future<void> _reloadProgramDetails() async {
|
||||
final current = state.valueOrNull;
|
||||
if (current?.activeProgram == null) return;
|
||||
final weeks = await _programRepo.getWeeks(current!.activeProgram!.id);
|
||||
final workouts =
|
||||
await _programRepo.getWorkouts(current.activeProgram!.id);
|
||||
state = AsyncValue.data(
|
||||
current.copyWith(weeks: weeks, workouts: workouts),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _reloadFull() async {
|
||||
state = await AsyncValue.guard(() => build());
|
||||
}
|
||||
}
|
||||
30
lib/presentation/calendar/calendar_controller.g.dart
Normal file
30
lib/presentation/calendar/calendar_controller.g.dart
Normal file
@@ -0,0 +1,30 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'calendar_controller.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$calendarControllerHash() =>
|
||||
r'747a59ba47bf4d1b6a66e3bcc82276e4ad81eb1a';
|
||||
|
||||
/// See also [CalendarController].
|
||||
@ProviderFor(CalendarController)
|
||||
final calendarControllerProvider =
|
||||
AutoDisposeAsyncNotifierProvider<
|
||||
CalendarController,
|
||||
CalendarState
|
||||
>.internal(
|
||||
CalendarController.new,
|
||||
name: r'calendarControllerProvider',
|
||||
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
|
||||
? null
|
||||
: _$calendarControllerHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef _$CalendarController = AutoDisposeAsyncNotifier<CalendarState>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member, deprecated_member_use_from_same_package
|
||||
116
lib/presentation/calendar/calendar_page.dart
Normal file
116
lib/presentation/calendar/calendar_page.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
20
lib/presentation/calendar/calendar_state.dart
Normal file
20
lib/presentation/calendar/calendar_state.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program_week.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program_workout.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/training_plan.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/exercise.dart';
|
||||
|
||||
part 'calendar_state.freezed.dart';
|
||||
|
||||
@freezed
|
||||
class CalendarState with _$CalendarState {
|
||||
const factory CalendarState({
|
||||
@Default([]) List<ProgramEntity> programs,
|
||||
ProgramEntity? activeProgram,
|
||||
@Default([]) List<ProgramWeekEntity> weeks,
|
||||
@Default([]) List<ProgramWorkoutEntity> workouts,
|
||||
@Default([]) List<TrainingPlanEntity> plans,
|
||||
@Default([]) List<ExerciseEntity> exercises,
|
||||
}) = _CalendarState;
|
||||
}
|
||||
329
lib/presentation/calendar/calendar_state.freezed.dart
Normal file
329
lib/presentation/calendar/calendar_state.freezed.dart
Normal file
@@ -0,0 +1,329 @@
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'calendar_state.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
final _privateConstructorUsedError = UnsupportedError(
|
||||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models',
|
||||
);
|
||||
|
||||
/// @nodoc
|
||||
mixin _$CalendarState {
|
||||
List<ProgramEntity> get programs => throw _privateConstructorUsedError;
|
||||
ProgramEntity? get activeProgram => throw _privateConstructorUsedError;
|
||||
List<ProgramWeekEntity> get weeks => throw _privateConstructorUsedError;
|
||||
List<ProgramWorkoutEntity> get workouts => throw _privateConstructorUsedError;
|
||||
List<TrainingPlanEntity> get plans => throw _privateConstructorUsedError;
|
||||
List<ExerciseEntity> get exercises => throw _privateConstructorUsedError;
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
$CalendarStateCopyWith<CalendarState> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $CalendarStateCopyWith<$Res> {
|
||||
factory $CalendarStateCopyWith(
|
||||
CalendarState value,
|
||||
$Res Function(CalendarState) then,
|
||||
) = _$CalendarStateCopyWithImpl<$Res, CalendarState>;
|
||||
@useResult
|
||||
$Res call({
|
||||
List<ProgramEntity> programs,
|
||||
ProgramEntity? activeProgram,
|
||||
List<ProgramWeekEntity> weeks,
|
||||
List<ProgramWorkoutEntity> workouts,
|
||||
List<TrainingPlanEntity> plans,
|
||||
List<ExerciseEntity> exercises,
|
||||
});
|
||||
|
||||
$ProgramEntityCopyWith<$Res>? get activeProgram;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$CalendarStateCopyWithImpl<$Res, $Val extends CalendarState>
|
||||
implements $CalendarStateCopyWith<$Res> {
|
||||
_$CalendarStateCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? programs = null,
|
||||
Object? activeProgram = freezed,
|
||||
Object? weeks = null,
|
||||
Object? workouts = null,
|
||||
Object? plans = null,
|
||||
Object? exercises = null,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
programs: null == programs
|
||||
? _value.programs
|
||||
: programs // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramEntity>,
|
||||
activeProgram: freezed == activeProgram
|
||||
? _value.activeProgram
|
||||
: activeProgram // ignore: cast_nullable_to_non_nullable
|
||||
as ProgramEntity?,
|
||||
weeks: null == weeks
|
||||
? _value.weeks
|
||||
: weeks // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramWeekEntity>,
|
||||
workouts: null == workouts
|
||||
? _value.workouts
|
||||
: workouts // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramWorkoutEntity>,
|
||||
plans: null == plans
|
||||
? _value.plans
|
||||
: plans // ignore: cast_nullable_to_non_nullable
|
||||
as List<TrainingPlanEntity>,
|
||||
exercises: null == exercises
|
||||
? _value.exercises
|
||||
: exercises // ignore: cast_nullable_to_non_nullable
|
||||
as List<ExerciseEntity>,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
$ProgramEntityCopyWith<$Res>? get activeProgram {
|
||||
if (_value.activeProgram == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $ProgramEntityCopyWith<$Res>(_value.activeProgram!, (value) {
|
||||
return _then(_value.copyWith(activeProgram: value) as $Val);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$CalendarStateImplCopyWith<$Res>
|
||||
implements $CalendarStateCopyWith<$Res> {
|
||||
factory _$$CalendarStateImplCopyWith(
|
||||
_$CalendarStateImpl value,
|
||||
$Res Function(_$CalendarStateImpl) then,
|
||||
) = __$$CalendarStateImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call({
|
||||
List<ProgramEntity> programs,
|
||||
ProgramEntity? activeProgram,
|
||||
List<ProgramWeekEntity> weeks,
|
||||
List<ProgramWorkoutEntity> workouts,
|
||||
List<TrainingPlanEntity> plans,
|
||||
List<ExerciseEntity> exercises,
|
||||
});
|
||||
|
||||
@override
|
||||
$ProgramEntityCopyWith<$Res>? get activeProgram;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$CalendarStateImplCopyWithImpl<$Res>
|
||||
extends _$CalendarStateCopyWithImpl<$Res, _$CalendarStateImpl>
|
||||
implements _$$CalendarStateImplCopyWith<$Res> {
|
||||
__$$CalendarStateImplCopyWithImpl(
|
||||
_$CalendarStateImpl _value,
|
||||
$Res Function(_$CalendarStateImpl) _then,
|
||||
) : super(_value, _then);
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? programs = null,
|
||||
Object? activeProgram = freezed,
|
||||
Object? weeks = null,
|
||||
Object? workouts = null,
|
||||
Object? plans = null,
|
||||
Object? exercises = null,
|
||||
}) {
|
||||
return _then(
|
||||
_$CalendarStateImpl(
|
||||
programs: null == programs
|
||||
? _value._programs
|
||||
: programs // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramEntity>,
|
||||
activeProgram: freezed == activeProgram
|
||||
? _value.activeProgram
|
||||
: activeProgram // ignore: cast_nullable_to_non_nullable
|
||||
as ProgramEntity?,
|
||||
weeks: null == weeks
|
||||
? _value._weeks
|
||||
: weeks // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramWeekEntity>,
|
||||
workouts: null == workouts
|
||||
? _value._workouts
|
||||
: workouts // ignore: cast_nullable_to_non_nullable
|
||||
as List<ProgramWorkoutEntity>,
|
||||
plans: null == plans
|
||||
? _value._plans
|
||||
: plans // ignore: cast_nullable_to_non_nullable
|
||||
as List<TrainingPlanEntity>,
|
||||
exercises: null == exercises
|
||||
? _value._exercises
|
||||
: exercises // ignore: cast_nullable_to_non_nullable
|
||||
as List<ExerciseEntity>,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
|
||||
class _$CalendarStateImpl implements _CalendarState {
|
||||
const _$CalendarStateImpl({
|
||||
final List<ProgramEntity> programs = const [],
|
||||
this.activeProgram,
|
||||
final List<ProgramWeekEntity> weeks = const [],
|
||||
final List<ProgramWorkoutEntity> workouts = const [],
|
||||
final List<TrainingPlanEntity> plans = const [],
|
||||
final List<ExerciseEntity> exercises = const [],
|
||||
}) : _programs = programs,
|
||||
_weeks = weeks,
|
||||
_workouts = workouts,
|
||||
_plans = plans,
|
||||
_exercises = exercises;
|
||||
|
||||
final List<ProgramEntity> _programs;
|
||||
@override
|
||||
@JsonKey()
|
||||
List<ProgramEntity> get programs {
|
||||
if (_programs is EqualUnmodifiableListView) return _programs;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(_programs);
|
||||
}
|
||||
|
||||
@override
|
||||
final ProgramEntity? activeProgram;
|
||||
final List<ProgramWeekEntity> _weeks;
|
||||
@override
|
||||
@JsonKey()
|
||||
List<ProgramWeekEntity> get weeks {
|
||||
if (_weeks is EqualUnmodifiableListView) return _weeks;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(_weeks);
|
||||
}
|
||||
|
||||
final List<ProgramWorkoutEntity> _workouts;
|
||||
@override
|
||||
@JsonKey()
|
||||
List<ProgramWorkoutEntity> get workouts {
|
||||
if (_workouts is EqualUnmodifiableListView) return _workouts;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(_workouts);
|
||||
}
|
||||
|
||||
final List<TrainingPlanEntity> _plans;
|
||||
@override
|
||||
@JsonKey()
|
||||
List<TrainingPlanEntity> get plans {
|
||||
if (_plans is EqualUnmodifiableListView) return _plans;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(_plans);
|
||||
}
|
||||
|
||||
final List<ExerciseEntity> _exercises;
|
||||
@override
|
||||
@JsonKey()
|
||||
List<ExerciseEntity> get exercises {
|
||||
if (_exercises is EqualUnmodifiableListView) return _exercises;
|
||||
// ignore: implicit_dynamic_type
|
||||
return EqualUnmodifiableListView(_exercises);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'CalendarState(programs: $programs, activeProgram: $activeProgram, weeks: $weeks, workouts: $workouts, plans: $plans, exercises: $exercises)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$CalendarStateImpl &&
|
||||
const DeepCollectionEquality().equals(other._programs, _programs) &&
|
||||
(identical(other.activeProgram, activeProgram) ||
|
||||
other.activeProgram == activeProgram) &&
|
||||
const DeepCollectionEquality().equals(other._weeks, _weeks) &&
|
||||
const DeepCollectionEquality().equals(other._workouts, _workouts) &&
|
||||
const DeepCollectionEquality().equals(other._plans, _plans) &&
|
||||
const DeepCollectionEquality().equals(
|
||||
other._exercises,
|
||||
_exercises,
|
||||
));
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
runtimeType,
|
||||
const DeepCollectionEquality().hash(_programs),
|
||||
activeProgram,
|
||||
const DeepCollectionEquality().hash(_weeks),
|
||||
const DeepCollectionEquality().hash(_workouts),
|
||||
const DeepCollectionEquality().hash(_plans),
|
||||
const DeepCollectionEquality().hash(_exercises),
|
||||
);
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$CalendarStateImplCopyWith<_$CalendarStateImpl> get copyWith =>
|
||||
__$$CalendarStateImplCopyWithImpl<_$CalendarStateImpl>(this, _$identity);
|
||||
}
|
||||
|
||||
abstract class _CalendarState implements CalendarState {
|
||||
const factory _CalendarState({
|
||||
final List<ProgramEntity> programs,
|
||||
final ProgramEntity? activeProgram,
|
||||
final List<ProgramWeekEntity> weeks,
|
||||
final List<ProgramWorkoutEntity> workouts,
|
||||
final List<TrainingPlanEntity> plans,
|
||||
final List<ExerciseEntity> exercises,
|
||||
}) = _$CalendarStateImpl;
|
||||
|
||||
@override
|
||||
List<ProgramEntity> get programs;
|
||||
@override
|
||||
ProgramEntity? get activeProgram;
|
||||
@override
|
||||
List<ProgramWeekEntity> get weeks;
|
||||
@override
|
||||
List<ProgramWorkoutEntity> get workouts;
|
||||
@override
|
||||
List<TrainingPlanEntity> get plans;
|
||||
@override
|
||||
List<ExerciseEntity> get exercises;
|
||||
|
||||
/// Create a copy of CalendarState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$CalendarStateImplCopyWith<_$CalendarStateImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
252
lib/presentation/calendar/widgets/program_selector.dart
Normal file
252
lib/presentation/calendar/widgets/program_selector.dart
Normal file
@@ -0,0 +1,252 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
import 'package:trainhub_flutter/core/constants/ui_constants.dart';
|
||||
import 'package:trainhub_flutter/core/theme/app_colors.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program.dart';
|
||||
|
||||
class ProgramSelector extends StatelessWidget {
|
||||
final List<ProgramEntity> programs;
|
||||
final ProgramEntity? activeProgram;
|
||||
final ValueChanged<ProgramEntity> onProgramSelected;
|
||||
final VoidCallback onCreateProgram;
|
||||
final VoidCallback onDuplicateProgram;
|
||||
final VoidCallback onDeleteProgram;
|
||||
|
||||
const ProgramSelector({
|
||||
super.key,
|
||||
required this.programs,
|
||||
required this.activeProgram,
|
||||
required this.onProgramSelected,
|
||||
required this.onCreateProgram,
|
||||
required this.onDuplicateProgram,
|
||||
required this.onDeleteProgram,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: UIConstants.pagePadding,
|
||||
vertical: UIConstants.spacing12,
|
||||
),
|
||||
decoration: const BoxDecoration(
|
||||
color: AppColors.surfaceContainer,
|
||||
border: Border(
|
||||
bottom: BorderSide(color: AppColors.border),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// Program icon
|
||||
Container(
|
||||
width: 36,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.accentMuted,
|
||||
borderRadius: BorderRadius.circular(UIConstants.smallBorderRadius),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.calendar_month_rounded,
|
||||
color: AppColors.accent,
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: UIConstants.spacing12),
|
||||
|
||||
// Program dropdown
|
||||
Expanded(
|
||||
child: _ProgramDropdown(
|
||||
programs: programs,
|
||||
activeProgram: activeProgram,
|
||||
onProgramSelected: onProgramSelected,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: UIConstants.spacing12),
|
||||
|
||||
// Action buttons
|
||||
_ToolbarButton(
|
||||
icon: Icons.add_rounded,
|
||||
label: 'New Program',
|
||||
onPressed: onCreateProgram,
|
||||
isPrimary: true,
|
||||
),
|
||||
const SizedBox(width: UIConstants.spacing8),
|
||||
_ToolbarButton(
|
||||
icon: Icons.copy_rounded,
|
||||
label: 'Duplicate',
|
||||
onPressed: activeProgram != null ? onDuplicateProgram : null,
|
||||
),
|
||||
const SizedBox(width: UIConstants.spacing8),
|
||||
_ToolbarButton(
|
||||
icon: Icons.delete_outline_rounded,
|
||||
label: 'Delete',
|
||||
onPressed: activeProgram != null ? onDeleteProgram : null,
|
||||
isDestructive: true,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ProgramDropdown extends StatelessWidget {
|
||||
final List<ProgramEntity> programs;
|
||||
final ProgramEntity? activeProgram;
|
||||
final ValueChanged<ProgramEntity> onProgramSelected;
|
||||
|
||||
const _ProgramDropdown({
|
||||
required this.programs,
|
||||
required this.activeProgram,
|
||||
required this.onProgramSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
height: 38,
|
||||
padding: const EdgeInsets.symmetric(horizontal: UIConstants.spacing12),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.zinc950,
|
||||
borderRadius: BorderRadius.circular(UIConstants.smallBorderRadius),
|
||||
border: Border.all(color: AppColors.border),
|
||||
),
|
||||
child: DropdownButtonHideUnderline(
|
||||
child: DropdownButton<String>(
|
||||
value: activeProgram?.id,
|
||||
isExpanded: true,
|
||||
icon: const Icon(
|
||||
Icons.unfold_more_rounded,
|
||||
size: 18,
|
||||
color: AppColors.textMuted,
|
||||
),
|
||||
dropdownColor: AppColors.surfaceContainer,
|
||||
borderRadius: BorderRadius.circular(UIConstants.smallBorderRadius),
|
||||
style: GoogleFonts.inter(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
hint: Text(
|
||||
'Select a program',
|
||||
style: GoogleFonts.inter(
|
||||
fontSize: 14,
|
||||
color: AppColors.textMuted,
|
||||
),
|
||||
),
|
||||
items: programs.map((p) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: p.id,
|
||||
child: Text(
|
||||
p.name,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (id) {
|
||||
if (id != null) {
|
||||
final program = programs.firstWhere((p) => p.id == id);
|
||||
onProgramSelected(program);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ToolbarButton extends StatefulWidget {
|
||||
final IconData icon;
|
||||
final String label;
|
||||
final VoidCallback? onPressed;
|
||||
final bool isPrimary;
|
||||
final bool isDestructive;
|
||||
|
||||
const _ToolbarButton({
|
||||
required this.icon,
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
this.isPrimary = false,
|
||||
this.isDestructive = false,
|
||||
});
|
||||
|
||||
@override
|
||||
State<_ToolbarButton> createState() => _ToolbarButtonState();
|
||||
}
|
||||
|
||||
class _ToolbarButtonState extends State<_ToolbarButton> {
|
||||
bool _isHovered = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isDisabled = widget.onPressed == null;
|
||||
|
||||
Color bgColor;
|
||||
Color fgColor;
|
||||
Color borderColor;
|
||||
|
||||
if (isDisabled) {
|
||||
bgColor = Colors.transparent;
|
||||
fgColor = AppColors.zinc600;
|
||||
borderColor = AppColors.border;
|
||||
} else if (widget.isPrimary) {
|
||||
bgColor = _isHovered ? AppColors.accent : AppColors.accentMuted;
|
||||
fgColor = _isHovered ? AppColors.zinc950 : AppColors.accent;
|
||||
borderColor = AppColors.accent.withValues(alpha: 0.3);
|
||||
} else if (widget.isDestructive) {
|
||||
bgColor =
|
||||
_isHovered ? AppColors.destructiveMuted : Colors.transparent;
|
||||
fgColor =
|
||||
_isHovered ? AppColors.destructive : AppColors.textSecondary;
|
||||
borderColor = _isHovered ? AppColors.destructive.withValues(alpha: 0.3) : AppColors.border;
|
||||
} else {
|
||||
bgColor = _isHovered ? AppColors.surfaceContainerHigh : Colors.transparent;
|
||||
fgColor = _isHovered ? AppColors.textPrimary : AppColors.textSecondary;
|
||||
borderColor = AppColors.border;
|
||||
}
|
||||
|
||||
return MouseRegion(
|
||||
onEnter: (_) => setState(() => _isHovered = true),
|
||||
onExit: (_) => setState(() => _isHovered = false),
|
||||
child: Tooltip(
|
||||
message: widget.label,
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: widget.onPressed,
|
||||
borderRadius:
|
||||
BorderRadius.circular(UIConstants.smallBorderRadius),
|
||||
child: AnimatedContainer(
|
||||
duration: UIConstants.animationDuration,
|
||||
height: 34,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: UIConstants.spacing12,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
borderRadius:
|
||||
BorderRadius.circular(UIConstants.smallBorderRadius),
|
||||
border: Border.all(color: borderColor),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(widget.icon, size: 16, color: fgColor),
|
||||
const SizedBox(width: UIConstants.spacing4),
|
||||
Text(
|
||||
widget.label,
|
||||
style: GoogleFonts.inter(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: fgColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
244
lib/presentation/calendar/widgets/program_week_view.dart
Normal file
244
lib/presentation/calendar/widgets/program_week_view.dart
Normal file
@@ -0,0 +1,244 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:trainhub_flutter/core/utils/id_generator.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program_week.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/program_workout.dart';
|
||||
import 'package:trainhub_flutter/domain/entities/training_plan.dart';
|
||||
|
||||
class ProgramWeekView extends StatelessWidget {
|
||||
final ProgramWeekEntity week;
|
||||
final List<ProgramWorkoutEntity> workouts;
|
||||
final List<TrainingPlanEntity> availablePlans;
|
||||
final Function(ProgramWorkoutEntity) onAddWorkout;
|
||||
final Function(String) onDeleteWorkout;
|
||||
|
||||
const ProgramWeekView({
|
||||
super.key,
|
||||
required this.week,
|
||||
required this.workouts,
|
||||
required this.availablePlans,
|
||||
required this.onAddWorkout,
|
||||
required this.onDeleteWorkout,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 24),
|
||||
elevation: 2,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"Week ${week.position}",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
const Divider(),
|
||||
SizedBox(
|
||||
height: 500, // Fixed height for the week grid, or make it dynamic
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: List.generate(7, (dayIndex) {
|
||||
final dayNum = dayIndex + 1;
|
||||
final dayWorkouts = workouts
|
||||
.where((w) => w.day == dayNum.toString())
|
||||
.toList();
|
||||
|
||||
return Expanded(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: dayIndex < 6
|
||||
? const Border(
|
||||
right: BorderSide(
|
||||
color: Colors.grey,
|
||||
width: 0.5,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
color: dayIndex % 2 == 0
|
||||
? Theme.of(context).colorScheme.surfaceContainerLow
|
||||
: null,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.surfaceContainerHigh,
|
||||
border: const Border(
|
||||
bottom: BorderSide(
|
||||
color: Colors.grey,
|
||||
width: 0.5,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
_getDayName(dayNum),
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
InkWell(
|
||||
onTap: () =>
|
||||
_showAddWorkoutSheet(context, dayNum),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: const Icon(
|
||||
Icons.add_circle_outline,
|
||||
size: 20,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(4),
|
||||
child: Column(
|
||||
children: [
|
||||
if (dayWorkouts.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 16.0),
|
||||
child: Center(
|
||||
child: Text(
|
||||
"Rest",
|
||||
style: TextStyle(
|
||||
color: Theme.of(
|
||||
context,
|
||||
).colorScheme.onSurfaceVariant,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
else
|
||||
...dayWorkouts.map(
|
||||
(w) => Card(
|
||||
margin: const EdgeInsets.only(
|
||||
bottom: 4,
|
||||
),
|
||||
child: ListTile(
|
||||
contentPadding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 0,
|
||||
),
|
||||
visualDensity: VisualDensity.compact,
|
||||
title: Text(
|
||||
w.name ?? 'Untitled',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: InkWell(
|
||||
onTap: () => onDeleteWorkout(w.id),
|
||||
borderRadius: BorderRadius.circular(
|
||||
12,
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.close,
|
||||
size: 14,
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
// Optional: Edit on tap
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _getDayName(int day) {
|
||||
const days = [
|
||||
'Monday',
|
||||
'Tuesday',
|
||||
'Wednesday',
|
||||
'Thursday',
|
||||
'Friday',
|
||||
'Saturday',
|
||||
'Sunday',
|
||||
];
|
||||
if (day >= 1 && day <= 7) return days[day - 1];
|
||||
return 'Day $day';
|
||||
}
|
||||
|
||||
void _showAddWorkoutSheet(BuildContext context, int dayNum) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
builder: (context) => Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16.0,
|
||||
vertical: 8.0,
|
||||
),
|
||||
child: Text(
|
||||
"Select Training Plan",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
const Divider(),
|
||||
if (availablePlans.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: Center(
|
||||
child: Text("No training plans available. Create one first!"),
|
||||
),
|
||||
),
|
||||
...availablePlans
|
||||
.map(
|
||||
(plan) => ListTile(
|
||||
leading: const Icon(Icons.fitness_center),
|
||||
title: Text(plan.name),
|
||||
subtitle: Text("${plan.totalExercises} exercises"),
|
||||
onTap: () {
|
||||
final newWorkout = ProgramWorkoutEntity(
|
||||
id: IdGenerator.generate(),
|
||||
programId: week.programId,
|
||||
weekId: week.id,
|
||||
day: dayNum.toString(),
|
||||
type: 'workout',
|
||||
name: plan.name,
|
||||
refId: plan.id,
|
||||
description: "${plan.sections.length} sections",
|
||||
completed: false,
|
||||
);
|
||||
onAddWorkout(newWorkout);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user