cpd_2022_zi/lib/providers/workout_provider.dart

116 lines
3.0 KiB
Dart

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/workout.dart';
import 'package:smoke_cess_app/providers/audio_provider.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart';
import '../globals.dart' as globals;
class WorkoutProvider extends ChangeNotifier {
final TimerProvider timerProvider;
final AudioProvider audioProvider;
bool isWorkoutStarted = false;
bool isWorkoutComplete = false;
int motivationBefore = 50;
int motivationAfter = 50;
int _workoutPhaseIndex = 0;
final List<String> _workoutPhases = [
'Warm-Up',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Cool-down'
];
WorkoutProvider(this.timerProvider, this.audioProvider);
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'];
void nextPhase() {
_onCompleteSubscription.cancel();
_audioPlayer.stop();
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
_workoutPhaseIndex += 1;
audioProvider.playSourceAfterBeep(currentPhaseSource);
timerProvider.startTimer(currentPhaseDuration);
} else {
//workout completed
audioProvider.playFinishSound;
stopWorkout();
}
}
void startWorkout() {
isWorkoutStarted = true;
isWorkoutComplete = false;
audioProvider.playSourceAfterBeep(currentPhaseSource);
timerProvider.startTimer(currentPhaseDuration);
}
void stopWorkout() {
isWorkoutStarted = false;
isWorkoutComplete = true;
_cleanUp();
notifyListeners();
}
void interruptWorkout() {
isWorkoutStarted = false;
isWorkoutComplete = false;
_cleanUp();
notifyListeners();
}
void _cleanUp() {
audioProvider.stop();
timerProvider.stopTimer();
}
void saveWorkout() {
Workout workout =
Workout(motivationBefore, motivationAfter, DateTime.now());
globals.databaseService.addWorkout(workout);
}
@override
void dispose() {
_cleanUp();
super.dispose();
}
}
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
}
};