wip trying to wrap timerprovider in prokoutprovider

main
Julian Gegner 2023-02-28 14:58:32 +01:00
parent b01485f27a
commit 4144bbaf69
3 changed files with 24 additions and 12 deletions

View File

@ -1,5 +1,3 @@
import 'dart:async';
import 'package:audioplayers/audioplayers.dart'; import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart';
@ -8,6 +6,8 @@ class WorkoutProvider extends ChangeNotifier {
TimerProvider timerProvider; TimerProvider timerProvider;
final AudioPlayer _audioPlayer = AudioPlayer(); final AudioPlayer _audioPlayer = AudioPlayer();
final Source _finishedSoundSource = AssetSource('finish.mp3');
final Source _beepSoundSource = AssetSource('beep.mp3');
WorkoutProvider(this.timerProvider); WorkoutProvider(this.timerProvider);
@ -39,13 +39,28 @@ class WorkoutProvider extends ChangeNotifier {
String get currentPhase => _workoutPhases[_workoutPhaseIndex]; String get currentPhase => _workoutPhases[_workoutPhaseIndex];
Duration get currentPhaseDuration => Duration get currentPhaseDuration =>
_phasesDuration[currentPhase] ?? const Duration(seconds: 0); _phasesDuration[currentPhase] ?? const Duration(seconds: 0);
bool get isPhaseComplete =>
timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0;
void nextPhase() { void nextPhase() {
_audioPlayer.stop(); _audioPlayer.stop();
if (_workoutPhaseIndex < _workoutPhases.length - 1) { if (_workoutPhaseIndex < _workoutPhases.length - 1) {
_audioPlayer.play(_beepSoundSource);
_workoutPhaseIndex += 1; _workoutPhaseIndex += 1;
_audioPlayer.play(_phaseSongSources[currentPhase]!); _audioPlayer.onPlayerComplete.listen((event) {
timerProvider.startTimer(currentPhaseDuration); _audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
});
} else {
_audioPlayer.play(_finishedSoundSource);
} }
} }
void startWorkout() {
_audioPlayer.play(_beepSoundSource);
_audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
});
}
} }

View File

@ -14,11 +14,6 @@ class TimerWidget extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text(formatTime(duration.inSeconds - timerProvider.elapsedSeconds)), Text(formatTime(duration.inSeconds - timerProvider.elapsedSeconds)),
ElevatedButton(
onPressed: () => timerProvider.started
? timerProvider.stopTimer()
: timerProvider.startTimer(duration),
child: Text(timerProvider.started ? 'Stop' : 'Start'))
], ],
); );
} }

View File

@ -15,8 +15,7 @@ class WorkoutTimerWidget extends StatelessWidget {
TimerProvider timerProvider = context.watch<TimerProvider>(); TimerProvider timerProvider = context.watch<TimerProvider>();
WorkoutProvider workoutProvider = context.watch<WorkoutProvider>(); WorkoutProvider workoutProvider = context.watch<WorkoutProvider>();
if (timerProvider.elapsedSeconds == if (workoutProvider.isPhaseComplete) {
workoutProvider.currentPhaseDuration.inSeconds) {
Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase());
} }
@ -24,7 +23,10 @@ class WorkoutTimerWidget extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text(workoutProvider.currentPhase), Text(workoutProvider.currentPhase),
TimerWidget(duration: workoutProvider.currentPhaseDuration) TimerWidget(duration: workoutProvider.currentPhaseDuration),
ElevatedButton(
onPressed: () => workoutProvider.startWorkout(),
child: Text(timerProvider.started ? 'Stop' : 'Start'))
], ],
); );
} }