2022-08-18 08:33:28 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-07-18 04:45:06 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-16 12:11:16 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-18 04:45:06 +00:00
|
|
|
import 'package:photos/ente_theme_data.dart';
|
2023-04-07 05:27:06 +00:00
|
|
|
import "package:photos/generated/l10n.dart";
|
2022-09-14 08:40:35 +00:00
|
|
|
import 'package:photos/models/search/search_result.dart';
|
2022-08-06 15:26:07 +00:00
|
|
|
import 'package:photos/services/search_service.dart';
|
2023-06-09 16:02:54 +00:00
|
|
|
import "package:photos/theme/ente_theme.dart";
|
2023-03-10 08:08:51 +00:00
|
|
|
import 'package:photos/ui/components/buttons/icon_button_widget.dart';
|
2023-06-09 16:02:54 +00:00
|
|
|
import "package:photos/ui/map/map_screen.dart";
|
2022-09-14 08:33:12 +00:00
|
|
|
import 'package:photos/ui/viewer/search/result/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-22 09:16:46 +00:00
|
|
|
import 'package:photos/utils/debouncer.dart';
|
2022-08-10 06:28:16 +00:00
|
|
|
import 'package:photos/utils/navigation_util.dart';
|
2022-07-18 04:45:06 +00:00
|
|
|
|
|
|
|
class SearchIconWidget extends StatefulWidget {
|
2022-12-30 12:10:17 +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-11-01 12:41:51 +00:00
|
|
|
child: IconButtonWidget(
|
2022-11-02 14:19:39 +00:00
|
|
|
iconButtonType: IconButtonType.primary,
|
2022-11-01 12:41:51 +00:00
|
|
|
icon: Icons.search,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
TransparentRoute(
|
|
|
|
builder: (BuildContext context) => const SearchWidget(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2022-08-05 11:08:25 +00:00
|
|
|
),
|
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-12-30 12:10:17 +00:00
|
|
|
const SearchWidget({Key? key}) : super(key: key);
|
2022-09-16 12:11:16 +00:00
|
|
|
|
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-26 08:46:43 +00:00
|
|
|
final _debouncer = Debouncer(const Duration(milliseconds: 100));
|
2022-09-16 12:11:16 +00:00
|
|
|
final Logger _logger = Logger((_SearchWidgetState).toString());
|
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(
|
2022-08-20 10:29:29 +00:00
|
|
|
color: Theme.of(context).colorScheme.searchResultsBackgroundColor,
|
2022-08-16 13:52:17 +00:00
|
|
|
child: SafeArea(
|
|
|
|
child: Padding(
|
2022-11-19 10:59:57 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
2022-08-16 13:52:17 +00:00
|
|
|
child: Column(
|
2023-06-09 16:02:54 +00:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
2022-08-16 13:52:17 +00:00
|
|
|
children: [
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
child: Container(
|
2022-11-19 10:59:57 +00:00
|
|
|
height: 44,
|
2022-08-16 13:52:17 +00:00
|
|
|
color: Theme.of(context).colorScheme.defaultBackgroundColor,
|
|
|
|
child: TextFormField(
|
2023-06-13 06:41:31 +00:00
|
|
|
style: Theme.of(context).textTheme.titleMedium,
|
2022-08-16 13:52:17 +00:00
|
|
|
// Below parameters are to disable auto-suggestion
|
|
|
|
enableSuggestions: false,
|
|
|
|
autocorrect: false,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
// Above parameters are to disable auto-suggestion
|
|
|
|
decoration: InputDecoration(
|
2023-04-07 05:27:06 +00:00
|
|
|
hintText: S.of(context).searchHintText,
|
2022-08-16 13:52:17 +00:00
|
|
|
filled: true,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(
|
2022-11-19 10:59:57 +00:00
|
|
|
vertical: 10,
|
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,
|
|
|
|
),
|
2022-11-19 10:59:57 +00:00
|
|
|
prefixIconConstraints: const BoxConstraints(
|
|
|
|
maxHeight: 44,
|
|
|
|
maxWidth: 44,
|
|
|
|
minHeight: 44,
|
|
|
|
minWidth: 44,
|
|
|
|
),
|
|
|
|
suffixIconConstraints: const BoxConstraints(
|
|
|
|
maxHeight: 44,
|
|
|
|
maxWidth: 44,
|
|
|
|
minHeight: 44,
|
|
|
|
minWidth: 44,
|
|
|
|
),
|
2022-08-16 13:52:17 +00:00
|
|
|
prefixIcon: Hero(
|
|
|
|
tag: "search_icon",
|
|
|
|
child: Icon(
|
|
|
|
Icons.search,
|
|
|
|
color: Theme.of(context)
|
|
|
|
.colorScheme
|
|
|
|
.iconColor
|
|
|
|
.withOpacity(0.5),
|
|
|
|
),
|
|
|
|
),
|
2022-08-23 11:02:00 +00:00
|
|
|
/*Using valueListenableBuilder inside a stateful widget because this widget is only rebuild when
|
|
|
|
setState is called when deboucncing is over and the spinner needs to be shown while debouncing */
|
2022-08-18 10:52:57 +00:00
|
|
|
suffixIcon: ValueListenableBuilder(
|
2022-08-22 08:26:01 +00:00
|
|
|
valueListenable: _debouncer.debounceActiveNotifier,
|
2022-08-18 10:52:57 +00:00
|
|
|
builder: (
|
|
|
|
BuildContext context,
|
2022-08-22 08:26:01 +00:00
|
|
|
bool isDebouncing,
|
2022-12-30 12:10:17 +00:00
|
|
|
Widget? child,
|
2022-08-18 10:52:57 +00:00
|
|
|
) {
|
2022-08-23 05:23:12 +00:00
|
|
|
return SearchSuffixIcon(
|
|
|
|
isDebouncing,
|
|
|
|
);
|
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 {
|
2022-08-23 11:02:00 +00:00
|
|
|
_query = value;
|
2022-08-16 13:52:17 +00:00
|
|
|
final List<SearchResult> allResults =
|
|
|
|
await getSearchResultsForQuery(value);
|
2022-09-16 12:11:16 +00:00
|
|
|
/*checking if _query == value to make sure that the results are from the current query
|
2022-08-23 11:02:00 +00:00
|
|
|
and not from the previous query (race condition).*/
|
|
|
|
if (mounted && _query == value) {
|
2022-08-16 13:52:17 +00:00
|
|
|
setState(() {
|
|
|
|
_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()
|
2023-06-09 16:02:54 +00:00
|
|
|
: const NavigateToMap(),
|
2022-08-16 13:52:17 +00:00
|
|
|
],
|
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-22 09:14:52 +00:00
|
|
|
_debouncer.cancelDebounce();
|
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 {
|
|
|
|
final Completer<List<SearchResult>> completer = Completer();
|
2022-08-05 11:08:25 +00:00
|
|
|
|
2022-08-23 05:23:12 +00:00
|
|
|
_debouncer.run(
|
|
|
|
() {
|
|
|
|
return _getSearchResultsFromService(query, completer);
|
|
|
|
},
|
|
|
|
);
|
2022-08-19 07:30:37 +00:00
|
|
|
|
|
|
|
return completer.future;
|
|
|
|
}
|
2022-08-18 12:42:38 +00:00
|
|
|
|
2022-08-22 08:26:01 +00:00
|
|
|
Future<void> _getSearchResultsFromService(
|
2022-08-19 07:30:37 +00:00
|
|
|
String query,
|
|
|
|
Completer completer,
|
|
|
|
) async {
|
2022-08-22 10:19:32 +00:00
|
|
|
final List<SearchResult> allResults = [];
|
|
|
|
if (query.isEmpty) {
|
|
|
|
completer.complete(allResults);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-16 12:11:16 +00:00
|
|
|
try {
|
|
|
|
if (_isYearValid(query)) {
|
|
|
|
final yearResults = await _searchService.getYearSearchResults(query);
|
|
|
|
allResults.addAll(yearResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
final holidayResults =
|
|
|
|
await _searchService.getHolidaySearchResults(query);
|
|
|
|
allResults.addAll(holidayResults);
|
|
|
|
|
|
|
|
final fileTypeSearchResults =
|
|
|
|
await _searchService.getFileTypeResults(query);
|
|
|
|
allResults.addAll(fileTypeSearchResults);
|
|
|
|
|
2022-12-26 09:21:27 +00:00
|
|
|
final captionAndDisplayNameResult =
|
|
|
|
await _searchService.getCaptionAndNameResults(query);
|
|
|
|
allResults.addAll(captionAndDisplayNameResult);
|
2022-11-05 05:52:28 +00:00
|
|
|
|
2022-09-16 12:11:16 +00:00
|
|
|
final fileExtnResult =
|
|
|
|
await _searchService.getFileExtensionResults(query);
|
|
|
|
allResults.addAll(fileExtnResult);
|
|
|
|
|
2023-03-07 02:12:54 +00:00
|
|
|
final locationResult = await _searchService.getLocationResults(query);
|
|
|
|
allResults.addAll(locationResult);
|
|
|
|
|
2022-09-16 12:11:16 +00:00
|
|
|
final collectionResults =
|
|
|
|
await _searchService.getCollectionSearchResults(query);
|
|
|
|
allResults.addAll(collectionResults);
|
|
|
|
|
|
|
|
final monthResults = await _searchService.getMonthSearchResults(query);
|
|
|
|
allResults.addAll(monthResults);
|
|
|
|
|
|
|
|
final possibleEvents = await _searchService.getDateResults(query);
|
|
|
|
allResults.addAll(possibleEvents);
|
|
|
|
} catch (e, s) {
|
|
|
|
_logger.severe("error during search", e, s);
|
2022-09-15 05:21:23 +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-22 05:18:28 +00:00
|
|
|
bool _isYearValid(String year) {
|
|
|
|
final yearAsInt = int.tryParse(year); //returns null if cannot be parsed
|
|
|
|
return yearAsInt != null && yearAsInt <= currentYear;
|
2022-08-11 13:22:30 +00:00
|
|
|
}
|
2022-08-19 06:28:20 +00:00
|
|
|
}
|
2023-06-09 16:02:54 +00:00
|
|
|
|
|
|
|
class NavigateToMap extends StatelessWidget {
|
|
|
|
const NavigateToMap({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-06-09 16:20:10 +00:00
|
|
|
final colorScheme = getEnteColorScheme(context);
|
2023-06-09 16:02:54 +00:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: IconButtonWidget(
|
|
|
|
icon: Icons.map_sharp,
|
|
|
|
iconButtonType: IconButtonType.primary,
|
2023-06-09 16:20:10 +00:00
|
|
|
defaultColor: colorScheme.backgroundElevated,
|
|
|
|
pressedColor: colorScheme.backgroundElevated2,
|
2023-06-09 16:02:54 +00:00
|
|
|
size: 28,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushReplacement(
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => MapScreen(
|
|
|
|
filesFutureFn: SearchService.instance.getAllFiles,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|