feat(app): Check for updates downloads the server's newest APK and opens the installer
This commit is contained in:
@@ -14,6 +14,8 @@ import 'settings_notifications_section.dart';
|
||||
import 'settings_schedules_section.dart';
|
||||
import 'settings_sections.dart';
|
||||
import 'settings_sign_in_section.dart';
|
||||
import 'settings_updates_section.dart';
|
||||
import '../updates/app_updates.dart';
|
||||
import 'settings_store.dart';
|
||||
import 'settings_templates_section.dart';
|
||||
|
||||
@@ -159,6 +161,16 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
),
|
||||
),
|
||||
],
|
||||
// A phone installs newer builds from the server.
|
||||
if (widget.showSignIn && appUpdatesSupported) ...<Widget>[
|
||||
const SizedBox(height: settingsSectionGap),
|
||||
_SettingsSection(
|
||||
title: 'App updates',
|
||||
child: SettingsUpdatesSection(
|
||||
endpoint: _controller.serverUrl,
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: settingsSectionGap),
|
||||
_SettingsSection(
|
||||
title: 'Workspace',
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
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!)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user