137 lines
4.2 KiB
Dart
137 lines
4.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../ui/adaptive/host.dart';
|
|
import '../artifacts/ui/artifact_external_open.dart';
|
|
import '../artifacts/ui/artifact_viewer_fetch.dart';
|
|
import '../updates/app_updates.dart';
|
|
|
|
/// Reads the server's update feed; replaced in tests.
|
|
typedef UpdateFeedReader = Future<AppUpdate?> Function(Uri base);
|
|
|
|
/// The web page's link to the Android app the server offers at `/updates/`
|
|
/// (`./install --apk`): its build, notes and a Download button.
|
|
class SettingsAndroidDownloadSection extends StatefulWidget {
|
|
const SettingsAndroidDownloadSection({
|
|
super.key,
|
|
this.base,
|
|
this.read = readUpdateFeed,
|
|
this.open = openArtifactExternally,
|
|
});
|
|
|
|
/// The feed's folder; by default `/updates/` on this page's own origin.
|
|
final Uri? base;
|
|
final UpdateFeedReader read;
|
|
final Future<bool> Function(Uri uri) open;
|
|
|
|
@override
|
|
State<SettingsAndroidDownloadSection> createState() =>
|
|
_SettingsAndroidDownloadSectionState();
|
|
}
|
|
|
|
/// The feed at [base], or null when the server offers no APK.
|
|
Future<AppUpdate?> readUpdateFeed(Uri base) async {
|
|
final result = await fetchArtifact(base.resolve('version.json'));
|
|
if (result.status != 200) return null;
|
|
return AppUpdate.parse(utf8.decode(result.bytes), base);
|
|
}
|
|
|
|
class _SettingsAndroidDownloadSectionState
|
|
extends State<SettingsAndroidDownloadSection> {
|
|
late final Uri _base = widget.base ?? Uri.base.resolve('/updates/');
|
|
AppUpdate? _update;
|
|
bool _loading = true;
|
|
String? _message;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
widget
|
|
.read(_base)
|
|
.then((update) {
|
|
if (mounted) setState(() => _update = update);
|
|
})
|
|
.catchError((Object _) {})
|
|
.whenComplete(() {
|
|
if (mounted) setState(() => _loading = false);
|
|
});
|
|
}
|
|
|
|
Future<void> _download(AppUpdate update) async {
|
|
final opened = await widget.open(update.apk);
|
|
if (!opened && mounted) {
|
|
setState(
|
|
() => _message =
|
|
'The browser blocked the download; allow pop-ups and retry.',
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final cupertino = usesCupertinoHost(context);
|
|
final muted = cupertino
|
|
? CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith(
|
|
color: CupertinoColors.secondaryLabel.resolveFrom(context),
|
|
)
|
|
: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
);
|
|
final update = _update;
|
|
if (_loading) {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Text('Checking for the Android app…', style: muted),
|
|
);
|
|
}
|
|
if (update == null) {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Text(
|
|
'This server offers no Android app yet. Build one with ./install --apk.',
|
|
style: muted,
|
|
),
|
|
);
|
|
}
|
|
final button = cupertino
|
|
? CupertinoButton.filled(
|
|
key: const Key('settings-android-download'),
|
|
onPressed: () => _download(update),
|
|
child: const Text('Download APK'),
|
|
)
|
|
: FilledButton.icon(
|
|
key: const Key('settings-android-download'),
|
|
onPressed: () => _download(update),
|
|
icon: const Icon(Icons.download),
|
|
label: const Text('Download APK'),
|
|
);
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Pipelines for Android · build ${update.versionCode}'),
|
|
if (update.notes.isNotEmpty) ...[
|
|
const SizedBox(height: 4),
|
|
Text(update.notes, style: muted),
|
|
],
|
|
const SizedBox(height: 12),
|
|
button,
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Open it on the phone, allow installs from this browser, then sign '
|
|
'in under Settings. Later builds arrive through Check for updates.',
|
|
style: muted,
|
|
),
|
|
if (_message case final message?) ...[
|
|
const SizedBox(height: 8),
|
|
Text(message, style: muted),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|