diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index 6b38b12..d2c07cf 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/providers/timer_provider.dart'; @@ -8,6 +6,8 @@ class WorkoutProvider extends ChangeNotifier { TimerProvider timerProvider; final AudioPlayer _audioPlayer = AudioPlayer(); + final Source _finishedSoundSource = AssetSource('finish.mp3'); + final Source _beepSoundSource = AssetSource('beep.mp3'); WorkoutProvider(this.timerProvider); @@ -39,13 +39,28 @@ class WorkoutProvider extends ChangeNotifier { String get currentPhase => _workoutPhases[_workoutPhaseIndex]; Duration get currentPhaseDuration => _phasesDuration[currentPhase] ?? const Duration(seconds: 0); + bool get isPhaseComplete => + timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0; void nextPhase() { _audioPlayer.stop(); if (_workoutPhaseIndex < _workoutPhases.length - 1) { + _audioPlayer.play(_beepSoundSource); _workoutPhaseIndex += 1; - _audioPlayer.play(_phaseSongSources[currentPhase]!); - timerProvider.startTimer(currentPhaseDuration); + _audioPlayer.onPlayerComplete.listen((event) { + _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); + }); + } } diff --git a/lib/widgets/timer_widget.dart b/lib/widgets/timer_widget.dart index 875697f..4cf1d51 100644 --- a/lib/widgets/timer_widget.dart +++ b/lib/widgets/timer_widget.dart @@ -14,11 +14,6 @@ class TimerWidget extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ Text(formatTime(duration.inSeconds - timerProvider.elapsedSeconds)), - ElevatedButton( - onPressed: () => timerProvider.started - ? timerProvider.stopTimer() - : timerProvider.startTimer(duration), - child: Text(timerProvider.started ? 'Stop' : 'Start')) ], ); } diff --git a/lib/widgets/workout_timer_widget.dart b/lib/widgets/workout_timer_widget.dart index bc12bac..49baf08 100644 --- a/lib/widgets/workout_timer_widget.dart +++ b/lib/widgets/workout_timer_widget.dart @@ -15,8 +15,7 @@ class WorkoutTimerWidget extends StatelessWidget { TimerProvider timerProvider = context.watch(); WorkoutProvider workoutProvider = context.watch(); - if (timerProvider.elapsedSeconds == - workoutProvider.currentPhaseDuration.inSeconds) { + if (workoutProvider.isPhaseComplete) { Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); } @@ -24,7 +23,10 @@ class WorkoutTimerWidget extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ Text(workoutProvider.currentPhase), - TimerWidget(duration: workoutProvider.currentPhaseDuration) + TimerWidget(duration: workoutProvider.currentPhaseDuration), + ElevatedButton( + onPressed: () => workoutProvider.startWorkout(), + child: Text(timerProvider.started ? 'Stop' : 'Start')) ], ); }