diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 599ed150..40bb013b 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -471,6 +471,7 @@ class _PipelinesAppState extends State client: _session.client, // A phone signs in at the web login; a browser has its own. showSignIn: !kIsWeb, + showAndroidDownload: kIsWeb, initialEndpoint: _session.endpoint, embedded: true, onOpenTemplates: _delegate.openTemplates, diff --git a/app/lib/features/settings/settings_android_download_section.dart b/app/lib/features/settings/settings_android_download_section.dart new file mode 100644 index 00000000..313adaa2 --- /dev/null +++ b/app/lib/features/settings/settings_android_download_section.dart @@ -0,0 +1,136 @@ +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 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 Function(Uri uri) open; + + @override + State createState() => + _SettingsAndroidDownloadSectionState(); +} + +/// The feed at [base], or null when the server offers no APK. +Future 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 { + 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 _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), + ], + ], + ), + ); + } +} diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index a864bc0a..8dac2371 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -11,6 +11,7 @@ import 'settings_controller.dart'; import 'settings_controls.dart'; import 'settings_layout.dart'; import 'settings_notifications_section.dart'; +import 'settings_android_download_section.dart'; import 'settings_schedules_section.dart'; import 'settings_sections.dart'; import 'settings_sign_in_section.dart'; @@ -39,6 +40,7 @@ class SettingsScreen extends StatefulWidget { this.onOpenSchedules, this.notifications, this.showSignIn = false, + this.showAndroidDownload = false, }); final transport.PipelinesClient client; @@ -46,6 +48,9 @@ class SettingsScreen extends StatefulWidget { /// Whether to offer the web sign-in (the native app; a browser has its own). final bool showSignIn; + /// A browser page links the Android app the server offers. + final bool showAndroidDownload; + /// A fixture/preview seed for the URL field. Persisted settings still load; /// this value takes precedence for the draft and is never written by itself. final Uri? initialEndpoint; @@ -161,6 +166,14 @@ class _SettingsScreenState extends State { ), ), ], + // The web page links the Android app the server offers. + if (widget.showAndroidDownload) ...[ + const SizedBox(height: settingsSectionGap), + const _SettingsSection( + title: 'Android app', + child: SettingsAndroidDownloadSection(), + ), + ], // A phone installs newer builds from the server. if (widget.showSignIn && appUpdatesSupported) ...[ const SizedBox(height: settingsSectionGap), diff --git a/app/test/features/settings/settings_android_download_test.dart b/app/test/features/settings/settings_android_download_test.dart new file mode 100644 index 00000000..0c3db4ce --- /dev/null +++ b/app/test/features/settings/settings_android_download_test.dart @@ -0,0 +1,53 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/settings_android_download_section.dart'; +import 'package:pipelines_app/features/updates/app_updates.dart'; + +void main() { + final base = Uri.parse('https://host.ts.net:8443/updates/'); + + testWidgets('offers the server APK for download', (tester) async { + final opened = []; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SettingsAndroidDownloadSection( + base: base, + read: (feed) async => AppUpdate.parse( + '{"versionCode": 29879834, "apk": "pipelines.apk", "notes": "New icon"}', + feed, + ), + open: (uri) async { + opened.add(uri); + return true; + }, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.text('Pipelines for Android · build 29879834'), findsOneWidget); + expect(find.text('New icon'), findsOneWidget); + await tester.tap(find.byKey(const Key('settings-android-download'))); + await tester.pumpAndSettle(); + expect(opened, [ + Uri.parse('https://host.ts.net:8443/updates/pipelines.apk'), + ]); + }); + + testWidgets('says when the server offers no APK', (tester) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SettingsAndroidDownloadSection( + base: base, + read: (_) async => null, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.textContaining('./install --apk'), findsOneWidget); + expect(find.byKey(const Key('settings-android-download')), findsNothing); + }); +}