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-03-04 12:14:29 +01:00
|
|
|
import 'package:smoke_cess_app/providers/audio_provider.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
|
|
|
|
2023-03-04 18:02:27 +01:00
|
|
|
enum WorkoutPhases {
|
|
|
|
warmUp,
|
|
|
|
highIntensity,
|
|
|
|
lowIntensity,
|
|
|
|
coolDown,
|
|
|
|
}
|
|
|
|
|
2023-02-27 01:20:00 +01:00
|
|
|
class WorkoutProvider extends ChangeNotifier {
|
2023-03-04 12:14:29 +01:00
|
|
|
final TimerProvider timerProvider;
|
|
|
|
final AudioProvider audioProvider;
|
2023-02-28 13:05:51 +01:00
|
|
|
|
2023-02-28 15:07:47 +01:00
|
|
|
bool isWorkoutStarted = false;
|
2023-03-02 15:08:39 +01:00
|
|
|
bool isWorkoutComplete = false;
|
2023-03-02 13:19:08 +01:00
|
|
|
int motivationBefore = 50;
|
|
|
|
int motivationAfter = 50;
|
2023-03-01 12:34:35 +01:00
|
|
|
|
2023-03-04 12:14:29 +01:00
|
|
|
int _workoutPhaseIndex = 0;
|
2023-03-04 18:02:27 +01:00
|
|
|
final List<WorkoutPhases> _workoutPhases = [
|
|
|
|
WorkoutPhases.warmUp,
|
|
|
|
WorkoutPhases.highIntensity,
|
|
|
|
WorkoutPhases.lowIntensity,
|
|
|
|
WorkoutPhases.highIntensity,
|
|
|
|
WorkoutPhases.lowIntensity,
|
|
|
|
WorkoutPhases.highIntensity,
|
|
|
|
WorkoutPhases.lowIntensity,
|
|
|
|
WorkoutPhases.highIntensity,
|
|
|
|
WorkoutPhases.coolDown,
|
2023-02-27 01:20:00 +01:00
|
|
|
];
|
2023-03-04 12:14:29 +01:00
|
|
|
|
|
|
|
WorkoutProvider(this.timerProvider, this.audioProvider);
|
2023-02-27 01:20:00 +01:00
|
|
|
|
2023-03-04 18:02:27 +01:00
|
|
|
WorkoutPhases get currentPhase => _workoutPhases[_workoutPhaseIndex];
|
2023-02-27 01:20:00 +01:00
|
|
|
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-04 18:02:27 +01:00
|
|
|
String get currentPhaseTitle => _workoutPhaseSettings[currentPhase]!['title'];
|
2023-02-27 01:20:00 +01:00
|
|
|
|
|
|
|
void nextPhase() {
|
2023-02-28 13:05:51 +01:00
|
|
|
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
|
|
|
|
_workoutPhaseIndex += 1;
|
2023-03-04 12:14:29 +01:00
|
|
|
audioProvider.playSourceAfterBeep(currentPhaseSource);
|
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-03-04 12:14:29 +01:00
|
|
|
audioProvider.playFinishSound;
|
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;
|
2023-03-04 12:14:29 +01:00
|
|
|
audioProvider.playSourceAfterBeep(currentPhaseSource);
|
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-03-04 12:14:29 +01:00
|
|
|
_cleanUp();
|
2023-03-02 16:06:24 +01:00
|
|
|
notifyListeners();
|
2023-02-28 15:07:47 +01:00
|
|
|
}
|
2023-03-02 13:19:08 +01:00
|
|
|
|
2023-03-03 15:02:27 +01:00
|
|
|
void interruptWorkout() {
|
|
|
|
isWorkoutStarted = false;
|
|
|
|
isWorkoutComplete = false;
|
2023-03-04 12:14:29 +01:00
|
|
|
_cleanUp();
|
2023-03-03 15:02:27 +01:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2023-03-04 12:14:29 +01:00
|
|
|
void _cleanUp() {
|
|
|
|
audioProvider.stop();
|
|
|
|
timerProvider.stopTimer();
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:19:08 +01:00
|
|
|
void saveWorkout() {
|
|
|
|
Workout workout =
|
|
|
|
Workout(motivationBefore, motivationAfter, DateTime.now());
|
|
|
|
globals.databaseService.addWorkout(workout);
|
|
|
|
}
|
2023-03-03 17:19:58 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2023-03-04 12:14:29 +01:00
|
|
|
_cleanUp();
|
2023-03-03 17:19:58 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
2023-02-27 01:20:00 +01:00
|
|
|
}
|
2023-03-02 13:19:08 +01:00
|
|
|
|
2023-03-04 18:02:27 +01:00
|
|
|
Map<WorkoutPhases, Map<String, dynamic>> _workoutPhaseSettings = {
|
|
|
|
WorkoutPhases.warmUp: {
|
|
|
|
'title': 'Warm Up',
|
2023-03-06 23:50:13 +01:00
|
|
|
'duration': const Duration(minutes: 5),
|
2023-03-02 13:19:08 +01:00
|
|
|
'source': AssetSource('warmUp.mp3'),
|
|
|
|
'color': Colors.green
|
|
|
|
},
|
2023-03-04 18:02:27 +01:00
|
|
|
WorkoutPhases.highIntensity: {
|
|
|
|
'title': 'High Intensity',
|
2023-03-06 23:50:13 +01:00
|
|
|
'duration': const Duration(minutes: 4),
|
2023-03-02 13:19:08 +01:00
|
|
|
'source': AssetSource('workout.mp3'),
|
|
|
|
'color': Colors.red
|
|
|
|
},
|
2023-03-04 18:02:27 +01:00
|
|
|
WorkoutPhases.lowIntensity: {
|
|
|
|
'title': 'Low Intensity',
|
2023-03-06 23:50:13 +01:00
|
|
|
'duration': const Duration(minutes: 3),
|
2023-03-02 13:19:08 +01:00
|
|
|
'source': AssetSource('workout.mp3'),
|
|
|
|
'color': Colors.orange
|
|
|
|
},
|
2023-03-04 18:02:27 +01:00
|
|
|
WorkoutPhases.coolDown: {
|
|
|
|
'title': 'Cool Down',
|
2023-03-06 23:50:13 +01:00
|
|
|
'duration': const Duration(minutes: 5),
|
2023-03-02 13:19:08 +01:00
|
|
|
'source': AssetSource('cool_down.mp3'),
|
|
|
|
'color': Colors.blue
|
|
|
|
}
|
|
|
|
};
|