ente/lib/ui/viewer/search/search_widget.dart

202 lines
6.6 KiB
Dart
Raw Normal View History

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';
import 'package:photos/services/search_service.dart';
import 'package:photos/ui/viewer/search/search_result_widgets/no_result_widget.dart';
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';
import 'package:photos/utils/date_time_util.dart';
2022-08-10 06:28:16 +00:00
import 'package:photos/utils/navigation_util.dart';
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-07-18 04:45:06 +00:00
}
}
2022-07-18 04:45:06 +00:00
class SearchWidget extends StatefulWidget {
const SearchWidget({Key key}) : super(key: key);
@override
State<SearchWidget> createState() => _SearchWidgetState();
}
class _SearchWidgetState extends State<SearchWidget> {
String _query = "";
final List<SearchResult> _results = [];
2022-08-16 10:19:26 +00:00
final _searchService = SearchService.instance;
final _debouncer = Debouncer(const Duration(milliseconds: 200));
@override
Widget build(BuildContext context) {
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),
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...",
filled: true,
contentPadding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
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),
),
),
suffixIcon: ValueListenableBuilder(
2022-08-19 09:16:26 +00:00
valueListenable: _debouncer.debounceNotifierGetter,
builder: (
BuildContext context,
2022-08-19 07:54:46 +00:00
Timer debounceTimer,
Widget child,
) {
2022-08-19 07:54:46 +00:00
return SearchSuffixIcon(debounceTimer);
},
),
),
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
),
),
),
_results.isNotEmpty
? SearchSuggestionsWidget(_results)
: _query.isNotEmpty
? const NoResultWidget()
: const SizedBox.shrink(),
],
2022-08-06 08:15:42 +00:00
),
),
2022-08-06 08:15:42 +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() {
_debouncer.cancel();
2022-08-18 12:42:38 +00:00
super.dispose();
}
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
final Completer<List<SearchResult>> completer = Completer();
2022-08-05 11:08:25 +00:00
_debouncer.run(() {
_getSearchResultsFromService(query, completer, allResults);
});
return completer.future;
}
2022-08-18 12:42:38 +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
final holidayResults = await _searchService.getHolidaySearchResults(query);
allResults.addAll(holidayResults);
2022-08-18 12:42:38 +00:00
final collectionResults =
await _searchService.getCollectionSearchResults(query);
allResults.addAll(collectionResults);
2022-08-18 12:42:38 +00:00
final locationResults =
await _searchService.getLocationSearchResults(query);
allResults.addAll(locationResults);
2022-08-18 12:42:38 +00:00
final monthResults = await _searchService.getMonthSearchResults(query);
allResults.addAll(monthResults);
completer.complete(allResults);
2022-08-05 11:08:25 +00:00
}
2022-08-17 08:35:16 +00:00
bool _isYearValid(int year) {
return year != null && year >= 1970 && year <= currentYear;
}
}