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

194 lines
6.8 KiB
Dart
Raw Normal View History

2024-01-12 20:29:47 +01:00
import 'dart:async';
2022-06-06 19:37:46 +02:00
2024-01-12 20:29:47 +01:00
import 'package:collection/collection.dart';
import 'package:copy_with/copy_with.dart';
2021-05-28 21:44:09 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2024-01-12 20:29:47 +01: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';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2024-01-12 20:29:47 +01:00
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/controller/account_controller.dart';
import 'package:nc_photos/controller/files_controller.dart';
import 'package:nc_photos/controller/pref_controller.dart';
import 'package:nc_photos/entity/file_descriptor.dart';
2024-01-12 20:29:47 +01:00
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/flutter_util.dart' as flutter_util;
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/snack_bar_manager.dart';
2021-08-02 13:21:27 +02:00
import 'package:nc_photos/widget/empty_list_indicator.dart';
2024-01-12 20:29:47 +01:00
import 'package:nc_photos/widget/finger_listener.dart';
import 'package:nc_photos/widget/network_thumbnail.dart';
import 'package:nc_photos/widget/page_visibility_mixin.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;
2024-01-12 20:29:47 +01:00
import 'package:nc_photos/widget/selectable_item_list.dart';
2021-07-25 16:27:19 +02:00
import 'package:nc_photos/widget/selection_app_bar.dart';
2024-01-12 20:29:47 +01:00
import 'package:nc_photos/widget/sliver_visualized_scale.dart';
2021-05-28 21:44:09 +02:00
import 'package:nc_photos/widget/viewer.dart';
import 'package:np_async/np_async.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2024-01-12 20:29:47 +01:00
import 'package:np_common/or_null.dart';
import 'package:to_string/to_string.dart';
2022-12-16 16:01:04 +01:00
part 'archive_browser.g.dart';
2024-01-12 20:29:47 +01:00
part 'archive_browser/app_bar.dart';
part 'archive_browser/bloc.dart';
part 'archive_browser/state_event.dart';
part 'archive_browser/type.dart';
part 'archive_browser/view.dart';
2021-05-28 21:44:09 +02:00
2024-01-12 20:29:47 +01:00
class ArchiveBrowser extends StatelessWidget {
2021-07-31 19:02:41 +02:00
static const routeName = "/archive-browser";
2021-05-28 21:44:09 +02:00
2024-01-12 20:29:47 +01:00
static Route buildRoute() => MaterialPageRoute(
builder: (_) => const ArchiveBrowser(),
2021-07-23 22:05:57 +02:00
);
2024-01-12 20:29:47 +01:00
const ArchiveBrowser({super.key});
2021-05-28 21:44:09 +02:00
@override
2024-01-12 20:29:47 +01:00
Widget build(BuildContext context) {
final accountController = context.read<AccountController>();
return BlocProvider(
create: (_) => _Bloc(
account: accountController.account,
filesController: accountController.filesController,
2024-01-12 20:29:47 +01:00
prefController: context.read(),
),
child: const _WrappedArchiveBrowser(),
);
}
}
class _WrappedArchiveBrowser extends StatefulWidget {
const _WrappedArchiveBrowser();
2021-05-28 21:44:09 +02:00
2024-01-12 20:29:47 +01:00
@override
State<StatefulWidget> createState() => _WrappedArchiveBrowserState();
2021-05-28 21:44:09 +02:00
}
2022-12-16 16:01:04 +01:00
@npLog
2024-01-12 20:29:47 +01:00
class _WrappedArchiveBrowserState extends State<_WrappedArchiveBrowser>
with RouteAware, PageVisibilityMixin {
2021-05-28 21:44:09 +02:00
@override
2024-01-12 20:29:47 +01:00
void initState() {
2021-05-28 21:44:09 +02:00
super.initState();
2024-01-12 20:29:47 +01:00
_bloc.add(const _LoadItems());
2021-05-28 21:44:09 +02:00
}
@override
2024-01-12 20:29:47 +01:00
Widget build(BuildContext context) {
2022-11-12 10:55:33 +01:00
return Scaffold(
2024-01-12 20:29:47 +01:00
body: MultiBlocListener(
listeners: [
_BlocListenerT<List<FileDescriptor>>(
selector: (state) => state.files,
listener: (context, files) {
_bloc.add(_TransformItems(files));
},
2021-08-02 13:21:27 +02:00
),
2024-01-12 20:29:47 +01:00
_BlocListenerT<ExceptionEvent?>(
selector: (state) => state.error,
listener: (context, error) {
if (error != null && isPageVisible()) {
if (error.error is _UnarchiveFailedError) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global()
.unarchiveSelectedFailureNotification(
(error.error as _UnarchiveFailedError).count)),
duration: k.snackBarDurationNormal,
));
} else {
2024-06-19 08:58:08 +02:00
SnackBarManager().showSnackBarForException(error.error);
2024-01-12 20:29:47 +01:00
}
}
},
2021-08-02 13:21:27 +02:00
),
],
2024-01-12 20:29:47 +01:00
child: FingerListener(
onFingerChanged: (finger) {
setState(() {
_finger = finger;
});
},
child: GestureDetector(
onScaleStart: (_) {
_bloc.add(const _StartScaling());
},
onScaleUpdate: (details) {
_bloc.add(_SetScale(details.scale));
},
onScaleEnd: (_) {
_bloc.add(const _EndScaling());
},
2022-11-12 10:55:33 +01:00
child: CustomScrollView(
2024-01-12 20:29:47 +01:00
physics:
_finger >= 2 ? const NeverScrollableScrollPhysics() : null,
2022-11-12 10:55:33 +01:00
slivers: [
2024-01-12 20:29:47 +01:00
_BlocSelector<bool>(
selector: (state) => state.selectedItems.isEmpty,
builder: (context, isEmpty) =>
isEmpty ? const _AppBar() : const _SelectionAppBar(),
),
SliverToBoxAdapter(
child: _BlocSelector<bool>(
selector: (state) => state.isLoading,
builder: (context, isLoading) => isLoading
? const LinearProgressIndicator()
: const SizedBox(height: 4),
),
),
_BlocBuilder(
buildWhen: (previous, current) =>
previous.transformedItems.isEmpty !=
current.transformedItems.isEmpty ||
previous.isLoading != current.isLoading,
builder: (context, state) => state.transformedItems.isEmpty &&
!state.isLoading
? SliverFillRemaining(
hasScrollBody: false,
child: EmptyListIndicator(
icon: Icons.archive_outlined,
text: L10n.global().listEmptyText,
),
)
: _BlocSelector<double?>(
selector: (state) => state.scale,
builder: (context, scale) => SliverTransitionedScale(
scale: scale,
baseSliver: const _ContentList(),
overlaySliver: const _ScalingList(),
),
),
2022-11-12 10:55:33 +01:00
),
],
2021-05-28 21:44:09 +02:00
),
),
),
2022-06-06 19:37:46 +02:00
),
);
2021-05-28 21:44:09 +02:00
}
2024-01-12 20:29:47 +01:00
late final _bloc = context.bloc;
2021-05-28 21:44:09 +02:00
2024-01-12 20:29:47 +01:00
var _finger = 0;
}
2021-05-28 21:44:09 +02:00
2024-01-12 20:29:47 +01:00
typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
// typedef _BlocListener = BlocListener<_Bloc, _State>;
typedef _BlocListenerT<T> = BlocListenerT<_Bloc, _State, T>;
typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
2022-06-06 19:37:46 +02:00
2024-01-12 20:29:47 +01:00
extension on BuildContext {
_Bloc get bloc => read<_Bloc>();
// _State get state => bloc.state;
void addEvent(_Event event) => bloc.add(event);
2021-05-28 21:44:09 +02:00
}
2024-01-12 20:29:47 +01:00
@npLog
// ignore: camel_case_types
class __ {}