nc-photos/lib/widget/album_dir_picker.dart

147 lines
4.1 KiB
Dart
Raw Normal View History

2021-06-29 11:44:35 +02:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
import 'package:nc_photos/entity/file.dart';
2021-06-29 11:44:35 +02:00
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/dir_picker_mixin.dart';
class AlbumDirPickerArguments {
AlbumDirPickerArguments(this.account);
final Account account;
}
class AlbumDirPicker extends StatefulWidget {
static const routeName = "/album-dir-picker";
2021-07-23 22:05:57 +02:00
static Route buildRoute(AlbumDirPickerArguments args) =>
MaterialPageRoute<List<File>>(
builder: (context) => AlbumDirPicker.fromArgs(args),
);
2021-06-29 11:44:35 +02:00
AlbumDirPicker({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-06-29 11:44:35 +02:00
}) : super(key: key);
2021-07-23 22:05:57 +02:00
AlbumDirPicker.fromArgs(AlbumDirPickerArguments args, {Key? key})
2021-06-29 11:44:35 +02:00
: this(
key: key,
account: args.account,
);
@override
createState() => _AlbumDirPickerState();
final Account account;
}
class _AlbumDirPickerState extends State<AlbumDirPicker>
with DirPickerMixin<AlbumDirPicker> {
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body: _buildContent(context),
),
);
}
@override
getPickerRoot() {
2021-07-01 19:07:05 +02:00
final root = api_util.getWebdavRootUrlRelative(widget.account);
2021-06-29 11:44:35 +02:00
if (widget.account.roots.length == 1) {
return "$root/${widget.account.roots.first}";
} else {
return root;
}
}
@override
getAccount() => widget.account;
@override
canPickDir(File dir) {
if (widget.account.roots.contains("")) {
return true;
}
final root = api_util.getWebdavRootUrlRelative(widget.account);
return widget.account.roots
.any((r) => dir.path == "$root/$r" || dir.path.startsWith("$root/$r/"));
}
2021-06-29 11:44:35 +02:00
Widget _buildContent(BuildContext context) {
return SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
2021-07-23 22:05:57 +02:00
AppLocalizations.of(context)!.albumDirPickerHeaderText,
2021-06-29 11:44:35 +02:00
style: Theme.of(context).textTheme.headline5,
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Align(
alignment: AlignmentDirectional.topStart,
child: Text(
2021-07-23 22:05:57 +02:00
AppLocalizations.of(context)!.albumDirPickerSubHeaderText,
2021-06-29 11:44:35 +02:00
),
),
],
),
),
Expanded(
child: buildDirPicker(context),
),
Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child:
Text(MaterialLocalizations.of(context).cancelButtonLabel),
),
ElevatedButton(
onPressed: () => _onConfirmPressed(context),
2021-07-23 22:05:57 +02:00
child: Text(AppLocalizations.of(context)!.confirmButtonLabel),
2021-06-29 11:44:35 +02:00
),
],
),
),
],
),
);
}
void _onConfirmPressed(BuildContext context) {
final picked = getPickedDirs();
if (picked.isEmpty) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(
2021-07-23 22:05:57 +02:00
AppLocalizations.of(context)!.albumDirPickerListEmptyNotification),
2021-06-29 11:44:35 +02:00
duration: k.snackBarDurationNormal,
));
} else {
_log.info(
"[_onConfirmPressed] Picked: ${picked.map((e) => e.strippedPath).toReadableString()}");
Navigator.of(context).pop(picked);
}
}
static final _log = Logger("widget.album_dir_picker._AlbumDirPickerState");
}