addFolder method

Folder? addFolder(
  1. Folder? folder,
  2. List<String> paths
)

Implementation

Folder? addFolder(Folder? folder, List<String> paths) {
  if (paths.isEmpty) {
    return folder;
  }

  final folderName = paths.first;
  final subFolders = paths.skip(1).toList();

  if (folder == null) {
    if (!folders.containsKey(folderName)) {
      folders.putIfAbsent(
        folderName,
        () => Folder(
          name: folderName,
          isExpanded: foldersExpanded,
        ),
      );
    }

    return addFolder(
      folders[folderName],
      subFolders,
    );
  }
  // TODO redundant code. needs refactoring.
  else {
    if (!folder.subFolders.containsKey(folderName)) {
      folder.subFolders.putIfAbsent(
        folderName,
        () => Folder(
          name: folderName,
          isExpanded: foldersExpanded,
        ),
      );
    }

    return addFolder(
      folder.subFolders[folderName],
      subFolders,
    );
  }
}