166 lines
4.5 KiB
Dart
166 lines
4.5 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../ui/adaptive/host.dart';
|
|
import '../updates/app_updates.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});
|
|
|
|
/// The server's websocket address; its builds sit beside it.
|
|
final String endpoint;
|
|
|
|
@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 = await fetchLatestUpdate(base);
|
|
if (!mounted) return;
|
|
final installed = _installed ?? 0;
|
|
setState(() {
|
|
if (latest.versionCode > installed) {
|
|
_available = latest;
|
|
_message = 'Build ${latest.versionCode} is available.';
|
|
} else {
|
|
_message = 'Up to date.';
|
|
}
|
|
});
|
|
} 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 installer.');
|
|
} 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'),
|
|
'Download and install',
|
|
_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!)),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|