diff --git a/lib/providers/timer_provider.dart b/lib/providers/timer_provider.dart index 8073309..ee34b19 100644 --- a/lib/providers/timer_provider.dart +++ b/lib/providers/timer_provider.dart @@ -18,5 +18,8 @@ class TimerProvider extends ChangeNotifier { })); } - void stopTimer() => _timer?.cancel(); + void stopTimer() { + started = false; + _timer?.cancel(); + } } diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index d2c07cf..2d31dc7 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -8,6 +8,7 @@ class WorkoutProvider extends ChangeNotifier { final AudioPlayer _audioPlayer = AudioPlayer(); final Source _finishedSoundSource = AssetSource('finish.mp3'); final Source _beepSoundSource = AssetSource('beep.mp3'); + bool isWorkoutStarted = false; WorkoutProvider(this.timerProvider); @@ -57,10 +58,19 @@ class WorkoutProvider extends ChangeNotifier { } void startWorkout() { + isWorkoutStarted = true; _audioPlayer.play(_beepSoundSource); _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(_phaseSongSources[currentPhase]!); timerProvider.startTimer(currentPhaseDuration); }); } + + void stopWorkout() { + isWorkoutStarted = false; + _workoutPhaseIndex = 0; + _audioPlayer.stop(); + timerProvider.stopTimer(); + notifyListeners(); + } } diff --git a/lib/widgets/workout_timer_widget.dart b/lib/widgets/workout_timer_widget.dart index 49baf08..131229c 100644 --- a/lib/widgets/workout_timer_widget.dart +++ b/lib/widgets/workout_timer_widget.dart @@ -25,7 +25,9 @@ class WorkoutTimerWidget extends StatelessWidget { Text(workoutProvider.currentPhase), TimerWidget(duration: workoutProvider.currentPhaseDuration), ElevatedButton( - onPressed: () => workoutProvider.startWorkout(), + onPressed: !workoutProvider.isWorkoutStarted + ? () => workoutProvider.startWorkout() + : () => workoutProvider.stopWorkout(), child: Text(timerProvider.started ? 'Stop' : 'Start')) ], );