From fa8d2f63145da701275044a7992b0c4c23d1b1e0 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Thu, 2 Mar 2023 15:08:39 +0100 Subject: [PATCH] fix onlisten stream error --- lib/providers/workout_provider.dart | 24 +++++++++++++++--------- lib/widgets/workout_timer_widget.dart | 6 +----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index c15c04c..add88de 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; import 'package:smoke_cess_app/models/workout.dart'; @@ -8,9 +10,11 @@ class WorkoutProvider extends ChangeNotifier { TimerProvider timerProvider; 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; @@ -50,18 +54,19 @@ class WorkoutProvider extends ChangeNotifier { Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; AssetSource get currentPhaseSource => _workoutPhaseSettings[currentPhase]!['source']; - bool get isWorkoutComplete => - _workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; + /* bool get isWorkoutComplete => + _workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */ void nextPhase() { + _onCompleteSubscription.cancel(); _audioPlayer.stop(); if (_workoutPhaseIndex < _workoutPhases.length - 1) { _audioPlayer.play(_beepSoundSource); _workoutPhaseIndex += 1; - _audioPlayer.onPlayerComplete.listen((event) { + _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(currentPhaseSource); - timerProvider.startTimer(currentPhaseDuration); }); + timerProvider.startTimer(currentPhaseDuration); } else { //workout completed _audioPlayer.play(_finishedSoundSource); @@ -71,19 +76,20 @@ class WorkoutProvider extends ChangeNotifier { void startWorkout() { isWorkoutStarted = true; - _audioPlayer.play(_beepSoundSource); - _audioPlayer.onPlayerComplete.listen((event) { + isWorkoutComplete = false; + _audioPlayer.play(_beepSoundSource).whenComplete(() => null); + _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(currentPhaseSource); - timerProvider.startTimer(currentPhaseDuration); }); + timerProvider.startTimer(currentPhaseDuration); } void stopWorkout() { isWorkoutStarted = false; - //_workoutPhaseIndex = 0; + isWorkoutComplete = true; _audioPlayer.stop(); timerProvider.stopTimer(); - notifyListeners(); + //notifyListeners(); } void saveWorkout() { diff --git a/lib/widgets/workout_timer_widget.dart b/lib/widgets/workout_timer_widget.dart index 2e39175..8023770 100644 --- a/lib/widgets/workout_timer_widget.dart +++ b/lib/widgets/workout_timer_widget.dart @@ -16,7 +16,7 @@ class WorkoutTimerWidget extends StatelessWidget { TimerProvider timerProvider = context.watch(); WorkoutProvider workoutProvider = context.watch(); - if (workoutProvider.isPhaseComplete && workoutProvider.isWorkoutStarted) { + if (workoutProvider.isPhaseComplete && !workoutProvider.isWorkoutComplete) { Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); } @@ -36,10 +36,6 @@ class WorkoutTimerWidget extends StatelessWidget { } } - if (workoutProvider.isWorkoutComplete) { - handleStartStopWorkout(); - } - return Column( mainAxisAlignment: MainAxisAlignment.center, children: [