use dedicated audioprovider
parent
df889fb3ec
commit
fd362e8531
|
@ -10,7 +10,7 @@ class IntervalTimerPage extends StatelessWidget {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return const ViewFormPage(
|
return ViewFormPage(
|
||||||
form: WorkoutForm(), view: WorkoutView(), page: Pages.timer);
|
form: WorkoutForm(), view: const WorkoutView(), page: Pages.timer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,38 +1,20 @@
|
||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:audioplayers/audioplayers.dart';
|
import 'package:audioplayers/audioplayers.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:smoke_cess_app/models/workout.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 'package:smoke_cess_app/providers/timer_provider.dart';
|
||||||
import '../globals.dart' as globals;
|
import '../globals.dart' as globals;
|
||||||
|
|
||||||
class WorkoutProvider extends ChangeNotifier {
|
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 isWorkoutStarted = false;
|
||||||
bool isWorkoutComplete = false;
|
bool isWorkoutComplete = false;
|
||||||
bool isMuted = false;
|
|
||||||
int motivationBefore = 50;
|
int motivationBefore = 50;
|
||||||
int motivationAfter = 50;
|
int motivationAfter = 50;
|
||||||
|
|
||||||
void mutePlayer() {
|
int _workoutPhaseIndex = 0;
|
||||||
isMuted = true;
|
|
||||||
_audioPlayer.setVolume(0);
|
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
void unMutePlayer() {
|
|
||||||
isMuted = false;
|
|
||||||
_audioPlayer.setVolume(1);
|
|
||||||
notifyListeners();
|
|
||||||
}
|
|
||||||
|
|
||||||
WorkoutProvider(this.timerProvider);
|
|
||||||
|
|
||||||
final List<String> _workoutPhases = [
|
final List<String> _workoutPhases = [
|
||||||
'Warm-Up',
|
'Warm-Up',
|
||||||
'High Intensity',
|
'High Intensity',
|
||||||
|
@ -44,7 +26,8 @@ class WorkoutProvider extends ChangeNotifier {
|
||||||
'High Intensity',
|
'High Intensity',
|
||||||
'Cool-down'
|
'Cool-down'
|
||||||
];
|
];
|
||||||
int _workoutPhaseIndex = 0;
|
|
||||||
|
WorkoutProvider(this.timerProvider, this.audioProvider);
|
||||||
|
|
||||||
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
|
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
|
||||||
Duration get currentPhaseDuration =>
|
Duration get currentPhaseDuration =>
|
||||||
|
@ -54,22 +37,15 @@ class WorkoutProvider extends ChangeNotifier {
|
||||||
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
|
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
|
||||||
AssetSource get currentPhaseSource =>
|
AssetSource get currentPhaseSource =>
|
||||||
_workoutPhaseSettings[currentPhase]!['source'];
|
_workoutPhaseSettings[currentPhase]!['source'];
|
||||||
/* bool get isWorkoutComplete =>
|
|
||||||
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */
|
|
||||||
|
|
||||||
void nextPhase() {
|
void nextPhase() {
|
||||||
_onCompleteSubscription.cancel();
|
|
||||||
_audioPlayer.stop();
|
|
||||||
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
|
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
|
||||||
_audioPlayer.play(_beepSoundSource);
|
|
||||||
_workoutPhaseIndex += 1;
|
_workoutPhaseIndex += 1;
|
||||||
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
audioProvider.playSourceAfterBeep(currentPhaseSource);
|
||||||
_audioPlayer.play(currentPhaseSource);
|
|
||||||
});
|
|
||||||
timerProvider.startTimer(currentPhaseDuration);
|
timerProvider.startTimer(currentPhaseDuration);
|
||||||
} else {
|
} else {
|
||||||
//workout completed
|
//workout completed
|
||||||
_audioPlayer.play(_finishedSoundSource);
|
audioProvider.playFinishSound;
|
||||||
stopWorkout();
|
stopWorkout();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,29 +53,29 @@ class WorkoutProvider extends ChangeNotifier {
|
||||||
void startWorkout() {
|
void startWorkout() {
|
||||||
isWorkoutStarted = true;
|
isWorkoutStarted = true;
|
||||||
isWorkoutComplete = false;
|
isWorkoutComplete = false;
|
||||||
_audioPlayer.play(_beepSoundSource).whenComplete(() => null);
|
audioProvider.playSourceAfterBeep(currentPhaseSource);
|
||||||
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
|
||||||
_audioPlayer.play(currentPhaseSource);
|
|
||||||
});
|
|
||||||
timerProvider.startTimer(currentPhaseDuration);
|
timerProvider.startTimer(currentPhaseDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stopWorkout() {
|
void stopWorkout() {
|
||||||
isWorkoutStarted = false;
|
isWorkoutStarted = false;
|
||||||
isWorkoutComplete = true;
|
isWorkoutComplete = true;
|
||||||
_audioPlayer.stop();
|
_cleanUp();
|
||||||
timerProvider.stopTimer();
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void interruptWorkout() {
|
void interruptWorkout() {
|
||||||
isWorkoutStarted = false;
|
isWorkoutStarted = false;
|
||||||
isWorkoutComplete = false;
|
isWorkoutComplete = false;
|
||||||
_audioPlayer.stop();
|
_cleanUp();
|
||||||
timerProvider.stopTimer();
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _cleanUp() {
|
||||||
|
audioProvider.stop();
|
||||||
|
timerProvider.stopTimer();
|
||||||
|
}
|
||||||
|
|
||||||
void saveWorkout() {
|
void saveWorkout() {
|
||||||
Workout workout =
|
Workout workout =
|
||||||
Workout(motivationBefore, motivationAfter, DateTime.now());
|
Workout(motivationBefore, motivationAfter, DateTime.now());
|
||||||
|
@ -108,7 +84,7 @@ class WorkoutProvider extends ChangeNotifier {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
interruptWorkout();
|
_cleanUp();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:smoke_cess_app/providers/workout_provider.dart';
|
|
||||||
|
import '../providers/audio_provider.dart';
|
||||||
|
|
||||||
class MuteButton extends StatelessWidget {
|
class MuteButton extends StatelessWidget {
|
||||||
const MuteButton({super.key});
|
const MuteButton({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
WorkoutProvider workoutProvider = context.watch<WorkoutProvider>();
|
AudioProvider workoutProvider = context.watch<AudioProvider>();
|
||||||
|
|
||||||
return IconButton(
|
return IconButton(
|
||||||
onPressed: workoutProvider.isMuted
|
onPressed: workoutProvider.isMuted
|
||||||
|
|
|
@ -1,21 +1,26 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:smoke_cess_app/providers/audio_provider.dart';
|
||||||
import '../providers/timer_provider.dart';
|
import '../providers/timer_provider.dart';
|
||||||
import '../providers/workout_provider.dart';
|
import '../providers/workout_provider.dart';
|
||||||
import 'mute_button.dart';
|
import 'mute_button.dart';
|
||||||
import 'workout_timer_widget.dart';
|
import 'workout_timer_widget.dart';
|
||||||
|
|
||||||
class WorkoutForm extends StatelessWidget {
|
class WorkoutForm extends StatelessWidget {
|
||||||
const WorkoutForm({super.key});
|
WorkoutForm({super.key});
|
||||||
|
|
||||||
|
final TimerProvider timerProvider = TimerProvider();
|
||||||
|
final AudioProvider audioProvider = AudioProvider();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
TimerProvider timerProvider = TimerProvider();
|
|
||||||
return MultiProvider(
|
return MultiProvider(
|
||||||
providers: [
|
providers: [
|
||||||
ChangeNotifierProvider(create: (context) => timerProvider),
|
ChangeNotifierProvider(create: (context) => timerProvider),
|
||||||
|
ChangeNotifierProvider(create: (context) => audioProvider),
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(
|
||||||
create: (context) => WorkoutProvider(timerProvider)),
|
create: (context) =>
|
||||||
|
WorkoutProvider(timerProvider, audioProvider)),
|
||||||
],
|
],
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
|
Loading…
Reference in New Issue