@@ -49,9 +49,26 @@ class SearchResult {
4949 : result.image.contextLink.hashCode;
5050}
5151
52+ class Promotion {
53+ final customsearch.Promotion promotion;
54+
55+ Promotion (this .promotion);
56+ }
57+
58+ /// A wrapper class to aggregate all the search result fields that we need.
59+ class SearchResults {
60+ List <SearchResult > searchResults;
61+ List <Promotion > promotions;
62+
63+ SearchResults ({this .searchResults, this .promotions}) {
64+ this .searchResults ?? = List <SearchResult >();
65+ this .promotions ?? = List <Promotion >();
66+ }
67+ }
68+
5269/// Abstract class for Search Data Source.
5370abstract class SearchDataSource {
54- Future <List < SearchResult > > search (String query, {String searchType});
71+ Future <SearchResults > search (String query, {String searchType});
5572}
5673
5774class _StaticSearchResponse {
@@ -63,9 +80,7 @@ class _StaticSearchResponse {
6380 {this .assetPath, this .searchType, this .searchResponseJsonString});
6481}
6582
66-
6783class FakeSearchDataSource implements SearchDataSource {
68-
6984 final Map <String , _StaticSearchResponse > searchResponses = {
7085 'web' : _StaticSearchResponse (
7186 assetPath: 'res/sampledata/nytimes_sample_data.json' ),
@@ -76,7 +91,6 @@ class FakeSearchDataSource implements SearchDataSource {
7691 assetPath: 'res/sampledata/nytimes_with_promotion.json' ),
7792 };
7893
79-
8094 FakeSearchDataSource () {
8195 searchResponses.keys.forEach ((key) {
8296 loadAssetToSearchResponse (key, searchResponses[key].assetPath);
@@ -89,19 +103,20 @@ class FakeSearchDataSource implements SearchDataSource {
89103 }
90104
91105 @override
92- Future <List <SearchResult >> search (String query, {String searchType}) async {
93- var results = List <SearchResult >();
106+ Future <SearchResults > search (String query, {String searchType}) async {
94107 if (! searchResponses.containsKey (query)) {
95- return results ;
108+ return SearchResults () ;
96109 }
97110 if (searchResponses[query].searchType != searchType) {
98- return results ;
111+ return SearchResults () ;
99112 }
113+ var results = List <SearchResult >();
100114 Map searchMap = jsonDecode (searchResponses[query].searchResponseJsonString);
101115 customsearch.Search search = customsearch.Search .fromJson (searchMap);
102116 search.items.forEach (
103117 (item) => results.add (SearchResult .escapeLineBreakInSnippet (item)));
104- return Set <SearchResult >.from (results).toList ();
118+ return SearchResults (
119+ searchResults: Set <SearchResult >.from (results).toList ());
105120 }
106121}
107122
@@ -117,14 +132,15 @@ class CustomSearchDataSource implements SearchDataSource {
117132 }
118133
119134 @override
120- Future <List < SearchResult > > search (String query, {String searchType}) async {
135+ Future <SearchResults > search (String query, {String searchType}) async {
121136 var results = List <SearchResult >();
122137 customsearch.Search search =
123138 await this .api.cse.list (query, cx: this .cx, searchType: searchType);
124139 if (search.items != null ) {
125140 search.items.forEach ((item) => results.add (SearchResult (item)));
126141 }
127- return Set <SearchResult >.from (results).toList ();
142+ return SearchResults (
143+ searchResults: Set <SearchResult >.from (results).toList ());
128144 }
129145}
130146
0 commit comments