import "dart:async"; import "package:flutter/foundation.dart"; import "package:flutter/widgets.dart"; import "package:garage_ui/garage_ui.dart"; import "branding.dart"; // What's on screen before the app's own runApp. A fixed layout and nothing to // configure but the logo and the name: logo, name, one status line, a bar, // and when a required update wont go in, why and a Retry. enum SplashStage { checking, downloading, installing, starting } class SplashController extends ChangeNotifier { SplashStage _stage = SplashStage.checking; double? _progress; String? _error; Completer? _retry; SplashStage get stage => _stage; /// 0 to 1 while theres a number to show, null = busy double? get progress => _progress; /// set when a required update failed. the Retry button shows with it String? get error => _error; String get statusLine => switch (_stage) { SplashStage.checking => "Checking for updates", SplashStage.downloading => "Downloading update", SplashStage.installing => "Installing", SplashStage.starting => "Starting", }; void show(SplashStage stage, {double? progress}) { _stage = stage; _progress = progress; _error = null; notifyListeners(); } set progress(double? v) { if (v == _progress) return; _progress = v; notifyListeners(); } /// Puts the error up and waits for Retry to be pressed. Future fail(String message) { _error = message; // a bar sweeping away under an error reads as still trying _progress ??= 0; final c = _retry = Completer(); notifyListeners(); return c.future; } void retry() { final c = _retry; _retry = null; _error = null; notifyListeners(); if (c != null && !c.isCompleted) c.complete(); } } class UpdateSplash extends StatelessWidget { const UpdateSplash({required this.branding, required this.controller, super.key}); final UpdaterBranding branding; final SplashController controller; @override Widget build(BuildContext context) { final density = GarageTheme.of(context).density; final logo = branding.buildLogo(); return Scaffold( child: Center( child: SizedBox( width: density.fontSize * 22, child: ListenableBuilder( listenable: controller, builder: (context, _) { final error = controller.error; return Column( mainAxisSize: MainAxisSize.min, children: [ if (logo != null) ...[ SizedBox.square(dimension: density.controlHeight * 2, child: logo), const Gap.lg(), ], Text(branding.name).large().semiBold(), const Gap.sm(), Text(controller.statusLine).muted(), const Gap.lg(), // the bar has no width of its own, it takes the columns SizedBox( width: double.infinity, child: LinearProgressIndicator(value: controller.progress), ), if (error != null) ...[ const Gap.xl(), Text(error).muted(), const Gap.md(), Button( style: const ButtonStyle.outline(), onPressed: controller.retry, child: const Text("Retry"), ), ], ], ); }, ), ), ), ); } } /// The tiny app `launch` runs to get the splash on screen. The host app's own /// runApp replaces it, and [onGone] is how the updater finds out it has. class UpdateSplashApp extends StatefulWidget { const UpdateSplashApp({ required this.branding, required this.controller, this.onGone, super.key, }); final UpdaterBranding branding; final SplashController controller; final VoidCallback? onGone; static final defaultTheme = ThemeData( colorScheme: GarageSchemes.carbon, density: const Density.product(), ); @override State createState() => _UpdateSplashAppState(); } class _UpdateSplashAppState extends State { late final _delegate = _SplashDelegate( (context) => UpdateSplash(branding: widget.branding, controller: widget.controller), ); late final _router = RouterConfig(routerDelegate: _delegate); @override void dispose() { _delegate.dispose(); widget.onGone?.call(); super.dispose(); } @override Widget build(BuildContext context) { return GarageApp.router( routerConfig: _router, theme: widget.branding.theme ?? UpdateSplashApp.defaultTheme, title: widget.branding.name, ); } } // one page and nowhere to go. GarageApp only comes as a router, and pulling // go_router into every app that wants updates for this would be daft class _SplashDelegate extends RouterDelegate with ChangeNotifier { _SplashDelegate(this.builder); final WidgetBuilder builder; @override Widget build(BuildContext context) => builder(context); @override Future popRoute() => SynchronousFuture(false); @override Future setNewRoutePath(Object configuration) => SynchronousFuture(null); }