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

306 lines
8.9 KiB
Dart
Raw Normal View History

2022-06-06 19:37:46 +02:00
import 'dart:ui';
2021-05-28 21:44:09 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kiwi/kiwi.dart';
2021-05-28 21:44:09 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/scan_account_dir.dart';
2022-06-06 19:37:46 +02:00
import 'package:nc_photos/compute_queue.dart';
2021-09-04 14:35:04 +02:00
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/k.dart' as k;
2022-06-06 19:37:46 +02:00
import 'package:nc_photos/language_util.dart' as language_util;
import 'package:nc_photos/object_extension.dart';
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/update_property.dart';
2022-06-06 19:37:46 +02:00
import 'package:nc_photos/widget/builder/photo_list_item_builder.dart';
2021-08-02 13:21:27 +02:00
import 'package:nc_photos/widget/empty_list_indicator.dart';
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/widget/photo_list_item.dart';
2021-12-19 12:44:41 +01:00
import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
2021-07-25 16:27:19 +02:00
import 'package:nc_photos/widget/selection_app_bar.dart';
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/widget/viewer.dart';
2021-07-25 10:52:24 +02:00
import 'package:nc_photos/widget/zoom_menu_button.dart';
2021-05-28 21:44:09 +02:00
2021-07-31 19:02:41 +02:00
class ArchiveBrowserArguments {
ArchiveBrowserArguments(this.account);
2021-05-28 21:44:09 +02:00
final Account account;
}
2021-07-31 19:02:41 +02:00
class ArchiveBrowser extends StatefulWidget {
static const routeName = "/archive-browser";
2021-05-28 21:44:09 +02:00
2021-07-31 19:02:41 +02:00
static Route buildRoute(ArchiveBrowserArguments args) => MaterialPageRoute(
builder: (context) => ArchiveBrowser.fromArgs(args),
2021-07-23 22:05:57 +02:00
);
2021-09-15 08:58:06 +02:00
const ArchiveBrowser({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-05-28 21:44:09 +02:00
}) : super(key: key);
2021-07-31 19:02:41 +02:00
ArchiveBrowser.fromArgs(ArchiveBrowserArguments args, {Key? key})
2021-05-28 21:44:09 +02:00
: this(
key: key,
account: args.account,
);
@override
2021-07-31 19:02:41 +02:00
createState() => _ArchiveBrowserState();
2021-05-28 21:44:09 +02:00
final Account account;
}
2021-07-31 19:02:41 +02:00
class _ArchiveBrowserState extends State<ArchiveBrowser>
with SelectableItemStreamListMixin<ArchiveBrowser> {
2021-05-28 21:44:09 +02:00
@override
initState() {
super.initState();
_initBloc();
2021-10-27 22:40:54 +02:00
_thumbZoomLevel = Pref().getAlbumBrowserZoomLevelOr(0);
2021-05-28 21:44:09 +02:00
}
@override
build(BuildContext context) {
return AppTheme(
child: Scaffold(
body: BlocListener<ScanAccountDirBloc, ScanAccountDirBlocState>(
2021-05-28 21:44:09 +02:00
bloc: _bloc,
listener: (context, state) => _onStateChange(context, state),
child: BlocBuilder<ScanAccountDirBloc, ScanAccountDirBlocState>(
2021-05-28 21:44:09 +02:00
bloc: _bloc,
builder: (context, state) => _buildContent(context, state),
),
),
),
);
}
2022-06-06 19:37:46 +02:00
@override
onItemTap(SelectableItem item, int index) {
item.as<PhotoListFileItem>()?.run((fileItem) {
Navigator.pushNamed(
context,
Viewer.routeName,
arguments:
ViewerArguments(widget.account, _backingFiles, fileItem.fileIndex),
);
});
}
2021-05-28 21:44:09 +02:00
void _initBloc() {
if (_bloc.state is ScanAccountDirBlocInit) {
2021-05-28 21:44:09 +02:00
_log.info("[_initBloc] Initialize bloc");
_reqQuery();
} else {
// process the current state
2022-06-20 13:49:58 +02:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2021-07-03 13:32:23 +02:00
setState(() {
_onStateChange(context, _bloc.state);
});
});
2021-05-28 21:44:09 +02:00
}
}
Widget _buildContent(BuildContext context, ScanAccountDirBlocState state) {
2022-06-06 19:37:46 +02:00
if (state is ScanAccountDirBlocSuccess &&
!_buildItemQueue.isProcessing &&
itemStreamListItems.isEmpty) {
2021-08-02 13:21:27 +02:00
return Column(
children: [
AppBar(
title: Text(L10n.global().albumArchiveLabel),
2021-08-02 13:21:27 +02:00
elevation: 0,
),
Expanded(
child: EmptyListIndicator(
icon: Icons.archive_outlined,
text: L10n.global().listEmptyText,
2021-05-28 21:44:09 +02:00
),
2021-08-02 13:21:27 +02:00
),
],
);
} else {
return Stack(
children: [
buildItemStreamListOuter(
context,
child: Theme(
data: Theme.of(context).copyWith(
colorScheme: Theme.of(context).colorScheme.copyWith(
secondary: AppTheme.getOverscrollIndicatorColor(context),
),
2021-08-02 13:21:27 +02:00
),
child: CustomScrollView(
slivers: [
_buildAppBar(context),
2022-05-12 15:23:23 +02:00
buildItemStreamList(
maxCrossAxisExtent: _thumbSize.toDouble(),
2021-07-06 07:10:36 +02:00
),
2021-08-02 13:21:27 +02:00
],
),
2021-05-28 21:44:09 +02:00
),
),
2022-06-06 19:37:46 +02:00
if (state is ScanAccountDirBlocLoading ||
_buildItemQueue.isProcessing)
2021-09-15 08:58:06 +02:00
const Align(
2021-08-02 13:21:27 +02:00
alignment: Alignment.bottomCenter,
2021-09-15 08:58:06 +02:00
child: LinearProgressIndicator(),
2021-08-02 13:21:27 +02:00
),
],
);
}
2021-05-28 21:44:09 +02:00
}
Widget _buildAppBar(BuildContext context) {
if (isSelectionMode) {
return _buildSelectionAppBar(context);
} else {
return _buildNormalAppBar(context);
}
}
Widget _buildSelectionAppBar(BuildContext context) {
2021-07-25 16:27:19 +02:00
return SelectionAppBar(
count: selectedListItems.length,
onClosePressed: () {
setState(() {
clearSelectedItems();
});
},
actions: [
IconButton(
icon: const Icon(Icons.unarchive),
tooltip: L10n.global().unarchiveTooltip,
2021-05-28 21:44:09 +02:00
onPressed: () {
2021-07-25 16:27:19 +02:00
_onSelectionAppBarUnarchivePressed();
2021-05-28 21:44:09 +02:00
},
),
2021-07-25 16:27:19 +02:00
],
2021-05-28 21:44:09 +02:00
);
}
Widget _buildNormalAppBar(BuildContext context) {
return SliverAppBar(
title: Text(L10n.global().albumArchiveLabel),
2021-05-28 21:44:09 +02:00
floating: true,
actions: [
2021-07-25 10:52:24 +02:00
ZoomMenuButton(
initialZoom: _thumbZoomLevel,
minZoom: 0,
maxZoom: 2,
onZoomChanged: (value) {
setState(() {
_thumbZoomLevel = value.round();
});
2021-10-27 22:40:54 +02:00
Pref().setAlbumBrowserZoomLevel(_thumbZoomLevel);
2021-07-25 10:52:24 +02:00
},
2021-05-28 21:44:09 +02:00
),
],
);
}
void _onStateChange(BuildContext context, ScanAccountDirBlocState state) {
if (state is ScanAccountDirBlocInit) {
2021-05-28 21:44:09 +02:00
itemStreamListItems = [];
} else if (state is ScanAccountDirBlocSuccess ||
state is ScanAccountDirBlocLoading) {
2021-05-28 21:44:09 +02:00
_transformItems(state.files);
} else if (state is ScanAccountDirBlocFailure) {
_transformItems(state.files);
2021-05-28 21:44:09 +02:00
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.exception)),
2021-05-28 21:44:09 +02:00
duration: k.snackBarDurationNormal,
));
} else if (state is ScanAccountDirBlocInconsistent) {
2021-05-28 21:44:09 +02:00
_reqQuery();
}
}
Future<void> _onSelectionAppBarUnarchivePressed() async {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global()
2021-05-28 21:44:09 +02:00
.unarchiveSelectedProcessingNotification(selectedListItems.length)),
duration: k.snackBarDurationShort,
));
final selectedFiles = selectedListItems
2022-06-06 19:37:46 +02:00
.whereType<PhotoListFileItem>()
2021-05-28 21:44:09 +02:00
.map((e) => e.file)
.toList();
setState(() {
clearSelectedItems();
});
final c = KiwiContainer().resolve<DiContainer>();
2021-05-28 21:44:09 +02:00
final failures = <File>[];
for (final f in selectedFiles) {
try {
await UpdateProperty(c.fileRepo)
2021-05-28 21:44:09 +02:00
.updateIsArchived(widget.account, f, false);
} catch (e, stacktrace) {
_log.shout(
2021-12-02 09:27:11 +01:00
"[_onSelectionAppBarUnarchivePressed] Failed while unarchiving file: ${logFilename(f.path)}",
2021-05-28 21:44:09 +02:00
e,
stacktrace);
failures.add(f);
}
}
if (failures.isEmpty) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().unarchiveSelectedSuccessNotification),
2021-05-28 21:44:09 +02:00
duration: k.snackBarDurationNormal,
));
} else {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global()
2021-05-28 21:44:09 +02:00
.unarchiveSelectedFailureNotification(failures.length)),
duration: k.snackBarDurationNormal,
));
}
}
void _transformItems(List<File> files) {
2022-06-06 19:37:46 +02:00
_buildItemQueue.addJob(
PhotoListItemBuilderArguments(
widget.account,
files,
isArchived: true,
sorter: photoListFileDateTimeSorter,
locale: language_util.getSelectedLocale() ??
PlatformDispatcher.instance.locale,
),
buildPhotoListItem,
(result) {
if (mounted) {
setState(() {
_backingFiles = result.backingFiles;
itemStreamListItems = result.listItems;
});
2021-05-28 21:44:09 +02:00
}
2022-06-06 19:37:46 +02:00
},
);
2021-05-28 21:44:09 +02:00
}
void _reqQuery() {
_bloc.add(const ScanAccountDirBlocQuery());
2021-05-28 21:44:09 +02:00
}
late final _bloc = ScanAccountDirBloc.of(widget.account);
2021-05-28 21:44:09 +02:00
var _backingFiles = <File>[];
2022-06-06 19:37:46 +02:00
final _buildItemQueue =
ComputeQueue<PhotoListItemBuilderArguments, PhotoListItemBuilderResult>();
2021-05-28 21:44:09 +02:00
var _thumbZoomLevel = 0;
2021-12-19 12:44:41 +01:00
int get _thumbSize => photo_list_util.getThumbSize(_thumbZoomLevel);
2021-05-28 21:44:09 +02:00
2021-07-31 19:02:41 +02:00
static final _log = Logger("widget.archive_browser._ArchiveBrowserState");
2021-05-28 21:44:09 +02:00
}