Skip to content

Commit

Permalink
Add promotion static data, prepare for #6.
Browse files Browse the repository at this point in the history
Refactor static data source to support different query in one class.
  • Loading branch information
guojiex committed Jan 18, 2019
1 parent 227404d commit d49f5c1
Show file tree
Hide file tree
Showing 6 changed files with 1,572 additions and 55 deletions.
2 changes: 1 addition & 1 deletion lib/CustomImageSearchPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CustomImageSearchDemo extends StatefulWidget {

class _CustomImageSearchDemoState extends State<CustomImageSearchDemo> {
final CustomSearchSearchDelegate _delegate =
CustomSearchSearchDelegate.fakeStaticImageSearchSource();
CustomSearchSearchDelegate.fakeStaticSource();

// final CustomSearchSearchDelegate _delegate =
// new CustomSearchSearchDelegate.imageSearch(
Expand Down
2 changes: 1 addition & 1 deletion lib/CustomWebSearchPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CustomWebSearchDemo extends StatefulWidget {

class _CustomWebSearchDemoState extends State<CustomWebSearchDemo> {
final CustomSearchSearchDelegate _delegate = CustomSearchSearchDelegate
.fakeStaticWebSearchSource();
.fakeStaticSource();

// final CustomSearchSearchDelegate _delegate = new CustomSearchSearchDelegate(
// dataSource: CustomSearchDataSource(cx: '', apiKey: ''),
Expand Down
53 changes: 34 additions & 19 deletions lib/search_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,51 @@ abstract class SearchDataSource {
Future<List<SearchResult>> search(String query, {String searchType});
}

class FakeSearchDataSource implements SearchDataSource {
String jsonString;
static const String _webSearchAssetPath =
'res/sampledata/nytimes_sample_data.json';
static const String _imageSearchAssetPath =
'res/sampledata/nytimes_image_sample_data.json';
class _StaticSearchResponse {
final String assetPath;
final String searchType;
String searchResponseJsonString;

FakeSearchDataSource({this.jsonString});
_StaticSearchResponse(
{this.assetPath, this.searchType, this.searchResponseJsonString});
}

FakeSearchDataSource.loadWebSearchResultFromAsset() {
this._initFromAsset(_webSearchAssetPath);
}

FakeSearchDataSource.loadImageSearchResultFromAsset() {
this._initFromAsset(_imageSearchAssetPath);
}
class FakeSearchDataSource implements SearchDataSource {

void _initFromAsset(String assetPath) {
loadAsset(assetPath).then((loadedStr) => this.jsonString = loadedStr);
final Map<String, _StaticSearchResponse> searchResponses = {
'web': _StaticSearchResponse(
assetPath: 'res/sampledata/nytimes_sample_data.json'),
'image': _StaticSearchResponse(
assetPath: 'res/sampledata/nytimes_sample_data.json',
searchType: 'image'),
'promotion': _StaticSearchResponse(
assetPath: 'res/sampledata/nytimes_with_promotion.json'),
};


FakeSearchDataSource() {
searchResponses.keys.forEach((key) {
loadAssetToSearchResponse(key, searchResponses[key].assetPath);
});
}

Future<String> loadAsset(String assetPath) async {
return await rootBundle.loadString(assetPath);
void loadAssetToSearchResponse(String searchKey, String assetPath) async {
searchResponses[searchKey].searchResponseJsonString =
await rootBundle.loadString(assetPath);
}

@override
Future<List<SearchResult>> search(String query, {String searchType}) async {
Map searchMap = jsonDecode(jsonString);
customsearch.Search search = customsearch.Search.fromJson(searchMap);
var results = List<SearchResult>();
if (!searchResponses.containsKey(query)) {
return results;
}
if (searchResponses[query].searchType != searchType) {
return results;
}
Map searchMap = jsonDecode(searchResponses[query].searchResponseJsonString);
customsearch.Search search = customsearch.Search.fromJson(searchMap);
search.items.forEach(
(item) => results.add(SearchResult.escapeLineBreakInSnippet(item)));
return Set<SearchResult>.from(results).toList();
Expand Down
78 changes: 45 additions & 33 deletions lib/search_result_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ class WebSearchResultCard extends StatelessWidget {
Widget _generateTitleTile(BuildContext context) {
final ThemeData theme = Theme.of(context);
return ListTile(
title: Text(
this.searchResult.result.title,
style: theme.textTheme.headline.copyWith(
fontSize: 15.0, fontWeight: FontWeight.bold, color: Colors.blue),
title: Padding(
padding: EdgeInsets.only(top: 6.0),
child: Text(
this.searchResult.result.title,
style: theme.textTheme.headline.copyWith(
fontSize: 15.0, fontWeight: FontWeight.bold, color: Colors.blue),
),
),
subtitle: new Text(
this.searchResult.result.link,
Expand All @@ -57,26 +60,41 @@ class WebSearchResultCard extends StatelessWidget {

Widget _generateBodyTile(BuildContext context) {
final ThemeData theme = Theme.of(context);
return new Container(
padding: const EdgeInsets.only(
left: 15.0,
bottom: 8.0,
),
child: new Row(children: [
this.searchResult.result.pagemap['thumbnail'] != null
? new Image.network(
this.searchResult.result.pagemap['thumbnail'][0]['src'])
: null,
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 10.0, right: 12.0),
child: Text(
this.searchResult.result.snippet,
style: theme.textTheme.body1,
textAlign: TextAlign.left,
))),
]),
);
bool haveThumbnail = this.searchResult.result.pagemap['thumbnail'] != null;
if (!haveThumbnail) {
return Container(
padding: const EdgeInsets.only(
left: 14.0,
bottom: 8.0,
),
child: Container(
padding: const EdgeInsets.only(right: 6.0),
child: Text(
this.searchResult.result.snippet,
style: theme.textTheme.body1,
textAlign: TextAlign.left,
)),
);
} else {
return Container(
padding: const EdgeInsets.only(
left: 15.0,
bottom: 8.0,
),
child: new Row(children: [
Image.network(
this.searchResult.result.pagemap['thumbnail'][0]['src']),
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 10.0, right: 12.0),
child: Text(
this.searchResult.result.snippet,
style: theme.textTheme.body1,
textAlign: TextAlign.left,
))),
]),
);
}
}

Widget _buildSimpleLayout(BuildContext context) {
Expand Down Expand Up @@ -163,18 +181,11 @@ class CustomSearchSearchDelegate extends SearchDelegate<SearchResult> {
this.autoCompleteDataSource,
this.searchType = SearchType.image});

CustomSearchSearchDelegate.fakeStaticWebSearchSource() {
this.dataSource = FakeSearchDataSource.loadWebSearchResultFromAsset();
CustomSearchSearchDelegate.fakeStaticSource() {
this.dataSource = FakeSearchDataSource();
this.autoCompleteDataSource = CommonEnglishWordAutoCompleteDataSource();
this.searchType = SearchType.web;
}

CustomSearchSearchDelegate.fakeStaticImageSearchSource() {
this.dataSource = FakeSearchDataSource.loadImageSearchResultFromAsset();
this.autoCompleteDataSource = CommonEnglishWordAutoCompleteDataSource();
this.searchType = SearchType.image;
}

@override
ThemeData appBarTheme(BuildContext context) {
final ThemeData theme = Theme.of(context);
Expand Down Expand Up @@ -269,6 +280,7 @@ class CustomSearchSearchDelegate extends SearchDelegate<SearchResult> {
searchDelegate: this);
}));
case SearchType.web:
print(snapshot.data.length);
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
Expand Down
Loading

0 comments on commit d49f5c1

Please sign in to comment.