2023-03-02 15:08:39 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
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-03-02 13:19:08 +01:00
|
|
|
import 'package:smoke_cess_app/models/workout.dart';
|
2023-02-28 13:05:51 +01:00
|
|
|
import 'package:smoke_cess_app/providers/timer_provider.dart';
|
2023-03-02 13:19:08 +01:00
|
|
|
import '../globals.dart' as globals;
|
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-03-02 15:08:39 +01:00
|
|
|
late StreamSubscription _onCompleteSubscription;
|
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-02 15:08:39 +01:00
|
|
|
bool isWorkoutComplete = false;
|
2023-03-01 12:34:35 +01:00
|
|
|
bool isMuted = false;
|
2023-03-02 13:19:08 +01:00
|
|
|
int motivationBefore = 50;
|
|
|
|
int motivationAfter = 50;
|
2023-03-01 12:34:35 +01:00
|
|
|
|
|
|
|
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-02-27 01:20:00 +01:00
|
|
|
final List<String> _workoutPhases = [
|
|
|
|
'Warm-Up',
|
2023-03-02 13:19:08 +01:00
|
|
|
/*'High Intensity',
|
2023-02-27 01:20:00 +01:00
|
|
|
'Low Intensity',
|
|
|
|
'High Intensity',
|
|
|
|
'Low Intensity',
|
|
|
|
'High Intensity',
|
|
|
|
'Low Intensity',
|
2023-03-02 13:19:08 +01:00
|
|
|
'High Intensity', */
|
2023-02-27 01:20:00 +01:00
|
|
|
'Cool-down'
|
|
|
|
];
|
|
|
|
int _workoutPhaseIndex = 0;
|
|
|
|
|
|
|
|
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
|
|
|
|
Duration get currentPhaseDuration =>
|
2023-03-02 13:19:08 +01:00
|
|
|
_workoutPhaseSettings[currentPhase]!['duration'];
|
2023-02-28 14:58:32 +01:00
|
|
|
bool get isPhaseComplete =>
|
|
|
|
timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0;
|
2023-03-02 13:19:08 +01:00
|
|
|
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
|
|
|
|
AssetSource get currentPhaseSource =>
|
|
|
|
_workoutPhaseSettings[currentPhase]!['source'];
|
2023-03-02 15:08:39 +01:00
|
|
|
/* bool get isWorkoutComplete =>
|
|
|
|
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */
|
2023-02-27 01:20:00 +01:00
|
|
|
|
|
|
|
void nextPhase() {
|
2023-03-02 15:08:39 +01:00
|
|
|
_onCompleteSubscription.cancel();
|
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-03-02 15:08:39 +01:00
|
|
|
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
2023-03-02 13:19:08 +01:00
|
|
|
_audioPlayer.play(currentPhaseSource);
|
2023-02-28 14:58:32 +01:00
|
|
|
});
|
2023-03-02 15:08:39 +01:00
|
|
|
timerProvider.startTimer(currentPhaseDuration);
|
2023-02-28 14:58:32 +01:00
|
|
|
} else {
|
2023-03-02 13:19:08 +01:00
|
|
|
//workout completed
|
2023-02-28 14:58:32 +01:00
|
|
|
_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-03-02 15:08:39 +01:00
|
|
|
isWorkoutComplete = false;
|
|
|
|
_audioPlayer.play(_beepSoundSource).whenComplete(() => null);
|
|
|
|
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
2023-03-02 13:19:08 +01:00
|
|
|
_audioPlayer.play(currentPhaseSource);
|
2023-02-28 14:58:32 +01:00
|
|
|
});
|
2023-03-02 15:08:39 +01:00
|
|
|
timerProvider.startTimer(currentPhaseDuration);
|
2023-02-27 01:20:00 +01:00
|
|
|
}
|
2023-02-28 15:07:47 +01:00
|
|
|
|
|
|
|
void stopWorkout() {
|
|
|
|
isWorkoutStarted = false;
|
2023-03-02 15:08:39 +01:00
|
|
|
isWorkoutComplete = true;
|
2023-02-28 15:07:47 +01:00
|
|
|
_audioPlayer.stop();
|
|
|
|
timerProvider.stopTimer();
|
2023-03-02 15:08:39 +01:00
|
|
|
//notifyListeners();
|
2023-02-28 15:07:47 +01:00
|
|
|
}
|
2023-03-02 13:19:08 +01:00
|
|
|
|
|
|
|
void saveWorkout() {
|
|
|
|
Workout workout =
|
|
|
|
Workout(motivationBefore, motivationAfter, DateTime.now());
|
|
|
|
globals.databaseService.addWorkout(workout);
|
|
|
|
}
|
2023-02-27 01:20:00 +01:00
|
|
|
}
|
2023-03-02 13:19:08 +01:00
|
|
|
|
|
|
|
Map<String, Map<String, dynamic>> _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
|
|
|
|
}
|
|
|
|
};
|