use dedicated audioprovider
parent
df889fb3ec
commit
fd362e8531
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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: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<String> _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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<WorkoutProvider>();
|
||||
AudioProvider workoutProvider = context.watch<AudioProvider>();
|
||||
|
||||
return IconButton(
|
||||
onPressed: workoutProvider.isMuted
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue