Autoplay page narration after a three-second delay

This commit is contained in:
Hadrian Burkhardt
2026-11-02 05:35:14 +01:00
parent a3ead03b6b
commit 4887eca6d4
10 changed files with 327 additions and 73 deletions
+83 -58
View File
@@ -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;