better updates

This commit is contained in:
unknown 2021-12-03 23:00:13 +01:00
parent 92a93941ab
commit 4e1c1be6e4
2 changed files with 8 additions and 6 deletions

View File

@ -7,24 +7,26 @@ import 'package:filcnaplo/helpers/storage_helper.dart';
import 'package:filcnaplo/models/release.dart'; import 'package:filcnaplo/models/release.dart';
import 'package:open_file/open_file.dart'; import 'package:open_file/open_file.dart';
enum UpdateState { prepare, downloading, installing } enum UpdateState { none, preparing, downloading, installing }
typedef UpdateCallback = Function(double progress, UpdateState state); typedef UpdateCallback = Function(double progress, UpdateState state);
// TODO: cleanup old apk files // TODO: cleanup old apk files
extension UpdateHelper on Release { extension UpdateHelper on Release {
Future<void> install({UpdateCallback? updateCallback}) async { Future<void> install({UpdateCallback? updateCallback}) async {
updateCallback!(-1, UpdateState.preparing);
String downloads = await StorageHelper.downloadsPath(); String downloads = await StorageHelper.downloadsPath();
File apk = File("$downloads/filcnaplo-${version}.apk"); File apk = File("$downloads/filcnaplo-${version}.apk");
if (!await apk.exists()) { if (!await apk.exists()) {
updateCallback!(-1, UpdateState.downloading); updateCallback(-1, UpdateState.downloading);
var bytes = await download(updateCallback: updateCallback); var bytes = await download(updateCallback: updateCallback);
if (!await StorageHelper.write(apk.path, bytes)) throw "failed to write apk: permission denied"; if (!await StorageHelper.write(apk.path, bytes)) throw "failed to write apk: permission denied";
} }
updateCallback!(-1, UpdateState.installing); updateCallback(-1, UpdateState.installing);
var result = await OpenFile.open(apk.path); var result = await OpenFile.open(apk.path);
@ -33,7 +35,7 @@ extension UpdateHelper on Release {
throw result.message; throw result.message;
} }
updateCallback(-1, UpdateState.prepare); updateCallback(-1, UpdateState.none);
} }
Future<Uint8List> download({UpdateCallback? updateCallback}) async { Future<Uint8List> download({UpdateCallback? updateCallback}) async {

View File

@ -33,13 +33,13 @@ extension StringFormatUtils on String {
} }
extension DateFormatUtils on DateTime { extension DateFormatUtils on DateTime {
String format(BuildContext context, {bool timeOnly = false, bool weekday = false}) { String format(BuildContext context, {bool timeOnly = false, bool forceToday = false, bool weekday = false}) {
// Time only // Time only
if (timeOnly) return DateFormat("HH:mm").format(this); if (timeOnly) return DateFormat("HH:mm").format(this);
DateTime now = DateTime.now(); DateTime now = DateTime.now();
if (now.year == this.year && now.month == this.month && now.day == this.day) { if (now.year == this.year && now.month == this.month && now.day == this.day) {
if (this.hour == 0 && this.minute == 0 && this.second == 0) return "Today".i18n; if (this.hour == 0 && this.minute == 0 && this.second == 0 || forceToday) return "Today".i18n;
return DateFormat("HH:mm").format(this); return DateFormat("HH:mm").format(this);
} }
if (now.year == this.year && now.month == this.month && now.subtract(Duration(days: 1)).day == this.day) return "Yesterday".i18n; if (now.year == this.year && now.month == this.month && now.subtract(Duration(days: 1)).day == this.day) return "Yesterday".i18n;