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';
|
2022-07-05 22:20:24 +02:00
|
|
|
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';
|
2021-11-16 21:02:24 +01:00
|
|
|
import 'package:nc_photos/bloc/scan_account_dir.dart';
|
2021-09-04 14:35:04 +02:00
|
|
|
import 'package:nc_photos/debug_util.dart';
|
2022-07-05 22:20:24 +02:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-05-28 21:44:09 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2022-10-15 16:29:18 +02:00
|
|
|
import 'package:nc_photos/entity/file_descriptor.dart';
|
2023-07-17 09:35:45 +02:00
|
|
|
import 'package:nc_photos/entity/pref.dart';
|
2021-05-28 21:44:09 +02:00
|
|
|
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/snack_bar_manager.dart';
|
2022-10-15 16:29:18 +02:00
|
|
|
import 'package:nc_photos/use_case/inflate_file_descriptor.dart';
|
2021-05-28 21:44:09 +02:00
|
|
|
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';
|
2023-08-27 16:09:10 +02:00
|
|
|
import 'package:np_async/np_async.dart';
|
2022-12-16 16:01:04 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
2023-10-03 16:28:35 +02:00
|
|
|
import 'package:np_common/object_util.dart';
|
2022-12-16 16:01:04 +01:00
|
|
|
|
|
|
|
part 'archive_browser.g.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;
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:01:04 +01:00
|
|
|
@npLog
|
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) {
|
2022-11-12 10:55:33 +01:00
|
|
|
return Scaffold(
|
|
|
|
body: BlocListener<ScanAccountDirBloc, ScanAccountDirBlocState>(
|
|
|
|
bloc: _bloc,
|
|
|
|
listener: (context, state) => _onStateChange(context, state),
|
|
|
|
child: BlocBuilder<ScanAccountDirBloc, ScanAccountDirBlocState>(
|
2021-05-28 21:44:09 +02:00
|
|
|
bloc: _bloc,
|
2022-11-12 10:55:33 +01:00
|
|
|
builder: (context, state) => _buildContent(context, state),
|
2021-05-28 21:44:09 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-11-16 21:02:24 +01:00
|
|
|
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((_) {
|
2022-12-11 06:01:13 +01:00
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_onStateChange(context, _bloc.state);
|
|
|
|
});
|
|
|
|
}
|
2021-07-03 13:32:23 +02:00
|
|
|
});
|
2021-05-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:02:24 +01: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(
|
2021-08-29 13:51:43 +02:00
|
|
|
title: Text(L10n.global().albumArchiveLabel),
|
2021-08-02 13:21:27 +02:00
|
|
|
elevation: 0,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: EmptyListIndicator(
|
|
|
|
icon: Icons.archive_outlined,
|
2021-08-29 13:51:43 +02:00
|
|
|
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,
|
2022-11-12 10:55:33 +01:00
|
|
|
child: CustomScrollView(
|
|
|
|
slivers: [
|
|
|
|
_buildAppBar(context),
|
|
|
|
buildItemStreamList(
|
|
|
|
maxCrossAxisExtent: _thumbSize.toDouble(),
|
|
|
|
),
|
|
|
|
],
|
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),
|
2021-08-29 13:51:43 +02:00
|
|
|
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(
|
2021-08-29 13:51:43 +02:00
|
|
|
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
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-16 21:02:24 +01:00
|
|
|
void _onStateChange(BuildContext context, ScanAccountDirBlocState state) {
|
|
|
|
if (state is ScanAccountDirBlocInit) {
|
2021-05-28 21:44:09 +02:00
|
|
|
itemStreamListItems = [];
|
2021-11-16 21:02:24 +01:00
|
|
|
} else if (state is ScanAccountDirBlocSuccess ||
|
|
|
|
state is ScanAccountDirBlocLoading) {
|
2021-05-28 21:44:09 +02:00
|
|
|
_transformItems(state.files);
|
2021-11-16 21:02:24 +01:00
|
|
|
} else if (state is ScanAccountDirBlocFailure) {
|
2021-07-03 13:37:17 +02:00
|
|
|
_transformItems(state.files);
|
2021-05-28 21:44:09 +02:00
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(exception_util.toUserString(state.exception)),
|
2021-05-28 21:44:09 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
2021-11-16 21:02:24 +01:00
|
|
|
} else if (state is ScanAccountDirBlocInconsistent) {
|
2021-05-28 21:44:09 +02:00
|
|
|
_reqQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onSelectionAppBarUnarchivePressed() async {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global()
|
2021-05-28 21:44:09 +02:00
|
|
|
.unarchiveSelectedProcessingNotification(selectedListItems.length)),
|
|
|
|
duration: k.snackBarDurationShort,
|
|
|
|
));
|
2022-10-15 16:29:18 +02:00
|
|
|
final selection = 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();
|
|
|
|
});
|
2022-07-05 22:20:24 +02:00
|
|
|
final c = KiwiContainer().resolve<DiContainer>();
|
2022-10-15 16:29:18 +02:00
|
|
|
final selectedFiles =
|
|
|
|
await InflateFileDescriptor(c)(widget.account, selection);
|
2021-05-28 21:44:09 +02:00
|
|
|
final failures = <File>[];
|
|
|
|
for (final f in selectedFiles) {
|
|
|
|
try {
|
2024-01-12 19:18:23 +01:00
|
|
|
await UpdateProperty(c)
|
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(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global().unarchiveSelectedSuccessNotification),
|
2021-05-28 21:44:09 +02:00
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
SnackBarManager().showSnackBar(SnackBar(
|
2021-08-29 13:51:43 +02:00
|
|
|
content: Text(L10n.global()
|
2021-05-28 21:44:09 +02:00
|
|
|
.unarchiveSelectedFailureNotification(failures.length)),
|
|
|
|
duration: k.snackBarDurationNormal,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 16:29:18 +02:00
|
|
|
void _transformItems(List<FileDescriptor> 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() {
|
2021-11-16 21:02:24 +01:00
|
|
|
_bloc.add(const ScanAccountDirBlocQuery());
|
2021-05-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:02:24 +01:00
|
|
|
late final _bloc = ScanAccountDirBloc.of(widget.account);
|
2021-05-28 21:44:09 +02:00
|
|
|
|
2022-10-15 16:29:18 +02:00
|
|
|
var _backingFiles = <FileDescriptor>[];
|
2021-05-28 21:44:09 +02:00
|
|
|
|
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
|
|
|
}
|