init
This commit is contained in:
commit
2379b4476f
141 files changed
+6087
No files matched your search
@@ -0,0 +1,79 @@
|
||||
import 'package:code_editor/models/file_directory_history.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class FileDirectoryHistoryService {
|
||||
static final FileDirectoryHistoryService instance = FileDirectoryHistoryService._();
|
||||
FileDirectoryHistoryService._();
|
||||
|
||||
static const String _keyHistory = 'file_directory_history';
|
||||
static const int _maxHistoryCount = 15;
|
||||
|
||||
//记录
|
||||
Future<void> recordHistory({
|
||||
required String rootPath,
|
||||
String? lastOpenedFilePath,
|
||||
List<String>? openDirectoryPaths,
|
||||
}) async {
|
||||
final cleanPath = rootPath.trim();
|
||||
if(cleanPath.isEmpty) return;
|
||||
|
||||
List<FileDirectoryHistory> history = await getFullHistory();
|
||||
|
||||
final existingIndex = history.indexWhere((item) => item.rootPath == cleanPath);
|
||||
String? resolvedFile = lastOpenedFilePath;
|
||||
List<String>? resolvedOpenDirs = openDirectoryPaths;
|
||||
|
||||
if(existingIndex != -1) {
|
||||
resolvedFile ??= history[existingIndex].lastOpenedFilePath;
|
||||
resolvedOpenDirs ??= history[existingIndex].openDirectoryPaths;
|
||||
history.removeAt(existingIndex);
|
||||
}
|
||||
|
||||
history.insert(
|
||||
0,
|
||||
FileDirectoryHistory(
|
||||
rootPath: cleanPath,
|
||||
lastOpenedFilePath: resolvedFile,
|
||||
openDirectoryPaths: resolvedOpenDirs ?? const [],
|
||||
),
|
||||
);
|
||||
|
||||
if(history.length > _maxHistoryCount) {
|
||||
history.removeRange(_maxHistoryCount, history.length);
|
||||
}
|
||||
|
||||
await _saveHistoryList(history);
|
||||
}
|
||||
|
||||
//获取完整历史
|
||||
Future<List<FileDirectoryHistory>> getFullHistory() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final rawList = prefs.getStringList(_keyHistory) ?? [];
|
||||
return rawList.map((jsonStr) => FileDirectoryHistory.fromJson(jsonStr)).toList();
|
||||
}
|
||||
|
||||
//获取最近历史
|
||||
Future<FileDirectoryHistory?> getLastHistory() async {
|
||||
final history = await getFullHistory();
|
||||
return history.isNotEmpty ? history.first : null;
|
||||
}
|
||||
|
||||
//删除指定的历史
|
||||
Future<void> removeHistory(String path) async {
|
||||
List<FileDirectoryHistory> history = await getFullHistory();
|
||||
history.removeWhere((item) => item.rootPath == path);
|
||||
await _saveHistoryList(history);
|
||||
}
|
||||
|
||||
//清空历史
|
||||
Future<void> clearHistory() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.remove(_keyHistory);
|
||||
}
|
||||
|
||||
Future<void> _saveHistoryList(List<FileDirectoryHistory> list) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final jsonList = list.map((item) => item.toJson()).toList();
|
||||
await prefs.setStringList(_keyHistory, jsonList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:path/path.dart' as p;
|
||||
import '../models/file_item.dart';
|
||||
|
||||
class FileService {
|
||||
static final FileService instance = FileService._();
|
||||
FileService._();
|
||||
|
||||
//构建文件目录树
|
||||
Future<List<FileItem>> buildTree(
|
||||
String dirPath, {
|
||||
int depth = 0,
|
||||
List<String> openDirectoryPaths = const [],
|
||||
}) async {
|
||||
final rootDir = Directory(dirPath);
|
||||
if(!await rootDir.exists()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
List<FileItem> items = [];
|
||||
|
||||
try {
|
||||
final List<FileSystemEntity> entities = await rootDir.list().toList();
|
||||
for(var entity in entities) {
|
||||
final fileName = p.basename(entity.path);
|
||||
if(fileName.startsWith('.')) continue;
|
||||
|
||||
if(entity is Directory) {
|
||||
final isDirOpen = openDirectoryPaths.contains(entity.path);
|
||||
final children = await buildTree(
|
||||
entity.path,
|
||||
depth: depth + 1,
|
||||
openDirectoryPaths: openDirectoryPaths,
|
||||
);
|
||||
items.add(FileItem(
|
||||
name: fileName,
|
||||
path: entity.path,
|
||||
isDirectory: true,
|
||||
children: children,
|
||||
depth: depth,
|
||||
isOpen: isDirOpen,
|
||||
));
|
||||
}else if(entity is File) {
|
||||
items.add(FileItem(
|
||||
name: fileName,
|
||||
path: entity.path,
|
||||
isDirectory: false,
|
||||
depth: depth,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
items.sort((a, b) {
|
||||
if(a.isDirectory && !b.isDirectory) return -1;
|
||||
if(!a.isDirectory && b.isDirectory) return 1;
|
||||
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
||||
});
|
||||
}catch(e) {
|
||||
print('扫描目录出错: $e');
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
//阅读文件内容
|
||||
Future<String> readFileContent(String path) async {
|
||||
final file = File(path);
|
||||
|
||||
if(!await file.exists()) {
|
||||
throw FileSystemException('文件不存在或已被移除', path);
|
||||
}
|
||||
|
||||
try {
|
||||
return await file.readAsString(
|
||||
encoding: const Utf8Codec(allowMalformed: true),
|
||||
);
|
||||
}catch (e) {
|
||||
throw FileSystemException('读取文件失败: $e', path);
|
||||
}
|
||||
}
|
||||
|
||||
// 写入保存文件
|
||||
Future<void> saveFile(String path, String content) async {
|
||||
final file = File(path);
|
||||
await file.writeAsString(content, encoding: utf8, flush: true);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user