Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refactor + fix last commit in a better way

ashilkn98ef5189a917
diff --git a/lib/states/all_sections_examples_state.dart b/lib/states/all_sections_examples_state.dart
new file mode 100644
index 0000000000..cc3f4757e6
--- /dev/null
+++ b/lib/states/all_sections_examples_state.dart
@@ -0,0 +1,92 @@
+import "dart:async";
+
+import "package:flutter/material.dart";
+import "package:flutter/scheduler.dart";
+import "package:photos/core/constants.dart";
+import "package:photos/core/event_bus.dart";
+import "package:photos/events/sync_status_update_event.dart";
+import "package:photos/models/search/search_result.dart";
+import "package:photos/models/search/search_types.dart";
+
+class AllSectionsExamplesProvider extends StatefulWidget {
+ final Widget child;
+ const AllSectionsExamplesProvider({super.key, required this.child});
+
+ @override
+ State<AllSectionsExamplesProvider> createState() =>
+ _AllSectionsExamplesProviderState();
+}
+
+class _AllSectionsExamplesProviderState
+ extends State<AllSectionsExamplesProvider> {
+ Future<List<List<SearchResult>>> allSectionsExamplesFuture = Future.value([]);
+
+ final allSectionsExamples = <Future<List<SearchResult>>>[];
+ late StreamSubscription<SyncStatusUpdate> _syncStatusSubscription;
+ @override
+ void initState() {
+ super.initState();
+ SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
+ _syncStatusSubscription =
+ Bus.instance.on<SyncStatusUpdate>().listen((event) {
+ if (event.status == SyncStatus.completedBackup) {
+ setState(() {
+ allSectionsExamples.clear();
+ aggregateSectionsExamples();
+ });
+ }
+ });
+ setState(() {
+ aggregateSectionsExamples();
+ });
+ });
+ }
+
+ void aggregateSectionsExamples() {
+ for (SectionType sectionType in SectionType.values) {
+ if (sectionType == SectionType.face ||
+ sectionType == SectionType.content) {
+ continue;
+ }
+ allSectionsExamples.add(
+ sectionType.getData(limit: searchSectionLimit, context: context),
+ );
+ }
+ allSectionsExamplesFuture =
+ Future.wait<List<SearchResult>>(allSectionsExamples);
+ }
+
+ @override
+ void dispose() {
+ _syncStatusSubscription.cancel();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return InheritedAllSectionsExamples(
+ allSectionsExamplesFuture,
+ child: widget.child,
+ );
+ }
+}
+
+class InheritedAllSectionsExamples extends InheritedWidget {
+ final Future<List<List<SearchResult>>> allSectionsExamplesFuture;
+ const InheritedAllSectionsExamples(
+ this.allSectionsExamplesFuture, {
+ super.key,
+ required super.child,
+ });
+
+ static InheritedAllSectionsExamples of(BuildContext context) {
+ return context
+ .dependOnInheritedWidgetOfExactType<InheritedAllSectionsExamples>()!;
+ }
+
+ @override
+ bool updateShouldNotify(covariant InheritedAllSectionsExamples oldWidget) {
+ return oldWidget.allSectionsExamplesFuture.hashCode !=
+ allSectionsExamplesFuture.hashCode;
+ }
+}
diff --git a/lib/ui/search_tab.dart b/lib/ui/search_tab.dart
index 4681b58d29..90ee862145 100644
--- a/lib/ui/search_tab.dart
+++ b/lib/ui/search_tab.dart
@@ -1,12 +1,8 @@
-import "dart:async";
-
import "package:flutter/material.dart";
-import "package:flutter/scheduler.dart";
import "package:photos/core/constants.dart";
-import "package:photos/core/event_bus.dart";
-import "package:photos/events/sync_status_update_event.dart";
import "package:photos/models/search/search_result.dart";
import "package:photos/models/search/search_types.dart";
+import "package:photos/states/all_sections_examples_state.dart";
import "package:photos/states/search_results_state.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/viewer/search/result/no_result_widget.dart";
@@ -15,8 +11,6 @@ import "package:photos/ui/viewer/search/search_suggestions.dart";
import "package:photos/ui/viewer/search/search_widget_new.dart";
import "package:photos/ui/viewer/search/tab_empty_state.dart";
-Future<List<List<SearchResult>>> allSectionsExamplesFuture = Future.value([]);
-
class SearchTab extends StatefulWidget {
const SearchTab({Key? key}) : super(key: key);
@@ -47,13 +41,15 @@ class _SearchTabState extends State<SearchTab> {
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 8),
- child: IndexedStack(
- index: index,
- children: [
- const AllSearchSections(),
- SearchSuggestionsWidget(_searchResults),
- const NoResultWidget(),
- ],
+ child: AllSectionsExamplesProvider(
+ child: IndexedStack(
+ index: index,
+ children: [
+ const AllSearchSections(),
+ SearchSuggestionsWidget(_searchResults),
+ const NoResultWidget(),
+ ],
+ ),
),
);
}
@@ -67,46 +63,9 @@ class AllSearchSections extends StatefulWidget {
}
class _AllSearchSectionsState extends State<AllSearchSections> {
- final allSectionsExamples = <Future<List<SearchResult>>>[];
- late StreamSubscription<SyncStatusUpdate> _syncStatusSubscription;
-
@override
- void initState() {
- super.initState();
- SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
- _syncStatusSubscription =
- Bus.instance.on<SyncStatusUpdate>().listen((event) {
- if (event.status == SyncStatus.completedBackup) {
- setState(() {
- allSectionsExamples.clear();
- aggregateSectionsExamples();
- });
- }
- });
- setState(() {
- aggregateSectionsExamples();
- });
- });
- }
-
- void aggregateSectionsExamples() {
- for (SectionType sectionType in SectionType.values) {
- if (sectionType == SectionType.face ||
- sectionType == SectionType.content) {
- continue;
- }
- allSectionsExamples.add(
- sectionType.getData(limit: searchSectionLimit, context: context),
- );
- }
- allSectionsExamplesFuture =
- Future.wait<List<SearchResult>>(allSectionsExamples);
- }
-
- @override
- void dispose() {
- _syncStatusSubscription.cancel();
- super.dispose();
+ void didChangeDependencies() {
+ super.didChangeDependencies();
}
@override
@@ -116,7 +75,8 @@ class _AllSearchSectionsState extends State<AllSearchSections> {
searchTypes.remove(SectionType.face);
searchTypes.remove(SectionType.content);
return FutureBuilder(
- future: allSectionsExamplesFuture,
+ future:
+ InheritedAllSectionsExamples.of(context).allSectionsExamplesFuture,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data!.every((element) => element.isEmpty)) {
diff --git a/lib/ui/viewer/search/result/no_result_widget.dart b/lib/ui/viewer/search/result/no_result_widget.dart
index b37aa8ca21..729f25b2b4 100644
--- a/lib/ui/viewer/search/result/no_result_widget.dart
+++ b/lib/ui/viewer/search/result/no_result_widget.dart
@@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
-import "package:flutter/scheduler.dart";
import "package:photos/models/search/search_types.dart";
+import "package:photos/states/all_sections_examples_state.dart";
import "package:photos/theme/ente_theme.dart";
-import "package:photos/ui/search_tab.dart";
class NoResultWidget extends StatefulWidget {
const NoResultWidget({Key? key}) : super(key: key);
@@ -21,21 +20,26 @@ class _NoResultWidgetState extends State<NoResultWidget> {
// remove face and content sectionType
searchTypes.remove(SectionType.face);
searchTypes.remove(SectionType.content);
- SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
- allSectionsExamplesFuture.then((value) {
- for (int i = 0; i < searchTypes.length; i++) {
- final querySuggestions = <String>[];
- for (int j = 0; j < 2 && j < value[i].length; j++) {
- querySuggestions.add(value[i][j].name());
- }
- if (querySuggestions.isNotEmpty) {
- searchTypeToQuerySuggestion.addAll({
- searchTypes[i].sectionTitle(context): querySuggestions,
- });
- }
+ }
+
+ @override
+ void didChangeDependencies() {
+ super.didChangeDependencies();
+ InheritedAllSectionsExamples.of(context)
+ .allSectionsExamplesFuture
+ .then((value) {
+ for (int i = 0; i < searchTypes.length; i++) {
+ final querySuggestions = <String>[];
+ for (int j = 0; j < 2 && j < value[i].length; j++) {
+ querySuggestions.add(value[i][j].name());
+ }
+ if (querySuggestions.isNotEmpty) {
+ searchTypeToQuerySuggestion.addAll({
+ searchTypes[i].sectionTitle(context): querySuggestions,
+ });
}
- setState(() {});
- });
+ }
+ setState(() {});
});
}