-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from Lenny4/dev
show the size of all folders
- Loading branch information
Showing
12 changed files
with
270 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'folder_property.g.dart'; | ||
|
||
@JsonSerializable() | ||
class FolderProperty { | ||
int? size; | ||
String path; | ||
int? nbChildren; | ||
List<FolderProperty> folderProperties; | ||
|
||
FolderProperty( | ||
{this.size, | ||
required this.path, | ||
this.nbChildren, | ||
required this.folderProperties}); | ||
|
||
factory FolderProperty.fromJson(Map<String, dynamic> json) => | ||
_$FolderPropertyFromJson(json); | ||
|
||
Map<String, dynamic> toJson() => _$FolderPropertyToJson(this); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:deepfacelab_client/class/folder_property.dart'; | ||
import 'package:deepfacelab_client/service/workspace_service.dart'; | ||
|
||
import '../class/workspace.dart'; | ||
|
||
class FileManagerService { | ||
Future<FolderProperty> _updateFolderProperty( | ||
{required FolderProperty folderProperty, | ||
required List<FileSystemEntity> fileSystemEntities}) async { | ||
folderProperty.nbChildren = fileSystemEntities.length; | ||
var size = 0; | ||
List<String> foldersFound = []; | ||
for (var fileSystemEntity in fileSystemEntities) { | ||
if (fileSystemEntity is File) { | ||
size += await fileSystemEntity.length(); | ||
} | ||
if (fileSystemEntity is Directory) { | ||
var thisFileSystemEntities = | ||
await Directory(fileSystemEntity.path).list().toList(); | ||
foldersFound.add(fileSystemEntity.path); | ||
var thisFolderProperty = folderProperty.folderProperties | ||
.firstWhereOrNull( | ||
(FolderProperty f) => f.path == fileSystemEntity.path); | ||
if (thisFolderProperty == null) { | ||
thisFolderProperty = | ||
FolderProperty(path: fileSystemEntity.path, folderProperties: []); | ||
folderProperty.folderProperties.add(thisFolderProperty); | ||
} | ||
await _updateFolderProperty( | ||
folderProperty: thisFolderProperty, | ||
fileSystemEntities: thisFileSystemEntities); | ||
size += thisFolderProperty.size!; | ||
} | ||
} | ||
folderProperty.folderProperties = folderProperty.folderProperties | ||
.where((f) => foldersFound.contains(f.path)) | ||
.toList(); | ||
folderProperty.size = size; | ||
return folderProperty; | ||
} | ||
|
||
Future<FolderProperty> updateFolderProperty( | ||
{required String path, | ||
required Workspace workspace, | ||
List<FileSystemEntity>? fileSystemEntities, | ||
bool force = false}) async { | ||
// region get thisFolderProperty | ||
workspace.folderProperty ??= | ||
FolderProperty(path: workspace.path, folderProperties: []); | ||
FolderProperty? thisFolderProperty = workspace.folderProperty; | ||
var pathArray = path | ||
.replaceAll(workspace.path, '') | ||
.split(Platform.pathSeparator) | ||
.where((element) => element != '') | ||
.toList(); | ||
var i = 0; | ||
while (thisFolderProperty != null && thisFolderProperty.path != path) { | ||
thisFolderProperty = thisFolderProperty.folderProperties.firstWhereOrNull( | ||
(f) => f.path.endsWith(Platform.pathSeparator + pathArray[i])); | ||
i++; | ||
} | ||
// endregion | ||
if (thisFolderProperty == null) { | ||
return await updateFolderProperty( | ||
path: workspace.path, workspace: workspace, force: true); | ||
} | ||
fileSystemEntities ??= await Directory(path).list().toList(); | ||
if (!force && thisFolderProperty.nbChildren == fileSystemEntities.length) { | ||
return thisFolderProperty; | ||
} | ||
if (thisFolderProperty.path != workspace.path) { | ||
return await updateFolderProperty( | ||
path: workspace.path, workspace: workspace, force: true); | ||
} | ||
thisFolderProperty = await _updateFolderProperty( | ||
folderProperty: thisFolderProperty, | ||
fileSystemEntities: fileSystemEntities); | ||
workspace.folderProperty = thisFolderProperty; | ||
WorkspaceService().createUpdateWorkspace( | ||
oldWorkspace: workspace, newWorkspace: workspace); | ||
return thisFolderProperty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.