Files
minstrel/flutter_client/lib/update/update_banner.dart
T
bvandeusen 02d9f39845 feat(flutter): in-app update banner + Android install intent (#397 phase 2)
Closes the operator-facing half of #397. Soft banner mounts at the
top of the shell when the server has a newer APK bundled at
/api/client/version; tap Install → APK downloads to cache → Android
PackageInstaller intent fires → user taps Install in the system
dialog → app restarts on the new version.

### Dart side

- lib/update/update_info.dart — wire shape for /api/client/version
- lib/update/client_update_provider.dart — Riverpod AsyncNotifier
  polling on app start + every 24h. Compares semver via pub_semver
  with non-semver string-equality fallback. Server 404 = "no update
  channel" = silent. Companion shouldShowUpdateBannerProvider gates
  on a per-version dismissed-set (in-memory; resets on restart).
- lib/update/installer.dart — UpdateInstaller wraps dio.download +
  the MethodChannel call to MainActivity.kt.
- lib/update/update_banner.dart — Material banner with idle /
  downloading (with progress) / error stages. Mounted in
  _ShellWithPlayerBar above the route content; SizedBox.shrink()
  when no update available so non-shell routes (login, server-url)
  see no banner anyway.
- isVersionNewer() extracted as a pure function and tested across
  semver, prerelease ordering, leading-v normalization, and
  non-semver fallback (date-tag style).

### Android side

- AndroidManifest.xml — REQUEST_INSTALL_PACKAGES permission;
  FileProvider with authorities ${applicationId}.fileprovider.
- res/xml/file_paths.xml — exposes the cache directory so the
  downloaded APK can be served via content:// URI (file:// is
  blocked since Android 7).
- MainActivity.kt — MethodChannel handler builds the FileProvider
  URI and fires Intent.ACTION_VIEW with FLAG_GRANT_READ_URI_PERMISSION
  so the system installer can read across the process boundary.

Phase 3 (CI sequencing — bake APK + version file into /app/client/
during release.yml's tag flow) is the remaining piece. Without it
the server returns 404 from /api/client/version and the banner
silently does nothing — graceful degradation, but no actual updates.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 19:42:04 -04:00

130 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../theme/theme_extension.dart';
import 'client_update_provider.dart';
import 'update_info.dart';
/// Soft banner mounted at the top of the shell. Renders nothing when
/// no update is available or the user has dismissed this version's
/// banner. Tapping Install downloads the APK and fires the system
/// install intent (Android only).
class UpdateBanner extends ConsumerStatefulWidget {
const UpdateBanner({super.key});
@override
ConsumerState<UpdateBanner> createState() => _UpdateBannerState();
}
enum _Stage { idle, downloading, error }
class _UpdateBannerState extends ConsumerState<UpdateBanner> {
_Stage _stage = _Stage.idle;
double _progress = 0;
String? _error;
Future<void> _onInstall(UpdateInfo info) async {
setState(() {
_stage = _Stage.downloading;
_progress = 0;
_error = null;
});
try {
final installer = await ref.read(updateInstallerProvider.future);
final path = await installer.download(
info.apkUrl,
onProgress: (p) => setState(() => _progress = p),
);
await installer.install(path);
// Stage stays 'downloading' — Android system install dialog has
// taken over. If user cancels, the banner is still here.
} catch (e) {
if (!mounted) return;
setState(() {
_stage = _Stage.error;
_error = '$e';
});
}
}
void _onDismiss(UpdateInfo info) {
ref.read(dismissUpdateProvider)(info.version);
}
@override
Widget build(BuildContext context) {
final info = ref.watch(shouldShowUpdateBannerProvider);
if (info == null) return const SizedBox.shrink();
final fs = Theme.of(context).extension<FabledSwordTheme>()!;
return Material(
color: fs.iron,
child: SafeArea(
bottom: false,
child: Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 4, 8),
child: Row(
children: [
Icon(Icons.system_update, color: fs.parchment, size: 18),
const SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
_stage == _Stage.error
? 'Update failed'
: 'Update Minstrel · ${info.version} available',
style: TextStyle(color: fs.parchment, fontSize: 13),
overflow: TextOverflow.ellipsis,
),
if (_stage == _Stage.downloading) ...[
const SizedBox(height: 4),
LinearProgressIndicator(
value: _progress > 0 ? _progress : null,
minHeight: 2,
backgroundColor: fs.obsidian,
valueColor: AlwaysStoppedAnimation(fs.accent),
),
],
if (_stage == _Stage.error && _error != null) ...[
const SizedBox(height: 2),
Text(
_error!,
style: TextStyle(color: fs.ash, fontSize: 11),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
],
],
),
),
if (_stage == _Stage.idle)
TextButton(
onPressed: () => _onInstall(info),
style: TextButton.styleFrom(
foregroundColor: fs.accent,
minimumSize: const Size(64, 36),
),
child: const Text('Install'),
),
if (_stage == _Stage.error)
TextButton(
onPressed: () => _onInstall(info),
style: TextButton.styleFrom(foregroundColor: fs.accent),
child: const Text('Retry'),
),
IconButton(
tooltip: 'Dismiss',
icon: Icon(Icons.close, size: 18, color: fs.ash),
onPressed: () => _onDismiss(info),
),
],
),
),
),
);
}
}