nc-photos/lib/widget/root_picker.dart

220 lines
6.2 KiB
Dart
Raw Normal View History

import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/widgets.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';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
2021-04-10 06:28:12 +02:00
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/use_case/ls_single_file.dart';
import 'package:nc_photos/widget/dir_picker.dart';
import 'package:nc_photos/widget/processing_dialog.dart';
2021-04-10 06:28:12 +02:00
class RootPickerArguments {
RootPickerArguments(this.account);
final Account account;
}
class RootPicker extends StatefulWidget {
static const routeName = "/root-picker";
2021-07-23 22:05:57 +02:00
static Route buildRoute(RootPickerArguments args) =>
MaterialPageRoute<Account>(
builder: (context) => RootPicker.fromArgs(args),
);
2021-09-15 08:58:06 +02:00
const RootPicker({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
2021-07-23 22:05:57 +02:00
RootPicker.fromArgs(RootPickerArguments args, {Key? key})
2021-04-10 06:28:12 +02:00
: this(
key: key,
account: args.account,
);
@override
createState() => _RootPickerState();
final Account account;
}
class _RootPickerState extends State<RootPicker> {
@override
initState() {
super.initState();
_initAccount();
}
void _initAccount() async {
try {
2021-09-15 08:58:06 +02:00
const fileSrc = FileWebdavDataSource();
final files = <File>[];
for (final r in widget.account.roots) {
if (r.isNotEmpty) {
_ensureInitDialog();
2021-10-17 11:48:00 +02:00
files.add(await LsSingleFile(fileSrc)(widget.account,
"${api_util.getWebdavRootUrlRelative(widget.account)}/$r"));
}
}
setState(() {
_initialPicks = files;
});
} catch (e) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e)),
duration: k.snackBarDurationNormal,
));
} finally {
_dismissInitDialog();
}
}
2021-04-10 06:28:12 +02:00
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
2021-06-29 07:42:45 +02:00
body: _buildContent(context),
2021-04-10 06:28:12 +02:00
),
);
}
2021-06-29 07:42:45 +02:00
Widget _buildContent(BuildContext context) {
2021-04-10 06:28:12 +02:00
return SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
L10n.global().rootPickerHeaderText,
2021-04-10 06:28:12 +02:00
style: Theme.of(context).textTheme.headline5,
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Align(
alignment: AlignmentDirectional.topStart,
child: Text(
L10n.global().rootPickerSubHeaderText,
2021-04-10 06:28:12 +02:00
),
),
],
),
),
Expanded(
child: _initialPicks == null
? Container()
: DirPicker(
key: _pickerKey,
account: widget.account,
rootDir: api_util.getWebdavRootUrlRelative(widget.account),
initialPicks: _initialPicks,
onConfirmed: (picks) => _onPickerConfirmed(context, picks),
),
2021-04-10 06:28:12 +02:00
),
2021-04-20 12:12:33 +02:00
Padding(
2021-04-10 06:28:12 +02:00
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2021-07-03 11:11:18 +02:00
TextButton(
onPressed: () => _onSkipPressed(context),
child: Text(L10n.global().skipButtonLabel),
2021-07-03 11:11:18 +02:00
),
2021-04-10 06:28:12 +02:00
ElevatedButton(
onPressed: _onConfirmPressed,
child: Text(L10n.global().confirmButtonLabel),
2021-04-10 06:28:12 +02:00
),
],
),
),
],
),
);
}
void _onSkipPressed(BuildContext context) {
showDialog(
context: context,
builder: (context) => AlertDialog(
content:
Text(L10n.global().rootPickerSkipConfirmationDialogContent),
2021-04-10 06:28:12 +02:00
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child:
Text(MaterialLocalizations.of(context).cancelButtonLabel),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: Text(MaterialLocalizations.of(context).okButtonLabel),
),
],
)).then((value) {
if (value == true) {
Navigator.of(context).pop(widget.account.copyWith(roots: [""]));
2021-04-10 06:28:12 +02:00
}
});
}
void _onConfirmPressed() {
_pickerKey.currentState?.confirm();
}
void _onPickerConfirmed(BuildContext context, List<File> picks) {
final roots = picks.map((e) => e.strippedPath).toList();
if (roots.isEmpty) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().rootPickerListEmptyNotification),
duration: k.snackBarDurationNormal,
));
return;
}
2021-04-10 06:28:12 +02:00
final newAccount = widget.account.copyWith(roots: roots);
_log.info("[_onPickerConfirmed] Account is good: $newAccount");
2021-04-10 06:28:12 +02:00
Navigator.of(context).pop(newAccount);
}
void _ensureInitDialog() {
if (_isInitDialogShown) {
return;
}
_isInitDialogShown = true;
2021-07-23 22:05:57 +02:00
SchedulerBinding.instance!.addPostFrameCallback((_) {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => ProcessingDialog(
text: L10n.global().genericProcessingDialogContent),
);
});
}
void _dismissInitDialog() {
if (!_isInitDialogShown) {
return;
}
Navigator.of(context).pop();
}
final _pickerKey = GlobalKey<DirPickerState>();
List<File>? _initialPicks;
bool _isInitDialogShown = false;
2021-04-10 06:28:12 +02:00
static final _log = Logger("widget.root_picker._RootPickerState");
}