fe5e0be131
- 空泳道策略(F2 文档设置 →「泳道」):动作全部移走的泳道可保留(默认,序号不变) 或自动移除;条形空行 / 圆周空环 / 流程空列与左侧栏占位行同步 - 泳道加宽(1–5 槽):双击条形视图左侧泳道编号 / 右键空白泳道 / 左侧栏「N 槽」徽标; 条形分子行(浅色虚线分隔)、圆周同色加宽环带 + 槽位虚线、流程图分子列且泳道带 随内容智能加宽;拖拽动作同时换道 + 换槽位;旧数据零迁移(row 语义改为槽位并自动钳制) - 流程图智能排开:同源同层扇出连线在空隙内均匀铺开;同层节点水平重叠自动推开 - 显示修复:画布左栏标题按真实行高排版(灰色副标题不再被换行标题压住, 持续换行到泳道高度不足);多槽位采用「槽位紧凑 + 泳道间拉开」的间隔 - 「时序创建和编辑」更名为「文档设置」(含对话框标题/帮助/文档);新增自动保存 (默认 10 分钟,可选 1/5/10/30 或关闭;写入文件并同步 .bak) - 授权临时冻结(kLicenseRequired=false):桌面版导出无需 .lic;关于页授权状态 「开放授权版本」、EDC 版权、开发者与交流群(邮箱 liuyu65@126.com、微信 online-goose) - 更新体验:直接升级到最新版本提示、下载过程保持屏幕常亮提示、 下载/上传统一限速 320KB/s(客户端限速 + 发布脚本 --limit-rate 320k) - docs/HELP.md 与帮助中心同步更新;新增 docs/ROADMAP.md 开发路线图 - flutter analyze 0 问题、40 项测试全过;演示截图更新(含泳道加宽与拥挤场景验证图)
198 lines
7.1 KiB
Dart
198 lines
7.1 KiB
Dart
/// 应用内更新服务:从 Gitea 仓库的「发布(Releases)」检查新版本并下载更新包。
|
||
///
|
||
/// 服务器约定(当前仓库 http://124.220.63.9:30000/admin/Sequence-Diagram):
|
||
/// - 检查:`GET /api/v1/repos/{owner}/{repo}/releases/latest`(公开仓库,无需鉴权);
|
||
/// - 更新说明:使用 Release 的正文(Markdown);
|
||
/// - 安装包:使用 Release 附件——安卓为 `.apk`,Windows 为 `.zip`(绿色版整包)。
|
||
///
|
||
/// 采用 Release 附件而不是把安装包提交进 git:仓库历史不会被大文件撑大,
|
||
/// 客户端仍可通过稳定的下载地址获取(Gitea 会 302 到附件实际存储位置)。
|
||
library;
|
||
|
||
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
import '../version.dart';
|
||
|
||
/// Gitea 服务器与仓库定位。
|
||
class UpdateServer {
|
||
/// 服务器根地址。
|
||
static const String host = 'http://124.220.63.9:30000';
|
||
/// 仓库拥有者。
|
||
static const String owner = 'admin';
|
||
/// 仓库名称。
|
||
static const String repo = 'Sequence-Diagram';
|
||
|
||
/// 版本检查接口(最新正式发布)。
|
||
static String get latestReleaseApi =>
|
||
'$host/api/v1/repos/$owner/$repo/releases/latest';
|
||
}
|
||
|
||
/// 一次可用的更新。
|
||
class UpdateInfo {
|
||
/// 构造。
|
||
const UpdateInfo({
|
||
required this.version,
|
||
required this.notes,
|
||
required this.assetName,
|
||
required this.assetUrl,
|
||
required this.assetSize,
|
||
});
|
||
|
||
/// 远端版本号(已去掉 `v` 前缀,如 `1.2.1`)。
|
||
final String version;
|
||
|
||
/// 更新说明(Markdown 文本)。
|
||
final String notes;
|
||
|
||
/// 安装包文件名(如 `sequence_diagram_android_v1.2.1_release.apk`)。
|
||
final String assetName;
|
||
|
||
/// 安装包下载地址。
|
||
final String assetUrl;
|
||
|
||
/// 安装包大小(字节;未知为 0)。
|
||
final int assetSize;
|
||
}
|
||
|
||
/// 版本比较:[remote] 是否比 [current] 更新。
|
||
///
|
||
/// 按点分数字段逐段比较,容忍 `v` 前缀与 `-` / `+` 后缀(如 `v1.2.1-rc1`)。
|
||
bool isNewerVersion(String remote, String current) {
|
||
List<int> parse(String v) {
|
||
final String core =
|
||
v.trim().replaceAll(RegExp(r'^[vV]'), '').split(RegExp(r'[-+]')).first;
|
||
final List<int> parts = <int>[];
|
||
for (final String s in core.split('.')) {
|
||
parts.add(int.tryParse(s) ?? 0);
|
||
}
|
||
return parts;
|
||
}
|
||
|
||
final List<int> a = parse(remote);
|
||
final List<int> b = parse(current);
|
||
final int n = a.length > b.length ? a.length : b.length;
|
||
for (int i = 0; i < n; i++) {
|
||
final int x = i < a.length ? a[i] : 0;
|
||
final int y = i < b.length ? b[i] : 0;
|
||
if (x != y) return x > y;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// 客户端下载限速(字节/秒):更新包下载时主动限速,降低自建服务器带宽压力。
|
||
///
|
||
/// 实现方式:边收边按目标速率等待(TCP 反压会让服务端发送同步变慢),
|
||
/// 因此即使多端并发,每端最多占用该速率,不会把服务器的带宽打满。
|
||
const int kUpdateDownloadLimit = 320 * 1024;
|
||
|
||
/// 更新服务:检查 + 下载。
|
||
class UpdateService {
|
||
UpdateService._();
|
||
|
||
/// 检查更新。
|
||
///
|
||
/// [currentVersion] 为当前版本(缺省用 [kAppVersion];自检工具可传入模拟值)。
|
||
/// 远端版本高于当前版本且存在当前平台的安装包时返回 [UpdateInfo];
|
||
/// 无更新(或尚未发布任何版本)返回 null;网络 / 服务器异常抛出 [Exception],
|
||
/// 由调用方决定是否静默。
|
||
static Future<UpdateInfo?> check({String? currentVersion}) async {
|
||
final String current = currentVersion ?? kAppVersion;
|
||
final HttpClient client = HttpClient()
|
||
..connectionTimeout = const Duration(seconds: 8);
|
||
try {
|
||
final HttpClientRequest req =
|
||
await client.getUrl(Uri.parse(UpdateServer.latestReleaseApi));
|
||
final HttpClientResponse res = await req.close();
|
||
if (res.statusCode == 404) return null; // 还没有任何发布
|
||
final String text = await res.transform(utf8.decoder).join();
|
||
if (res.statusCode != 200) {
|
||
throw Exception('服务器返回 ${res.statusCode}');
|
||
}
|
||
final Map<String, dynamic> j = jsonDecode(text) as Map<String, dynamic>;
|
||
final String tag = (j['tag_name'] as String?) ?? '';
|
||
if (!isNewerVersion(tag, current)) return null;
|
||
final String notes = (j['body'] as String?) ?? '';
|
||
// 选择当前平台的安装包:安卓取 .apk;Windows 优先 .zip(绿色版整包)。
|
||
bool isTarget(String n) {
|
||
final String l = n.toLowerCase();
|
||
if (Platform.isAndroid) return l.endsWith('.apk');
|
||
return l.endsWith('.zip');
|
||
}
|
||
|
||
final List<dynamic> assets =
|
||
(j['assets'] as List<dynamic>?) ?? const <dynamic>[];
|
||
String name = '';
|
||
String url = '';
|
||
int size = 0;
|
||
for (final dynamic a in assets) {
|
||
final Map<String, dynamic> m = a as Map<String, dynamic>;
|
||
final String an = (m['name'] as String?) ?? '';
|
||
if (an.isEmpty || !isTarget(an)) continue;
|
||
name = an;
|
||
url = (m['browser_download_url'] as String?) ?? '';
|
||
size = (m['size'] as num?)?.toInt() ?? 0;
|
||
break;
|
||
}
|
||
if (name.isEmpty || url.isEmpty) return null;
|
||
return UpdateInfo(
|
||
version: tag.trim().replaceAll(RegExp(r'^[vV]'), ''),
|
||
notes: notes,
|
||
assetName: name,
|
||
assetUrl: url,
|
||
assetSize: size,
|
||
);
|
||
} finally {
|
||
client.close(force: true);
|
||
}
|
||
}
|
||
|
||
/// 流式下载 [url] 到 [destPath]。
|
||
///
|
||
/// [onProgress] 回调 `(0..1 进度, 已接收字节)`;总长未知时进度为 -1。
|
||
/// [isCancelled] 返回 true 时中断下载并抛出「已取消」。
|
||
static Future<void> download({
|
||
required String url,
|
||
required String destPath,
|
||
void Function(double progress, int received)? onProgress,
|
||
bool Function()? isCancelled,
|
||
}) async {
|
||
final HttpClient client = HttpClient()
|
||
..connectionTimeout = const Duration(seconds: 15);
|
||
IOSink? sink;
|
||
try {
|
||
final HttpClientRequest req = await client.getUrl(Uri.parse(url));
|
||
final HttpClientResponse res = await req.close();
|
||
if (res.statusCode != 200) {
|
||
throw Exception('下载失败:服务器返回 ${res.statusCode}');
|
||
}
|
||
final int total = res.contentLength; // -1 表示未知
|
||
final File f = File(destPath);
|
||
await f.parent.create(recursive: true);
|
||
sink = f.openWrite();
|
||
int received = 0;
|
||
final Stopwatch sw = Stopwatch()..start();
|
||
await for (final List<int> chunk in res) {
|
||
if (isCancelled?.call() ?? false) throw Exception('已取消');
|
||
sink.add(chunk);
|
||
received += chunk.length;
|
||
// 主动限速(默认 320KB/s):读得过快时按目标速率补齐等待。
|
||
final int expectedMs = received * 1000 ~/ kUpdateDownloadLimit;
|
||
final int aheadMs = expectedMs - sw.elapsedMilliseconds;
|
||
if (aheadMs > 0) await Future<void>.delayed(Duration(milliseconds: aheadMs));
|
||
onProgress?.call(total > 0 ? received / total : -1, received);
|
||
}
|
||
await sink.flush();
|
||
await sink.close();
|
||
sink = null;
|
||
} finally {
|
||
if (sink != null) {
|
||
try {
|
||
await sink.close();
|
||
} catch (_) {}
|
||
}
|
||
client.close(force: true);
|
||
}
|
||
}
|
||
}
|