Files
logic-puzzle/lib/domain/library.dart
T
Hadrian Burkhardt 0718e3f252 Open Lumen on a responsive bookshelf and land pending story work
- Start the app on a shelf of every installed book instead of the last
  book's home. Cards show the cover, translated metadata, available
  languages, restored patterns and either "Continue reading" or a way in.
- Keep each book's saved route: the shelf is never written to
  flutter_screen, so choosing a book resumes its own leaf.
- Lay the shelf out responsively: dense single-column cards on phones,
  a two-column grid from 720 logical pixels, a centred 860 px column on
  tablets and a chevron that exists in the bundled handwriting font.
- Keep Settings reachable from the shelf and let the home page's story
  library button open the shelf instead of a dialog.
- Add tool/shelf_screenshots_test.dart, which renders the shelf at phone
  and tablet metrics, checks that every card and Settings stay reachable
  without overflow, and writes artifacts/library/shelf-*.png.
- Commit the pending Midnight Invitation package (campaign, notebook,
  narration and artwork), cast drama and ambience tooling, region
  selection, phone/tablet integration coverage and related docs.

flutter analyze is clean and all 74 widget/domain tests pass.
2026-11-05 14:29:50 +01:00

99 lines
3.3 KiB
Dart

import 'dart:convert';
import 'package:flutter/services.dart';
import 'book.dart';
import '../services/translation.dart';
/// A catalogue entry identifies a story independently of its translations.
class StoryDefinition {
StoryDefinition(Map<String, dynamic> data)
: id = data['id'],
title = data['title'],
description = data['description'] ?? '',
coverAsset = data['coverAsset'],
paperAsset = data['paperAsset'],
defaultLanguage = (data['defaultLanguage'] as String? ?? 'en')
.replaceAll('_', '-')
.toLowerCase(),
campaignAsset = data['campaignAsset'],
notebookAsset = data['notebookAsset'],
scriptsAsset = data['scriptsAsset'],
tracksAsset = data['tracksAsset'],
atmosphereAsset = data['atmosphereAsset'],
presentation = data['presentation'] ?? 'standard',
continueAt = data['continueAt'],
initiallyAccessible = data['initiallyAccessible'] ?? 0,
languages = {
for (final entry in (data['languages'] as Map).entries)
(entry.key as String).replaceAll('_', '-').toLowerCase(): entry.value,
};
final String id, title, description, defaultLanguage, presentation;
final String campaignAsset, notebookAsset;
final String? coverAsset,
paperAsset,
scriptsAsset,
tracksAsset,
atmosphereAsset;
final int? continueAt;
final int initiallyAccessible;
final Map<String, dynamic> languages;
String languageFor(String requested) {
final tag = requested.replaceAll('_', '-').toLowerCase();
if (languages.containsKey(tag)) return tag;
final base = tag.split('-').first;
return languages.containsKey(base) ? base : defaultLanguage;
}
}
class StoryLibrary {
StoryLibrary(
this.stories, {
AssetBundle? bundle,
this.copy = TranslationCatalog.english,
}) : bundle = bundle ?? rootBundle {
if (stories.isEmpty ||
stories.map((s) => s.id).toSet().length != stories.length) {
throw const FormatException('The library needs unique story IDs.');
}
for (final story in stories) {
if (!RegExp(r'^[a-z0-9-]+$').hasMatch(story.id) ||
!story.languages.containsKey(story.defaultLanguage)) {
throw FormatException('Invalid story definition: ${story.id}');
}
}
}
final List<StoryDefinition> stories;
final AssetBundle bundle;
final TranslationCatalog copy;
static Future<StoryLibrary> load({AssetBundle? bundle}) async {
final assets = bundle ?? rootBundle;
final data = jsonDecode(await assets.loadString('assets/library.json'));
return StoryLibrary(
(data['stories'] as List).map((s) => StoryDefinition(s)).toList(),
bundle: assets,
copy: await TranslationCatalog.load(assets),
);
}
StoryDefinition definition(String id) =>
stories.where((s) => s.id == id).firstOrNull ?? stories.first;
final Map<String, Future<Book>> _books = {};
Future<Book> open(String id, {String language = 'en'}) {
final story = definition(id);
final key = '${story.id}:${story.languageFor(language)}';
return _books.putIfAbsent(
key,
() => Book.loadDefinition(story, language: language, bundle: bundle)
.catchError((Object error, StackTrace stack) {
_books.remove(key);
Error.throwWithStackTrace(error, stack);
}),
);
}
}