Autoplay page narration after a three-second delay
This commit is contained in:
+83
-58
@@ -64,9 +64,15 @@ class LumenApp extends StatelessWidget {
|
||||
}
|
||||
|
||||
class Notebook extends StatefulWidget {
|
||||
const Notebook({super.key, required this.game, required this.adsEnabled});
|
||||
const Notebook({
|
||||
super.key,
|
||||
required this.game,
|
||||
required this.adsEnabled,
|
||||
this.narrator,
|
||||
});
|
||||
final Game game;
|
||||
final bool adsEnabled;
|
||||
final Narrator? narrator;
|
||||
@override
|
||||
State<Notebook> createState() => _NotebookState();
|
||||
}
|
||||
@@ -84,7 +90,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
narrator = Narrator(g.book.tracks);
|
||||
narrator = widget.narrator ?? Narrator(g.book.tracks);
|
||||
ambience = Ambience();
|
||||
narrator.addListener(_syncAtmosphere);
|
||||
ads = Ads(enabled: widget.adsEnabled);
|
||||
@@ -94,6 +100,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
unawaited(ads.start());
|
||||
_syncAtmosphere();
|
||||
if (mounted) _scheduleAutoplay();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -101,6 +108,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
if (_route != route) {
|
||||
_route = route;
|
||||
unawaited(narrator.stop());
|
||||
_scheduleAutoplay();
|
||||
}
|
||||
_syncAtmosphere();
|
||||
if (mounted) setState(() {});
|
||||
@@ -109,6 +117,19 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
bool get atmosphereEnabled =>
|
||||
!prefs.contains('ambience_enabled') || prefs.flag('ambience_enabled');
|
||||
|
||||
void _scheduleAutoplay() {
|
||||
final recordedPage =
|
||||
_foreground &&
|
||||
[Screen.story, Screen.briefing, Screen.reveal].contains(g.screen) &&
|
||||
!(g.screen == Screen.story &&
|
||||
g.storyPage.type == StoryPageType.note &&
|
||||
!prefs.flag('notebook_note_open'));
|
||||
narrator.scheduleAutoplay(
|
||||
recordedPage ? recordingId : null,
|
||||
prefs.decimal('recording_speed'),
|
||||
);
|
||||
}
|
||||
|
||||
void _syncAtmosphere() {
|
||||
final inStory =
|
||||
g.mode == Mode.campaign &&
|
||||
@@ -738,6 +759,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
|
||||
void openLetter() {
|
||||
unawaited(prefs.set('notebook_note_open', true));
|
||||
_scheduleAutoplay();
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@@ -776,64 +798,67 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> soundSettings() => showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) => StatefulBuilder(
|
||||
builder: (context, refresh) {
|
||||
void save(String key, Object value) {
|
||||
unawaited(prefs.set(key, value));
|
||||
_syncAtmosphere();
|
||||
if (mounted) setState(() {});
|
||||
refresh(() {});
|
||||
}
|
||||
Future<void> soundSettings() {
|
||||
narrator.cancelAutoplay();
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) => StatefulBuilder(
|
||||
builder: (context, refresh) {
|
||||
void save(String key, Object value) {
|
||||
unawaited(prefs.set(key, value));
|
||||
_syncAtmosphere();
|
||||
if (mounted) setState(() {});
|
||||
refresh(() {});
|
||||
}
|
||||
|
||||
return AlertDialog(
|
||||
title: Text('Sound & atmosphere', style: hand(29)),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text('Environmental sounds', style: hand(23)),
|
||||
value: atmosphereEnabled,
|
||||
onChanged: (value) => save('ambience_enabled', value),
|
||||
),
|
||||
writing(
|
||||
'Water at the harbour and bridge, wind around the tower. The atmosphere grows quieter while the story is read.',
|
||||
size: 20,
|
||||
),
|
||||
writing('Atmosphere volume', size: 20),
|
||||
Slider(
|
||||
label:
|
||||
'${(prefs.decimal('ambience_volume', .7) * 100).round()}%',
|
||||
value: prefs.decimal('ambience_volume', .7).clamp(0.0, 1.0),
|
||||
divisions: 10,
|
||||
onChanged: atmosphereEnabled
|
||||
? (value) => save('ambience_volume', value)
|
||||
: null,
|
||||
),
|
||||
writing(
|
||||
'Readings play when you press Play. Your device volume controls the voice.',
|
||||
size: 18,
|
||||
color: muted,
|
||||
),
|
||||
if (ambience.error.isNotEmpty)
|
||||
writing(ambience.error, size: 18),
|
||||
],
|
||||
return AlertDialog(
|
||||
title: Text('Sound & atmosphere', style: hand(29)),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text('Environmental sounds', style: hand(23)),
|
||||
value: atmosphereEnabled,
|
||||
onChanged: (value) => save('ambience_enabled', value),
|
||||
),
|
||||
writing(
|
||||
'Water at the harbour and bridge, wind around the tower. The atmosphere grows quieter while the story is read.',
|
||||
size: 20,
|
||||
),
|
||||
writing('Atmosphere volume', size: 20),
|
||||
Slider(
|
||||
label:
|
||||
'${(prefs.decimal('ambience_volume', .7) * 100).round()}%',
|
||||
value: prefs.decimal('ambience_volume', .7).clamp(0.0, 1.0),
|
||||
divisions: 10,
|
||||
onChanged: atmosphereEnabled
|
||||
? (value) => save('ambience_volume', value)
|
||||
: null,
|
||||
),
|
||||
writing(
|
||||
'Readings start 3 seconds after opening a page. You can play, pause or stop them at any time. Your device volume controls the voice.',
|
||||
size: 18,
|
||||
color: muted,
|
||||
),
|
||||
if (ambience.error.isNotEmpty)
|
||||
writing(ambience.error, size: 18),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('Keep going'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(dialogContext),
|
||||
child: const Text('Keep going'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget contents() {
|
||||
final chapter = g.contentsChapter;
|
||||
|
||||
@@ -16,6 +16,8 @@ class Narrator extends ChangeNotifier {
|
||||
final List<StreamSubscription<dynamic>> _subscriptions = [];
|
||||
String page = '', error = '';
|
||||
bool loading = false;
|
||||
Timer? _autoplay;
|
||||
bool get autoplayPending => _autoplay?.isActive ?? false;
|
||||
int _generation = 0;
|
||||
bool get playing =>
|
||||
_player.playing && _player.processingState != ProcessingState.completed;
|
||||
@@ -36,7 +38,9 @@ class Narrator extends ChangeNotifier {
|
||||
String get status => error.isNotEmpty
|
||||
? error
|
||||
: page.isEmpty
|
||||
? 'A recorded reading, kept with this page'
|
||||
? autoplayPending
|
||||
? 'The reading starts automatically in 3 seconds'
|
||||
: 'A recorded reading, kept with this page'
|
||||
: finished
|
||||
? 'End of the reading · listen again whenever you like'
|
||||
: '${playing ? 'Playing' : 'Paused'} · ${_time(position)} / ${_time(_player.duration ?? Duration.zero)}';
|
||||
@@ -61,7 +65,26 @@ class Narrator extends ChangeNotifier {
|
||||
return tracks[page]?['speaker'] ?? '';
|
||||
}
|
||||
|
||||
void scheduleAutoplay(String? id, double speed) {
|
||||
cancelAutoplay();
|
||||
if (id == null || !tracks.containsKey(id)) return;
|
||||
_autoplay = Timer(const Duration(seconds: 3), () {
|
||||
_autoplay = null;
|
||||
notifyListeners();
|
||||
if (!playing && !loading) unawaited(toggle(id, speed));
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void cancelAutoplay() {
|
||||
if (_autoplay == null) return;
|
||||
_autoplay!.cancel();
|
||||
_autoplay = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> toggle(String id, double speed) async {
|
||||
cancelAutoplay();
|
||||
if (loading) return;
|
||||
if (playing && page == id) {
|
||||
await _player.pause();
|
||||
@@ -105,10 +128,12 @@ class Narrator extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> pause() async {
|
||||
cancelAutoplay();
|
||||
await _player.pause();
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
cancelAutoplay();
|
||||
++_generation;
|
||||
page = '';
|
||||
loading = false;
|
||||
@@ -119,6 +144,7 @@ class Narrator extends ChangeNotifier {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_autoplay?.cancel();
|
||||
for (final s in _subscriptions) {
|
||||
unawaited(s.cancel());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user