2024-06-13 18:10:10 -04:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-06 15:35:30 -04:00
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2024-08-23 04:21:46 -04:00
|
|
|
import 'package:vaani/features/player/providers/audiobook_player.dart';
|
2024-09-28 01:27:56 -04:00
|
|
|
import 'package:vaani/features/sleep_timer/core/sleep_timer.dart' as core;
|
2024-08-23 04:21:46 -04:00
|
|
|
import 'package:vaani/settings/app_settings_provider.dart';
|
2024-08-23 04:50:49 -04:00
|
|
|
import 'package:vaani/shared/extensions/time_of_day.dart';
|
2024-06-06 15:35:30 -04:00
|
|
|
|
|
|
|
|
part 'sleep_timer_provider.g.dart';
|
|
|
|
|
|
|
|
|
|
@Riverpod(keepAlive: true)
|
2024-06-13 18:10:10 -04:00
|
|
|
class SleepTimer extends _$SleepTimer {
|
|
|
|
|
@override
|
|
|
|
|
core.SleepTimer? build() {
|
|
|
|
|
final appSettings = ref.watch(appSettingsProvider);
|
|
|
|
|
final sleepTimerSettings = appSettings.playerSettings.sleepTimerSettings;
|
|
|
|
|
bool isEnabled = sleepTimerSettings.autoTurnOnTimer;
|
|
|
|
|
if (!isEnabled) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((!sleepTimerSettings.alwaysAutoTurnOnTimer) &&
|
|
|
|
|
(sleepTimerSettings.autoTurnOnTime
|
|
|
|
|
.toTimeOfDay()
|
|
|
|
|
.isAfter(TimeOfDay.now()) &&
|
|
|
|
|
sleepTimerSettings.autoTurnOffTime
|
|
|
|
|
.toTimeOfDay()
|
|
|
|
|
.isBefore(TimeOfDay.now()))) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sleepTimer = core.SleepTimer(
|
2024-08-20 08:36:39 -04:00
|
|
|
duration: sleepTimerSettings.defaultDuration,
|
2024-06-13 18:10:10 -04:00
|
|
|
player: ref.watch(simpleAudiobookPlayerProvider),
|
|
|
|
|
);
|
|
|
|
|
ref.onDispose(sleepTimer.dispose);
|
|
|
|
|
return sleepTimer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setTimer(Duration resultingDuration) {
|
|
|
|
|
if (state != null) {
|
|
|
|
|
state!.duration = resultingDuration;
|
|
|
|
|
ref.notifyListeners();
|
|
|
|
|
} else {
|
|
|
|
|
final timer = core.SleepTimer(
|
|
|
|
|
duration: resultingDuration,
|
|
|
|
|
player: ref.watch(simpleAudiobookPlayerProvider),
|
|
|
|
|
);
|
|
|
|
|
ref.onDispose(timer.dispose);
|
|
|
|
|
state = timer;
|
2024-09-28 01:27:56 -04:00
|
|
|
state!.startCountDown();
|
2024-06-13 18:10:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 01:27:56 -04:00
|
|
|
void restartTimer() {
|
|
|
|
|
state?.restartTimer();
|
|
|
|
|
|
|
|
|
|
ref.notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 18:10:10 -04:00
|
|
|
void cancelTimer() {
|
|
|
|
|
state?.dispose();
|
|
|
|
|
state = null;
|
|
|
|
|
}
|
2024-06-06 15:35:30 -04:00
|
|
|
}
|