Skip to content

Commit

Permalink
feat: openfoodfacts#2315 - added 4 user-related searches in the profi…
Browse files Browse the repository at this point in the history
…le screen

New files:
* `contributor_product_query.dart`: User Product Search, in "contributor" mode.
* `informer_product_query.dart`: User Product Search, in "informer" mode.
* `paged_search_product_query.dart`: Back-end paged queries around search.
* `paged_user_product_query.dart`: Back-end paged queries around User.
* `photographer_product_query.dart`: User Product Search, in "photographer" mode.
* `to_be_completed_product_query.dart`: User Product Search, in "to be completed" mode.

Impacted files:
* `category_product_query.dart`: refactored with new class `PagedSearchProductQuery`
* `keywords_product_query.dart`: refactored with new class `PagedSearchProductQuery`
* `paged_product_query.dart`: now limited to page management
* `product_list.dart`: added `enum`s and constructors around user queries
* `product_list_page.dart`: added cases for the new 4 product list types
* `product_query_page_helper.dart`: added cases for the new 4 product list types
* `user_preferences_account.dart`
  • Loading branch information
monsieurtanuki committed Jun 19, 2022
1 parent 3ae4791 commit 54166e5
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 23 deletions.
68 changes: 68 additions & 0 deletions packages/smooth_app/lib/data_models/product_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ enum ProductListType {

/// End-user product list
USER,

/// End-user as a contributor
HTTP_USER_CONTRIBUTOR,

/// End-user as an informer
HTTP_USER_INFORMER,

/// End-user as a photographer
HTTP_USER_PHOTOGRAPHER,

/// End-user for products to be completed
HTTP_USER_TO_BE_COMPLETED,
}

extension ProductListTypeExtension on ProductListType {
static const Map<ProductListType, String> _keys = <ProductListType, String>{
ProductListType.HTTP_SEARCH_KEYWORDS: 'http/search/keywords',
ProductListType.HTTP_SEARCH_CATEGORY: 'http/search/category',
ProductListType.HTTP_USER_CONTRIBUTOR: 'http/user/contributor',
ProductListType.HTTP_USER_INFORMER: 'http/user/informer',
ProductListType.HTTP_USER_PHOTOGRAPHER: 'http/user/photographer',
ProductListType.HTTP_USER_TO_BE_COMPLETED: 'http/user/to_be_completed',
ProductListType.SCAN_SESSION: 'scan_session',
ProductListType.HISTORY: 'history',
ProductListType.USER: 'user',
Expand Down Expand Up @@ -59,6 +75,50 @@ class ProductList {
pageNumber: pageNumber,
);

ProductList.contributor(
final String userId, {
required int pageSize,
required int pageNumber,
}) : this._(
listType: ProductListType.HTTP_USER_CONTRIBUTOR,
parameters: userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

ProductList.informer(
final String userId, {
required int pageSize,
required int pageNumber,
}) : this._(
listType: ProductListType.HTTP_USER_INFORMER,
parameters: userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

ProductList.photographer(
final String userId, {
required int pageSize,
required int pageNumber,
}) : this._(
listType: ProductListType.HTTP_USER_PHOTOGRAPHER,
parameters: userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

ProductList.toBeCompleted(
final String userId, {
required int pageSize,
required int pageNumber,
}) : this._(
listType: ProductListType.HTTP_USER_TO_BE_COMPLETED,
parameters: userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

ProductList.history() : this._(listType: ProductListType.HISTORY);

ProductList.scanSession() : this._(listType: ProductListType.SCAN_SESSION);
Expand Down Expand Up @@ -151,6 +211,10 @@ class ProductList {
switch (listType) {
case ProductListType.HTTP_SEARCH_KEYWORDS:
case ProductListType.HTTP_SEARCH_CATEGORY:
case ProductListType.HTTP_USER_CONTRIBUTOR:
case ProductListType.HTTP_USER_INFORMER:
case ProductListType.HTTP_USER_PHOTOGRAPHER:
case ProductListType.HTTP_USER_TO_BE_COMPLETED:
case ProductListType.USER:
return false;
case ProductListType.SCAN_SESSION:
Expand All @@ -167,6 +231,10 @@ class ProductList {
return parameters;
case ProductListType.HTTP_SEARCH_KEYWORDS:
case ProductListType.HTTP_SEARCH_CATEGORY:
case ProductListType.HTTP_USER_CONTRIBUTOR:
case ProductListType.HTTP_USER_INFORMER:
case ProductListType.HTTP_USER_PHOTOGRAPHER:
case ProductListType.HTTP_USER_TO_BE_COMPLETED:
return '$parameters,$pageSize,$pageNumber';
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/database/category_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:openfoodfacts/model/parameter/TagFilter.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_product_query.dart';
import 'package:smooth_app/database/paged_search_product_query.dart';

/// Back-end query about a category.
class CategoryProductQuery extends PagedProductQuery {
class CategoryProductQuery extends PagedSearchProductQuery {
CategoryProductQuery(this.categoryTag);

// e.g. 'en:unsweetened-natural-soy-milks'
Expand Down
21 changes: 21 additions & 0 deletions packages/smooth_app/lib/database/contributor_product_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_user_product_query.dart';

/// User Product Search, in "contributor" mode.
class ContributorProductQuery extends PagedUserProductQuery {
ContributorProductQuery(final String userId) : super(userId);

@override
ProductList getProductList() => ProductList.contributor(
userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

@override
String getPath() => 'contributor/$userId.json';

@override
String toString() =>
'ContributorProductQuery("$userId", $pageSize, $pageNumber)';
}
21 changes: 21 additions & 0 deletions packages/smooth_app/lib/database/informer_product_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_user_product_query.dart';

/// User Product Search, in "informer" mode.
class InformerProductQuery extends PagedUserProductQuery {
InformerProductQuery(final String userId) : super(userId);

@override
ProductList getProductList() => ProductList.informer(
userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

@override
String getPath() => 'informer/$userId.json';

@override
String toString() =>
'InformerProductQuery("$userId", $pageSize, $pageNumber)';
}
4 changes: 2 additions & 2 deletions packages/smooth_app/lib/database/keywords_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:openfoodfacts/model/parameter/SearchTerms.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_product_query.dart';
import 'package:smooth_app/database/paged_search_product_query.dart';

/// Back-end query around user-entered keywords.
class KeywordsProductQuery extends PagedProductQuery {
class KeywordsProductQuery extends PagedSearchProductQuery {
KeywordsProductQuery(this.keywords);

final String keywords;
Expand Down
19 changes: 0 additions & 19 deletions packages/smooth_app/lib/database/paged_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/database/product_query.dart';

/// Paged product query (with [pageSize] and [pageNumber]).
Expand All @@ -16,22 +15,4 @@ abstract class PagedProductQuery implements ProductQuery {
void toNextPage() => _pageNumber++;

void toTopPage() => _pageNumber = _startPageNumber;

Parameter getParameter();

@override
Future<SearchResult> getSearchResult() async =>
OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
ProductSearchQueryConfiguration(
fields: ProductQuery.fields,
parametersList: <Parameter>[
PageSize(size: pageSize),
PageNumber(page: _pageNumber),
getParameter(),
],
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
),
);
}
24 changes: 24 additions & 0 deletions packages/smooth_app/lib/database/paged_search_product_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/database/paged_product_query.dart';
import 'package:smooth_app/database/product_query.dart';

/// Back-end paged queries around search.
abstract class PagedSearchProductQuery extends PagedProductQuery {
Parameter getParameter();

@override
Future<SearchResult> getSearchResult() async =>
OpenFoodAPIClient.searchProducts(
ProductQuery.getUser(),
ProductSearchQueryConfiguration(
fields: ProductQuery.fields,
parametersList: <Parameter>[
PageSize(size: pageSize),
PageNumber(page: pageNumber),
getParameter(),
],
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
),
);
}
59 changes: 59 additions & 0 deletions packages/smooth_app/lib/database/paged_user_product_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:openfoodfacts/utils/QueryType.dart';
import 'package:openfoodfacts/utils/UriHelper.dart';
import 'package:smooth_app/database/paged_product_query.dart';
import 'package:smooth_app/database/product_query.dart';

/// Back-end paged queries around User.
abstract class PagedUserProductQuery extends PagedProductQuery {
PagedUserProductQuery(this.userId);

final String userId;

@override
Future<SearchResult> getSearchResult() async => _searchProducts(
getPath(),
ProductQuery.getUser(),
pageSize,
pageNumber,
OpenFoodAPIConfiguration.globalQueryType,
);

String getPath();

static Future<SearchResult> _searchProducts(
// TODO(monsieurtanuki): move to off-dart, but probably not as is
final String path,
final User user,
final int pageSize,
final int pageNumber,
final QueryType? queryType,
) async {
final Uri uri = UriHelper.getUri(
path: path,
queryType: queryType,
queryParameters: <String, String>{
'page_size': '$pageSize',
'page': '$pageNumber',
},
);
final Response response = await HttpHelper().doGetRequest(
uri,
queryType: queryType,
user: OpenFoodAPIConfiguration.globalUser,
);
final String jsonStr = response
.body; // TODO(monsieurtanuki): what about _replaceQuotes(response.body);
final SearchResult result = SearchResult.fromJson(
json.decode(jsonStr) as Map<String, dynamic>,
);

// TODO(monsieurtanuki): what about _removeImages(result, configuration);

return result;
}
}
21 changes: 21 additions & 0 deletions packages/smooth_app/lib/database/photographer_product_query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_user_product_query.dart';

/// User Product Search, in "photographer" mode.
class PhotographerProductQuery extends PagedUserProductQuery {
PhotographerProductQuery(final String userId) : super(userId);

@override
ProductList getProductList() => ProductList.photographer(
userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

@override
String getPath() => 'photographer/$userId.json';

@override
String toString() =>
'PhotographerProductQuery("$userId", $pageSize, $pageNumber)';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/paged_user_product_query.dart';

/// User Product Search, in "to be completed" mode.
class ToBeCompletedProductQuery extends PagedUserProductQuery {
ToBeCompletedProductQuery(final String userId) : super(userId);

@override
ProductList getProductList() => ProductList.toBeCompleted(
userId,
pageSize: pageSize,
pageNumber: pageNumber,
);

@override
String getPath() => 'informer/$userId/state/to-be-completed.json';

@override
String toString() =>
'ToBeCompletedProductQuery("$userId", $pageSize, $pageNumber)';
}
Loading

0 comments on commit 54166e5

Please sign in to comment.