Refactor: extract fn to check if file under dir

This commit is contained in:
Ming Ming 2021-11-16 01:55:39 +08:00
parent d72f64d4f4
commit 87d2696a99
3 changed files with 19 additions and 7 deletions

View file

@ -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/")) {

View file

@ -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<DirPicker> {
"[_pickedAllExclude] Unpicking '${item.file.path}' and picking children");
final products = <LsDirBlocItem>[];
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<DirPicker> {
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;
}
}

View file

@ -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<File> files;