app_update_dialog.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // @dart=2.9
  2. import 'package:flutter/material.dart';
  3. import 'package:logging/logging.dart';
  4. // import 'package:open_file/open_file.dart';
  5. import 'package:photos/core/configuration.dart';
  6. import 'package:photos/core/network.dart';
  7. import 'package:photos/ente_theme_data.dart';
  8. import 'package:photos/services/update_service.dart';
  9. import 'package:photos/theme/ente_theme.dart';
  10. import 'package:url_launcher/url_launcher_string.dart';
  11. class AppUpdateDialog extends StatefulWidget {
  12. final LatestVersionInfo latestVersionInfo;
  13. const AppUpdateDialog(this.latestVersionInfo, {Key key}) : super(key: key);
  14. @override
  15. State<AppUpdateDialog> createState() => _AppUpdateDialogState();
  16. }
  17. class _AppUpdateDialogState extends State<AppUpdateDialog> {
  18. @override
  19. Widget build(BuildContext context) {
  20. final List<Widget> changelog = [];
  21. final enteTextTheme = getEnteTextTheme(context);
  22. final enteColor = getEnteColorScheme(context);
  23. for (final log in widget.latestVersionInfo.changelog) {
  24. changelog.add(
  25. Padding(
  26. padding: const EdgeInsets.fromLTRB(0, 4, 0, 4),
  27. child: Row(
  28. mainAxisAlignment: MainAxisAlignment.start,
  29. crossAxisAlignment: CrossAxisAlignment.start,
  30. children: [
  31. Text(
  32. "- ",
  33. style: enteTextTheme.small.copyWith(color: enteColor.textMuted),
  34. ),
  35. Flexible(
  36. child: Text(
  37. log,
  38. softWrap: true,
  39. style:
  40. enteTextTheme.small.copyWith(color: enteColor.textMuted),
  41. ),
  42. )
  43. ],
  44. ),
  45. ),
  46. );
  47. }
  48. final content = Column(
  49. crossAxisAlignment: CrossAxisAlignment.start,
  50. mainAxisSize: MainAxisSize.min,
  51. children: [
  52. Text(
  53. "A new version of ente is available.",
  54. style: enteTextTheme.body.copyWith(color: enteColor.textMuted),
  55. ),
  56. const Padding(padding: EdgeInsets.all(8)),
  57. Column(
  58. crossAxisAlignment: CrossAxisAlignment.start,
  59. children: changelog,
  60. ),
  61. const Padding(padding: EdgeInsets.all(8)),
  62. SizedBox(
  63. width: double.infinity,
  64. height: 56,
  65. child: OutlinedButton(
  66. style: Theme.of(context).outlinedButtonTheme.style.copyWith(
  67. textStyle: MaterialStateProperty.resolveWith<TextStyle>(
  68. (Set<MaterialState> states) {
  69. return enteTextTheme.bodyBold;
  70. },
  71. ),
  72. ),
  73. onPressed: () async {
  74. Navigator.pop(context);
  75. showDialog(
  76. context: context,
  77. builder: (BuildContext context) {
  78. return ApkDownloaderDialog(widget.latestVersionInfo);
  79. },
  80. barrierDismissible: false,
  81. );
  82. },
  83. child: const Text(
  84. "Update",
  85. ),
  86. ),
  87. ),
  88. const Padding(padding: EdgeInsets.all(8)),
  89. Center(
  90. child: InkWell(
  91. child: Text(
  92. "Install manually",
  93. style: Theme.of(context)
  94. .textTheme
  95. .caption
  96. .copyWith(decoration: TextDecoration.underline),
  97. ),
  98. onTap: () => launchUrlString(
  99. widget.latestVersionInfo.url,
  100. mode: LaunchMode.externalApplication,
  101. ),
  102. ),
  103. )
  104. ],
  105. );
  106. final shouldForceUpdate =
  107. UpdateService.instance.shouldForceUpdate(widget.latestVersionInfo);
  108. return WillPopScope(
  109. onWillPop: () async => !shouldForceUpdate,
  110. child: AlertDialog(
  111. title: Column(
  112. crossAxisAlignment: CrossAxisAlignment.start,
  113. children: [
  114. Icon(
  115. Icons.auto_awesome_outlined,
  116. size: 48,
  117. color: enteColor.strokeMuted,
  118. ),
  119. const SizedBox(
  120. height: 16,
  121. ),
  122. Text(
  123. shouldForceUpdate
  124. ? "Critical update available"
  125. : "Update available",
  126. style: enteTextTheme.h3Bold,
  127. ),
  128. ],
  129. ),
  130. content: content,
  131. ),
  132. );
  133. }
  134. }
  135. class ApkDownloaderDialog extends StatefulWidget {
  136. final LatestVersionInfo versionInfo;
  137. const ApkDownloaderDialog(this.versionInfo, {Key key}) : super(key: key);
  138. @override
  139. State<ApkDownloaderDialog> createState() => _ApkDownloaderDialogState();
  140. }
  141. class _ApkDownloaderDialogState extends State<ApkDownloaderDialog> {
  142. String _saveUrl;
  143. double _downloadProgress;
  144. @override
  145. void initState() {
  146. super.initState();
  147. _saveUrl = Configuration.instance.getTempDirectory() +
  148. "ente-" +
  149. widget.versionInfo.name +
  150. ".apk";
  151. _downloadApk();
  152. }
  153. @override
  154. Widget build(BuildContext context) {
  155. return WillPopScope(
  156. onWillPop: () async => false,
  157. child: AlertDialog(
  158. title: const Text(
  159. "Downloading...",
  160. style: TextStyle(
  161. fontSize: 16,
  162. ),
  163. textAlign: TextAlign.center,
  164. ),
  165. content: LinearProgressIndicator(
  166. value: _downloadProgress,
  167. valueColor: AlwaysStoppedAnimation<Color>(
  168. Theme.of(context).colorScheme.greenAlternative,
  169. ),
  170. ),
  171. ),
  172. );
  173. }
  174. Future<void> _downloadApk() async {
  175. try {
  176. await Network.instance.getDio().download(
  177. widget.versionInfo.url,
  178. _saveUrl,
  179. onReceiveProgress: (count, _) {
  180. setState(() {
  181. _downloadProgress = count / widget.versionInfo.size;
  182. });
  183. },
  184. );
  185. Navigator.of(context, rootNavigator: true).pop('dialog');
  186. // OpenFile.open(_saveUrl);
  187. } catch (e) {
  188. Logger("ApkDownloader").severe(e);
  189. final AlertDialog alert = AlertDialog(
  190. title: const Text("Sorry"),
  191. content: const Text("The download could not be completed"),
  192. actions: [
  193. TextButton(
  194. child: const Text(
  195. "Ignore",
  196. style: TextStyle(
  197. color: Colors.white,
  198. ),
  199. ),
  200. onPressed: () {
  201. Navigator.of(context, rootNavigator: true).pop('dialog');
  202. Navigator.of(context, rootNavigator: true).pop('dialog');
  203. },
  204. ),
  205. TextButton(
  206. child: Text(
  207. "Retry",
  208. style: TextStyle(
  209. color: Theme.of(context).colorScheme.greenAlternative,
  210. ),
  211. ),
  212. onPressed: () {
  213. Navigator.of(context, rootNavigator: true).pop('dialog');
  214. Navigator.of(context, rootNavigator: true).pop('dialog');
  215. showDialog(
  216. context: context,
  217. builder: (BuildContext context) {
  218. return ApkDownloaderDialog(widget.versionInfo);
  219. },
  220. barrierDismissible: false,
  221. );
  222. },
  223. ),
  224. ],
  225. );
  226. showDialog(
  227. context: context,
  228. builder: (BuildContext context) {
  229. return alert;
  230. },
  231. barrierColor: Colors.black87,
  232. );
  233. return;
  234. }
  235. }
  236. }