import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart'; class WorkoutProvider extends ChangeNotifier { TimerProvider timerProvider; final AudioPlayer _audioPlayer = AudioPlayer(); final Source _finishedSoundSource = AssetSource('finish.mp3'); final Source _beepSoundSource = AssetSource('beep.mp3'); bool isWorkoutStarted = false; bool isMuted = false; void mutePlayer() { isMuted = true; _audioPlayer.setVolume(0); notifyListeners(); } void unMutePlayer() { isMuted = false; _audioPlayer.setVolume(1); notifyListeners(); } WorkoutProvider(this.timerProvider); //TODO: outsource all Maps to JSON File! final List _workoutPhases = [ 'Warm-Up', 'High Intensity', 'Low Intensity', 'High Intensity', 'Low Intensity', 'High Intensity', 'Low Intensity', 'High Intensity', 'Cool-down' ]; final Map _phasesDuration = { 'Warm-Up': const Duration(seconds: 5), 'High Intensity': const Duration(seconds: 4), 'Low Intensity': const Duration(seconds: 3), 'Cool-down': const Duration(seconds: 5) }; final Map _phaseSongSources = { 'Warm-Up': AssetSource('warmUp.mp3'), 'High Intensity': AssetSource('workout.mp3'), 'Low Intensity': AssetSource('workout.mp3'), 'Cool-down': AssetSource('cool_down.mp3') }; final Map _phaseColors = { 'Warm-Up': Colors.green, 'High Intensity': Colors.red, 'Low Intensity': Colors.orange, 'Cool-down': Colors.blue }; int _workoutPhaseIndex = 0; String get currentPhase => _workoutPhases[_workoutPhaseIndex]; Duration get currentPhaseDuration => _phasesDuration[currentPhase] ?? const Duration(seconds: 0); bool get isPhaseComplete => timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0; Color get currentPhaseColor => _phaseColors[currentPhase] ?? Colors.blue; void nextPhase() { _audioPlayer.stop(); if (_workoutPhaseIndex < _workoutPhases.length - 1) { _audioPlayer.play(_beepSoundSource); _workoutPhaseIndex += 1; _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(_phaseSongSources[currentPhase]!); timerProvider.startTimer(currentPhaseDuration); }); } else { _audioPlayer.play(_finishedSoundSource); stopWorkout(); } } void startWorkout() { isWorkoutStarted = true; _audioPlayer.play(_beepSoundSource); _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(_phaseSongSources[currentPhase]!); timerProvider.startTimer(currentPhaseDuration); }); } void stopWorkout() { isWorkoutStarted = false; _workoutPhaseIndex = 0; _audioPlayer.stop(); timerProvider.stopTimer(); notifyListeners(); } }