2021-11-11 02:10:18 +00:00
|
|
|
import 'dart:async' show Future;
|
2021-11-11 09:05:48 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'package:event/event.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-11-11 02:10:18 +00:00
|
|
|
import 'package:flutter/services.dart' show rootBundle;
|
2021-11-11 09:05:48 +00:00
|
|
|
import 'package:pikapika/basic/Common.dart';
|
2021-11-11 02:10:18 +00:00
|
|
|
|
2021-11-11 03:00:38 +00:00
|
|
|
import '../Method.dart';
|
|
|
|
|
|
|
|
const _versionUrl =
|
2021-11-11 09:05:48 +00:00
|
|
|
"https://api.github.com/repos/niuhuan/pikapika/releases/latest";
|
2021-11-11 02:10:18 +00:00
|
|
|
const _versionAssets = 'lib/assets/version.txt';
|
|
|
|
|
|
|
|
late String _version;
|
2021-11-11 09:05:48 +00:00
|
|
|
String? _latestVersion;
|
2021-11-18 01:53:30 +00:00
|
|
|
String? _latestVersionInfo;
|
2021-11-11 09:05:48 +00:00
|
|
|
|
2021-11-11 02:10:18 +00:00
|
|
|
Future initVersion() async {
|
2021-11-11 09:05:48 +00:00
|
|
|
// 当前版本
|
2021-11-11 02:10:18 +00:00
|
|
|
try {
|
|
|
|
_version = (await rootBundle.loadString(_versionAssets)).trim();
|
|
|
|
} catch (e) {
|
|
|
|
_version = "dirty";
|
|
|
|
}
|
2021-11-11 09:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var versionEvent = Event<EventArgs>();
|
|
|
|
|
|
|
|
String currentVersion() {
|
|
|
|
return _version;
|
|
|
|
}
|
|
|
|
|
|
|
|
String? latestVersion() {
|
|
|
|
return _latestVersion;
|
|
|
|
}
|
|
|
|
|
2021-11-18 01:53:30 +00:00
|
|
|
String? latestVersionInfo() {
|
|
|
|
return _latestVersionInfo;
|
|
|
|
}
|
|
|
|
|
2021-11-11 09:05:48 +00:00
|
|
|
Future autoCheckNewVersion() {
|
|
|
|
return _versionCheck();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future manualCheckNewVersion(BuildContext context) async {
|
|
|
|
try {
|
|
|
|
defaultToast(context, "检查更新中");
|
|
|
|
await _versionCheck();
|
|
|
|
defaultToast(context, "检查更新成功");
|
|
|
|
} catch (e) {
|
|
|
|
defaultToast(context, "检查更新失败 : $e");
|
|
|
|
}
|
2021-11-11 02:10:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 09:05:48 +00:00
|
|
|
bool dirtyVersion() {
|
2022-02-25 11:20:53 +00:00
|
|
|
return "dirty" == _version;
|
2021-11-11 09:05:48 +00:00
|
|
|
}
|
2021-11-11 03:00:38 +00:00
|
|
|
|
2021-11-11 09:05:48 +00:00
|
|
|
// maybe exception
|
2021-11-11 03:00:38 +00:00
|
|
|
Future _versionCheck() async {
|
2022-02-25 11:20:53 +00:00
|
|
|
if (!dirtyVersion()) {
|
2022-02-11 05:33:42 +00:00
|
|
|
// 检查更新只能使用defaultHttpClient, 而不能使用pika的client, 否则会 "tls handshake failure"
|
|
|
|
var json = jsonDecode(await method.defaultHttpClientGet(_versionUrl));
|
2021-11-11 09:05:48 +00:00
|
|
|
if (json["name"] != null) {
|
|
|
|
String latestVersion = (json["name"]);
|
|
|
|
if (latestVersion != _version) {
|
|
|
|
_latestVersion = latestVersion;
|
2021-11-18 01:53:30 +00:00
|
|
|
_latestVersionInfo = json["body"] ?? "";
|
2021-11-11 09:05:48 +00:00
|
|
|
}
|
2021-11-11 03:00:38 +00:00
|
|
|
}
|
2021-11-11 09:05:48 +00:00
|
|
|
} // else dirtyVersion
|
|
|
|
versionEvent.broadcast();
|
|
|
|
}
|
2022-09-05 03:05:46 +00:00
|
|
|
|
|
|
|
var _display = true;
|
|
|
|
|
|
|
|
void versionPop(BuildContext context) {
|
|
|
|
if (latestVersion() != null && _display) {
|
|
|
|
_display = false;
|
|
|
|
TopConfirm.topConfirm(
|
|
|
|
context,
|
|
|
|
"发现新版本",
|
|
|
|
"发现新版本 ${latestVersion()} , 请到关于页面更新",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TopConfirm {
|
|
|
|
static topConfirm(BuildContext context, String title, String message,
|
|
|
|
{Function()? afterIKnown}) {
|
|
|
|
late OverlayEntry overlayEntry;
|
|
|
|
overlayEntry = OverlayEntry(builder: (BuildContext context) {
|
|
|
|
return LayoutBuilder(
|
|
|
|
builder: (
|
|
|
|
BuildContext context,
|
|
|
|
BoxConstraints constraints,
|
|
|
|
) {
|
|
|
|
var mq = MediaQuery.of(context).size.width - 30;
|
|
|
|
return Material(
|
|
|
|
color: Colors.transparent,
|
|
|
|
child: Container(
|
|
|
|
width: constraints.maxWidth,
|
|
|
|
height: constraints.maxHeight,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.black.withOpacity(.35),
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Expanded(child: Container()),
|
|
|
|
Container(
|
|
|
|
width: mq,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.white,
|
|
|
|
borderRadius: BorderRadius.circular(10),
|
|
|
|
),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Container(height: 30),
|
|
|
|
Text(
|
|
|
|
title,
|
|
|
|
style: const TextStyle(
|
|
|
|
color: Colors.black,
|
|
|
|
fontSize: 28,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 15),
|
|
|
|
Text(
|
|
|
|
message,
|
|
|
|
style: const TextStyle(
|
|
|
|
color: Colors.black,
|
|
|
|
fontSize: 16,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 25),
|
|
|
|
MaterialButton(
|
|
|
|
elevation: 0,
|
|
|
|
color: Colors.black.withOpacity(.1),
|
|
|
|
onPressed: () {
|
|
|
|
overlayEntry.remove();
|
|
|
|
},
|
|
|
|
child: const Text("朕知道了"),
|
|
|
|
),
|
|
|
|
Container(height: 30),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(child: Container()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
OverlayState? overlay = Overlay.of(context);
|
|
|
|
if (overlay != null) {
|
|
|
|
overlay.insert(overlayEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|