import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../ui/adaptive/host.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 createState() => _SettingsUpdatesSectionState(); } class _SettingsUpdatesSectionState extends State { 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 _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 _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) { final cupertino = usesCupertinoHost(context); Widget button( Key key, String label, VoidCallback? onPressed, bool primary, ) => cupertino ? (primary ? CupertinoButton.filled( key: key, onPressed: onPressed, child: Text(label), ) : CupertinoButton( key: key, onPressed: onPressed, child: Text(label), )) : (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' ? 'Install and restart' : 'Download update', _busy ? null : _install, true, ), ], ), if (_progress != null) Padding( padding: const EdgeInsets.only(top: 8), child: cupertino ? CupertinoActivityIndicator.partiallyRevealed( progress: _progress!, ) : LinearProgressIndicator(value: _progress), ), if (_message != null) Padding( padding: const EdgeInsets.only(top: 4), child: Semantics(liveRegion: true, child: Text(_message!)), ), ], ); } }