nc-photos/app/lib/widget/album_dir_picker.dart

148 lines
4.3 KiB
Dart
Raw Normal View History

2021-06-29 11:44:35 +02:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
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.dart';
2021-06-29 11:44:35 +02:00
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-09-15 08:58:06 +02:00
const 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> {
2021-06-29 11:44:35 +02:00
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body: _buildContent(context),
),
);
}
Widget _buildContent(BuildContext context) {
return SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
L10n.global().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(
L10n.global().albumDirPickerSubHeaderText,
2021-06-29 11:44:35 +02:00
),
),
],
),
),
Expanded(
child: DirPicker(
key: _pickerKey,
account: widget.account,
strippedRootDir: _strippedRootDir,
validator: (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/"));
},
onConfirmed: (picks) => _onPickerConfirmed(context, picks),
),
2021-06-29 11:44:35 +02:00
),
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,
child: Text(L10n.global().confirmButtonLabel),
2021-06-29 11:44:35 +02:00
),
],
),
),
],
),
);
}
void _onConfirmPressed() {
_pickerKey.currentState?.confirm();
}
void _onPickerConfirmed(BuildContext context, List<File> picks) {
if (picks.isEmpty) {
2021-06-29 11:44:35 +02:00
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().albumDirPickerListEmptyNotification),
2021-06-29 11:44:35 +02:00
duration: k.snackBarDurationNormal,
));
} else {
_log.info(
"[_onPickerConfirmed] Picked: ${picks.map((e) => e.strippedPath).toReadableString()}");
Navigator.of(context).pop(picks);
2021-06-29 11:44:35 +02:00
}
}
String _getPickerRoot() {
if (widget.account.roots.length == 1 &&
widget.account.roots.first.isNotEmpty) {
return widget.account.roots.first;
} else {
return "";
}
}
final _pickerKey = GlobalKey<DirPickerState>();
late final _strippedRootDir = _getPickerRoot();
2021-06-29 11:44:35 +02:00
static final _log = Logger("widget.album_dir_picker._AlbumDirPickerState");
}