From aa2f85af93d847ebe2250c0470e69aa9d5726688 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Wed, 17 Aug 2022 16:20:40 +0530 Subject: [PATCH] Made service for month search and added results to allResults --- lib/data/months.dart | 26 +++++++------- lib/models/search/month_search_result.dart | 6 ++-- lib/services/search_service.dart | 42 ++++++++++++++++++++-- lib/ui/viewer/search/search_widget.dart | 3 ++ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/lib/data/months.dart b/lib/data/months.dart index 9aa92f6bf..700a35533 100644 --- a/lib/data/months.dart +++ b/lib/data/months.dart @@ -1,16 +1,16 @@ import 'package:photos/models/search/month_search_result.dart'; -List allMonths = [ - MonthWihMonthNumber('January', 1), - MonthWihMonthNumber('February', 2), - MonthWihMonthNumber('March', 3), - MonthWihMonthNumber('April', 4), - MonthWihMonthNumber('May', 5), - MonthWihMonthNumber('June', 6), - MonthWihMonthNumber('July', 7), - MonthWihMonthNumber('August', 8), - MonthWihMonthNumber('September', 9), - MonthWihMonthNumber('October', 10), - MonthWihMonthNumber('November', 11), - MonthWihMonthNumber('December', 12), +List allMonths = [ + MonthData('January', 1), + MonthData('February', 2), + MonthData('March', 3), + MonthData('April', 4), + MonthData('May', 5), + MonthData('June', 6), + MonthData('July', 7), + MonthData('August', 8), + MonthData('September', 9), + MonthData('October', 10), + MonthData('November', 11), + MonthData('December', 12), ]; diff --git a/lib/models/search/month_search_result.dart b/lib/models/search/month_search_result.dart index 934e9cabb..6a4405732 100644 --- a/lib/models/search/month_search_result.dart +++ b/lib/models/search/month_search_result.dart @@ -7,8 +7,8 @@ class MonthSearchResult extends SearchResult { MonthSearchResult(this.month, this.files); } -class MonthWihMonthNumber { - final String month; +class MonthData { + final String name; final int monthNumber; - MonthWihMonthNumber(this.month, this.monthNumber); + MonthData(this.name, this.monthNumber); } diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index 661b5229c..65807966c 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -4,6 +4,7 @@ import 'package:photos/core/configuration.dart'; import 'package:photos/core/event_bus.dart'; import 'package:photos/core/network.dart'; import 'package:photos/data/holidays.dart'; +import 'package:photos/data/months.dart'; import 'package:photos/db/files_db.dart'; import 'package:photos/events/local_photos_updated_event.dart'; import 'package:photos/models/collection.dart'; @@ -14,6 +15,7 @@ import 'package:photos/models/search/album_search_result.dart'; import 'package:photos/models/search/holiday_search_result.dart'; import 'package:photos/models/search/location_api_response.dart'; import 'package:photos/models/search/location_search_result.dart'; +import 'package:photos/models/search/month_search_result.dart'; import 'package:photos/models/search/year_search_result.dart'; import 'package:photos/services/collections_service.dart'; import 'package:photos/utils/date_time_util.dart'; @@ -168,13 +170,13 @@ class SearchService { Future> getHolidaySearchResults( String query, ) async { - final List holidaySearchResult = []; + final List holidaySearchResults = []; final nonCaseSensitiveRegexForQuery = RegExp(query, caseSensitive: false); for (var holiday in allHolidays) { if (holiday.name.contains(nonCaseSensitiveRegexForQuery)) { - holidaySearchResult.add( + holidaySearchResults.add( HolidaySearchResult( holiday.name, await FilesDB.instance.getFilesCreatedWithinDurations( @@ -186,7 +188,29 @@ class SearchService { ); } } - return holidaySearchResult; + return holidaySearchResults; + } + + Future> getMonthSearchResults(String query) async { + final List monthSearchResults = []; + final nonCaseSensitiveRegexForQuery = RegExp(query, caseSensitive: false); + + for (var month in allMonths) { + if (month.name.startsWith(nonCaseSensitiveRegexForQuery)) { + monthSearchResults.add( + MonthSearchResult( + month.name, + await FilesDB.instance.getFilesCreatedWithinDurations( + _getDurationsOfMonthInEveryYear(month.monthNumber), + null, + order: 'DESC', + ), + ), + ); + } + } + + return monthSearchResults; } List> _getDurationsOfHolidayInEveryYear(int day, int month) { @@ -200,6 +224,18 @@ class SearchService { return durationsOfHolidayInEveryYear; } + List> _getDurationsOfMonthInEveryYear(int month) { + final List> durationsOfMonthInEveryYear = []; + for (var year = 1970; year < currentYear; year++) { + durationsOfMonthInEveryYear.add([ + DateTime.utc(year, month, 1).microsecondsSinceEpoch, + DateTime.utc(year, (month == 12 ? 1 : month + 1), 1) + .microsecondsSinceEpoch, + ]); + } + return durationsOfMonthInEveryYear; + } + bool _isValidLocation(Location location) { return location != null && location.latitude != null && diff --git a/lib/ui/viewer/search/search_widget.dart b/lib/ui/viewer/search/search_widget.dart index f758aaf1e..beec4830b 100644 --- a/lib/ui/viewer/search/search_widget.dart +++ b/lib/ui/viewer/search/search_widget.dart @@ -160,6 +160,9 @@ class _SearchWidgetState extends State { await _searchService.getLocationSearchResults(query); allResults.addAll(locationResults); + final monthResults = await _searchService.getMonthSearchResults(query); + allResults.addAll(monthResults); + return allResults; }