2020-10-13 21:40:22 +00:00
|
|
|
import 'dart:async';
|
2022-04-23 06:29:46 +00:00
|
|
|
import 'dart:math';
|
2020-10-13 21:40:22 +00:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2020-10-28 15:25:32 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2020-10-13 21:40:22 +00:00
|
|
|
import 'package:photos/core/event_bus.dart';
|
2020-10-31 17:59:47 +00:00
|
|
|
import 'package:photos/events/collection_updated_event.dart';
|
2021-05-04 15:26:17 +00:00
|
|
|
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';
|
2020-10-29 00:30:02 +00:00
|
|
|
import 'package:photos/models/collection_items.dart';
|
2021-02-08 09:31:02 +00:00
|
|
|
import 'package:photos/services/collections_service.dart';
|
2022-07-01 14:18:05 +00:00
|
|
|
import 'package:photos/ui/common/loading_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/incoming_album_item.dart";
|
|
|
|
import "package:photos/ui/tabs/shared/outgoing_album_item.dart";
|
2020-10-13 21:40:22 +00:00
|
|
|
|
2023-06-06 09:57:17 +00:00
|
|
|
class SharedCollectionsTab extends StatefulWidget {
|
|
|
|
const SharedCollectionsTab({Key? key}) : super(key: key);
|
2020-10-13 21:40:22 +00:00
|
|
|
|
|
|
|
@override
|
2023-06-06 09:57:17 +00:00
|
|
|
State<SharedCollectionsTab> createState() => _SharedCollectionsTabState();
|
2020-10-13 21:40:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 09:57:17 +00:00
|
|
|
class _SharedCollectionsTabState extends State<SharedCollectionsTab>
|
2020-11-10 11:36:51 +00:00
|
|
|
with AutomaticKeepAliveClientMixin {
|
2021-10-30 06:18:45 +00:00
|
|
|
final Logger _logger = Logger("SharedCollectionGallery");
|
2022-12-30 12:10:17 +00:00
|
|
|
late StreamSubscription<LocalPhotosUpdatedEvent> _localFilesSubscription;
|
2023-01-07 12:45:15 +00:00
|
|
|
late StreamSubscription<CollectionUpdatedEvent>
|
|
|
|
_collectionUpdatesSubscription;
|
2022-12-30 12:10:17 +00:00
|
|
|
late StreamSubscription<UserLoggedOutEvent> _loggedOutEvent;
|
2020-10-13 21:40:22 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2022-07-03 09:45:00 +00:00
|
|
|
_localFilesSubscription =
|
|
|
|
Bus.instance.on<LocalPhotosUpdatedEvent>().listen((event) {
|
2022-11-11 11:30:37 +00:00
|
|
|
debugPrint("SetState Shared Collections on ${event.reason}");
|
2021-05-04 15:26:17 +00:00
|
|
|
setState(() {});
|
|
|
|
});
|
2022-07-03 09:45:00 +00:00
|
|
|
_collectionUpdatesSubscription =
|
|
|
|
Bus.instance.on<CollectionUpdatedEvent>().listen((event) {
|
2022-11-11 11:30:37 +00:00
|
|
|
debugPrint("SetState Shared Collections on ${event.reason}");
|
2020-10-31 17:59:47 +00:00
|
|
|
setState(() {});
|
2020-10-13 21:40:22 +00:00
|
|
|
});
|
2021-03-17 21:07:17 +00:00
|
|
|
_loggedOutEvent = Bus.instance.on<UserLoggedOutEvent>().listen((event) {
|
|
|
|
setState(() {});
|
|
|
|
});
|
2020-10-13 21:40:22 +00:00
|
|
|
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>(
|
2023-06-14 08:57:33 +00:00
|
|
|
future: Future.value(
|
|
|
|
CollectionsService.instance
|
|
|
|
.getCollectionsWithThumbnails(includedOwnedByOthers: true),
|
2023-06-14 10:42:46 +00:00
|
|
|
).then((collections) async {
|
|
|
|
return _getSharedCollections(collections);
|
2020-10-13 21:40:22 +00:00
|
|
|
}),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
2023-02-24 07:50:35 +00:00
|
|
|
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());
|
2023-02-24 07:50:35 +00:00
|
|
|
}
|
2022-12-30 12:10:17 +00:00
|
|
|
return _getSharedCollectionsGallery(snapshot.data!);
|
2020-10-13 21:40:22 +00:00
|
|
|
} 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));
|
2020-10-13 21:40:22 +00:00
|
|
|
} else {
|
2022-07-03 06:04:42 +00:00
|
|
|
return const EnteLoadingWidget();
|
2020-10-13 21:40:22 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:38:59 +00:00
|
|
|
Widget _getSharedCollectionsGallery(SharedCollections collections) {
|
2022-06-10 09:05:35 +00:00
|
|
|
const double horizontalPaddingOfGridRow = 16;
|
2022-06-12 14:22:05 +00:00
|
|
|
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(
|
2021-02-08 09:26:49 +00:00
|
|
|
child: Container(
|
|
|
|
margin: const EdgeInsets.only(bottom: 50),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-06-10 09:05:35 +00:00
|
|
|
const SizedBox(height: 12),
|
2023-04-07 04:57:54 +00:00
|
|
|
SectionTitle(title: S.of(context).sharedWithMe),
|
2022-06-12 16:39:28 +00:00
|
|
|
const SizedBox(height: 12),
|
2022-06-19 09:24:40 +00:00
|
|
|
collections.incoming.isNotEmpty
|
2022-06-10 09:05:35 +00:00
|
|
|
? Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
child: GridView.builder(
|
|
|
|
shrinkWrap: true,
|
2023-06-06 16:43:32 +00:00
|
|
|
scrollDirection: Axis.vertical,
|
2022-07-04 06:02:17 +00:00
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
2022-06-10 09:05:35 +00:00
|
|
|
itemBuilder: (context, index) {
|
2023-06-06 16:43:32 +00:00
|
|
|
return IncomingAlbumItem(
|
2022-06-11 09:11:08 +00:00
|
|
|
collections.incoming[index],
|
|
|
|
);
|
2022-06-10 09:05:35 +00:00
|
|
|
},
|
|
|
|
itemCount: collections.incoming.length,
|
|
|
|
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
|
2022-06-11 09:04:39 +00:00
|
|
|
crossAxisCount: albumsCountInOneRow,
|
|
|
|
mainAxisSpacing: 12,
|
|
|
|
crossAxisSpacing: crossAxisSpacingOfGrid,
|
2022-07-03 09:45:00 +00:00
|
|
|
childAspectRatio:
|
|
|
|
sideOfThumbnail / (sideOfThumbnail + 24),
|
2022-06-11 09:04:39 +00:00
|
|
|
), //24 is height of album title
|
2021-02-08 09:26:49 +00:00
|
|
|
),
|
|
|
|
)
|
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),
|
2022-06-15 11:29:00 +00:00
|
|
|
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,
|
2021-02-08 09:26:49 +00:00
|
|
|
)
|
2023-06-06 16:43:32 +00:00
|
|
|
: const OutgoingAlbumEmptyState(),
|
2022-06-10 09:05:35 +00:00
|
|
|
const SizedBox(height: 32),
|
2021-02-08 09:26:49 +00:00
|
|
|
],
|
|
|
|
),
|
2020-11-02 14:38:59 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 08:57:33 +00:00
|
|
|
SharedCollections _getSharedCollections(
|
|
|
|
List<CollectionWithThumbnail> allCollections,
|
|
|
|
) {
|
2023-06-06 16:43:32 +00:00
|
|
|
final List<CollectionWithThumbnail> outgoing = [];
|
|
|
|
final List<CollectionWithThumbnail> incoming = [];
|
2023-06-14 08:57:33 +00:00
|
|
|
for (final collectionWithThumbnail in allCollections) {
|
|
|
|
final Collection c = collectionWithThumbnail.collection;
|
|
|
|
|
2023-06-06 16:43:32 +00:00
|
|
|
if (c.owner!.id == Configuration.instance.getUserID()) {
|
|
|
|
if (c.hasSharees || c.hasLink || c.isSharedFilesCollection()) {
|
2023-06-14 08:57:33 +00:00
|
|
|
outgoing.add(collectionWithThumbnail);
|
2023-06-06 16:43:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-14 08:57:33 +00:00
|
|
|
incoming.add(collectionWithThumbnail);
|
2023-06-06 16:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
outgoing.sort((first, second) {
|
|
|
|
if (second.collection.isSharedFilesCollection() ==
|
|
|
|
first.collection.isSharedFilesCollection()) {
|
|
|
|
return second.collection.updationTime
|
|
|
|
.compareTo(first.collection.updationTime);
|
|
|
|
} else {
|
|
|
|
if (first.collection.isSharedFilesCollection()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
incoming.sort((first, second) {
|
|
|
|
return second.collection.updationTime
|
|
|
|
.compareTo(first.collection.updationTime);
|
|
|
|
});
|
|
|
|
return SharedCollections(outgoing, incoming);
|
2021-04-27 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-05-04 15:26:17 +00:00
|
|
|
_localFilesSubscription.cancel();
|
|
|
|
_collectionUpdatesSubscription.cancel();
|
2021-04-27 15:35:03 +00:00
|
|
|
_loggedOutEvent.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool get wantKeepAlive => true;
|
|
|
|
}
|