199 lines
5.7 KiB
Dart
199 lines
5.7 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'app_updates.dart';
|
|
import 'launch_update.dart';
|
|
|
|
Future<bool?> showLaunchUpdateDialog(
|
|
BuildContext context, {
|
|
required bool web,
|
|
required bool cupertino,
|
|
String sourceName = '',
|
|
String nativePlatform = 'android',
|
|
}) => cupertino
|
|
? showCupertinoDialog<bool>(
|
|
context: context,
|
|
builder: (context) => CupertinoAlertDialog(
|
|
title: const Text('Update available'),
|
|
content: Text(
|
|
web
|
|
? 'A newer Pipelines web build is available.'
|
|
: 'A newer ${nativePlatform == 'macos'
|
|
? 'macOS'
|
|
: nativePlatform == 'linux'
|
|
? 'Linux'
|
|
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}',
|
|
),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Later'),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDefaultAction: true,
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text(web ? 'Refresh' : 'Download'),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Update available'),
|
|
content: Text(
|
|
web
|
|
? 'A newer Pipelines web build is available.'
|
|
: 'A newer ${nativePlatform == 'macos'
|
|
? 'macOS'
|
|
: nativePlatform == 'linux'
|
|
? 'Linux'
|
|
: 'Android'} build is available${sourceName.isEmpty ? '' : ' from $sourceName'}.${nativePlatform == 'android' ? '' : ' Download and extract the archive to update the app.'}',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Later'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: Text(web ? 'Refresh' : 'Download'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
Future<void> showLaunchUpdateFailure(
|
|
BuildContext context,
|
|
Object error, {
|
|
required bool cupertino,
|
|
}) async {
|
|
if (cupertino) {
|
|
await showCupertinoDialog<void>(
|
|
context: context,
|
|
builder: (context) => CupertinoAlertDialog(
|
|
title: const Text('Update failed'),
|
|
content: Text('$error'),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
} else {
|
|
await showDialog<void>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Update failed'),
|
|
content: Text('$error'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Reuses the Settings installer while keeping download progress visible.
|
|
Future<void> installLaunchUpdate(
|
|
BuildContext context,
|
|
AppUpdate update, {
|
|
required UpdateInstaller installer,
|
|
required bool cupertino,
|
|
}) async {
|
|
final result = cupertino
|
|
? await showCupertinoDialog<_InstallResult>(
|
|
context: context,
|
|
builder: (_) => _InstallProgress(
|
|
update: update,
|
|
installer: installer,
|
|
cupertino: true,
|
|
),
|
|
)
|
|
: await showDialog<_InstallResult>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => _InstallProgress(
|
|
update: update,
|
|
installer: installer,
|
|
cupertino: false,
|
|
),
|
|
);
|
|
if (result?.error case final Object error) throw error;
|
|
}
|
|
|
|
class _InstallResult {
|
|
const _InstallResult([this.error]);
|
|
final Object? error;
|
|
}
|
|
|
|
class _InstallProgress extends StatefulWidget {
|
|
const _InstallProgress({
|
|
required this.update,
|
|
required this.installer,
|
|
required this.cupertino,
|
|
});
|
|
final AppUpdate update;
|
|
final UpdateInstaller installer;
|
|
final bool cupertino;
|
|
|
|
@override
|
|
State<_InstallProgress> createState() => _InstallProgressState();
|
|
}
|
|
|
|
class _InstallProgressState extends State<_InstallProgress> {
|
|
double? _progress;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
|
Object? error;
|
|
try {
|
|
await widget.installer(
|
|
widget.update,
|
|
onProgress: (progress) {
|
|
if (mounted) setState(() => _progress = progress.clamp(0, 1));
|
|
},
|
|
);
|
|
} catch (failure) {
|
|
error = failure;
|
|
}
|
|
if (mounted) Navigator.of(context).pop(_InstallResult(error));
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final progress = _progress;
|
|
final content = Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
progress == null
|
|
? 'Downloading update…'
|
|
: 'Downloading update… ${(progress * 100).round()}%',
|
|
),
|
|
const SizedBox(height: 16),
|
|
widget.cupertino
|
|
? const CupertinoActivityIndicator()
|
|
: LinearProgressIndicator(value: progress),
|
|
],
|
|
);
|
|
return PopScope(
|
|
canPop: false,
|
|
child: widget.cupertino
|
|
? CupertinoAlertDialog(
|
|
title: const Text('App update'),
|
|
content: content,
|
|
)
|
|
: AlertDialog(title: const Text('App update'), content: content),
|
|
);
|
|
}
|
|
}
|