ente/lib/ui/tabs/shared_collections_tab.dart

189 lines
6.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
2020-10-28 15:25:32 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
import 'package:photos/events/collection_updated_event.dart';
import 'package:photos/events/local_photos_updated_event.dart';
2021-03-17 21:07:17 +00:00
import 'package:photos/events/user_logged_out_event.dart';
2023-04-07 04:57:54 +00:00
import "package:photos/generated/l10n.dart";
2023-01-26 05:38:45 +00:00
import 'package:photos/models/collection.dart';
import 'package:photos/models/collection_items.dart';
2021-02-08 09:31:02 +00:00
import 'package:photos/services/collections_service.dart';
2023-06-23 13:43:15 +00:00
import "package:photos/ui/collections/horizontal_grid_view.dart";
import "package:photos/ui/collections/vertical_grid_view.dart";
import 'package:photos/ui/common/loading_widget.dart';
2023-06-23 13:43:15 +00:00
import "package:photos/ui/components/buttons/icon_button_widget.dart";
2023-06-06 16:43:32 +00:00
import 'package:photos/ui/tabs/section_title.dart';
import "package:photos/ui/tabs/shared/empty_state.dart";
import "package:photos/ui/tabs/shared/outgoing_album_item.dart";
2023-06-23 13:43:15 +00:00
import "package:photos/utils/navigation_util.dart";
2023-06-06 09:57:17 +00:00
class SharedCollectionsTab extends StatefulWidget {
const SharedCollectionsTab({Key? key}) : super(key: key);
@override
2023-06-06 09:57:17 +00:00
State<SharedCollectionsTab> createState() => _SharedCollectionsTabState();
}
2023-06-06 09:57:17 +00:00
class _SharedCollectionsTabState extends State<SharedCollectionsTab>
2020-11-10 11:36:51 +00:00
with AutomaticKeepAliveClientMixin {
final Logger _logger = Logger("SharedCollectionGallery");
late StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
late StreamSubscription<CollectionUpdatedEvent>
_collectionUpdatesSubscription;
late StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
@override
void initState() {
2022-07-03 09:45:00 +00:00
_localFilesSubscription =
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
debugPrint("SetState Shared Collections on ${event.reason}");
setState(() {});
});
2022-07-03 09:45:00 +00:00
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
debugPrint("SetState Shared Collections on ${event.reason}");
setState(() {});
});
2021-03-17 21:07:17 +00:00
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
2020-11-30 08:57:11 +00:00
super.build(context);
2020-11-02 14:38:59 +00:00
return FutureBuilder<SharedCollections>(
future: Future.value(_getSharedCollections()),
builder: (context, snapshot) {
if (snapshot.hasData) {
if ((snapshot.data?.incoming.length ?? 0) == 0 &&
(snapshot.data?.outgoing.length ?? 0) == 0) {
2023-06-06 16:43:32 +00:00
return const Center(child: SharedEmptyStateWidget());
}
return _getSharedCollectionsGallery(snapshot.data!);
} else if (snapshot.hasError) {
2023-01-25 14:44:44 +00:00
_logger.severe(
"critical: failed to load share gallery",
snapshot.error,
snapshot.stackTrace,
);
2023-04-08 04:05:16 +00:00
return Center(child: Text(S.of(context).somethingWentWrong));
} else {
return const EnteLoadingWidget();
}
},
);
}
2020-11-02 14:38:59 +00:00
Widget _getSharedCollectionsGallery(SharedCollections collections) {
const maxThumbnailWidth = 160.0;
const double horizontalPaddingOfGridRow = 16;
const double crossAxisSpacingOfGrid = 9;
2022-08-29 14:43:31 +00:00
final Size size = MediaQuery.of(context).size;
final int albumsCountInOneRow = max(size.width ~/ 220.0, 2);
final double totalWhiteSpaceOfRow = (horizontalPaddingOfGridRow * 2) +
2022-07-03 09:45:00 +00:00
(albumsCountInOneRow - 1) * crossAxisSpacingOfGrid;
final double sideOfThumbnail = (size.width / albumsCountInOneRow) -
(totalWhiteSpaceOfRow / albumsCountInOneRow);
2020-11-02 14:38:59 +00:00
return SingleChildScrollView(
child: Container(
margin: const EdgeInsets.only(bottom: 50),
child: Column(
children: [
const SizedBox(height: 12),
2023-06-23 13:43:15 +00:00
SectionTitleRow(
SectionTitle(title: S.of(context).sharedWithMe),
trailingWidget: collections.incoming.isNotEmpty
? IconButtonWidget(
icon: Icons.chevron_right,
iconButtonType: IconButtonType.secondary,
onTap: () {
unawaited(
routeToPage(
context,
CollectionVerticalGridView(
collections.incoming,
appTitle: SectionTitle(
title: S.of(context).sharedWithMe,
),
),
),
2022-06-11 09:11:08 +00:00
);
},
2023-06-23 13:43:15 +00:00
)
: null,
),
const SizedBox(height: 12),
collections.incoming.isNotEmpty
? CollectionsHorizontalGridView(collections.incoming)
2023-06-06 16:43:32 +00:00
: const IncomingAlbumEmptyState(),
2023-03-07 14:02:35 +00:00
const SizedBox(height: 16),
2023-04-07 04:57:54 +00:00
SectionTitle(title: S.of(context).sharedByMe),
const SizedBox(height: 12),
2022-06-19 09:24:40 +00:00
collections.outgoing.isNotEmpty
2022-06-11 09:21:39 +00:00
? ListView.builder(
shrinkWrap: true,
2022-07-04 06:02:17 +00:00
padding: const EdgeInsets.only(bottom: 12),
physics: const NeverScrollableScrollPhysics(),
2022-06-11 09:21:39 +00:00
itemBuilder: (context, index) {
2023-06-06 16:43:32 +00:00
return OutgoingAlbumItem(
c: collections.outgoing[index],
2022-06-11 09:21:39 +00:00
);
},
itemCount: collections.outgoing.length,
)
2023-06-06 16:43:32 +00:00
: const OutgoingAlbumEmptyState(),
const SizedBox(height: 32),
],
),
2020-11-02 14:38:59 +00:00
),
);
}
SharedCollections _getSharedCollections() {
final List<Collection> outgoing = [];
final List<Collection> incoming = [];
final List<Collection> collections =
CollectionsService.instance.getCollectionsForUI(includedShared: true);
for (final c in collections) {
2023-06-06 16:43:32 +00:00
if (c.owner!.id == Configuration.instance.getUserID()) {
if (c.hasSharees || c.hasLink || c.isSharedFilesCollection()) {
outgoing.add(c);
2023-06-06 16:43:32 +00:00
}
} else {
incoming.add(c);
2023-06-06 16:43:32 +00:00
}
}
outgoing.sort((first, second) {
if (second.isSharedFilesCollection() == first.isSharedFilesCollection()) {
return second.updationTime.compareTo(first.updationTime);
2023-06-06 16:43:32 +00:00
} else {
if (first.isSharedFilesCollection()) {
2023-06-06 16:43:32 +00:00
return 1;
}
return -1;
}
});
incoming.sort((first, second) {
return second.updationTime.compareTo(first.updationTime);
2023-06-06 16:43:32 +00:00
});
return SharedCollections(outgoing, incoming);
}
@override
void dispose() {
_localFilesSubscription.cancel();
_collectionUpdatesSubscription.cancel();
_loggedOutEvent.cancel();
super.dispose();
}
@override
bool get wantKeepAlive => true;
}