import 'dart:async'; import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/models/workout.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart'; import '../globals.dart' as globals; class WorkoutProvider extends ChangeNotifier { TimerProvider timerProvider; final AudioPlayer _audioPlayer = AudioPlayer(); late StreamSubscription _onCompleteSubscription; final Source _finishedSoundSource = AssetSource('finish.mp3'); final Source _beepSoundSource = AssetSource('beep.mp3'); bool isWorkoutStarted = false; bool isWorkoutComplete = false; bool isMuted = false; int motivationBefore = 50; int motivationAfter = 50; void mutePlayer() { isMuted = true; _audioPlayer.setVolume(0); notifyListeners(); } void unMutePlayer() { isMuted = false; _audioPlayer.setVolume(1); notifyListeners(); } WorkoutProvider(this.timerProvider); final List _workoutPhases = [ 'Warm-Up', 'High Intensity', 'Low Intensity', 'High Intensity', 'Low Intensity', 'High Intensity', 'Low Intensity', 'High Intensity', 'Cool-down' ]; int _workoutPhaseIndex = 0; String 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']; /* bool get isWorkoutComplete => _workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */ void nextPhase() { _onCompleteSubscription.cancel(); _audioPlayer.stop(); if (_workoutPhaseIndex < _workoutPhases.length - 1) { _audioPlayer.play(_beepSoundSource); _workoutPhaseIndex += 1; _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(currentPhaseSource); }); timerProvider.startTimer(currentPhaseDuration); } else { //workout completed _audioPlayer.play(_finishedSoundSource); stopWorkout(); } } void startWorkout() { isWorkoutStarted = true; isWorkoutComplete = false; _audioPlayer.play(_beepSoundSource).whenComplete(() => null); _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(currentPhaseSource); }); timerProvider.startTimer(currentPhaseDuration); } void stopWorkout() { isWorkoutStarted = false; isWorkoutComplete = true; _audioPlayer.stop(); timerProvider.stopTimer(); notifyListeners(); } void saveWorkout() { Workout workout = Workout(motivationBefore, motivationAfter, DateTime.now()); globals.databaseService.addWorkout(workout); } } Map> _workoutPhaseSettings = { 'Warm-Up': { 'duration': const Duration(seconds: 5), 'source': AssetSource('warmUp.mp3'), 'color': Colors.green }, 'High Intensity': { 'duration': const Duration(seconds: 4), 'source': AssetSource('workout.mp3'), 'color': Colors.red }, 'Low Intensity': { 'duration': const Duration(seconds: 3), 'source': AssetSource('workout.mp3'), 'color': Colors.orange }, 'Cool-down': { 'duration': const Duration(seconds: 5), 'source': AssetSource('cool_down.mp3'), 'color': Colors.blue } };