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

368 lines
11 KiB
Dart
Raw Normal View History

2023-09-11 19:01:47 +02:00
import 'dart:async';
2022-07-25 07:51:52 +02:00
import 'package:collection/collection.dart';
2023-09-11 19:01:47 +02:00
import 'package:copy_with/copy_with.dart';
2021-10-06 22:32:36 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:kiwi/kiwi.dart';
2021-10-06 22:32:36 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
2024-06-17 18:04:53 +02:00
import 'package:nc_photos/bloc_util.dart';
2023-09-11 19:01:47 +02:00
import 'package:nc_photos/controller/account_controller.dart';
import 'package:nc_photos/controller/account_pref_controller.dart';
import 'package:nc_photos/controller/sharings_controller.dart';
import 'package:nc_photos/di_container.dart';
2021-10-12 12:15:58 +02:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/album/data_source.dart';
import 'package:nc_photos/entity/collection/builder.dart';
import 'package:nc_photos/entity/file.dart';
2021-10-12 12:15:58 +02:00
import 'package:nc_photos/entity/file/data_source.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/entity/share.dart';
2023-09-11 19:01:47 +02:00
import 'package:nc_photos/exception_event.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/object_extension.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/use_case/import_potential_shared_album.dart';
import 'package:nc_photos/widget/collection_browser.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/widget/empty_list_indicator.dart';
2022-12-18 07:20:51 +01:00
import 'package:nc_photos/widget/network_thumbnail.dart';
2023-09-11 19:01:47 +02:00
import 'package:nc_photos/widget/page_visibility_mixin.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/widget/shared_file_viewer.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2023-09-11 19:01:47 +02:00
import 'package:np_collection/np_collection.dart';
import 'package:np_common/or_null.dart';
import 'package:np_ui/np_ui.dart';
2023-09-11 19:01:47 +02:00
import 'package:to_string/to_string.dart';
2022-12-16 16:01:04 +01:00
part 'sharing_browser.g.dart';
2023-09-11 19:01:47 +02:00
part 'sharing_browser/bloc.dart';
part 'sharing_browser/state_event.dart';
part 'sharing_browser/type.dart';
typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
typedef _BlocListener = BlocListener<_Bloc, _State>;
// typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
2021-10-06 22:32:36 +02:00
class SharingBrowserArguments {
SharingBrowserArguments(this.account);
final Account account;
}
/// Show a list of all shares associated with this account
2023-09-11 19:01:47 +02:00
class SharingBrowser extends StatelessWidget {
2021-10-06 22:32:36 +02:00
static const routeName = "/sharing-browser";
2023-09-11 19:01:47 +02:00
static Route buildRoute() => MaterialPageRoute(
builder: (_) => const SharingBrowser(),
2021-10-06 22:32:36 +02:00
);
2023-09-11 19:01:47 +02:00
const SharingBrowser({super.key});
2021-10-06 22:32:36 +02:00
@override
2023-09-11 19:01:47 +02:00
Widget build(BuildContext context) {
final accountController = context.read<AccountController>();
return BlocProvider(
create: (_) => _Bloc(
account: accountController.account,
accountPrefController: accountController.accountPrefController,
sharingsController: accountController.sharingsController,
),
child: const _WrappedSharingBrowser(),
);
}
}
2021-10-06 22:32:36 +02:00
2023-09-11 19:01:47 +02:00
class _WrappedSharingBrowser extends StatefulWidget {
const _WrappedSharingBrowser();
@override
State<StatefulWidget> createState() => _WrappedSharingBrowserState();
2021-10-06 22:32:36 +02:00
}
2022-12-16 16:01:04 +01:00
@npLog
2023-09-11 19:01:47 +02:00
class _WrappedSharingBrowserState extends State<_WrappedSharingBrowser>
with RouteAware, PageVisibilityMixin {
2021-10-06 22:32:36 +02:00
@override
initState() {
super.initState();
2023-09-11 19:01:47 +02:00
_bloc.add(const _Init());
AccountPref.of(_bloc.account).run((obj) {
2023-05-17 19:42:08 +02:00
if (obj.hasNewSharedAlbumOr()) {
obj.setNewSharedAlbum(false);
}
});
2021-10-06 22:32:36 +02:00
}
@override
2023-09-11 19:01:47 +02:00
Widget build(BuildContext context) {
return MultiBlocListener(
listeners: [
_BlocListener(
listenWhen: (previous, current) => previous.items != current.items,
listener: (context, state) {
_bloc.add(_TransformItems(state.items));
},
),
_BlocListener(
listenWhen: (previous, current) => previous.error != current.error,
listener: (context, state) {
if (state.error != null && isPageVisible()) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(state.error!.error)),
duration: k.snackBarDurationNormal,
));
}
},
),
],
child: Scaffold(
body: _BlocBuilder(
buildWhen: (previous, current) =>
previous.items.isEmpty != current.items.isEmpty ||
previous.isLoading != current.isLoading,
builder: (context, state) {
if (state.items.isEmpty && !state.isLoading) {
return const _EmptyContentList();
} else {
return Stack(
children: [
CustomScrollView(
slivers: [
const _AppBar(),
SliverToBoxAdapter(
child: _BlocBuilder(
buildWhen: (previous, current) =>
previous.isLoading != current.isLoading,
builder: (context, state) => state.isLoading
? const LinearProgressIndicator()
: const SizedBox(height: 4),
),
),
const _ContentList(),
],
),
],
);
}
},
2021-10-06 22:32:36 +02:00
),
),
);
}
2023-09-11 19:01:47 +02:00
late final _bloc = context.read<_Bloc>();
}
2021-10-06 22:32:36 +02:00
2023-09-11 19:01:47 +02:00
class _AppBar extends StatelessWidget {
const _AppBar();
@override
Widget build(BuildContext context) {
return SliverAppBar(
title: Text(L10n.global().collectionSharingLabel),
floating: true,
);
2021-10-06 22:32:36 +02:00
}
2023-09-11 19:01:47 +02:00
}
2021-10-06 22:32:36 +02:00
2023-09-11 19:01:47 +02:00
class _EmptyContentList extends StatelessWidget {
const _EmptyContentList();
@override
Widget build(BuildContext context) {
2021-10-06 22:32:36 +02:00
return Column(
children: [
AppBar(
title: Text(L10n.global().collectionSharingLabel),
elevation: 0,
),
Expanded(
child: EmptyListIndicator(
icon: Icons.share_outlined,
text: L10n.global().listEmptyText,
),
),
],
);
}
2023-09-11 19:01:47 +02:00
}
2021-10-06 22:32:36 +02:00
2023-09-11 19:01:47 +02:00
class _ContentList extends StatelessWidget {
const _ContentList();
2021-10-06 22:32:36 +02:00
2023-09-11 19:01:47 +02:00
@override
Widget build(BuildContext context) {
return _BlocBuilder(
buildWhen: (previous, current) =>
previous.transformedItems != current.transformedItems,
builder: (_, state) => SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) =>
_buildItem(context, state.transformedItems[index]),
childCount: state.transformedItems.length,
),
),
2021-10-12 12:15:58 +02:00
);
}
2023-09-11 19:01:47 +02:00
Widget _buildItem(BuildContext context, _Item data) {
if (data is _FileShareItem) {
return _buildFileItem(context, data);
} else if (data is _AlbumShareItem) {
return _buildAlbumItem(context, data);
2021-10-12 12:15:58 +02:00
} else {
2023-09-11 19:01:47 +02:00
throw ArgumentError("Unknown item type: ${data.runtimeType}");
2021-10-06 22:32:36 +02:00
}
}
2023-09-11 19:01:47 +02:00
Widget _buildFileItem(BuildContext context, _FileShareItem item) {
return _FileTile(
account: item.account,
item: item,
isLinkShare: item.shares.any((e) => e.url?.isNotEmpty == true),
onTap: () {
Navigator.of(context).pushNamed(SharedFileViewer.routeName,
arguments: SharedFileViewerArguments(
item.account, item.file, item.shares));
},
);
2021-10-12 12:15:58 +02:00
}
2023-09-11 19:01:47 +02:00
Widget _buildAlbumItem(BuildContext context, _AlbumShareItem item) {
return _AlbumTile(
account: item.account,
item: item,
onTap: () {
Navigator.of(context).pushNamed(
CollectionBrowser.routeName,
arguments: CollectionBrowserArguments(
CollectionBuilder.byAlbum(item.account, item.album),
),
);
2023-09-11 19:01:47 +02:00
},
);
}
2021-10-06 22:32:36 +02:00
}
2021-10-11 19:06:49 +02:00
class _ListTile extends StatelessWidget {
const _ListTile({
required this.leading,
required this.label,
required this.description,
this.trailing,
2023-09-10 10:32:15 +02:00
this.onTap,
2021-10-11 19:06:49 +02:00
});
@override
2023-09-11 19:01:47 +02:00
Widget build(BuildContext context) {
return UnboundedListTile(
leading: leading,
title: Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
2021-10-11 19:06:49 +02:00
),
subtitle: Text(description),
trailing: trailing,
onTap: onTap,
2021-10-11 19:06:49 +02:00
);
}
final Widget leading;
final String label;
final String description;
final Widget? trailing;
2023-09-10 10:32:15 +02:00
final VoidCallback? onTap;
}
class _FileTile extends StatelessWidget {
const _FileTile({
required this.account,
required this.item,
required this.isLinkShare,
this.onTap,
});
@override
Widget build(BuildContext context) {
2023-09-11 19:01:47 +02:00
final dateStr = _getDateFormat(context).format(item.sharedTime!.toLocal());
2023-09-10 10:32:15 +02:00
return _ListTile(
2023-09-11 19:01:47 +02:00
leading: item.shares.first.itemType == ShareItemType.folder
2023-09-10 10:32:15 +02:00
? const SizedBox(
height: _leadingSize,
width: _leadingSize,
child: Icon(Icons.folder, size: 32),
)
: NetworkRectThumbnail(
account: account,
imageUrl:
NetworkRectThumbnail.imageUrlForFile(account, item.file),
dimension: _leadingSize,
errorBuilder: (_) => const Icon(Icons.folder, size: 32),
),
2023-09-11 19:01:47 +02:00
label: item.name,
description: item.sharedBy == null
2023-09-10 10:32:15 +02:00
? L10n.global().fileLastSharedDescription(dateStr)
2023-09-11 19:01:47 +02:00
: L10n.global()
.fileLastSharedByOthersDescription(item.sharedBy!, dateStr),
2023-09-10 10:32:15 +02:00
trailing: isLinkShare ? const Icon(Icons.link) : null,
onTap: onTap,
);
}
final Account account;
2023-09-11 19:01:47 +02:00
final _FileShareItem item;
2023-09-10 10:32:15 +02:00
final bool isLinkShare;
final VoidCallback? onTap;
}
class _AlbumTile extends StatelessWidget {
const _AlbumTile({
required this.account,
required this.item,
this.onTap,
});
@override
Widget build(BuildContext context) {
2023-09-11 19:01:47 +02:00
final dateStr = _getDateFormat(context).format(item.sharedTime!.toLocal());
2023-09-10 10:32:15 +02:00
final cover = item.album.coverProvider.getCover(item.album);
return _ListTile(
leading: cover == null
? const SizedBox(
height: _leadingSize,
width: _leadingSize,
child: Icon(Icons.photo_album, size: 32),
)
: NetworkRectThumbnail(
account: account,
imageUrl: NetworkRectThumbnail.imageUrlForFile(account, cover),
dimension: _leadingSize,
errorBuilder: (_) => const Icon(Icons.photo_album, size: 32),
),
label: item.album.name,
2023-09-11 19:01:47 +02:00
description: item.sharedBy == null
2023-09-10 10:32:15 +02:00
? L10n.global().fileLastSharedDescription(dateStr)
2023-09-11 19:01:47 +02:00
: L10n.global()
.albumLastSharedByOthersDescription(item.sharedBy!, dateStr),
2023-09-10 10:32:15 +02:00
trailing: const Icon(Icons.photo_album_outlined),
onTap: onTap,
);
}
final Account account;
2023-09-11 19:01:47 +02:00
final _AlbumShareItem item;
2023-09-10 10:32:15 +02:00
final VoidCallback? onTap;
2021-10-11 19:06:49 +02:00
}
2021-10-12 12:15:58 +02:00
const _leadingSize = 56.0;
DateFormat _getDateFormat(BuildContext context) => DateFormat(
DateFormat.YEAR_ABBR_MONTH_DAY,
Localizations.localeOf(context).languageCode);