nc-photos/lib/widget/archive_browser.dart

381 lines
11 KiB
Dart
Raw Normal View History

2021-05-28 21:44:09 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-06 15:03:29 +02:00
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
2021-05-28 21:44:09 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/api/api_util.dart' as api_util;
2021-11-01 10:50:13 +01:00
import 'package:nc_photos/app_db.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';
2021-09-04 14:35:04 +02:00
import 'package:nc_photos/debug_util.dart';
2021-05-28 21:44:09 +02:00
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/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/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';
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),
),
),
),
);
}
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
2021-07-23 22:05:57 +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) {
if (state is ScanAccountDirBlocSuccess && 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),
SliverPadding(
padding: const EdgeInsets.only(top: 8),
sliver: 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
),
),
if (state is ScanAccountDirBlocLoading)
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();
}
}
void _onItemTap(int index) {
Navigator.pushNamed(context, Viewer.routeName,
arguments: ViewerArguments(widget.account, _backingFiles, index));
}
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
.whereType<_FileListItem>()
.map((e) => e.file)
.toList();
setState(() {
clearSelectedItems();
});
2021-11-01 10:50:13 +01:00
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
2021-05-28 21:44:09 +02:00
final failures = <File>[];
for (final f in selectedFiles) {
try {
await UpdateProperty(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) {
_backingFiles = files
.where((f) => f.isArchived == true)
2021-05-28 21:44:09 +02:00
.sorted(compareFileDateTimeDescending);
itemStreamListItems = () sync* {
for (int i = 0; i < _backingFiles.length; ++i) {
final f = _backingFiles[i];
final previewUrl = api_util.getFilePreviewUrl(widget.account, f,
2021-09-16 12:25:08 +02:00
width: k.photoThumbSize, height: k.photoThumbSize);
2021-05-28 21:44:09 +02:00
if (file_util.isSupportedImageFormat(f)) {
yield _ImageListItem(
file: f,
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
);
} else if (file_util.isSupportedVideoFormat(f)) {
yield _VideoListItem(
file: f,
account: widget.account,
previewUrl: previewUrl,
onTap: () => _onItemTap(i),
);
} else {
_log.shout(
"[_transformItems] Unsupported file format: ${f.contentType}");
}
}
2021-07-07 20:51:34 +02:00
}()
.toList();
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>[];
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
}
2021-07-06 15:03:29 +02:00
abstract class _ListItem implements SelectableItem {
_ListItem({
2021-07-23 22:05:57 +02:00
VoidCallback? onTap,
2021-07-06 15:03:29 +02:00
}) : _onTap = onTap;
@override
get onTap => _onTap;
@override
get isSelectable => true;
@override
get staggeredTile => const StaggeredTile.count(1, 1);
2021-07-23 22:05:57 +02:00
final VoidCallback? _onTap;
2021-07-06 15:03:29 +02:00
}
abstract class _FileListItem extends _ListItem {
2021-05-28 21:44:09 +02:00
_FileListItem({
2021-07-23 22:05:57 +02:00
required this.file,
VoidCallback? onTap,
2021-07-06 15:03:29 +02:00
}) : super(onTap: onTap);
2021-05-28 21:44:09 +02:00
@override
operator ==(Object other) {
return other is _FileListItem && file.path == other.file.path;
}
@override
get hashCode => file.path.hashCode;
final File file;
}
class _ImageListItem extends _FileListItem {
_ImageListItem({
2021-07-23 22:05:57 +02:00
required File file,
required this.account,
required this.previewUrl,
VoidCallback? onTap,
2021-05-28 21:44:09 +02:00
}) : super(file: file, onTap: onTap);
@override
buildWidget(BuildContext context) {
return PhotoListImage(
account: account,
previewUrl: previewUrl,
2021-06-22 07:24:37 +02:00
isGif: file.contentType == "image/gif",
2021-05-28 21:44:09 +02:00
);
}
final Account account;
final String previewUrl;
}
class _VideoListItem extends _FileListItem {
_VideoListItem({
2021-07-23 22:05:57 +02:00
required File file,
required this.account,
required this.previewUrl,
VoidCallback? onTap,
2021-05-28 21:44:09 +02:00
}) : super(file: file, onTap: onTap);
@override
buildWidget(BuildContext context) {
return PhotoListVideo(
account: account,
previewUrl: previewUrl,
);
}
final Account account;
final String previewUrl;
}