From fd362e85313fb132a14498b8012cc8758572316d Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sat, 4 Mar 2023 12:14:29 +0100 Subject: [PATCH] use dedicated audioprovider --- lib/pages/interval_page.dart | 4 +- lib/providers/audio_provider.dart | 47 +++++++++++++++++++++++ lib/providers/workout_provider.dart | 58 +++++++++-------------------- lib/widgets/mute_button.dart | 5 ++- lib/widgets/workout_form.dart | 11 ++++-- 5 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 lib/providers/audio_provider.dart diff --git a/lib/pages/interval_page.dart b/lib/pages/interval_page.dart index 5117a8d..2326168 100644 --- a/lib/pages/interval_page.dart +++ b/lib/pages/interval_page.dart @@ -10,7 +10,7 @@ class IntervalTimerPage extends StatelessWidget { @override Widget build(BuildContext context) { - return const ViewFormPage( - form: WorkoutForm(), view: WorkoutView(), page: Pages.timer); + return ViewFormPage( + form: WorkoutForm(), view: const WorkoutView(), page: Pages.timer); } } diff --git a/lib/providers/audio_provider.dart b/lib/providers/audio_provider.dart new file mode 100644 index 0000000..a141cb2 --- /dev/null +++ b/lib/providers/audio_provider.dart @@ -0,0 +1,47 @@ +import 'dart:async'; + +import 'package:audioplayers/audioplayers.dart'; +import 'package:flutter/cupertino.dart'; + +class AudioProvider extends ChangeNotifier { + final AudioPlayer _audioPlayer = AudioPlayer(); + bool _isMuted = false; + final Source _finishedSoundSource = AssetSource('finish.mp3'); + final Source _beepSoundSource = AssetSource('beep.mp3'); + StreamSubscription? _onCompleteSubscription; + + bool get isMuted => _isMuted; + + void stop() => _resetPlayer(); + + void playFinishSound() { + _resetPlayer(); + _audioPlayer.play(_finishedSoundSource); + } + + void mutePlayer() { + _isMuted = true; + _audioPlayer.setVolume(0); + notifyListeners(); + } + + void unMutePlayer() { + _isMuted = false; + _audioPlayer.setVolume(1); + notifyListeners(); + } + + //resets player position and delete listening subscription + void _resetPlayer() { + _audioPlayer.stop(); + _onCompleteSubscription?.cancel(); + } + + void playSourceAfterBeep(AssetSource source) { + _resetPlayer(); + _audioPlayer.play(_beepSoundSource); + _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { + _audioPlayer.play(source); + }); + } +} diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index 0cf3804..5d85c18 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -1,38 +1,20 @@ -import 'dart:async'; - 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 { - TimerProvider timerProvider; + final TimerProvider timerProvider; + final AudioProvider audioProvider; - final AudioPlayer _audioPlayer = AudioPlayer(); - late StreamSubscription _onCompleteSubscription; - final Source _finishedSoundSource = AssetSource('finish.mp3'); - final Source _beepSoundSource = AssetSource('beep.mp3'); bool isWorkoutStarted = false; bool isWorkoutComplete = false; - bool isMuted = false; int motivationBefore = 50; int motivationAfter = 50; - void mutePlayer() { - isMuted = true; - _audioPlayer.setVolume(0); - notifyListeners(); - } - - void unMutePlayer() { - isMuted = false; - _audioPlayer.setVolume(1); - notifyListeners(); - } - - WorkoutProvider(this.timerProvider); - + int _workoutPhaseIndex = 0; final List _workoutPhases = [ 'Warm-Up', 'High Intensity', @@ -44,7 +26,8 @@ class WorkoutProvider extends ChangeNotifier { 'High Intensity', 'Cool-down' ]; - int _workoutPhaseIndex = 0; + + WorkoutProvider(this.timerProvider, this.audioProvider); String get currentPhase => _workoutPhases[_workoutPhaseIndex]; Duration get currentPhaseDuration => @@ -54,22 +37,15 @@ class WorkoutProvider extends ChangeNotifier { Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; AssetSource get currentPhaseSource => _workoutPhaseSettings[currentPhase]!['source']; - /* bool get isWorkoutComplete => - _workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */ void nextPhase() { - _onCompleteSubscription.cancel(); - _audioPlayer.stop(); if (_workoutPhaseIndex < _workoutPhases.length - 1) { - _audioPlayer.play(_beepSoundSource); _workoutPhaseIndex += 1; - _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { - _audioPlayer.play(currentPhaseSource); - }); + audioProvider.playSourceAfterBeep(currentPhaseSource); timerProvider.startTimer(currentPhaseDuration); } else { //workout completed - _audioPlayer.play(_finishedSoundSource); + audioProvider.playFinishSound; stopWorkout(); } } @@ -77,29 +53,29 @@ class WorkoutProvider extends ChangeNotifier { void startWorkout() { isWorkoutStarted = true; isWorkoutComplete = false; - _audioPlayer.play(_beepSoundSource).whenComplete(() => null); - _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { - _audioPlayer.play(currentPhaseSource); - }); + audioProvider.playSourceAfterBeep(currentPhaseSource); timerProvider.startTimer(currentPhaseDuration); } void stopWorkout() { isWorkoutStarted = false; isWorkoutComplete = true; - _audioPlayer.stop(); - timerProvider.stopTimer(); + _cleanUp(); notifyListeners(); } void interruptWorkout() { isWorkoutStarted = false; isWorkoutComplete = false; - _audioPlayer.stop(); - timerProvider.stopTimer(); + _cleanUp(); notifyListeners(); } + void _cleanUp() { + audioProvider.stop(); + timerProvider.stopTimer(); + } + void saveWorkout() { Workout workout = Workout(motivationBefore, motivationAfter, DateTime.now()); @@ -108,7 +84,7 @@ class WorkoutProvider extends ChangeNotifier { @override void dispose() { - interruptWorkout(); + _cleanUp(); super.dispose(); } } diff --git a/lib/widgets/mute_button.dart b/lib/widgets/mute_button.dart index 863bb52..29bebf7 100644 --- a/lib/widgets/mute_button.dart +++ b/lib/widgets/mute_button.dart @@ -1,13 +1,14 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:smoke_cess_app/providers/workout_provider.dart'; + +import '../providers/audio_provider.dart'; class MuteButton extends StatelessWidget { const MuteButton({super.key}); @override Widget build(BuildContext context) { - WorkoutProvider workoutProvider = context.watch(); + AudioProvider workoutProvider = context.watch(); return IconButton( onPressed: workoutProvider.isMuted diff --git a/lib/widgets/workout_form.dart b/lib/widgets/workout_form.dart index a176cd0..3e75882 100644 --- a/lib/widgets/workout_form.dart +++ b/lib/widgets/workout_form.dart @@ -1,21 +1,26 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/audio_provider.dart'; import '../providers/timer_provider.dart'; import '../providers/workout_provider.dart'; import 'mute_button.dart'; import 'workout_timer_widget.dart'; class WorkoutForm extends StatelessWidget { - const WorkoutForm({super.key}); + WorkoutForm({super.key}); + + final TimerProvider timerProvider = TimerProvider(); + final AudioProvider audioProvider = AudioProvider(); @override Widget build(BuildContext context) { - TimerProvider timerProvider = TimerProvider(); return MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => timerProvider), + ChangeNotifierProvider(create: (context) => audioProvider), ChangeNotifierProvider( - create: (context) => WorkoutProvider(timerProvider)), + create: (context) => + WorkoutProvider(timerProvider, audioProvider)), ], child: Column( mainAxisAlignment: MainAxisAlignment.center,