From b7f546644cb1c7f43737308a7ca3b7b3140dca95 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:12:06 +0530 Subject: [PATCH 01/12] Remove separate cache for files --- lib/services/search_service.dart | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index a564ba7d7..03f22e3c5 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -15,7 +15,6 @@ import 'package:photos/services/collections_service.dart'; import 'package:photos/services/user_service.dart'; class SearchService { - List _cachedFiles; Future> _future; final _dio = Network.instance.getDio(); final _config = Configuration.instance; @@ -37,19 +36,16 @@ class SearchService { }); Bus.instance.on().listen((event) { - _cachedFiles = null; + _future = null; getAllFiles(); }); } Future> getAllFiles() async { - if (_cachedFiles != null) { - return _cachedFiles; - } if (_future != null) { return _future; } - _future = _fetchAllFiles(); + _future = FilesDB.instance.getAllFilesFromDB(); return _future; } @@ -69,7 +65,7 @@ class SearchService { } void clearCache() { - _cachedFiles.clear(); + _future = null; } Future> getLocationSearchResults( @@ -160,9 +156,4 @@ class SearchService { location.longitude < locationData.bbox[2] && location.latitude < locationData.bbox[3]; } - - Future> _fetchAllFiles() async { - _cachedFiles = await FilesDB.instance.getAllFilesFromDB(); - return _cachedFiles; - } } From e453bd801fcfee54c6a61fee6324ede4953340fe Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:12:12 +0530 Subject: [PATCH 02/12] Remove unnecessary log line --- lib/ui/viewer/file/file_icons_widget.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ui/viewer/file/file_icons_widget.dart b/lib/ui/viewer/file/file_icons_widget.dart index 0aa9e28f9..27e494bbe 100644 --- a/lib/ui/viewer/file/file_icons_widget.dart +++ b/lib/ui/viewer/file/file_icons_widget.dart @@ -8,7 +8,6 @@ class ThumbnailPlaceHolder extends StatelessWidget { @override Widget build(BuildContext context) { - debugPrint("building placeHolder for thumbnail"); return Container( alignment: Alignment.center, color: Theme.of(context).colorScheme.galleryThumbBackgroundColor, From f325d6d602c9d00a68982113d65778a6c09e18cc Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:12:29 +0530 Subject: [PATCH 03/12] Create a unique tag for location search --- .../search/search_result_widgets/location_result_widget.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart index 3cfbd3f2b..c6b838b58 100644 --- a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart +++ b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart @@ -53,7 +53,7 @@ class LocationSearchResultWidget extends StatelessWidget { ), ), Hero( - tag: "location_search" + locationSearchResult.files[0].tag(), + tag: "location_search" + locationSearchResult.location, child: SizedBox( height: 50, width: 50, @@ -70,6 +70,7 @@ class LocationSearchResultWidget extends StatelessWidget { FilesInLocationPage( locationSearchResult: locationSearchResult, ), + forceCustomPageRoute: true, ); }, ); From cf26347d83a66bd3ff360d4d3b367c9d34b069cc Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:12:40 +0530 Subject: [PATCH 04/12] Remove unnecessary setState --- lib/ui/viewer/search/search_widget.dart | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/ui/viewer/search/search_widget.dart b/lib/ui/viewer/search/search_widget.dart index 345e7806e..a69e43f9a 100644 --- a/lib/ui/viewer/search/search_widget.dart +++ b/lib/ui/viewer/search/search_widget.dart @@ -29,15 +29,11 @@ class _SearchIconWidgetState extends State { tag: "search_icon", child: IconButton( onPressed: () { - setState( - () { - Navigator.push( - context, - TransparentRoute( - builder: (BuildContext context) => const SearchWidget(), - ), - ); - }, + Navigator.push( + context, + TransparentRoute( + builder: (BuildContext context) => const SearchWidget(), + ), ); }, icon: const Icon(Icons.search), From e254e6e64e766eaad042006e829f6ca5f9fbe836 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:14:42 +0530 Subject: [PATCH 05/12] Sort location search results by the latest file first --- lib/services/search_service.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index 03f22e3c5..5776ee56a 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -96,6 +96,9 @@ class SearchService { filesInLocation.add(file); } } + filesInLocation.sort( + (first, second) => second.creationTime.compareTo(first.creationTime), + ); if (filesInLocation.isNotEmpty) { locationSearchResults.add( LocationSearchResult(locationData.place, filesInLocation), From 91e2226eaa870af1f6c693897280e9e5f20a3db4 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:34:58 +0530 Subject: [PATCH 06/12] Gracefully handle all errors while querying by location --- lib/services/search_service.dart | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index 5776ee56a..01747aef0 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -71,9 +71,9 @@ class SearchService { Future> getLocationSearchResults( String query, ) async { + final List locationSearchResults = []; try { final List allFiles = await SearchService.instance.getAllFiles(); - final List locationSearchResults = []; final response = await _dio.get( _config.getHttpEndpoint() + "/search/location", @@ -105,11 +105,10 @@ class SearchService { ); } } - return locationSearchResults; - } on DioError catch (e) { - _logger.info(e); - rethrow; + } catch (e) { + _logger.severe(e); } + return locationSearchResults; } // getFilteredCollectionsWithThumbnail removes deleted or archived or From 70e1d9442add3fdbea32f369201a7c530565fc9d Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 13:40:29 +0530 Subject: [PATCH 07/12] Fix hero animation for location search results --- .../search/collections/files_in_location_page.dart | 8 +++++--- .../search_result_widgets/location_result_widget.dart | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/ui/viewer/search/collections/files_in_location_page.dart b/lib/ui/viewer/search/collections/files_in_location_page.dart index 40004bb08..9a984d1db 100644 --- a/lib/ui/viewer/search/collections/files_in_location_page.dart +++ b/lib/ui/viewer/search/collections/files_in_location_page.dart @@ -11,13 +11,15 @@ import 'package:photos/ui/viewer/gallery/gallery_overlay_widget.dart'; class FilesInLocationPage extends StatelessWidget { final LocationSearchResult locationSearchResult; + final String tagPrefix; final _selectedFiles = SelectedFiles(); - static const String kTagPrefix = "location_search"; static const GalleryType appBarType = GalleryType.searchResults; static const GalleryType overlayType = GalleryType.searchResults; - FilesInLocationPage({ + + FilesInLocationPage( this.locationSearchResult, + this.tagPrefix, { Key key, }) : super(key: key); @@ -57,7 +59,7 @@ class FilesInLocationPage extends StatelessWidget { null, ), ], - tagPrefix: kTagPrefix, + tagPrefix: tagPrefix, selectedFiles: _selectedFiles, initialFiles: [locationSearchResult.files[0]], footer: const SizedBox(height: 120), diff --git a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart index c6b838b58..57bb74a00 100644 --- a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart +++ b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart @@ -6,12 +6,17 @@ import 'package:photos/ui/viewer/search/collections/files_in_location_page.dart' import 'package:photos/utils/navigation_util.dart'; class LocationSearchResultWidget extends StatelessWidget { + static const String kTagPrefix = "location_search"; + final LocationSearchResult locationSearchResult; const LocationSearchResultWidget(this.locationSearchResult, {Key key}) : super(key: key); + @override Widget build(BuildContext context) { final noOfMemories = locationSearchResult.files.length; + final heroTagPrefix = kTagPrefix + locationSearchResult.location; + return GestureDetector( behavior: HitTestBehavior.opaque, child: Container( @@ -53,7 +58,7 @@ class LocationSearchResultWidget extends StatelessWidget { ), ), Hero( - tag: "location_search" + locationSearchResult.location, + tag: heroTagPrefix + locationSearchResult.files[0].tag(), child: SizedBox( height: 50, width: 50, @@ -67,9 +72,7 @@ class LocationSearchResultWidget extends StatelessWidget { onTap: () { routeToPage( context, - FilesInLocationPage( - locationSearchResult: locationSearchResult, - ), + FilesInLocationPage(locationSearchResult, heroTagPrefix), forceCustomPageRoute: true, ); }, From 811e67b780b6fddc7da30ed7741bed8737c09a7d Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 14:15:56 +0530 Subject: [PATCH 08/12] Remove unnecessary Row and Flexible widget --- lib/ui/viewer/search/search_widget.dart | 106 +++++++++++------------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/lib/ui/viewer/search/search_widget.dart b/lib/ui/viewer/search/search_widget.dart index a69e43f9a..179de69fe 100644 --- a/lib/ui/viewer/search/search_widget.dart +++ b/lib/ui/viewer/search/search_widget.dart @@ -59,65 +59,59 @@ class _SearchWidgetState extends State { child: Column( children: [ const SizedBox(height: 8), - Row( - children: [ - Flexible( - child: Container( - color: Theme.of(context).colorScheme.defaultBackgroundColor, - child: TextFormField( - style: Theme.of(context).textTheme.subtitle1, - decoration: InputDecoration( - hintText: 'Search for albums, locations & files', - filled: true, - contentPadding: const EdgeInsets.symmetric( - horizontal: 16, - vertical: 14, - ), - border: UnderlineInputBorder( - borderSide: BorderSide.none, - borderRadius: BorderRadius.circular(8), - ), - 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: IconButton( - onPressed: () { - Navigator.pop(context); - }, - icon: Icon( - Icons.close, - color: Theme.of(context) - .colorScheme - .iconColor - .withOpacity(0.5), - ), - ), - ), - onChanged: (value) async { - final List allResults = - await getSearchResultsForQuery(value); - if (mounted) { - setState(() { - results.clear(); - results.addAll(allResults); - }); - } - }, - autofocus: true, + Container( + color: Theme.of(context).colorScheme.defaultBackgroundColor, + child: TextFormField( + style: Theme.of(context).textTheme.subtitle1, + decoration: InputDecoration( + hintText: 'Search for albums, locations & files', + filled: true, + contentPadding: const EdgeInsets.symmetric( + horizontal: 16, + vertical: 14, + ), + border: UnderlineInputBorder( + borderSide: BorderSide.none, + borderRadius: BorderRadius.circular(8), + ), + 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: IconButton( + onPressed: () { + Navigator.pop(context); + }, + icon: Icon( + Icons.close, + color: Theme.of(context) + .colorScheme + .iconColor + .withOpacity(0.5), ), ), ), - ], + onChanged: (value) async { + final List allResults = + await getSearchResultsForQuery(value); + if (mounted) { + setState(() { + results.clear(); + results.addAll(allResults); + }); + } + }, + autofocus: true, + ), ), results.isNotEmpty ? SearchSuggestionsWidget(results) From 56339cea5db28d137b5a478dc213b13856a00d6b Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 14:16:24 +0530 Subject: [PATCH 09/12] [Unrelated] Ensure that the widget is mounted before using BuildContext --- lib/ui/viewer/file/zoomable_live_image.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ui/viewer/file/zoomable_live_image.dart b/lib/ui/viewer/file/zoomable_live_image.dart index ea413917e..983df7730 100644 --- a/lib/ui/viewer/file/zoomable_live_image.dart +++ b/lib/ui/viewer/file/zoomable_live_image.dart @@ -166,7 +166,7 @@ class _ZoomableLiveImageState extends State void _showLivePhotoToast() async { var preferences = await SharedPreferences.getInstance(); int promptTillNow = preferences.getInt(kLivePhotoToastCounterKey) ?? 0; - if (promptTillNow < kMaxLivePhotoToastCount) { + if (promptTillNow < kMaxLivePhotoToastCount && mounted) { showToast(context, "Press and hold to play video"); preferences.setInt(kLivePhotoToastCounterKey, promptTillNow + 1); } From f25045b92ac7969f143ae1af34410429f8427f07 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 17:18:09 +0530 Subject: [PATCH 10/12] Fetch 10 results for location searches --- lib/services/search_service.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index 01747aef0..ba396f710 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -77,7 +77,7 @@ class SearchService { final response = await _dio.get( _config.getHttpEndpoint() + "/search/location", - queryParameters: {"query": query, "limit": 4}, + queryParameters: {"query": query, "limit": 10}, options: Options( headers: {"X-Auth-Token": _config.getToken()}, ), From 08841b22c3c3fc72ace2832c5686dd4f5f7faf80 Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 18:00:15 +0530 Subject: [PATCH 11/12] Rename variable --- lib/services/search_service.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index ba396f710..890374d39 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -15,7 +15,7 @@ import 'package:photos/services/collections_service.dart'; import 'package:photos/services/user_service.dart'; class SearchService { - Future> _future; + Future> _cachedFilesFuture; final _dio = Network.instance.getDio(); final _config = Configuration.instance; final _logger = Logger((UserService).toString()); @@ -30,23 +30,23 @@ class SearchService { Future.delayed(const Duration(seconds: 5), () async { /* In case home screen loads before 5 seconds and user starts search, future will not be null.So here getAllFiles won't run again in that case. */ - if (_future == null) { + if (_cachedFilesFuture == null) { getAllFiles(); } }); Bus.instance.on().listen((event) { - _future = null; + _cachedFilesFuture = null; getAllFiles(); }); } Future> getAllFiles() async { - if (_future != null) { - return _future; + if (_cachedFilesFuture != null) { + return _cachedFilesFuture; } - _future = FilesDB.instance.getAllFilesFromDB(); - return _future; + _cachedFilesFuture = FilesDB.instance.getAllFilesFromDB(); + return _cachedFilesFuture; } Future> getFileSearchResults(String query) async { @@ -65,7 +65,7 @@ class SearchService { } void clearCache() { - _future = null; + _cachedFilesFuture = null; } Future> getLocationSearchResults( From dabe266455f15f6b891bfb963433b087447ea6aa Mon Sep 17 00:00:00 2001 From: vishnukvmd Date: Thu, 11 Aug 2022 18:03:30 +0530 Subject: [PATCH 12/12] Update variable name --- .../search/search_result_widgets/location_result_widget.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart index 57bb74a00..00c012fae 100644 --- a/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart +++ b/lib/ui/viewer/search/search_result_widgets/location_result_widget.dart @@ -6,7 +6,7 @@ import 'package:photos/ui/viewer/search/collections/files_in_location_page.dart' import 'package:photos/utils/navigation_util.dart'; class LocationSearchResultWidget extends StatelessWidget { - static const String kTagPrefix = "location_search"; + static const String _tagPrefix = "location_search"; final LocationSearchResult locationSearchResult; const LocationSearchResultWidget(this.locationSearchResult, {Key key}) @@ -15,7 +15,7 @@ class LocationSearchResultWidget extends StatelessWidget { @override Widget build(BuildContext context) { final noOfMemories = locationSearchResult.files.length; - final heroTagPrefix = kTagPrefix + locationSearchResult.location; + final heroTagPrefix = _tagPrefix + locationSearchResult.location; return GestureDetector( behavior: HitTestBehavior.opaque,