2022-08-18 08:33:28 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-07-18 04:45:06 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2022-08-04 16:16:16 +00:00
|
|
|
import 'package:photos/models/search/search_results.dart';
|
2022-08-06 15:26:07 +00:00
|
|
|
import 'package:photos/services/search_service.dart';
|
2022-08-13 13:37:17 +00:00
|
|
|
import 'package:photos/ui/viewer/search/search_result_widgets/no_result_widget.dart';
|
2022-08-18 10:52:57 +00:00
|
|
|
import 'package:photos/ui/viewer/search/search_suffix_icon_widget.dart';
|
2022-08-10 06:28:16 +00:00
|
|
|
import 'package:photos/ui/viewer/search/search_suggestions.dart';
|
2022-08-16 11:49:58 +00:00
|
|
|
import 'package:photos/utils/date_time_util.dart';
|
2022-08-10 06:28:16 +00:00
|
|
|
import 'package:photos/utils/navigation_util.dart';
|
2022-08-19 07:30:37 +00:00
|
|
|
import 'package:photos/utils/search_debouncer.dart';
|
2022-07-18 04:45:06 +00:00
|
|
|
|
|
|
|
class SearchIconWidget extends StatefulWidget {
|
2022-07-28 05:08:20 +00:00
|
|
|
const SearchIconWidget({Key key}) : super(key: key);
|
2022-07-18 04:45:06 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<SearchIconWidget> createState() => _SearchIconWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchIconWidgetState extends State<SearchIconWidget> {
|
2022-07-28 05:08:20 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-07-18 04:45:06 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-05 11:08:25 +00:00
|
|
|
return Hero(
|
2022-08-06 18:11:23 +00:00
|
|
|
tag: "search_icon",
|
2022-08-05 11:08:25 +00:00
|
|
|
child: IconButton(
|
|
|
|
onPressed: () {
|
2022-08-11 07:42:40 +00:00
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
TransparentRoute(
|
|
|
|
builder: (BuildContext context) => const SearchWidget(),
|
|
|
|
),
|
2022-08-05 11:08:25 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
),
|
2022-08-05 07:21:39 +00:00
|
|
|
);
|
2022-07-18 04:45:06 +00:00
|
|
|
}
|
2022-08-05 06:12:16 +00:00
|
|
|
}
|
2022-07-18 04:45:06 +00:00
|
|
|
|
2022-08-05 06:12:16 +00:00
|
|
|
class SearchWidget extends StatefulWidget {
|
2022-08-05 07:21:39 +00:00
|
|
|
const SearchWidget({Key key}) : super(key: key);
|
2022-08-05 06:12:16 +00:00
|
|
|
@override
|
|
|
|
State<SearchWidget> createState() => _SearchWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchWidgetState extends State<SearchWidget> {
|
2022-08-13 13:49:45 +00:00
|
|
|
String _query = "";
|
|
|
|
final List<SearchResult> _results = [];
|
2022-08-16 10:19:26 +00:00
|
|
|
final _searchService = SearchService.instance;
|
2022-08-19 06:28:20 +00:00
|
|
|
final _debouncer = Debouncer(const Duration(milliseconds: 200));
|
2022-08-13 13:49:45 +00:00
|
|
|
|
2022-08-05 06:12:16 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-08-16 13:52:17 +00:00
|
|
|
return GestureDetector(
|
|
|
|
onTap: () {
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
color: Colors.black.withOpacity(0.32),
|
|
|
|
child: SafeArea(
|
|
|
|
child: Padding(
|
2022-08-18 14:01:38 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
2022-08-16 13:52:17 +00:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
child: Container(
|
|
|
|
color: Theme.of(context).colorScheme.defaultBackgroundColor,
|
|
|
|
child: TextFormField(
|
|
|
|
style: Theme.of(context).textTheme.subtitle1,
|
|
|
|
// Below parameters are to disable auto-suggestion
|
|
|
|
enableSuggestions: false,
|
|
|
|
autocorrect: false,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
// Above parameters are to disable auto-suggestion
|
|
|
|
decoration: InputDecoration(
|
2022-08-19 11:51:57 +00:00
|
|
|
hintText: "Places, moments, albums...",
|
2022-08-16 13:52:17 +00:00
|
|
|
filled: true,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16,
|
|
|
|
vertical: 14,
|
2022-08-16 13:17:53 +00:00
|
|
|
),
|
2022-08-16 13:52:17 +00:00
|
|
|
border: const UnderlineInputBorder(
|
|
|
|
borderSide: BorderSide.none,
|
|
|
|
),
|
|
|
|
focusedBorder: const UnderlineInputBorder(
|
|
|
|
borderSide: BorderSide.none,
|
|
|
|
),
|
|
|
|
prefixIcon: Hero(
|
|
|
|
tag: "search_icon",
|
|
|
|
child: Icon(
|
|
|
|
Icons.search,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.iconColor
|
|
|
|
.withOpacity(0.5),
|
|
|
|
),
|
|
|
|
),
|
2022-08-18 10:52:57 +00:00
|
|
|
suffixIcon: ValueListenableBuilder(
|
2022-08-19 09:16:26 +00:00
|
|
|
valueListenable: _debouncer.debounceNotifierGetter,
|
2022-08-18 10:52:57 +00:00
|
|
|
builder: (
|
|
|
|
BuildContext context,
|
2022-08-19 07:54:46 +00:00
|
|
|
Timer debounceTimer,
|
2022-08-18 10:52:57 +00:00
|
|
|
Widget child,
|
|
|
|
) {
|
2022-08-19 07:54:46 +00:00
|
|
|
return SearchSuffixIcon(debounceTimer);
|
2022-08-16 13:52:17 +00:00
|
|
|
},
|
2022-08-16 13:17:53 +00:00
|
|
|
),
|
|
|
|
),
|
2022-08-16 13:52:17 +00:00
|
|
|
onChanged: (value) async {
|
|
|
|
final List<SearchResult> allResults =
|
|
|
|
await getSearchResultsForQuery(value);
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
|
|
|
_query = value;
|
|
|
|
_results.clear();
|
|
|
|
_results.addAll(allResults);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
autofocus: true,
|
2022-08-06 08:15:42 +00:00
|
|
|
),
|
2022-08-05 06:12:16 +00:00
|
|
|
),
|
2022-08-05 07:21:39 +00:00
|
|
|
),
|
2022-08-16 13:52:17 +00:00
|
|
|
_results.isNotEmpty
|
|
|
|
? SearchSuggestionsWidget(_results)
|
|
|
|
: _query.isNotEmpty
|
|
|
|
? const NoResultWidget()
|
|
|
|
: const SizedBox.shrink(),
|
|
|
|
],
|
2022-08-06 08:15:42 +00:00
|
|
|
),
|
2022-08-16 13:17:53 +00:00
|
|
|
),
|
2022-08-06 08:15:42 +00:00
|
|
|
),
|
2022-08-05 07:21:39 +00:00
|
|
|
),
|
|
|
|
);
|
2022-07-18 04:45:06 +00:00
|
|
|
}
|
2022-08-05 11:08:25 +00:00
|
|
|
|
2022-08-18 12:42:38 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-08-19 06:28:20 +00:00
|
|
|
_debouncer.cancel();
|
2022-08-18 12:42:38 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-08-19 06:28:20 +00:00
|
|
|
Future<List<SearchResult>> getSearchResultsForQuery(String query) async {
|
2022-08-10 06:28:16 +00:00
|
|
|
final List<SearchResult> allResults = [];
|
2022-08-18 12:42:38 +00:00
|
|
|
if (query.isEmpty) {
|
2022-08-19 09:06:25 +00:00
|
|
|
if (_debouncer.isActive()) {
|
|
|
|
_debouncer.cancel();
|
2022-08-18 13:50:53 +00:00
|
|
|
}
|
|
|
|
return (allResults);
|
2022-08-18 12:42:38 +00:00
|
|
|
}
|
2022-08-18 13:50:53 +00:00
|
|
|
|
2022-08-19 06:28:20 +00:00
|
|
|
final Completer<List<SearchResult>> completer = Completer();
|
2022-08-05 11:08:25 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
_debouncer.run(() {
|
|
|
|
_getSearchResultsFromService(query, completer, allResults);
|
|
|
|
});
|
|
|
|
|
|
|
|
return completer.future;
|
|
|
|
}
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
void _getSearchResultsFromService(
|
|
|
|
String query,
|
|
|
|
Completer completer,
|
|
|
|
List<SearchResult> allResults,
|
|
|
|
) async {
|
|
|
|
final queryAsIntForYear = int.tryParse(query);
|
|
|
|
if (_isYearValid(queryAsIntForYear)) {
|
|
|
|
final yearResult =
|
|
|
|
await _searchService.getYearSearchResults(queryAsIntForYear);
|
|
|
|
allResults.add(yearResult); //only one year will be returned
|
|
|
|
}
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
final holidayResults = await _searchService.getHolidaySearchResults(query);
|
|
|
|
allResults.addAll(holidayResults);
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
final collectionResults =
|
|
|
|
await _searchService.getCollectionSearchResults(query);
|
|
|
|
allResults.addAll(collectionResults);
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
final locationResults =
|
|
|
|
await _searchService.getLocationSearchResults(query);
|
|
|
|
allResults.addAll(locationResults);
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
final monthResults = await _searchService.getMonthSearchResults(query);
|
|
|
|
allResults.addAll(monthResults);
|
2022-08-17 10:50:40 +00:00
|
|
|
|
2022-08-19 07:30:37 +00:00
|
|
|
completer.complete(allResults);
|
2022-08-05 11:08:25 +00:00
|
|
|
}
|
2022-08-11 13:22:30 +00:00
|
|
|
|
2022-08-17 08:35:16 +00:00
|
|
|
bool _isYearValid(int year) {
|
2022-08-16 11:49:58 +00:00
|
|
|
return year != null && year >= 1970 && year <= currentYear;
|
2022-08-11 13:22:30 +00:00
|
|
|
}
|
2022-08-19 06:28:20 +00:00
|
|
|
}
|