Skip to content

Commit 79ae8ef

Browse files
committed
[taichi_file_manager] bump version to 0.0.4
1. 修复 toStructure 和 flatten 无法正常将文件转化为 EntityFolder 的问题 2. 前端完成添加文件/文件夹的功能
1 parent b15eee9 commit 79ae8ef

File tree

4 files changed

+325
-74
lines changed

4 files changed

+325
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
11
// ignore_for_file: avoid_init_to_null
22

3+
// import 'dart:convert';
4+
35
import 'package:dio/dio.dart';
46
import 'package:flutter/material.dart';
57
import 'package:taichi_file_manager/api.dart';
68
import 'package:taichi_file_manager/models/file_response.dart';
79
import 'package:taichi_file_manager/models/my_tree_node.dart';
810
import 'package:taichi_file_manager/utils/dio_utils.dart';
911

10-
class FileTreeController extends ChangeNotifier {
11-
String _currentTreeNodeName = "";
12-
String get currentTreeNodeName => _currentTreeNodeName;
13-
14-
changeTreeNodeName(String s) {
15-
_currentTreeNodeName = s;
16-
notifyListeners();
17-
}
18-
19-
late EntityFolder? _entity = null;
20-
21-
init() async {
22-
DioUtils dioUtils = DioUtils();
23-
Response? response = await dioUtils.get(Server + Apis["getFile"]!);
24-
25-
if (response != null) {
26-
// debugPrint(response.toString());
27-
FileResponse res = FileResponse.fromJson(response.data);
28-
List<EntityFile> files =
29-
res.savedFiles!.map((e) => EntityFile.fromSavedFile(e)).toList();
30-
List<String> paths = [];
31-
for (final i in files) {
32-
paths.add('${i.fatherPath}/${i.name}');
33-
paths.add(i.fatherPath);
34-
}
35-
// debugPrint("[files]:${files.length}");
36-
FlattenObject flattenObject = FlattenObject(files: files, path: paths);
37-
EntityFolder? f = toStructured(flattenObject);
38-
// debugPrint("[f]:${f?.toJson()}");
39-
_entity = f;
40-
// debugPrint("[flatten]:${flatten(_entity!).path}");
41-
_currentEntity = _entity;
42-
}
43-
44-
// String fileStructureStr = """
12+
// const String testStr = """
4513
// {
4614
// "name": "root",
4715
// "children": [
@@ -78,13 +46,44 @@ class FileTreeController extends ChangeNotifier {
7846
// "depth": 0,
7947
// "fatherPath": ""
8048
// }
81-
// """;
8249

83-
// _entity = EntityFolder.fromJson(jsonDecode(fileStructureStr));
50+
// """;
51+
52+
class FileTreeController extends ChangeNotifier {
53+
String _currentTreeNodeName = "";
54+
String get currentTreeNodeName => _currentTreeNodeName;
55+
56+
changeTreeNodeName(String s) {
57+
_currentTreeNodeName = s;
58+
notifyListeners();
59+
}
60+
61+
late EntityFolder? _entity = null;
62+
63+
init() async {
64+
DioUtils dioUtils = DioUtils();
65+
Response? response = await dioUtils.get(Server + Apis["getFile"]!);
66+
67+
if (response != null) {
68+
// debugPrint(response.toString());
69+
FileResponse res = FileResponse.fromJson(response.data);
70+
List<EntityFile> files =
71+
res.savedFiles!.map((e) => EntityFile.fromSavedFile(e)).toList();
72+
List<String> paths = [];
73+
for (final i in files) {
74+
paths.add('${i.fatherPath}/${i.name}');
75+
paths.add(i.fatherPath);
76+
}
77+
78+
FlattenObject flattenObject = FlattenObject(
79+
files: files.reversed.toList(), path: paths.reversed.toList());
8480

85-
// debugPrint("[flatten]:${flatten(_entity!).path}");
81+
EntityFolder? f = toStructured(flattenObject);
82+
_entity = f;
83+
_currentEntity = _entity;
84+
_currentTreeNodeName = _entity?.name ?? "";
85+
}
8686

87-
// _currentEntity = _entity;
8887
notifyListeners();
8988
}
9089

@@ -93,10 +92,26 @@ class FileTreeController extends ChangeNotifier {
9392
late EntityFolder? _currentEntity = null;
9493

9594
List<Object>? get currentFolderContent => _currentEntity?.children;
96-
int? get depth => _currentEntity?.depth;
95+
int? get depth => (_currentEntity?.depth ?? 0) + 1;
96+
String? get currentFatherPath => _getCurrentFatherPath();
97+
98+
String? _getCurrentFatherPath() {
99+
if (depth == 1) {
100+
return "../root";
101+
} else {
102+
return "${_currentEntity?.fatherPath ?? ""}/${_currentEntity?.name ?? ""}";
103+
}
104+
}
97105

98106
changeCurrentEntity(EntityFolder e) {
99107
_currentEntity = e;
108+
_currentTreeNodeName = e.name;
109+
notifyListeners();
110+
}
111+
112+
changeTree(EntityFolder e) {
113+
// debugPrint("[entityfolder]:${e.toJson()}");
114+
_entity = e;
100115
notifyListeners();
101116
}
102117
}

taichi_file_manager/frontend/lib/models/my_tree_node.dart

+91-31
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class EntityFolder {
177177

178178
FlattenObject flatten(EntityFolder entityFolder) {
179179
var names = _getPath(entityFolder);
180+
names = names.toSet().toList();
180181
// print(names);
181182
var files = _getFiles(entityFolder);
182183
// names = _merge(names);
@@ -189,6 +190,7 @@ List<String> _getPath(EntityFolder entityFolder) {
189190
for (var i in entityFolder.children) {
190191
if (i.runtimeType == EntityFile) {
191192
names.add("${(i as EntityFile).fatherPath}/${i.name}");
193+
names.add((i).fatherPath);
192194
} else {
193195
if (!(i as EntityFolder).hasChildren) {
194196
var s = "${i.fatherPath}/${i.name}";
@@ -226,6 +228,11 @@ class FlattenObject {
226228
List<EntityFile> files;
227229

228230
FlattenObject({this.files = const [], this.path = const []});
231+
232+
@override
233+
String toString() {
234+
return path.toString() + files.map((e) => e.toJson()).toList().toString();
235+
}
229236
}
230237

231238
EntityFolder? toStructured(FlattenObject object,
@@ -250,15 +257,15 @@ EntityFolder? toStructured(FlattenObject object,
250257
int maxDepth = 0;
251258
for (var s in object.path) {
252259
if (!isAFile(s)) {
260+
// print(s);
253261
var slist = s.split("/");
262+
// print(s);
254263
// slist.remove("..");
255264
// slist.remove("root");
256-
// print(slist.length);
257265

258266
for (int i = 2; i < slist.length; i++) {
259267
EntityFolder _en;
260268
if (i == 2) {
261-
// print("name:${slist[i]}");
262269
_en = EntityFolder(
263270
name: slist[i],
264271
depth: i - 1,
@@ -279,16 +286,14 @@ EntityFolder? toStructured(FlattenObject object,
279286
name: name, depth: i - 1, children: [], fatherPath: fatherPath);
280287
if (maxDepth <= _en.depth) maxDepth = _en.depth;
281288
}
289+
// print("[dddd]:${_en.toJson()}");
282290
if (!allFolders.contains(_en)) allFolders.add(_en);
283291
}
284292
}
285293
}
286-
// print(maxDepth);
287-
// for (final i in object.files) {
288-
// if (i.depth > maxDepth) {
289-
// maxDepth = i.depth;
290-
// }
291-
// }
294+
// print(allFolders.length);
295+
296+
// _depthEntityMap[1] = [entityFolder];
292297

293298
Map<int, List<EntityFolder>> _depthEntityMap = {};
294299
_depthEntityMap[0] = [entityFolder];
@@ -299,7 +304,7 @@ EntityFolder? toStructured(FlattenObject object,
299304
_depthEntityMap[i] = _res;
300305
}
301306

302-
// print("_depthEntityMap:$_depthEntityMap");
307+
// print(_depthEntityMap);
303308

304309
generateFromMap(_depthEntityMap, maxDepth, object.files);
305310
// print(jsonEncode(_depthEntityMap[0]![0].toJson()));
@@ -310,32 +315,31 @@ EntityFolder? toStructured(FlattenObject object,
310315
void generateFromMap(Map<int, List<EntityFolder>> depthEntityMap, int maxDepth,
311316
List<EntityFile> files) {
312317
// print(maxDepth);
313-
314-
for (int index = maxDepth; index > 0; index--) {
315-
for (var j in depthEntityMap[index]!) {
316-
// print("j:${j.toJson()}");
317-
for (var i in depthEntityMap[index - 1]!) {
318-
// print("i:${i.toJson()}");
319-
List<EntityFile> caches = [];
320-
321-
for (var f in files) {
322-
if (f.fatherPath.endsWith(i.name)) {
323-
i.addFile(f);
324-
caches.add(f);
318+
for (int index = maxDepth; index >= 0; index--) {
319+
if (index > 0) {
320+
for (var j in depthEntityMap[index]!) {
321+
for (var i in depthEntityMap[index - 1]!) {
322+
// List<EntityFile> caches = [];
323+
324+
for (var f in files) {
325+
if (f.fatherPath.endsWith("${j.fatherPath}/${j.name}")) {
326+
j.addFile(f);
327+
// caches.add(f);
328+
}
325329
}
326330

327-
if (f.fatherPath.endsWith(j.name)) {
328-
j.addFile(f);
329-
caches.add(f);
331+
if (j.fatherPath.endsWith(i.name)) {
332+
i.children.add(j);
330333
}
331334
}
332-
333-
for (var f in caches) {
334-
files.remove(f);
335-
}
336-
337-
if (j.fatherPath.endsWith(i.name)) {
338-
i.children.add(j);
335+
}
336+
} else {
337+
for (var j in depthEntityMap[index]!) {
338+
for (var f in files) {
339+
if (f.fatherPath.endsWith("${j.fatherPath}/${j.name}")) {
340+
j.addFile(f);
341+
// caches.add(f);
342+
}
339343
}
340344
}
341345
}
@@ -549,3 +553,59 @@ bool isAFile(String s) {
549553
return _tmp.contains(".");
550554
}
551555
}
556+
557+
extension FindObject on EntityFolder {
558+
Object? findObject(int dep, String objectName) {
559+
if (depth == dep && name == objectName) {
560+
return this;
561+
}
562+
563+
for (var i in children) {
564+
if (i is EntityFile) {
565+
if (i.depth == dep && i.name == objectName) {
566+
return i;
567+
} else {
568+
continue;
569+
}
570+
}
571+
572+
if (i is EntityFolder) {
573+
if (i.depth == dep && i.name == objectName) {
574+
return i;
575+
} else {
576+
var r = (i).findObject(dep, objectName);
577+
if (r != null) {
578+
return r;
579+
} else {
580+
continue;
581+
}
582+
}
583+
}
584+
}
585+
586+
return null;
587+
}
588+
}
589+
590+
extension FindParent on EntityFile {
591+
EntityFolder? findParent(EntityFolder en) {
592+
if (depth == en.depth + 1 && fatherPath.endsWith(en.name)) {
593+
return en;
594+
}
595+
596+
for (var i in en.children) {
597+
if (i is EntityFile) {
598+
continue;
599+
} else {
600+
var f = findParent(i as EntityFolder);
601+
if (f != null) {
602+
return f;
603+
} else {
604+
continue;
605+
}
606+
}
607+
}
608+
609+
return null;
610+
}
611+
}

0 commit comments

Comments
 (0)