Skip to content

Commit

Permalink
feature/#147 - local look-up - first step towards autocomplete text f…
Browse files Browse the repository at this point in the history
…ield

Implemented a look-up on the local database, and linked it to the text field.
For the moment, the matching products are only printed in the console.
The next step would be to display the results with an autocomplete text field.

Impacted files:
* `dao_product.dart`: new method `List<Product> getSuggestions(String)`
* `home_page.dart`: added an `onUpdate` method on the text field to print the products that match locally
  • Loading branch information
monsieurtanuki committed Feb 27, 2021
1 parent 1112a17 commit f546254
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ class DaoProduct {
return result;
}

/// Returns the products that match a string
///
/// The search is actually ugly, but it's a start.
// TODO(monsieurtanuki): check only the fields that are deemed relevant
// TODO(monsieurtanuki): consider space-separated fields as distinct keywords
Future<List<Product>> getSuggestions(
final String pattern, final int minLength) async {
final List<Product> result = <Product>[];
if (pattern == null || pattern.trim().length < minLength) {
return result;
}
final List<Map<String, dynamic>> queryResults =
await localDatabase.database.query(
TABLE_PRODUCT,
columns: <String>[
TABLE_PRODUCT_COLUMN_BARCODE,
_TABLE_PRODUCT_COLUMN_JSON,
],
where: '$TABLE_PRODUCT_COLUMN_BARCODE like ?'
' or $_TABLE_PRODUCT_COLUMN_JSON like ?',
whereArgs: <String>['%$pattern%', '%$pattern%'],
);
if (queryResults.isEmpty) {
return result;
}
for (final Map<String, dynamic> row in queryResults) {
result.add(_getProductFromQueryResult(row));
}
return result;
}

Future<void> put(final Product product) async =>
await _upsert(product, localDatabase.database);

Expand Down
11 changes: 11 additions & 0 deletions packages/smooth_app/lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'package:openfoodfacts/model/Attribute.dart';
import 'package:smooth_app/data_models/user_preferences_model.dart';
import 'package:smooth_app/themes/smooth_theme.dart';
import 'package:smooth_app/cards/product_cards/product_list_preview.dart';
import 'package:openfoodfacts/model/Product.dart';

class HomePage extends StatefulWidget {
@override
Expand Down Expand Up @@ -93,6 +94,16 @@ class _HomePageState extends State<HomePage> {
),
),
title: TextField(
onChanged: (final String value) =>
_daoProduct.getSuggestions(value, 3).then(
(List<Product> list) {
print(
'${list.length} products locally found with $value:');
for (final Product item in list) {
print('${item.barcode}: ${item.productName}');
}
},
),
onSubmitted: (final String value) => ChoosePage.onSubmitted(
value,
context,
Expand Down

0 comments on commit f546254

Please sign in to comment.