Fix folders outside of the roots could be picked

This commit is contained in:
Ming Ming 2021-07-02 01:07:37 +08:00
parent 7386a69d79
commit 5ba39ecc0a
2 changed files with 48 additions and 29 deletions

View file

@ -5,6 +5,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart'; import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util; import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/iterable_extension.dart'; import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/snack_bar_manager.dart'; import 'package:nc_photos/snack_bar_manager.dart';
@ -61,6 +62,13 @@ class _AlbumDirPickerState extends State<AlbumDirPicker>
@override @override
getAccount() => widget.account; getAccount() => widget.account;
@override
canPickDir(File dir) {
final root = api_util.getWebdavRootUrlRelative(widget.account);
return widget.account.roots
.any((r) => dir.path == "$root/$r" || dir.path.startsWith("$root/$r/"));
}
Widget _buildContent(BuildContext context) { Widget _buildContent(BuildContext context) {
return SafeArea( return SafeArea(
child: Column( child: Column(

View file

@ -44,6 +44,9 @@ mixin DirPickerMixin<T extends StatefulWidget> on State<T> {
@protected @protected
List<File> getPickedDirs() => UnmodifiableListView(_picks); List<File> getPickedDirs() => UnmodifiableListView(_picks);
@protected
bool canPickDir(File file) => true;
void _initBloc() { void _initBloc() {
_log.info("[_initBloc] Initialize bloc"); _log.info("[_initBloc] Initialize bloc");
_bloc = LsDirBloc(); _bloc = LsDirBloc();
@ -117,42 +120,50 @@ mixin DirPickerMixin<T extends StatefulWidget> on State<T> {
} }
Widget _buildItem(BuildContext context, LsDirBlocItem item) { Widget _buildItem(BuildContext context, LsDirBlocItem item) {
final canPick = canPickDir(item.file);
final pickState = _isItemPicked(item); final pickState = _isItemPicked(item);
IconData iconData; IconData iconData;
switch (pickState) { if (canPick) {
case _PickState.picked: switch (pickState) {
iconData = Icons.check_box; case _PickState.picked:
break; iconData = Icons.check_box;
case _PickState.childPicked: break;
iconData = Icons.indeterminate_check_box; case _PickState.childPicked:
break; iconData = Icons.indeterminate_check_box;
case _PickState.notPicked: break;
default: case _PickState.notPicked:
iconData = Icons.check_box_outline_blank; default:
break; iconData = Icons.check_box_outline_blank;
break;
}
} }
return ListTile( return ListTile(
dense: true, dense: true,
leading: IconButton( leading: canPick
icon: AnimatedSwitcher( ? IconButton(
duration: k.animationDurationShort, icon: AnimatedSwitcher(
transitionBuilder: (child, animation) => duration: k.animationDurationShort,
ScaleTransition(child: child, scale: animation), transitionBuilder: (child, animation) =>
child: Icon( ScaleTransition(child: child, scale: animation),
iconData, child: Icon(
key: ValueKey(pickState), iconData,
), key: ValueKey(pickState),
), ),
onPressed: () { ),
if (pickState == _PickState.picked) { onPressed: () {
_unpick(item); if (pickState == _PickState.picked) {
} else { _unpick(item);
_pick(item); } else {
} _pick(item);
}, }
), },
)
: IconButton(
icon: Icon(null),
onPressed: null,
),
title: Text(path.basename(item.file.path)), title: Text(path.basename(item.file.path)),
trailing: trailing:
item.children.isNotEmpty ? const Icon(Icons.arrow_forward_ios) : null, item.children.isNotEmpty ? const Icon(Icons.arrow_forward_ios) : null,