Files
EasyDesignCollection/lib/persistence/recent_store.dart
T
admin 68c2f5d8f1 1.3.5:更名 EDC|易得设计,空白启动首页 + 可扩展图元库识别,架构图大版本
- 品牌:更名 EDC|易得设计(exe/安卓/仓库 EasyDesignCollection;旧地址 301 兼容老客户端更新)
- 首页:空白启动 + 六模块「新建文档」选择 + 最近打开;品牌菜单重做
- 架构图:手绘识别统一图形;Visio 式可扩展图元库(导入 / 存为图元 / 分类折叠 /
  手绘匹配 / 多笔回溯组合);识别后图形尺寸编辑(宽高长径 + 画布标注 + 双击改值);
  IT 系统图元;mm 标尺(右轴数值 / 实线游标 / A0 默认画布);图层、F8 正交 / F10 极轴、
  对齐均布;连线端点换绑重吸附;DXF / SVG / PNG 导出;缩小时画布溢出到侧栏的裁剪修复
- 时序图:分文件保存(*.seq.json / *.arch.json;旧 .edc.json 兼容)
- 路径:运行时目录迁移 %APPDATA%/EDC;素材库 library/;安卓 Documents/EDC
- 清理:参数化草图入口冻结;Windows Ink 原生桥移除;dist/ 退役
- 构建:exe + APK 双端同步(1.3.5+26);发布脚本切换 release/ 新目录结构
2026-10-11 10:19:01 +08:00

147 lines
4.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 最近打开的文档记录(最多 [RecentStore.maxEntries] 条,按时间倒序)。
///
/// 存储位置:应用数据目录(桌面 `%APPDATA%/EDC/recent.json`,
/// 安卓为应用内部目录;与自动快照同根)。路径书写只读展示,不保证文件仍存在,
/// 失效条目在打开失败时自动移除。
library;
import 'dart:convert';
import 'dart:io';
import 'storage.dart';
/// 最近打开条目。
class RecentDoc {
/// 构造。
const RecentDoc({
required this.path,
required this.moduleId,
required this.name,
required this.atMs,
});
/// 文件路径。
final String path;
/// 模块 id(sequence / architecture / …)。
final String moduleId;
/// 展示名(一般取文件名)。
final String name;
/// 打开时间(毫秒时间戳)。
final int atMs;
/// 打开时间。
DateTime get at => DateTime.fromMillisecondsSinceEpoch(atMs);
/// 序列化。
Map<String, dynamic> toJson() => <String, dynamic>{
'path': path,
'moduleId': moduleId,
'name': name,
'at': atMs,
};
/// 反序列化(无效返回 null)。
static RecentDoc? fromJson(dynamic raw) {
if (raw is! Map<String, dynamic>) return null;
final String path = raw['path'] as String? ?? '';
final String moduleId = raw['moduleId'] as String? ?? '';
if (path.isEmpty || moduleId.isEmpty) return null;
return RecentDoc(
path: path,
moduleId: moduleId,
name: raw['name'] as String? ?? path.split(RegExp(r'[\\/]')).last,
atMs: (raw['at'] as num?)?.toInt() ?? 0,
);
}
}
/// 最近打开存储(静态工具类)。
class RecentStore {
RecentStore._();
/// 最多保留条数。
static const int maxEntries = 15;
static File _file(String appDir) =>
File('$appDir${Platform.pathSeparator}recent.json');
/// 读取列表(文件缺失 / 解析失败返回空列表,不抛异常)。
static Future<List<RecentDoc>> load() async {
try {
await Storage.instance.init();
final File f = _file(Storage.instance.appDir.path);
if (!await f.exists()) return <RecentDoc>[];
final dynamic raw = jsonDecode(await f.readAsString());
if (raw is! List<dynamic>) return <RecentDoc>[];
final List<RecentDoc> out = <RecentDoc>[];
for (final dynamic e in raw) {
final RecentDoc? r = RecentDoc.fromJson(e);
if (r != null) out.add(r);
}
out.sort((RecentDoc a, RecentDoc b) => b.atMs.compareTo(a.atMs));
return out;
} catch (_) {
return <RecentDoc>[];
}
}
/// 记录一次打开 / 保存(去重后置顶,超限裁剪)。
static Future<void> remember({
required String path,
required String moduleId,
String? name,
}) async {
if (path.isEmpty || moduleId.isEmpty) return;
final List<RecentDoc> cur = await load();
cur.removeWhere((RecentDoc r) => r.path == path);
cur.insert(
0,
RecentDoc(
path: path,
moduleId: moduleId,
name: (name == null || name.isEmpty)
? path.split(RegExp(r'[\\/]')).last
: name,
atMs: DateTime.now().millisecondsSinceEpoch,
),
);
while (cur.length > maxEntries) {
cur.removeLast();
}
await _save(cur);
}
/// 移除一条(打开失败时清理失效条目)。
static Future<void> remove(String path) async {
final List<RecentDoc> cur = await load();
cur.removeWhere((RecentDoc r) => r.path == path);
await _save(cur);
}
/// 清空全部记录。
static Future<void> clear() async {
await _save(<RecentDoc>[]);
}
static Future<void> _save(List<RecentDoc> list) async {
try {
await Storage.instance.init();
final File f = _file(Storage.instance.appDir.path);
if (!await f.parent.exists()) {
await f.parent.create(recursive: true);
}
await f.writeAsString(
jsonEncode(<Map<String, dynamic>>[
for (final RecentDoc r in list) r.toJson(),
]),
flush: true,
);
} catch (_) {
// 最近列表写入失败不影响主流程。
}
}
}