import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/models/workout.dart'; import 'package:smoke_cess_app/providers/audio_provider.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart'; import '../globals.dart' as globals; enum WorkoutPhases { warmUp, highIntensity, lowIntensity, coolDown, } class WorkoutProvider extends ChangeNotifier { final TimerProvider timerProvider; final AudioProvider audioProvider; bool isWorkoutStarted = false; bool isWorkoutComplete = false; int motivationBefore = 50; int motivationAfter = 50; int _workoutPhaseIndex = 0; final List _workoutPhases = [ WorkoutPhases.warmUp, WorkoutPhases.highIntensity, WorkoutPhases.lowIntensity, WorkoutPhases.highIntensity, WorkoutPhases.lowIntensity, WorkoutPhases.highIntensity, WorkoutPhases.lowIntensity, WorkoutPhases.highIntensity, WorkoutPhases.coolDown, ]; WorkoutProvider(this.timerProvider, this.audioProvider); WorkoutPhases get currentPhase => _workoutPhases[_workoutPhaseIndex]; Duration get currentPhaseDuration => _workoutPhaseSettings[currentPhase]!['duration']; bool get isPhaseComplete => timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0; Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; AssetSource get currentPhaseSource => _workoutPhaseSettings[currentPhase]!['source']; String get currentPhaseTitle => _workoutPhaseSettings[currentPhase]!['title']; void nextPhase() { if (_workoutPhaseIndex < _workoutPhases.length - 1) { _workoutPhaseIndex += 1; audioProvider.playSourceAfterBeep(currentPhaseSource); timerProvider.startTimer(currentPhaseDuration); } else { //workout completed audioProvider.playFinishSound; stopWorkout(); } } void startWorkout() { isWorkoutStarted = true; isWorkoutComplete = false; audioProvider.playSourceAfterBeep(currentPhaseSource); timerProvider.startTimer(currentPhaseDuration); } void stopWorkout() { isWorkoutStarted = false; isWorkoutComplete = true; _cleanUp(); notifyListeners(); } void interruptWorkout() { isWorkoutStarted = false; isWorkoutComplete = false; _cleanUp(); notifyListeners(); } void _cleanUp() { audioProvider.stop(); timerProvider.stopTimer(); } void saveWorkout() { Workout workout = Workout(motivationBefore, motivationAfter, DateTime.now()); globals.databaseService.addWorkout(workout); } @override void dispose() { _cleanUp(); super.dispose(); } } Map> _workoutPhaseSettings = { WorkoutPhases.warmUp: { 'title': 'Warm Up', 'duration': const Duration(minutes: 5), 'source': AssetSource('warmUp.mp3'), 'color': Colors.green }, WorkoutPhases.highIntensity: { 'title': 'High Intensity', 'duration': const Duration(minutes: 4), 'source': AssetSource('workout.mp3'), 'color': Colors.red }, WorkoutPhases.lowIntensity: { 'title': 'Low Intensity', 'duration': const Duration(minutes: 3), 'source': AssetSource('workout.mp3'), 'color': Colors.orange }, WorkoutPhases.coolDown: { 'title': 'Cool Down', 'duration': const Duration(minutes: 5), 'source': AssetSource('cool_down.mp3'), 'color': Colors.blue } };