Files
pipelines/app/lib/features/settings/settings_updates_section.dart
T
alan 6d58b3fb03 Install macOS updates in place and restart
Like Linux, the macOS app downloads the verified archive, unpacks it with
ditto beside the installed bundle, swaps it in (keeping the previous
bundle to roll back) and relaunches once the old process has quit.
2026-11-13 11:43:40 +00:00

162 lines
4.5 KiB
Dart

import 'package:flutter/material.dart';
import '../updates/app_updates.dart';
import 'server_connections.dart';
/// Check for updates: compares this build with the newest one the server
/// offers and installs it on request.
class SettingsUpdatesSection extends StatefulWidget {
const SettingsUpdatesSection({
super.key,
required this.endpoint,
this.servers,
});
/// The server's websocket address; its builds sit beside it.
final String endpoint;
final ServerConnections? servers;
@override
State<SettingsUpdatesSection> createState() => _SettingsUpdatesSectionState();
}
class _SettingsUpdatesSectionState extends State<SettingsUpdatesSection> {
int? _installed;
AppUpdate? _available;
bool _busy = false;
double? _progress;
String? _message;
@override
void initState() {
super.initState();
installedVersionCode().then((code) {
if (mounted) setState(() => _installed = code);
});
}
Future<void> _check() async {
final endpoint = Uri.tryParse(widget.endpoint.trim());
final base = endpoint == null ? null : updatesBaseFor(endpoint);
if (base == null) {
setState(() => _message = 'Set the server address first.');
return;
}
setState(() {
_busy = true;
_message = null;
_available = null;
});
try {
final latest = widget.servers == null
? await fetchLatestUpdate(base)
: await newestServerUpdate(
widget.servers!.catalog.servers,
stillTrusted: (source, uri) => updateSourceStillTrusted(
widget.servers!.catalog.servers,
source,
uri,
),
);
if (!mounted) return;
final installed = _installed ?? 0;
setState(() {
if (latest.versionCode > installed) {
_available = latest;
_message =
'Build ${latest.versionCode} is available${latest.sourceName.isEmpty ? '' : ' from ${latest.sourceName}'}.';
} else {
_message = 'No newer compatible build found on reachable servers.';
}
});
} catch (error) {
if (mounted) setState(() => _message = '$error');
} finally {
if (mounted) setState(() => _busy = false);
}
}
Future<void> _install() async {
final update = _available;
if (update == null) return;
setState(() {
_busy = true;
_progress = 0;
_message = 'Downloading…';
});
try {
await downloadAndInstall(
update,
onProgress: (value) {
if (mounted) setState(() => _progress = value);
},
);
if (mounted) setState(() => _message = 'Opened the downloaded update.');
} catch (error) {
if (mounted) setState(() => _message = '$error');
} finally {
if (mounted) {
setState(() {
_busy = false;
_progress = null;
});
}
}
}
@override
Widget build(BuildContext context) {
Widget button(
Key key,
String label,
VoidCallback? onPressed,
bool primary,
) => primary
? FilledButton(key: key, onPressed: onPressed, child: Text(label))
: OutlinedButton(key: key, onPressed: onPressed, child: Text(label));
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
_installed == null
? 'Installed build unknown.'
: 'Installed build $_installed.',
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
button(
const Key('settings-check-updates'),
'Check for updates',
_busy ? null : _check,
_available == null,
),
if (_available != null)
button(
const Key('settings-install-update'),
_available!.platform == 'linux' ||
_available!.platform == 'macos'
? 'Install and restart'
: 'Download update',
_busy ? null : _install,
true,
),
],
),
if (_progress != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: LinearProgressIndicator(value: _progress),
),
if (_message != null)
Padding(
padding: const EdgeInsets.only(top: 4),
child: Semantics(liveRegion: true, child: Text(_message!)),
),
],
);
}
}