147 lines
4.1 KiB
Dart
147 lines
4.1 KiB
Dart
|
|
/// 最近打开的文档记录(最多 [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 (_) {
|
|||
|
|
// 最近列表写入失败不影响主流程。
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|