mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
// ignore_for_file: avoid_print
|
||
|
||
import 'dart:io';
|
||
|
||
import 'package:dio/dio.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:toolbox/core/utils.dart';
|
||
import 'package:toolbox/data/provider/app.dart';
|
||
import 'package:toolbox/data/res/build_data.dart';
|
||
import 'package:toolbox/data/service/app.dart';
|
||
import 'package:toolbox/locator.dart';
|
||
|
||
Future<bool> isFileAvailable(String url) async {
|
||
try {
|
||
final resp = await Dio().head(url);
|
||
return resp.statusCode == 200;
|
||
} catch (e) {
|
||
print('update file not available: $e');
|
||
return false;
|
||
}
|
||
}
|
||
|
||
Future<void> doUpdate(BuildContext context, {bool force = false}) async {
|
||
final update = await locator<AppService>().getUpdate();
|
||
|
||
locator<AppProvider>().setNewestBuild(update.newest);
|
||
|
||
if (!force && update.newest <= BuildData.build) {
|
||
print('Update ignored due to current: ${BuildData.build}, '
|
||
'update: ${update.newest}');
|
||
return;
|
||
}
|
||
print('Update available: ${update.newest}');
|
||
|
||
if (Platform.isAndroid && !await isFileAvailable(update.android)) {
|
||
return;
|
||
}
|
||
|
||
showSnackBarWithAction(
|
||
context,
|
||
update.min > BuildData.build
|
||
? '您的版本过旧,请及时更新'
|
||
: '${BuildData.name}有更新啦,Ver:${update.newest}\n${update.changelog}',
|
||
'更新',
|
||
() => openUrl(Platform.isAndroid ? update.android : update.ios));
|
||
}
|