Refactor + fix last commit in a better way
This commit is contained in:
parent
6bcac41238
commit
98ef5189a9
92
lib/states/all_sections_examples_state.dart
Normal file
92
lib/states/all_sections_examples_state.dart
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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,6 +41,7 @@ class _SearchTabState extends State<SearchTab> {
|
|||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: AllSectionsExamplesProvider(
|
||||
child: IndexedStack(
|
||||
index: index,
|
||||
children: [
|
||||
|
@ -55,6 +50,7 @@ class _SearchTabState extends State<SearchTab> {
|
|||
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)) {
|
||||
|
|
|
@ -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,8 +20,14 @@ 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) {
|
||||
}
|
||||
|
||||
@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++) {
|
||||
|
@ -36,7 +41,6 @@ class _NoResultWidgetState extends State<NoResultWidget> {
|
|||
}
|
||||
setState(() {});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
Loading…
Reference in a new issue