Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Give list of streams to listen to for each search section and listen and rebuild sections when it's events are fired

ashilknc9a53ac66c39
diff --git a/lib/models/search/search_types.dart b/lib/models/search/search_types.dart
index 78caf736a9..a0d12504d2 100644
--- a/lib/models/search/search_types.dart
+++ b/lib/models/search/search_types.dart
@@ -1,5 +1,9 @@
import "package:flutter/material.dart";
import "package:logging/logging.dart";
+import "package:photos/core/event_bus.dart";
+import "package:photos/events/collection_updated_event.dart";
+import "package:photos/events/event.dart";
+import "package:photos/events/location_tag_updated_event.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/models/collection/collection.dart";
import "package:photos/models/collection/collection_items.dart";
@@ -297,4 +301,15 @@ extension SectionTypeExtensions on SectionType {
return SearchService.instance.getAllDescriptionSearchResults(limit);
}
}
+
+ List<Stream<Event>> updateEvents() {
+ switch (this) {
+ case SectionType.location:
+ return [Bus.instance.on<LocationTagUpdatedEvent>()];
+ case SectionType.album:
+ return [Bus.instance.on<CollectionUpdatedEvent>()];
+ default:
+ return [];
+ }
+ }
}
diff --git a/lib/ui/viewer/search/search_section.dart b/lib/ui/viewer/search/search_section.dart
index d01a3c6bcd..5e7de5e9b3 100644
--- a/lib/ui/viewer/search/search_section.dart
+++ b/lib/ui/viewer/search/search_section.dart
@@ -1,5 +1,8 @@
+import "dart:async";
+
import "package:collection/collection.dart";
import "package:flutter/material.dart";
+import "package:photos/events/event.dart";
import "package:photos/models/search/album_search_result.dart";
import "package:photos/models/search/generic_search_result.dart";
import "package:photos/models/search/recent_searches.dart";
@@ -15,7 +18,7 @@ import 'package:photos/ui/viewer/search/result/search_section_all_page.dart';
import "package:photos/ui/viewer/search/search_section_cta.dart";
import "package:photos/utils/navigation_util.dart";
-class SearchSection extends StatelessWidget {
+class SearchSection extends StatefulWidget {
final SectionType sectionType;
final List<SearchResult> examples;
final int limit;
@@ -27,13 +30,45 @@ class SearchSection extends StatelessWidget {
required this.limit,
}) : super(key: key);
+ @override
+ State<SearchSection> createState() => _SearchSectionState();
+}
+
+class _SearchSectionState extends State<SearchSection> {
+ late List<SearchResult> _examples;
+ final streamSubscriptions = <StreamSubscription>[];
+
+ @override
+ void initState() {
+ super.initState();
+ _examples = widget.examples;
+
+ final streamsToListenTo = widget.sectionType.updateEvents();
+ for (Stream<Event> stream in streamsToListenTo) {
+ streamSubscriptions.add(
+ stream.listen((event) async {
+ _examples = await widget.sectionType.getData();
+ setState(() {});
+ }),
+ );
+ }
+ }
+
+ @override
+ void dispose() {
+ for (var subscriptions in streamSubscriptions) {
+ subscriptions.cancel();
+ }
+ super.dispose();
+ }
+
@override
Widget build(BuildContext context) {
- debugPrint("Building section for ${sectionType.name}");
+ debugPrint("Building section for ${widget.sectionType.name}");
final textTheme = getEnteTextTheme(context);
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
- child: examples.isNotEmpty
+ child: widget.examples.isNotEmpty
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
@@ -43,18 +78,18 @@ class SearchSection extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(12),
child: Text(
- sectionType.sectionTitle(context),
+ widget.sectionType.sectionTitle(context),
style: textTheme.largeBold,
),
),
- examples.length < (limit - 1)
+ widget.examples.length < (widget.limit - 1)
? const SizedBox.shrink()
: GestureDetector(
onTap: () {
routeToPage(
context,
SearchSectionAllPage(
- sectionType: sectionType,
+ sectionType: widget.sectionType,
),
);
},
@@ -69,7 +104,7 @@ class SearchSection extends StatelessWidget {
],
),
const SizedBox(height: 2),
- SearchExampleRow(examples, sectionType),
+ SearchExampleRow(_examples, widget.sectionType),
],
)
: Padding(
@@ -83,12 +118,12 @@ class SearchSection extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
- sectionType.sectionTitle(context),
+ widget.sectionType.sectionTitle(context),
style: textTheme.largeBold,
),
const SizedBox(height: 24),
Text(
- sectionType.getEmptyStateText(context),
+ widget.sectionType.getEmptyStateText(context),
style: textTheme.smallMuted,
),
],
@@ -96,7 +131,7 @@ class SearchSection extends StatelessWidget {
),
),
const SizedBox(width: 8),
- SearchSectionEmptyCTAIcon(sectionType),
+ SearchSectionEmptyCTAIcon(widget.sectionType),
],
),
),