From 87d2696a998648b1547d4622317956d2930e716c Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 16 Nov 2021 01:55:39 +0800 Subject: [PATCH] Refactor: extract fn to check if file under dir --- lib/entity/file_util.dart | 13 +++++++++++++ lib/widget/dir_picker.dart | 8 ++++---- test/mock_type.dart | 5 ++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/entity/file_util.dart b/lib/entity/file_util.dart index 17cb45de..6d3d48db 100644 --- a/lib/entity/file_util.dart +++ b/lib/entity/file_util.dart @@ -25,6 +25,19 @@ bool isTrash(Account account, File file) => bool isAlbumFile(Account account, File file) => file.path.startsWith(remote_storage_util.getRemoteAlbumsDir(account)); +/// Return if [file] is located under [dir] +/// +/// Return false if [file] is [dir] itself (since it's not "under") +/// +/// See [isOrUnderDir] +bool isUnderDir(File file, File dir) => file.path.startsWith("${dir.path}/"); + +/// Return if [file] is [dir] or located under [dir] +/// +/// See [isUnderDir] +bool isOrUnderDir(File file, File dir) => + file.path == dir.path || isUnderDir(file, dir); + /// For a path "remote.php/dav/files/foo/bar.jpg", return foo CiString getUserDirName(File file) { if (file.path.startsWith("remote.php/dav/files/")) { diff --git a/lib/widget/dir_picker.dart b/lib/widget/dir_picker.dart index b5dc4ae3..e70778fb 100644 --- a/lib/widget/dir_picker.dart +++ b/lib/widget/dir_picker.dart @@ -8,6 +8,7 @@ import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/ls_dir.dart'; import 'package:nc_photos/entity/file.dart'; import 'package:nc_photos/entity/file/data_source.dart'; +import 'package:nc_photos/entity/file_util.dart' as file_util; import 'package:nc_photos/exception_util.dart' as exception_util; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/snack_bar_manager.dart'; @@ -331,8 +332,7 @@ class DirPickerState extends State { "[_pickedAllExclude] Unpicking '${item.file.path}' and picking children"); final products = []; for (final i in item.children ?? []) { - if (exclude.file.path == i.file.path || - exclude.file.path.startsWith("${i.file.path}/")) { + if (file_util.isOrUnderDir(exclude.file, i.file)) { // [i] is a parent of exclude products.addAll(_pickedAllExclude(item: i, exclude: exclude)); } else { @@ -362,12 +362,12 @@ class DirPickerState extends State { var product = _PickState.notPicked; for (final p in _picks) { // exact match, or parent is picked - if (p.path == item.file.path || item.file.path.startsWith("${p.path}/")) { + if (file_util.isOrUnderDir(item.file, p)) { product = _PickState.picked; // no need to check the remaining ones break; } - if (p.path.startsWith("${item.file.path}/")) { + if (file_util.isUnderDir(p, item.file)) { product = _PickState.childPicked; } } diff --git a/test/mock_type.dart b/test/mock_type.dart index 604aa3a0..67a2c62b 100644 --- a/test/mock_type.dart +++ b/test/mock_type.dart @@ -10,6 +10,7 @@ import 'package:nc_photos/app_db.dart'; import 'package:nc_photos/ci_string.dart'; import 'package:nc_photos/entity/album.dart'; import 'package:nc_photos/entity/file.dart'; +import 'package:nc_photos/entity/file_util.dart' as file_util; import 'package:nc_photos/entity/share.dart'; import 'package:nc_photos/entity/sharee.dart'; import 'package:nc_photos/or_null.dart'; @@ -180,9 +181,7 @@ class MockFileMemoryRepo extends MockFileRepo { @override list(Account account, File root) async { - return files - .where((f) => f.path == root.path || f.path.startsWith("${root.path}/")) - .toList(); + return files.where((f) => file_util.isOrUnderDir(f, root)).toList(); } final List files;