diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 10ff0ec..21049d2 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:smoke_cess_app/providers/page_provider.dart'; import 'package:smoke_cess_app/providers/tasks_provider.dart'; +import 'package:smoke_cess_app/providers/workout_provider.dart'; import 'package:smoke_cess_app/services/pages_service.dart'; import 'package:smoke_cess_app/providers/settings_provider.dart'; @@ -21,9 +22,11 @@ class MyHomePageState extends State { void _onItemTapped(int index) { PageProvider pageProvider = context.read(); + WorkoutProvider workoutProvider = context.read(); setState(() { if (_isConfigured) { pageProvider.showForm = false; + workoutProvider.interruptWorkout(); _selectedIndex = index; return; } diff --git a/lib/providers/timer_provider.dart b/lib/providers/timer_provider.dart index ee34b19..acf1a94 100644 --- a/lib/providers/timer_provider.dart +++ b/lib/providers/timer_provider.dart @@ -21,5 +21,6 @@ class TimerProvider extends ChangeNotifier { void stopTimer() { started = false; _timer?.cancel(); + _timer = null; } } diff --git a/lib/providers/workout_provider.dart b/lib/providers/workout_provider.dart index 5c0a80b..98c3163 100644 --- a/lib/providers/workout_provider.dart +++ b/lib/providers/workout_provider.dart @@ -92,6 +92,14 @@ class WorkoutProvider extends ChangeNotifier { notifyListeners(); } + void interruptWorkout() { + isWorkoutStarted = false; + isWorkoutComplete = false; + _audioPlayer.stop(); + timerProvider.stopTimer(); + notifyListeners(); + } + void saveWorkout() { Workout workout = Workout(motivationBefore, motivationAfter, DateTime.now()); diff --git a/lib/widgets/workout_timer_widget.dart b/lib/widgets/workout_timer_widget.dart index a85a09e..7b2cdb6 100644 --- a/lib/widgets/workout_timer_widget.dart +++ b/lib/widgets/workout_timer_widget.dart @@ -1,7 +1,10 @@ import 'dart:async'; +import 'package:awesome_dialog/awesome_dialog.dart'; import 'package:flutter/material.dart'; +import 'package:http/http.dart'; import 'package:provider/provider.dart'; +import 'package:smoke_cess_app/providers/page_provider.dart'; import 'package:smoke_cess_app/providers/tasks_provider.dart'; import 'package:smoke_cess_app/providers/workout_provider.dart'; import 'package:smoke_cess_app/services/pages_service.dart'; @@ -18,19 +21,29 @@ class WorkoutTimerWidget extends StatelessWidget { TimerProvider timerProvider = context.watch(); WorkoutProvider workoutProvider = context.watch(); TasksProvider tasksProvider = context.read(); + PageProvider pageProvider = context.read(); + + void handleStopWorkout() async { + await showMotivationPopup(context, (double value) { + workoutProvider.motivationAfter = value.toInt(); + workoutProvider.saveWorkout(); + tasksProvider.setTaskDone(Pages.timer); + }, 'Motivation nach dem Training'); + await AwesomeDialog( + context: context, + dialogType: DialogType.success, + title: 'Gespeichert', + desc: 'Der Eintrag wurde erfolgreich gespeichert', + ).show(); + pageProvider.swap(); + } if (workoutProvider.isPhaseComplete && !workoutProvider.isWorkoutComplete) { Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); } if (workoutProvider.isWorkoutComplete) { - Timer( - const Duration(milliseconds: 1), - () => showMotivationPopup(context, (double value) { - workoutProvider.motivationAfter = value.toInt(); - workoutProvider.saveWorkout(); - tasksProvider.setTaskDone(Pages.timer); - }, 'Motivation nach dem Training')); + Timer(const Duration(milliseconds: 1), handleStopWorkout); } void handleStartStopWorkout() { @@ -40,11 +53,8 @@ class WorkoutTimerWidget extends StatelessWidget { workoutProvider.startWorkout(); }, 'Motivation vor dem Training'); } else { - workoutProvider.stopWorkout(); - showMotivationPopup( - context, - (double value) => workoutProvider.motivationAfter = value.toInt(), - 'Motivation nach dem Training'); + workoutProvider.interruptWorkout(); + handleStopWorkout(); } }