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() {
|
2024-10-02 09:18:06 -04:00
|
|
|
final sleepTimerSettings = ref.watch(sleepTimerSettingsProvider);
|
|
|
|
|
if (!sleepTimerSettings.autoTurnOnTimer) {
|
2024-06-13 18:10:10 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((!sleepTimerSettings.alwaysAutoTurnOnTimer) &&
|
2024-10-02 09:18:06 -04:00
|
|
|
!shouldBuildRightNow(
|
|
|
|
|
sleepTimerSettings.autoTurnOnTime,
|
|
|
|
|
sleepTimerSettings.autoTurnOffTime,
|
|
|
|
|
)) {
|
2024-06-13 18:10:10 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 09:18:06 -04:00
|
|
|
void setTimer(Duration? resultingDuration, {bool notifyListeners = true}) {
|
|
|
|
|
if (resultingDuration == null || resultingDuration.inSeconds == 0) {
|
|
|
|
|
cancelTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-13 18:10:10 -04:00
|
|
|
if (state != null) {
|
|
|
|
|
state!.duration = resultingDuration;
|
2024-10-02 09:18:06 -04:00
|
|
|
if (notifyListeners) {
|
|
|
|
|
ref.notifyListeners();
|
|
|
|
|
}
|
2024-06-13 18:10:10 -04:00
|
|
|
} 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();
|
|
|
|
|
|
2024-09-30 02:34:13 -04:00
|
|
|
// ref.notifyListeners(); // see https://github.com/Dr-Blank/Vaani/pull/40 for more information on why this is commented out
|
2024-09-28 01:27:56 -04:00
|
|
|
}
|
|
|
|
|
|
2024-06-13 18:10:10 -04:00
|
|
|
void cancelTimer() {
|
|
|
|
|
state?.dispose();
|
|
|
|
|
state = null;
|
|
|
|
|
}
|
2024-06-06 15:35:30 -04:00
|
|
|
}
|
2024-10-02 09:18:06 -04:00
|
|
|
|
|
|
|
|
bool shouldBuildRightNow(Duration autoTurnOnTime, Duration autoTurnOffTime) {
|
|
|
|
|
final now = TimeOfDay.now();
|
|
|
|
|
return now.isBetween(
|
|
|
|
|
autoTurnOnTime.toTimeOfDay(),
|
|
|
|
|
autoTurnOffTime.toTimeOfDay(),
|
|
|
|
|
);
|
|
|
|
|
}
|