ente/lib/ui/settings/app_update_dialog.dart

248 lines
7.2 KiB
Dart
Raw Normal View History

2021-05-22 15:46:04 +00:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2022-09-17 09:02:58 +00:00
// import 'package:open_file/open_file.dart';
2021-05-22 15:46:04 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/core/network.dart';
2022-07-12 06:30:02 +00:00
import 'package:photos/ente_theme_data.dart';
2021-05-22 15:46:04 +00:00
import 'package:photos/services/update_service.dart';
import 'package:photos/theme/ente_theme.dart';
import 'package:url_launcher/url_launcher_string.dart';
2021-05-22 15:46:04 +00:00
class AppUpdateDialog extends StatefulWidget {
final LatestVersionInfo? latestVersionInfo;
2021-05-22 15:46:04 +00:00
const AppUpdateDialog(this.latestVersionInfo, {Key? key}) : super(key: key);
2021-05-22 15:46:04 +00:00
@override
2022-07-03 09:45:00 +00:00
State<AppUpdateDialog> createState() => _AppUpdateDialogState();
2021-05-22 15:46:04 +00:00
}
class _AppUpdateDialogState extends State<AppUpdateDialog> {
@override
Widget build(BuildContext context) {
final List<Widget> changelog = [];
final enteTextTheme = getEnteTextTheme(context);
final enteColor = getEnteColorScheme(context);
for (final log in widget.latestVersionInfo!.changelog) {
2022-06-11 08:23:52 +00:00
changelog.add(
Padding(
padding: const EdgeInsets.fromLTRB(0, 4, 0, 4),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"- ",
style: enteTextTheme.small.copyWith(color: enteColor.textMuted),
),
Flexible(
child: Text(
log,
softWrap: true,
style:
enteTextTheme.small.copyWith(color: enteColor.textMuted),
),
)
],
),
2022-06-11 08:23:52 +00:00
),
);
2021-05-22 15:46:04 +00:00
}
final content = Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"A new version of ente is available.",
style: enteTextTheme.body.copyWith(color: enteColor.textMuted),
2021-05-22 15:46:04 +00:00
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
2021-05-22 15:46:04 +00:00
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: changelog,
),
2022-07-04 06:02:17 +00:00
const Padding(padding: EdgeInsets.all(8)),
2022-07-03 06:49:00 +00:00
SizedBox(
2021-05-22 15:46:04 +00:00
width: double.infinity,
height: 56,
2022-05-30 02:30:51 +00:00
child: OutlinedButton(
style: Theme.of(context).outlinedButtonTheme.style!.copyWith(
2022-05-30 02:30:51 +00:00
textStyle: MaterialStateProperty.resolveWith<TextStyle>(
(Set<MaterialState> states) {
return enteTextTheme.bodyBold;
2022-05-30 02:30:51 +00:00
},
),
),
2021-05-22 15:46:04 +00:00
onPressed: () async {
Navigator.pop(context);
showDialog(
context: context,
builder: (BuildContext context) {
return ApkDownloaderDialog(widget.latestVersionInfo);
},
barrierDismissible: false,
);
},
2022-07-04 06:02:17 +00:00
child: const Text(
2022-07-03 09:45:00 +00:00
"Update",
),
2021-05-22 15:46:04 +00:00
),
),
const Padding(padding: EdgeInsets.all(8)),
Center(
child: InkWell(
child: Text(
"Install manually",
style: Theme.of(context)
.textTheme
.caption!
.copyWith(decoration: TextDecoration.underline),
),
onTap: () => launchUrlString(
widget.latestVersionInfo!.url,
mode: LaunchMode.externalApplication,
),
),
)
2021-05-22 15:46:04 +00:00
],
);
2022-07-03 09:49:33 +00:00
final shouldForceUpdate =
UpdateService.instance.shouldForceUpdate(widget.latestVersionInfo!);
2021-05-22 15:46:04 +00:00
return WillPopScope(
onWillPop: () async => !shouldForceUpdate,
2021-05-22 15:46:04 +00:00
child: AlertDialog(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.auto_awesome_outlined,
size: 48,
color: enteColor.strokeMuted,
),
const SizedBox(
height: 16,
),
Text(
shouldForceUpdate
? "Critical update available"
: "Update available",
style: enteTextTheme.h3Bold,
),
],
2022-06-11 08:23:52 +00:00
),
2021-05-22 15:46:04 +00:00
content: content,
),
);
}
}
class ApkDownloaderDialog extends StatefulWidget {
final LatestVersionInfo? versionInfo;
2021-05-22 15:46:04 +00:00
const ApkDownloaderDialog(this.versionInfo, {Key? key}) : super(key: key);
2021-05-22 15:46:04 +00:00
@override
2022-07-03 09:45:00 +00:00
State<ApkDownloaderDialog> createState() => _ApkDownloaderDialogState();
2021-05-22 15:46:04 +00:00
}
class _ApkDownloaderDialogState extends State<ApkDownloaderDialog> {
String? _saveUrl;
double? _downloadProgress;
2021-05-22 15:46:04 +00:00
@override
void initState() {
super.initState();
2022-07-03 09:49:33 +00:00
_saveUrl = Configuration.instance.getTempDirectory() +
"ente-" +
widget.versionInfo!.name +
2022-07-03 09:49:33 +00:00
".apk";
2021-05-22 15:46:04 +00:00
_downloadApk();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text(
2022-05-30 02:30:51 +00:00
"Downloading...",
2021-05-22 15:46:04 +00:00
style: TextStyle(
fontSize: 16,
),
textAlign: TextAlign.center,
),
content: LinearProgressIndicator(
value: _downloadProgress,
2022-07-12 06:30:02 +00:00
valueColor: AlwaysStoppedAnimation<Color>(
Theme.of(context).colorScheme.greenAlternative,
),
2021-05-22 15:46:04 +00:00
),
),
);
}
Future<void> _downloadApk() async {
try {
2022-06-11 08:23:52 +00:00
await Network.instance.getDio().download(
widget.versionInfo!.url,
2022-06-11 08:23:52 +00:00
_saveUrl,
onReceiveProgress: (count, _) {
setState(() {
_downloadProgress = count / widget.versionInfo!.size;
2022-06-11 08:23:52 +00:00
});
},
);
2021-05-22 16:20:38 +00:00
Navigator.of(context, rootNavigator: true).pop('dialog');
2022-09-17 09:02:58 +00:00
// OpenFile.open(_saveUrl);
2021-05-22 15:46:04 +00:00
} catch (e) {
Logger("ApkDownloader").severe(e);
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("Sorry"),
content: const Text("The download could not be completed"),
2021-05-22 16:20:38 +00:00
actions: [
TextButton(
2022-07-04 06:02:17 +00:00
child: const Text(
2022-06-18 12:23:51 +00:00
"Ignore",
2021-05-22 16:20:38 +00:00
style: TextStyle(
color: Colors.white,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
TextButton(
child: Text(
2022-06-18 12:23:51 +00:00
"Retry",
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
Navigator.of(context, rootNavigator: true).pop('dialog');
showDialog(
context: context,
builder: (BuildContext context) {
return ApkDownloaderDialog(widget.versionInfo);
},
barrierDismissible: false,
);
},
),
2021-05-22 16:20:38 +00:00
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
barrierColor: Colors.black87,
);
2021-05-22 15:46:04 +00:00
return;
}
}
}