Batch import folders as albums

This commit is contained in:
Ming Ming 2021-07-01 19:32:22 +08:00
parent 8faa31852c
commit 5257766151
5 changed files with 508 additions and 0 deletions

View file

@ -0,0 +1,169 @@
import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/provider.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/iterable_extension.dart';
import 'package:nc_photos/use_case/list_album.dart';
import 'package:nc_photos/use_case/ls.dart';
class ListImportableAlbumBlocItem {
ListImportableAlbumBlocItem(this.file, this.photoCount);
final File file;
final int photoCount;
}
abstract class ListImportableAlbumBlocEvent {
const ListImportableAlbumBlocEvent();
}
class ListImportableAlbumBlocQuery extends ListImportableAlbumBlocEvent {
const ListImportableAlbumBlocQuery(
this.account,
this.roots,
);
@override
toString() {
return "$runtimeType {"
"account: $account, "
"roots: ${roots.toReadableString()}, "
"}";
}
final Account account;
final List<File> roots;
}
abstract class ListImportableAlbumBlocState {
const ListImportableAlbumBlocState(this.items);
@override
toString() {
return "$runtimeType {"
"items: List {length: ${items.length}}, "
"}";
}
final List<ListImportableAlbumBlocItem> items;
}
class ListImportableAlbumBlocInit extends ListImportableAlbumBlocState {
ListImportableAlbumBlocInit() : super(const []);
}
class ListImportableAlbumBlocLoading extends ListImportableAlbumBlocState {
const ListImportableAlbumBlocLoading(List<ListImportableAlbumBlocItem> items)
: super(items);
}
class ListImportableAlbumBlocSuccess extends ListImportableAlbumBlocState {
const ListImportableAlbumBlocSuccess(List<ListImportableAlbumBlocItem> items)
: super(items);
}
class ListImportableAlbumBlocFailure extends ListImportableAlbumBlocState {
const ListImportableAlbumBlocFailure(
List<ListImportableAlbumBlocItem> items, this.exception)
: super(items);
@override
toString() {
return "$runtimeType {"
"super: ${super.toString()}, "
"exception: $exception, "
"}";
}
final dynamic exception;
}
/// Return all directories that potentially could be a new album
class ListImportableAlbumBloc
extends Bloc<ListImportableAlbumBlocEvent, ListImportableAlbumBlocState> {
ListImportableAlbumBloc() : super(ListImportableAlbumBlocInit());
@override
mapEventToState(ListImportableAlbumBlocEvent event) async* {
_log.info("[mapEventToState] $event");
if (event is ListImportableAlbumBlocQuery) {
yield* _onEventQuery(event);
}
}
Stream<ListImportableAlbumBlocState> _onEventQuery(
ListImportableAlbumBlocQuery ev) async* {
yield ListImportableAlbumBlocLoading([]);
try {
final fileRepo = FileRepo(FileCachedDataSource());
final albumRepo = AlbumRepo(AlbumCachedDataSource());
final albums = await ListAlbum(fileRepo, albumRepo)(ev.account);
final importedDirs = albums.map((a) {
if (a.provider is! AlbumDirProvider) {
return <File>[];
} else {
return (a.provider as AlbumDirProvider).dirs;
}
}).fold<List<File>>(
[], (previousValue, element) => previousValue + element);
final products = <ListImportableAlbumBlocItem>[];
int count = 0;
for (final r in ev.roots) {
await for (final ev
in _queryDir(fileRepo, ev.account, importedDirs, r)) {
if (ev is Exception || ev is Error) {
throw ev;
} else if (ev is ListImportableAlbumBlocItem) {
products.add(ev);
// don't emit events too frequently
if (++count >= 5) {
yield ListImportableAlbumBlocLoading(products);
}
}
}
}
yield ListImportableAlbumBlocSuccess(products);
} catch (e) {
_log.severe("[_onEventQuery] Exception while request", e);
yield ListImportableAlbumBlocFailure(state.items, e);
}
}
/// Query [dir] and emit all conforming dirs recursively (including [dir])
///
/// Emit ListImportableAlbumBlocItem or Exception
Stream<dynamic> _queryDir(FileRepo fileRepo, Account account,
List<File> importedDirs, File dir) async* {
try {
if (importedDirs.containsIf(dir, (a, b) => a.path == b.path)) {
return;
}
final files = await Ls(fileRepo)(account, dir);
// check number of supported files in this directory
final count = files.where((f) => file_util.isSupportedFormat(f)).length;
// arbitrary number
if (count >= 5) {
yield ListImportableAlbumBlocItem(dir, count);
}
for (final d in files.where((f) => f.isCollection == true)) {
yield* _queryDir(fileRepo, account, importedDirs, d);
}
} catch (e, stacktrace) {
_log.shout(
"[_queryDir] Failed while listing dir" +
(kDebugMode ? ": ${dir.path}" : ""),
e,
stacktrace);
yield e;
}
}
static final _log =
Logger("bloc.list_importable_album.ListImportableAlbumBloc");
}

View file

@ -503,6 +503,23 @@
"@convertBasicAlbumSuccessNotification": {
"description": "Successfully converted the album"
},
"importFoldersTooltip": "Import folders",
"@importFoldersTooltip": {
"description": "Menu entry in the album page to import folders as albums"
},
"albumImporterHeaderText": "Import folders as albums",
"@albumImporterHeaderText": {
"description": "Import folders as albums"
},
"albumImporterSubHeaderText": "Suggested folders are listed below. Depending on the number of files on your server, it might take a while to finish",
"@albumImporterSubHeaderText": {
"description": "Import folders as albums"
},
"importButtonLabel": "IMPORT",
"albumImporterProgressText": "Importing folders",
"@albumImporterProgressText": {
"description": "Message shown while importing"
},
"changelogTitle": "Changelog",
"@changelogTitle": {
"description": "Title of the changelog dialog"

View file

@ -0,0 +1,285 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.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/bloc/list_importable_album.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/file.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/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/use_case/create_album.dart';
import 'package:nc_photos/use_case/populate_album.dart';
import 'package:nc_photos/use_case/update_dynamic_album_cover.dart';
import 'package:nc_photos/use_case/update_dynamic_album_time.dart';
import 'package:path/path.dart' as path;
class AlbumImporterArguments {
AlbumImporterArguments(this.account);
final Account account;
}
class AlbumImporter extends StatefulWidget {
static const routeName = "/album-importer";
AlbumImporter({
Key key,
@required this.account,
}) : super(key: key);
AlbumImporter.fromArgs(AlbumImporterArguments args, {Key key})
: this(
key: key,
account: args.account,
);
@override
createState() => _AlbumImporterState();
final Account account;
}
class _AlbumImporterState extends State<AlbumImporter> {
@override
initState() {
super.initState();
_initBloc();
}
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body:
BlocListener<ListImportableAlbumBloc, ListImportableAlbumBlocState>(
bloc: _bloc,
listener: (context, state) => _onStateChange(context, state),
child: BlocBuilder<ListImportableAlbumBloc,
ListImportableAlbumBlocState>(
bloc: _bloc,
builder: (context, state) => _buildContent(context, state),
),
),
),
);
}
void _initBloc() {
_log.info("[_initBloc] Initialize bloc");
_bloc = ListImportableAlbumBloc();
_bloc.add(ListImportableAlbumBlocQuery(
widget.account,
widget.account.roots
.map((e) => File(
path:
"${api_util.getWebdavRootUrlRelative(widget.account)}/$e"))
.toList()));
}
Widget _buildContent(
BuildContext context, ListImportableAlbumBlocState state) {
return SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Text(
AppLocalizations.of(context).albumImporterHeaderText,
style: Theme.of(context).textTheme.headline5,
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Align(
alignment: AlignmentDirectional.topStart,
child: Text(
AppLocalizations.of(context).albumImporterSubHeaderText,
),
),
],
),
),
Expanded(
child: Stack(
children: [
Align(
alignment: Alignment.center,
child: _buildList(context, state),
),
if (state is ListImportableAlbumBlocLoading)
Align(
alignment: Alignment.topCenter,
child: const LinearProgressIndicator(),
),
],
),
),
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: () => _onImportPressed(context),
child: Text(AppLocalizations.of(context).importButtonLabel),
),
],
),
),
],
),
);
}
Widget _buildList(BuildContext context, ListImportableAlbumBlocState state) {
return Theme(
data: Theme.of(context).copyWith(
accentColor: AppTheme.getOverscrollIndicatorColor(context),
),
child: ListView.separated(
itemBuilder: (context, index) =>
_buildItem(context, _backingFiles[index]),
separatorBuilder: (context, index) => const Divider(),
itemCount: _backingFiles.length,
),
);
}
Widget _buildItem(BuildContext context, File file) {
final isPicked = _picks.containsIdentical(file);
final onTap = () {
setState(() {
if (isPicked) {
_picks.removeWhere((p) => identical(p, file));
} else {
_picks.add(file);
}
});
};
return ListTile(
dense: true,
leading: IconButton(
icon: AnimatedSwitcher(
duration: k.animationDurationShort,
transitionBuilder: (child, animation) =>
ScaleTransition(child: child, scale: animation),
child: Icon(
isPicked ? Icons.check_box : Icons.check_box_outline_blank,
key: ValueKey(isPicked),
),
),
onPressed: onTap,
),
title: Text(path.basename(file.path)),
subtitle: Text(file.strippedPath),
onTap: onTap,
);
}
void _onStateChange(
BuildContext context, ListImportableAlbumBlocState state) {
if (state is ListImportableAlbumBlocSuccess ||
state is ListImportableAlbumBlocLoading) {
_transformItems(state.items);
} else if (state is ListImportableAlbumBlocFailure) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception, context)),
duration: k.snackBarDurationNormal,
));
}
}
void _onImportPressed(BuildContext context) async {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) => WillPopScope(
onWillPop: () => Future.value(false),
child: AlertDialog(
content: Row(
mainAxisSize: MainAxisSize.min,
children: [
CircularProgressIndicator(),
const SizedBox(width: 24),
Text(AppLocalizations.of(context).albumImporterProgressText),
],
),
),
),
);
try {
await _createAllAlbums(context);
} finally {
// make sure we dismiss the dialog in any cases
Navigator.of(context).pop();
}
Navigator.of(context).pop();
}
Future<void> _createAllAlbums(BuildContext context) async {
for (final p in _picks) {
try {
var album = Album(
name: path.basename(p.path),
provider: AlbumDirProvider(
dirs: [p],
),
coverProvider: AlbumAutoCoverProvider(),
);
_log.info("[_onImportPressed] Creating dir album: $album");
final items = await PopulateAlbum()(widget.account, album);
final sortedFiles = items
.whereType<AlbumFileItem>()
.map((e) => e.file)
.where((element) => file_util.isSupportedFormat(element))
.sorted(compareFileDateTimeDescending);
album =
UpdateDynamicAlbumCover().updateWithSortedFiles(album, sortedFiles);
album =
UpdateDynamicAlbumTime().updateWithSortedFiles(album, sortedFiles);
final albumRepo = AlbumRepo(AlbumCachedDataSource());
await CreateAlbum(albumRepo)(widget.account, album);
} catch (e, stacktrace) {
_log.shout(
"[_createAllAlbums] Failed creating dir album", e, stacktrace);
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e, context)),
duration: k.snackBarDurationNormal,
));
}
}
}
void _transformItems(List<ListImportableAlbumBlocItem> items) {
_backingFiles = items
.sorted((a, b) => b.photoCount - a.photoCount)
.map((e) => e.file)
.toList();
}
ListImportableAlbumBloc _bloc;
var _backingFiles = <File>[];
final _picks = <File>[];
static final _log = Logger("widget.album_importer._AlbumImporterState");
}

View file

@ -22,6 +22,7 @@ import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/remove.dart';
import 'package:nc_photos/widget/album_grid_item.dart';
import 'package:nc_photos/widget/album_importer.dart';
import 'package:nc_photos/widget/album_viewer.dart';
import 'package:nc_photos/widget/archive_viewer.dart';
import 'package:nc_photos/widget/dynamic_album_viewer.dart';
@ -161,6 +162,19 @@ class _HomeAlbumsState extends State<HomeAlbums> {
Widget _buildNormalAppBar(BuildContext context) {
return HomeSliverAppBar(
account: widget.account,
menuActions: [
PopupMenuItem(
value: _menuValueImport,
child: Text(AppLocalizations.of(context).importFoldersTooltip),
),
],
onSelectedMenuActions: (option) {
switch (option) {
case _menuValueImport:
_onAppBarImportPressed(context);
break;
}
},
);
}
@ -363,6 +377,11 @@ class _HomeAlbumsState extends State<HomeAlbums> {
});
}
void _onAppBarImportPressed(BuildContext context) {
Navigator.of(context).pushNamed(AlbumImporter.routeName,
arguments: AlbumImporterArguments(widget.account));
}
Future<void> _onSelectionAppBarDeletePressed() async {
SnackBarManager().showSnackBar(SnackBar(
content: Text(AppLocalizations.of(context)
@ -452,6 +471,7 @@ class _HomeAlbumsState extends State<HomeAlbums> {
final _selectedItems = <_GridItem>[];
static final _log = Logger("widget.home_albums._HomeAlbumsState");
static const _menuValueImport = 0;
}
class _GridItem {

View file

@ -8,6 +8,7 @@ import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/widget/album_dir_picker.dart';
import 'package:nc_photos/widget/album_importer.dart';
import 'package:nc_photos/widget/album_viewer.dart';
import 'package:nc_photos/widget/archive_viewer.dart';
import 'package:nc_photos/widget/connect.dart';
@ -94,6 +95,7 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
route ??= _handleArchiveViewerRoute(settings);
route ??= _handleDynamicAlbumViewerRoute(settings);
route ??= _handleAlbumDirPickerRoute(settings);
route ??= _handleAlbumImporterRoute(settings);
return route;
}
@ -248,6 +250,21 @@ class _MyAppState extends State<MyApp> implements SnackBarHandler {
return null;
}
Route<dynamic> _handleAlbumImporterRoute(RouteSettings settings) {
try {
if (settings.name == AlbumImporter.routeName &&
settings.arguments != null) {
final AlbumImporterArguments args = settings.arguments;
return MaterialPageRoute(
builder: (context) => AlbumImporter.fromArgs(args),
);
}
} catch (e) {
_log.severe("[_handleAlbumImporterRoute] Failed while handling route", e);
}
return null;
}
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
AppEventListener<ThemeChangedEvent> _themeChangedListener;