Files
Garage-SDKs/garage_updates/lib/src/splash.dart
T
ImBenjiandClaude Opus 5.5 d3b232f0ee feat(garage_updates): an auto updater for garage apps
Asks Garage for the newest release of a project, downloads it with resume,
checks the sha256, installs it beside the old one and restarts into it.
Splash on garage_ui, a native stub and a first time installer for windows,
bundle swap on mac, the system installer on android. No-op on web.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KHotisA4hV2xmuZsHTksFX
2026-10-12 06:37:02 +01:00

187 lines
5.3 KiB
Dart

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<void>? _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<void> fail(String message) {
_error = message;
// a bar sweeping away under an error reads as still trying
_progress ??= 0;
final c = _retry = Completer<void>();
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<UpdateSplashApp> createState() => _UpdateSplashAppState();
}
class _UpdateSplashAppState extends State<UpdateSplashApp> {
late final _delegate = _SplashDelegate(
(context) => UpdateSplash(branding: widget.branding, controller: widget.controller),
);
late final _router = RouterConfig<Object>(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<Object> with ChangeNotifier {
_SplashDelegate(this.builder);
final WidgetBuilder builder;
@override
Widget build(BuildContext context) => builder(context);
@override
Future<bool> popRoute() => SynchronousFuture(false);
@override
Future<void> setNewRoutePath(Object configuration) => SynchronousFuture(null);
}