Skip to content

Commit

Permalink
show the size of all folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenny4 committed Jun 18, 2023
1 parent c56ef00 commit 6a16043
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 73 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

### Removed

## [0.2.0] - 2023-06-18

### Added

- Show the size of all folders in workspace

## [0.1.2] - 2023-06-14

Note: need to install it manually from the github if on windows.
Expand Down
22 changes: 22 additions & 0 deletions lib/class/folder_property.dart
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);
}
25 changes: 25 additions & 0 deletions lib/class/folder_property.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion lib/class/workspace.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:deepfacelab_client/class/folder_property.dart';
import 'package:deepfacelab_client/class/locale_storage_question.dart';
import 'package:json_annotation/json_annotation.dart';

Expand All @@ -8,9 +9,13 @@ class Workspace {
String name;
String path;
List<LocaleStorageQuestion>? localeStorageQuestions;
FolderProperty? folderProperty;

Workspace(
{required this.name, required this.path, this.localeStorageQuestions});
{required this.name,
required this.path,
this.localeStorageQuestions,
this.folderProperty});

factory Workspace.fromJson(Map<String, dynamic> json) =>
_$WorkspaceFromJson(json);
Expand Down
5 changes: 5 additions & 0 deletions lib/class/workspace.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:deepfacelab_client/screens/window_command_screen.dart';
import 'package:deepfacelab_client/screens/workspace_screen.dart';
import 'package:deepfacelab_client/service/locale_storage_service.dart';
import 'package:deepfacelab_client/widget/installation/has_requirements_widget.dart';
import 'package:file_sizes/file_sizes.dart';
import 'package:filesystem_picker/filesystem_picker.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -112,7 +113,8 @@ class Root extends HookWidget {
destination: NavigationRailDestination(
icon: const Icon(Icons.movie),
selectedIcon: const Icon(Icons.movie),
label: Text(workspace.name),
label: Text(
'${workspace.name}\n${FileSize.getSize(workspace.folderProperty?.size ?? 0)}'),
),
widget: WorkspaceScreen(initWorkspace: workspace)),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/workspace_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WorkspaceScreen extends HookWidget {
if (initWorkspace != null) ...[
const Divider(),
FileManagerWidget(
rootPath: initWorkspace!.path,
workspace: initWorkspace,
controller: mainController.value,
updateFileMissing: updateFileMissingController,
),
Expand Down
86 changes: 86 additions & 0 deletions lib/service/file_manager_service.dart
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;
}
}
3 changes: 1 addition & 2 deletions lib/service/workspace_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class WorkspaceService {
if (index != null) {
storage?.workspaces![index] = newWorkspace;
}
storage?.workspaces =
storage.workspaces?.map((w) => Workspace.fromJson(w.toJson())).toList();
storage?.workspaces = [...?storage.workspaces];
store.dispatch({'storage': storage});
}

Expand Down
Loading

0 comments on commit 6a16043

Please sign in to comment.