ente/lib/ui/collections/collection_list_page.dart

105 lines
2.9 KiB
Dart
Raw Normal View History

import "dart:async";
import 'package:flutter/material.dart';
import "package:photos/core/event_bus.dart";
import "package:photos/events/collection_updated_event.dart";
import "package:photos/models/collection.dart";
import "package:photos/models/collection_items.dart";
import "package:photos/services/collections_service.dart";
import "package:photos/ui/collections/flex_grid_view.dart";
enum UISectionType {
incomingCollections,
outgoingCollections,
homeCollections,
}
class CollectionListPage extends StatefulWidget {
final List<Collection>? collections;
final Widget? appTitle;
2023-07-26 10:19:11 +00:00
final double? initialScrollOffset;
final String tag;
final UISectionType sectionType;
const CollectionListPage(
this.collections, {
2023-07-26 10:18:45 +00:00
required this.sectionType,
this.appTitle,
2023-07-26 10:19:11 +00:00
this.initialScrollOffset,
this.tag = "",
Key? key,
}) : super(key: key);
@override
State<CollectionListPage> createState() => _CollectionListPageState();
}
class _CollectionListPageState extends State<CollectionListPage> {
late StreamSubscription<CollectionUpdatedEvent>
_collectionUpdatesSubscription;
List<Collection>? collections;
@override
void initState() {
super.initState();
collections = widget.collections;
_collectionUpdatesSubscription =
Bus.instance.on<CollectionUpdatedEvent>().listen((event) async {
2023-07-26 10:15:58 +00:00
refreshCollections();
});
}
@override
void dispose() {
_collectionUpdatesSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: CustomScrollView(
controller: ScrollController(
2023-07-26 10:19:11 +00:00
initialScrollOffset: widget.initialScrollOffset ?? 0,
),
slivers: [
SliverAppBar(
elevation: 0,
2023-07-19 05:08:40 +00:00
title: Hero(
tag: widget.tag,
child: widget.appTitle ?? const SizedBox.shrink(),
2023-07-19 05:08:40 +00:00
),
floating: true,
),
CollectionsFlexiGridViewWidget(
collections,
displayLimitCount: collections?.length ?? 0,
tag: widget.tag,
),
],
),
),
);
}
Future<void> refreshCollections() async {
2023-07-26 10:15:58 +00:00
if (widget.sectionType == UISectionType.incomingCollections ||
widget.sectionType == UISectionType.outgoingCollections) {
final SharedCollections sharedCollections =
CollectionsService.instance.getSharedCollections();
if (widget.sectionType == UISectionType.incomingCollections) {
2023-07-26 10:15:58 +00:00
collections = sharedCollections.incoming;
} else {
collections = sharedCollections.outgoing;
}
2023-07-26 10:15:58 +00:00
} else if (widget.sectionType == UISectionType.homeCollections) {
collections =
await CollectionsService.instance.getCollectionForOnEnteSection();
}
if (mounted) {
setState(() {});
}
}
}