mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2025-12-20 18:09:30 +00:00
feat: Add duration_picker dependency to pubspec.yaml
This commit is contained in:
parent
b98188d7fb
commit
fbd789f989
13 changed files with 558 additions and 49 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:duration_picker/duration_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
|
@ -7,7 +8,9 @@ import 'package:whispering_pages/constants/sizes.dart';
|
|||
import 'package:whispering_pages/features/player/providers/currently_playing_provider.dart';
|
||||
import 'package:whispering_pages/features/player/view/audiobook_player.dart';
|
||||
import 'package:whispering_pages/features/sleep_timer/core/sleep_timer.dart';
|
||||
import 'package:whispering_pages/features/sleep_timer/providers/sleep_timer_provider.dart';
|
||||
import 'package:whispering_pages/features/sleep_timer/providers/sleep_timer_provider.dart'
|
||||
show sleepTimerProvider;
|
||||
import 'package:whispering_pages/settings/app_settings_provider.dart';
|
||||
import 'package:whispering_pages/shared/extensions/inverse_lerp.dart';
|
||||
|
||||
import 'widgets/audiobook_player_seek_button.dart';
|
||||
|
|
@ -227,17 +230,84 @@ class SleepTimerButton extends HookConsumerWidget {
|
|||
final sleepTimer = ref.watch(sleepTimerProvider);
|
||||
// if sleep timer is not active, show the button with the sleep timer icon
|
||||
// if the sleep timer is active, show the remaining time in a pill shaped container
|
||||
return sleepTimer == null
|
||||
? IconButton(
|
||||
color: sleepTimer != null
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: Theme.of(context).colorScheme.onSurface,
|
||||
icon: const Icon(Icons.timer_rounded),
|
||||
onPressed: () {},
|
||||
)
|
||||
: RemainingSleepTimeDisplay(
|
||||
timer: sleepTimer,
|
||||
return Tooltip(
|
||||
message: 'Sleep Timer',
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
// show the sleep timer dialog
|
||||
final resultingDuration = await showDurationPicker(
|
||||
context: context,
|
||||
initialTime: ref
|
||||
.watch(appSettingsProvider)
|
||||
.playerSettings
|
||||
.sleepTimerSettings
|
||||
.defaultDuration,
|
||||
);
|
||||
if (resultingDuration != null) {
|
||||
// if 0 is selected, cancel the timer
|
||||
if (resultingDuration.inSeconds == 0) {
|
||||
ref.read(sleepTimerProvider.notifier).cancelTimer();
|
||||
} else {
|
||||
ref.read(sleepTimerProvider.notifier).setTimer(resultingDuration);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: sleepTimer == null
|
||||
? Icon(
|
||||
Icons.timer_rounded,
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
)
|
||||
: RemainingSleepTimeDisplay(
|
||||
timer: sleepTimer,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SleepTimerDialog extends HookConsumerWidget {
|
||||
const SleepTimerDialog({
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final sleepTimer = ref.watch(sleepTimerProvider);
|
||||
final sleepTimerSettings =
|
||||
ref.watch(appSettingsProvider).playerSettings.sleepTimerSettings;
|
||||
final timerDurationController = useTextEditingController(
|
||||
text: sleepTimerSettings.defaultDuration.inMinutes.toString(),
|
||||
);
|
||||
|
||||
return AlertDialog(
|
||||
title: const Text('Sleep Timer'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text('Set the duration for the sleep timer'),
|
||||
TextField(
|
||||
controller: timerDurationController,
|
||||
keyboardType: TextInputType.number,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Duration in minutes',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// sleepTimer.setTimer(
|
||||
// Duration(
|
||||
// minutes: int.tryParse(timerDurationController.text) ?? 0,
|
||||
// ),
|
||||
// );
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text('Set Timer'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,14 @@ import 'package:whispering_pages/features/player/core/audiobook_player.dart';
|
|||
/// timer is cancelled when the music player is paused or stopped
|
||||
class SleepTimer {
|
||||
/// The duration after which the music player will be paused
|
||||
final Duration duration;
|
||||
Duration _duration;
|
||||
|
||||
Duration get duration => _duration;
|
||||
|
||||
set duration(Duration value) {
|
||||
_duration = value;
|
||||
reset();
|
||||
}
|
||||
|
||||
/// The player to be paused
|
||||
final AudiobookPlayer player;
|
||||
|
|
@ -23,22 +30,30 @@ class SleepTimer {
|
|||
/// when the timer was started
|
||||
DateTime? startedAt;
|
||||
|
||||
SleepTimer({required this.duration, required this.player}) {
|
||||
player.playbackEventStream.listen((event) {
|
||||
if (event.processingState == ProcessingState.completed ||
|
||||
event.processingState == ProcessingState.idle) {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
/// subscriptions
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
|
||||
SleepTimer({required duration, required this.player}) : _duration = duration {
|
||||
_subscriptions.add(
|
||||
player.playbackEventStream.listen((event) {
|
||||
if (event.processingState == ProcessingState.completed ||
|
||||
event.processingState == ProcessingState.idle) {
|
||||
reset();
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
/// pause the player when the timer is up
|
||||
player.playerStateStream.listen((state) {
|
||||
if (state.playing && timer == null) {
|
||||
startTimer();
|
||||
} else if (!state.playing) {
|
||||
reset();
|
||||
}
|
||||
});
|
||||
_subscriptions.add(
|
||||
player.playerStateStream.listen((state) {
|
||||
if (state.playing && timer == null) {
|
||||
startTimer();
|
||||
} else if (!state.playing) {
|
||||
reset();
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
debugPrint('SleepTimer created with duration: $duration');
|
||||
}
|
||||
|
||||
|
|
@ -53,9 +68,12 @@ class SleepTimer {
|
|||
}
|
||||
}
|
||||
|
||||
/// starts the timer
|
||||
void startTimer() {
|
||||
/// starts the timer with the given duration or the default duration
|
||||
void startTimer([
|
||||
Duration? forDuration,
|
||||
]) {
|
||||
reset();
|
||||
duration = forDuration ?? duration;
|
||||
timer = Timer(duration, () {
|
||||
player.pause();
|
||||
reset();
|
||||
|
|
@ -84,6 +102,9 @@ class SleepTimer {
|
|||
/// dispose the timer
|
||||
void dispose() {
|
||||
reset();
|
||||
for (var sub in _subscriptions) {
|
||||
sub.cancel();
|
||||
}
|
||||
debugPrint('SleepTimer disposed');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,59 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:whispering_pages/features/player/providers/audiobook_player.dart';
|
||||
import 'package:whispering_pages/features/sleep_timer/core/sleep_timer.dart';
|
||||
import 'package:whispering_pages/features/sleep_timer/core/sleep_timer.dart'
|
||||
as core;
|
||||
import 'package:whispering_pages/settings/app_settings_provider.dart';
|
||||
import 'package:whispering_pages/shared/extensions/time_of_day.dart';
|
||||
|
||||
part 'sleep_timer_provider.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
SleepTimer? sleepTimer(SleepTimerRef ref) {
|
||||
final appSettings = ref.watch(appSettingsProvider);
|
||||
final sleepTimerSettings = appSettings.playerSettings.sleepTimerSettings;
|
||||
var sleepTimer = SleepTimer(
|
||||
// duration: sleepTimerSettings.defaultDuration,
|
||||
duration: const Duration(seconds: 5),
|
||||
player: ref.watch(simpleAudiobookPlayerProvider),
|
||||
);
|
||||
ref.onDispose(sleepTimer.dispose);
|
||||
return sleepTimer;
|
||||
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(
|
||||
// duration: sleepTimerSettings.defaultDuration,
|
||||
duration: const Duration(seconds: 5),
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void cancelTimer() {
|
||||
state?.dispose();
|
||||
state = null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,12 +6,13 @@ part of 'sleep_timer_provider.dart';
|
|||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$sleepTimerHash() => r'79646b12412f3300166db29328664a5e58e405bd';
|
||||
String _$sleepTimerHash() => r'de2f39febda3c2234e792f64199c51828206ea9b';
|
||||
|
||||
/// See also [sleepTimer].
|
||||
@ProviderFor(sleepTimer)
|
||||
final sleepTimerProvider = Provider<SleepTimer?>.internal(
|
||||
sleepTimer,
|
||||
/// See also [SleepTimer].
|
||||
@ProviderFor(SleepTimer)
|
||||
final sleepTimerProvider =
|
||||
NotifierProvider<SleepTimer, core.SleepTimer?>.internal(
|
||||
SleepTimer.new,
|
||||
name: r'sleepTimerProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$sleepTimerHash,
|
||||
|
|
@ -19,6 +20,6 @@ final sleepTimerProvider = Provider<SleepTimer?>.internal(
|
|||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef SleepTimerRef = ProviderRef<SleepTimer?>;
|
||||
typedef _$SleepTimer = Notifier<core.SleepTimer?>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue