2023-02-28 13:05:51 +01:00
|
|
|
import 'package:audioplayers/audioplayers.dart';
|
2023-02-27 01:20:00 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-02-28 13:05:51 +01:00
|
|
|
import 'package:smoke_cess_app/providers/timer_provider.dart';
|
2023-02-27 01:20:00 +01:00
|
|
|
|
|
|
|
class WorkoutProvider extends ChangeNotifier {
|
2023-02-28 13:05:51 +01:00
|
|
|
TimerProvider timerProvider;
|
|
|
|
|
|
|
|
final AudioPlayer _audioPlayer = AudioPlayer();
|
2023-02-28 14:58:32 +01:00
|
|
|
final Source _finishedSoundSource = AssetSource('finish.mp3');
|
|
|
|
final Source _beepSoundSource = AssetSource('beep.mp3');
|
2023-02-28 15:07:47 +01:00
|
|
|
bool isWorkoutStarted = false;
|
2023-03-01 12:34:35 +01:00
|
|
|
bool isMuted = false;
|
|
|
|
|
|
|
|
void mutePlayer() {
|
|
|
|
isMuted = true;
|
|
|
|
_audioPlayer.setVolume(0);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unMutePlayer() {
|
|
|
|
isMuted = false;
|
|
|
|
_audioPlayer.setVolume(1);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2023-02-28 13:05:51 +01:00
|
|
|
|
|
|
|
WorkoutProvider(this.timerProvider);
|
|
|
|
|
2023-03-01 13:07:01 +01:00
|
|
|
//TODO: outsource all Maps to JSON File!
|
2023-02-27 01:20:00 +01:00
|
|
|
final List<String> _workoutPhases = [
|
|
|
|
'Warm-Up',
|
|
|
|
'High Intensity',
|
|
|
|
'Low Intensity',
|
|
|
|
'High Intensity',
|
|
|
|
'Low Intensity',
|
|
|
|
'High Intensity',
|
|
|
|
'Low Intensity',
|
|
|
|
'High Intensity',
|
|
|
|
'Cool-down'
|
|
|
|
];
|
|
|
|
final Map<String, Duration> _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)
|
|
|
|
};
|
2023-02-28 13:05:51 +01:00
|
|
|
final Map<String, Source> _phaseSongSources = {
|
|
|
|
'Warm-Up': AssetSource('warmUp.mp3'),
|
|
|
|
'High Intensity': AssetSource('workout.mp3'),
|
|
|
|
'Low Intensity': AssetSource('workout.mp3'),
|
|
|
|
'Cool-down': AssetSource('cool_down.mp3')
|
|
|
|
};
|
2023-03-01 13:07:01 +01:00
|
|
|
final Map<String, Color> _phaseColors = {
|
|
|
|
'Warm-Up': Colors.green,
|
|
|
|
'High Intensity': Colors.red,
|
|
|
|
'Low Intensity': Colors.orange,
|
|
|
|
'Cool-down': Colors.blue
|
|
|
|
};
|
2023-02-27 01:20:00 +01:00
|
|
|
int _workoutPhaseIndex = 0;
|
|
|
|
|
|
|
|
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
|
|
|
|
Duration get currentPhaseDuration =>
|
|
|
|
_phasesDuration[currentPhase] ?? const Duration(seconds: 0);
|
2023-02-28 14:58:32 +01:00
|
|
|
bool get isPhaseComplete =>
|
|
|
|
timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0;
|
2023-03-01 13:07:01 +01:00
|
|
|
Color get currentPhaseColor => _phaseColors[currentPhase] ?? Colors.blue;
|
2023-02-27 01:20:00 +01:00
|
|
|
|
|
|
|
void nextPhase() {
|
2023-02-28 13:05:51 +01:00
|
|
|
_audioPlayer.stop();
|
|
|
|
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
|
2023-02-28 14:58:32 +01:00
|
|
|
_audioPlayer.play(_beepSoundSource);
|
2023-02-28 13:05:51 +01:00
|
|
|
_workoutPhaseIndex += 1;
|
2023-02-28 14:58:32 +01:00
|
|
|
_audioPlayer.onPlayerComplete.listen((event) {
|
|
|
|
_audioPlayer.play(_phaseSongSources[currentPhase]!);
|
|
|
|
timerProvider.startTimer(currentPhaseDuration);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_audioPlayer.play(_finishedSoundSource);
|
2023-03-01 12:57:02 +01:00
|
|
|
stopWorkout();
|
2023-02-28 14:58:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startWorkout() {
|
2023-02-28 15:07:47 +01:00
|
|
|
isWorkoutStarted = true;
|
2023-02-28 14:58:32 +01:00
|
|
|
_audioPlayer.play(_beepSoundSource);
|
|
|
|
_audioPlayer.onPlayerComplete.listen((event) {
|
2023-02-28 13:05:51 +01:00
|
|
|
_audioPlayer.play(_phaseSongSources[currentPhase]!);
|
|
|
|
timerProvider.startTimer(currentPhaseDuration);
|
2023-02-28 14:58:32 +01:00
|
|
|
});
|
2023-02-27 01:20:00 +01:00
|
|
|
}
|
2023-02-28 15:07:47 +01:00
|
|
|
|
|
|
|
void stopWorkout() {
|
|
|
|
isWorkoutStarted = false;
|
|
|
|
_workoutPhaseIndex = 0;
|
|
|
|
_audioPlayer.stop();
|
|
|
|
timerProvider.stopTimer();
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2023-02-27 01:20:00 +01:00
|
|
|
}
|