ente/lib/ui/app_update_dialog.dart

197 lines
5.6 KiB
Dart
Raw Normal View History

2021-05-22 15:46:04 +00:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:open_file/open_file.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/network.dart';
import 'package:photos/services/update_service.dart';
class AppUpdateDialog extends StatefulWidget {
final LatestVersionInfo latestVersionInfo;
AppUpdateDialog(this.latestVersionInfo, {Key key}) : super(key: key);
@override
_AppUpdateDialogState createState() => _AppUpdateDialogState();
}
class _AppUpdateDialogState extends State<AppUpdateDialog> {
@override
Widget build(BuildContext context) {
final List<Widget> changelog = [];
for (final log in widget.latestVersionInfo.changelog) {
changelog.add(Padding(
padding: const EdgeInsets.fromLTRB(8, 4, 0, 4),
2022-03-05 20:52:00 +00:00
child: Text("- " + log, style: Theme.of(context).textTheme.caption),
2021-05-22 15:46:04 +00:00
));
}
final content = Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
widget.latestVersionInfo.name,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
Padding(padding: EdgeInsets.all(8)),
2022-05-19 10:04:59 +00:00
Text("Changelog",
2021-05-22 15:46:04 +00:00
style: TextStyle(
fontSize: 18,
)),
Padding(padding: EdgeInsets.all(4)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: changelog,
),
Padding(padding: EdgeInsets.all(8)),
Container(
width: double.infinity,
height: 64,
2021-05-26 22:11:24 +00:00
padding: const EdgeInsets.fromLTRB(50, 0, 50, 0),
2022-05-30 02:30:51 +00:00
child: OutlinedButton(
child: Text(
"Update",
),
style: Theme.of(context).outlinedButtonTheme.style.copyWith(
textStyle: MaterialStateProperty.resolveWith<TextStyle>(
(Set<MaterialState> states) {
return Theme.of(context).textTheme.subtitle1;
},
),
),
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,
);
},
),
),
],
);
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(
2022-03-05 20:52:00 +00:00
title: Text(shouldForceUpdate
2022-05-19 10:04:59 +00:00
? "Critical update available"
: "Update available"),
2021-05-22 15:46:04 +00:00
content: content,
),
);
}
}
class ApkDownloaderDialog extends StatefulWidget {
final LatestVersionInfo versionInfo;
ApkDownloaderDialog(this.versionInfo, {Key key}) : super(key: key);
@override
_ApkDownloaderDialogState createState() => _ApkDownloaderDialogState();
}
class _ApkDownloaderDialogState extends State<ApkDownloaderDialog> {
String _saveUrl;
double _downloadProgress;
@override
void initState() {
super.initState();
_saveUrl = Configuration.instance.getTempDirectory() +
"ente-" +
widget.versionInfo.name +
".apk";
_downloadApk();
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: 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,
valueColor:
AlwaysStoppedAnimation<Color>(Theme.of(context).buttonColor),
),
),
);
}
Future<void> _downloadApk() async {
try {
await Network.instance.getDio().download(widget.versionInfo.url, _saveUrl,
onReceiveProgress: (count, _) {
setState(() {
_downloadProgress = count / widget.versionInfo.size;
});
});
2021-05-22 16:20:38 +00:00
Navigator.of(context, rootNavigator: true).pop('dialog');
OpenFile.open(_saveUrl);
2021-05-22 15:46:04 +00:00
} catch (e) {
Logger("ApkDownloader").severe(e);
2021-05-22 16:20:38 +00:00
AlertDialog alert = AlertDialog(
title: Text("Sorry"),
content: Text("The download could not be completed"),
2021-05-22 16:20:38 +00:00
actions: [
TextButton(
child: Text(
"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(
"retry",
style: TextStyle(
color: Theme.of(context).buttonColor,
),
),
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;
}
}
}