halkyon/packages/halkyon_app/lib/src/app/person_scope.dart
Tizian 「ティツィアーン」 e23cc77f7c
halkyon_app: instances, sign-in and sync plumbing for Halkyon's apps
What every Halkyon app's shell needs, taken from Halkyon Learn's and set
on halkyon_base's client: the instances a device keeps an account on,
the first start's tiles and where an instance keeps people's data, the
account switcher; signing in anonymously, with GitHub or an email code,
switching person and the start screen; the gates that build an app's
dependencies per instance and per person; and the sync service with its
status, Settings card and banner. Texts of its own in English and
German take the app's name; what only the app can say, it passes in.
Fakes for apps' tests are in testing.dart.

An instance's basics now name the app it serves (`app`, set by the
host's configuration), and an app accepts only an instance of its own.
SerialQueue and CoalescingRunner move into halkyon_core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-19 15:31:40 +02:00

91 lines
2.8 KiB
Dart

import 'dart:async';
import 'package:halkyon_app/l10n/halkyon_app_localizations.dart';
import 'package:halkyon_app/src/app/app_frame.dart';
import 'package:halkyon_app/src/app/app_scope.dart';
import 'package:halkyon_app/src/auth/model/auth_status.dart';
import 'package:material_ui/material_ui.dart';
/// Opens [person]'s dependencies (`AppDependencies.openPerson`), shows
/// [child] with them, and disposes them when it goes away. `SessionGate`
/// gives every person a new `PersonScope`.
class const PersonScope({
/// Whose dependencies these are.
required final SignedIn person,
/// What the person sees.
required final Widget child,
super.key,
}) extends StatefulWidget {
/// The dependencies of the person signed in above [context], as the app's
/// own type [P] (`PersonScope.of<PersonDependencies>(context)`).
static P of<P extends Disposable>(BuildContext context) {
final scope = context
.getInheritedWidgetOfExactType<_PersonDependenciesScope>();
assert(scope != null, 'No PersonScope above this widget.');
return scope!.dependencies as P;
}
@override
State<PersonScope> createState() => _PersonScopeState();
}
class _PersonScopeState() extends State<PersonScope> {
Future<Disposable>? _opening;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_opening ??= AppScope.of(context).openPerson(widget.person);
}
@override
void dispose() {
unawaited(
_opening?.then(
(dependencies) => dependencies.dispose(),
// Opening failed and said so on screen; there is nothing to close.
onError: (Object _) {},
),
);
super.dispose();
}
@override
Widget build(BuildContext context) => FutureBuilder(
future: _opening,
builder: (context, snapshot) => switch (snapshot) {
AsyncSnapshot(:final data?) => _PersonDependenciesScope(
dependencies: data,
child: widget.child,
),
AsyncSnapshot(hasError: true) => Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(HalkyonAppLocalizations.of(context).personDataFailed),
const SizedBox(height: 16),
FilledButton(
onPressed: () => setState(() {
_opening = AppScope.of(context).openPerson(widget.person);
}),
child: Text(HalkyonAppLocalizations.of(context).tryAgain),
),
],
),
),
),
_ => AppFrameScope.of(context).launchScreen,
},
);
}
class const _PersonDependenciesScope({
required final Disposable dependencies,
required super.child,
}) extends InheritedWidget {
@override
bool updateShouldNotify(_PersonDependenciesScope oldWidget) =>
dependencies != oldWidget.dependencies;
}