diff --git a/lib/pages/interval_page.dart b/lib/pages/interval_page.dart index 74ccffa..3537f03 100644 --- a/lib/pages/interval_page.dart +++ b/lib/pages/interval_page.dart @@ -170,10 +170,12 @@ class _IntervalTimerPageState extends State { @override Widget build(BuildContext context) { + TimerProvider timerProvider = TimerProvider(); return MultiProvider( providers: [ - ChangeNotifierProvider(create: (context) => TimerProvider()), - ChangeNotifierProvider(create: (context) => WorkoutProvider()) + ChangeNotifierProvider(create: (context) => timerProvider), + ChangeNotifierProvider( + create: (context) => WorkoutProvider(timerProvider)) ], child: WorkoutTimerWidget(), ); diff --git a/lib/providers/timer_provider.dart b/lib/providers/timer_provider.dart index f5a7689..8073309 100644 --- a/lib/providers/timer_provider.dart +++ b/lib/providers/timer_provider.dart @@ -11,7 +11,6 @@ class TimerProvider extends ChangeNotifier { started = true; _timer = Timer.periodic(const Duration(seconds: 1), ((timer) { if (timer.tick >= duration.inSeconds) { - print(elapsedSeconds); timer.cancel(); started = false; } diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index 0505347..6b38b12 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -1,6 +1,16 @@ +import 'dart:async'; + +import 'package:audioplayers/audioplayers.dart'; import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/providers/timer_provider.dart'; class WorkoutProvider extends ChangeNotifier { + TimerProvider timerProvider; + + final AudioPlayer _audioPlayer = AudioPlayer(); + + WorkoutProvider(this.timerProvider); + final List _workoutPhases = [ 'Warm-Up', 'High Intensity', @@ -18,6 +28,12 @@ class WorkoutProvider extends ChangeNotifier { 'Low Intensity': const Duration(seconds: 3), 'Cool-down': const Duration(seconds: 5) }; + final Map _phaseSongSources = { + 'Warm-Up': AssetSource('warmUp.mp3'), + 'High Intensity': AssetSource('workout.mp3'), + 'Low Intensity': AssetSource('workout.mp3'), + 'Cool-down': AssetSource('cool_down.mp3') + }; int _workoutPhaseIndex = 0; String get currentPhase => _workoutPhases[_workoutPhaseIndex]; @@ -25,9 +41,11 @@ class WorkoutProvider extends ChangeNotifier { _phasesDuration[currentPhase] ?? const Duration(seconds: 0); void nextPhase() { - _workoutPhaseIndex < _workoutPhases.length - ? _workoutPhaseIndex += 1 - : _workoutPhaseIndex = 0; - //notifyListeners(); + _audioPlayer.stop(); + if (_workoutPhaseIndex < _workoutPhases.length - 1) { + _workoutPhaseIndex += 1; + _audioPlayer.play(_phaseSongSources[currentPhase]!); + timerProvider.startTimer(currentPhaseDuration); + } } } diff --git a/lib/widgets/workout_timer_widget.dart b/lib/widgets/workout_timer_widget.dart index 1ba501c..bc12bac 100644 --- a/lib/widgets/workout_timer_widget.dart +++ b/lib/widgets/workout_timer_widget.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:smoke_cess_app/providers/workout_provider.dart'; @@ -15,9 +17,7 @@ class WorkoutTimerWidget extends StatelessWidget { if (timerProvider.elapsedSeconds == workoutProvider.currentPhaseDuration.inSeconds) { - print('Timer abgelaufen'); - workoutProvider.nextPhase(); - timerProvider.startTimer(workoutProvider.currentPhaseDuration); + Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); } return Column(