Skip to content
Open
130 changes: 130 additions & 0 deletions packages/smooth_app/lib/database/dao_autocomplete.dart
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's typically why I asked you to create 2 separate files. Which for some reason you ignored, and I'm a bit puzzled with that.
When I review, the whole file is considered as changed, and I have to read again ALL the file, when you may have simply edited one class.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import 'dart:async';
import 'dart:convert';

import 'package:smooth_app/database/abstract_sql_dao.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:sqflite/sqflite.dart';

class DaoAutocomplete extends AbstractSqlDao {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more clear if you had one dao per table. Like in the rest of the project btw.

DaoAutocomplete(super.localDatabase);

// autocomplete_namespace table
static const String _TABLE_NAMESPACE = 'autocomplete_namespace';
static const String _TABLE_NAMESPACE_COLUMN_ID = 'id';
static const String _TABLE_NAMESPACE_COLUMN_NAMESPACE = 'namespace';

// autocomplete_cache table
static const String _TABLE_CACHE = 'autocomplete_cache';
static const String _TABLE_CACHE_COLUMN_NAMESPACE_ID = 'namespace_id';
static const String _TABLE_CACHE_COLUMN_QUERY = 'query';
static const String _TABLE_CACHE_COLUMN_RESULTS = 'results';
static const String _TABLE_CACHE_COLUMN_LAST_UPDATE = 'last_update';

static const List<String> _columnsCache = <String>[
_TABLE_CACHE_COLUMN_NAMESPACE_ID,
_TABLE_CACHE_COLUMN_QUERY,
_TABLE_CACHE_COLUMN_RESULTS,
_TABLE_CACHE_COLUMN_LAST_UPDATE,
];

/// Session-level in-memory cache for namespace string → integer id.
/// Eliminates redundant DB lookups - there are only 7-22 namespaces
/// for a typical user so this map stays tiny for the lifetime of the
/// app process.
static final Map<String, int> _namespaceCache = <String, int>{};

static FutureOr<void> onUpgrade(
final Database db,
final int oldVersion,
final int newVersion,
) async {
if (oldVersion < 10) {
await db.execute(
'create table $_TABLE_NAMESPACE('
'$_TABLE_NAMESPACE_COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT'
',$_TABLE_NAMESPACE_COLUMN_NAMESPACE TEXT NOT NULL UNIQUE'
')',
);
await db.execute(
'create index idx_namespace'
' on $_TABLE_NAMESPACE($_TABLE_NAMESPACE_COLUMN_NAMESPACE)',
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
);
await db.execute(
'create table $_TABLE_CACHE('
'$_TABLE_CACHE_COLUMN_NAMESPACE_ID INTEGER NOT NULL'
',$_TABLE_CACHE_COLUMN_QUERY TEXT NOT NULL'
',$_TABLE_CACHE_COLUMN_RESULTS TEXT NOT NULL'
',$_TABLE_CACHE_COLUMN_LAST_UPDATE INTEGER NOT NULL'
',PRIMARY KEY'
'($_TABLE_CACHE_COLUMN_NAMESPACE_ID'
',$_TABLE_CACHE_COLUMN_QUERY) ON CONFLICT REPLACE'
')',
);
}
}

/// Returns the namespace id for the given [namespace] string.
/// Creates a new row if the namespace does not exist yet.
/// Result is cached in memory so subsequent calls within the same
/// app session never hit the database.
Future<int> _getOrCreateNamespaceId(final String namespace) async {
final int? cached = _namespaceCache[namespace];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's the DAO's job to cache data. Just make DAO what they're meant for: simple access to database.

In the rest of the code, I imagine something like "oh we're looking for suggestions of category in English, let's get the namespace for it from DaoNamespace, and while we're on the page let's use that same namespace while we're on looking for suggestions."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DAOs are now plain DB access; caching logic moved to new SuggestionCache class

if (cached != null) {
return cached;
}
final List<Map<String, dynamic>> rows = await localDatabase.database.query(
_TABLE_NAMESPACE,
columns: <String>[_TABLE_NAMESPACE_COLUMN_ID],
where: '$_TABLE_NAMESPACE_COLUMN_NAMESPACE = ?',
whereArgs: <String>[namespace],
);
final int id;
if (rows.isNotEmpty) {
id = rows.first[_TABLE_NAMESPACE_COLUMN_ID] as int;
} else {
id = await localDatabase.database.insert(
_TABLE_NAMESPACE,
<String, dynamic>{_TABLE_NAMESPACE_COLUMN_NAMESPACE: namespace},
);
}
_namespaceCache[namespace] = id;
return id;
}

/// Returns cached results for the given [namespace] and [query].
/// Returns null if not found.
Future<List<String>?> getResults(
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
final String namespace,
final String query,
) async {
final int namespaceId = await _getOrCreateNamespaceId(namespace);
final List<Map<String, dynamic>> rows = await localDatabase.database.query(
_TABLE_CACHE,
columns: _columnsCache,
where:
'$_TABLE_CACHE_COLUMN_NAMESPACE_ID = ?'
' AND $_TABLE_CACHE_COLUMN_QUERY = ?',
whereArgs: <dynamic>[namespaceId, query],
);
if (rows.isEmpty) {
return null;
}
final String json = rows.first[_TABLE_CACHE_COLUMN_RESULTS] as String;
return (jsonDecode(json) as List<dynamic>).cast<String>();
}

/// Stores [results] for the given [namespace] and [query].
Future<void> storeResults(
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
final String namespace,
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
final String query,
final List<String> results,
) async {
final int namespaceId = await _getOrCreateNamespaceId(namespace);
await localDatabase.database.insert(_TABLE_CACHE, <String, dynamic>{
_TABLE_CACHE_COLUMN_NAMESPACE_ID: namespaceId,
_TABLE_CACHE_COLUMN_QUERY: query,
_TABLE_CACHE_COLUMN_RESULTS: jsonEncode(results),
_TABLE_CACHE_COLUMN_LAST_UPDATE: LocalDatabase.nowInMillis(),
}, conflictAlgorithm: ConflictAlgorithm.replace);
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
}
}
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/database/local_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smooth_app/background/background_task_manager.dart';
import 'package:smooth_app/data_models/up_to_date_product_list_provider.dart';
import 'package:smooth_app/data_models/up_to_date_product_provider.dart';
import 'package:smooth_app/database/abstract_dao.dart';
import 'package:smooth_app/database/dao_autocomplete.dart';
import 'package:smooth_app/database/dao_folksonomy.dart';
import 'package:smooth_app/database/dao_hive_product.dart';
import 'package:smooth_app/database/dao_instant_string.dart';
Expand Down Expand Up @@ -78,7 +79,7 @@ class LocalDatabase extends ChangeNotifier {
final String databasePath = join(databasesRootPath, 'smoothie.db');
final Database database = await openDatabase(
databasePath,
version: 9,
version: 10,
singleInstance: true,
onUpgrade: _onUpgrade,
);
Expand Down Expand Up @@ -125,5 +126,6 @@ class LocalDatabase extends ChangeNotifier {
await DaoProductLastAccess.onUpgrade(db, oldVersion, newVersion);
await DaoOsmLocation.onUpgrade(db, oldVersion, newVersion);
await DaoFolksonomy.onUpgrade(db, oldVersion, newVersion);
await DaoAutocomplete.onUpgrade(db, oldVersion, newVersion);
}
}
114 changes: 114 additions & 0 deletions packages/smooth_app/lib/pages/input/server_suggestion.dart
Comment thread
Sherley-Sonali marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/pages/folksonomy/folksonomy_autocompleter.dart';
import 'package:smooth_app/query/product_query.dart';

/// Abstract interface for server-backed autocomplete with cache namespace support.
abstract class ServerSuggestion {
String getNamespace(String soFar);
Comment thread
Sherley-Sonali marked this conversation as resolved.
Outdated
Future<List<String>> getSuggestionsFromServer(String soFar);
}

/// Implementation for TagType autocompleters (categories, labels, origins, etc.)
class TagTypeServerSuggestion implements ServerSuggestion {
TagTypeServerSuggestion({required this.tagType, required this.productType});

final TagType tagType;
final ProductType productType;

OpenFoodFactsLanguage get _language => ProductQuery.getLanguage();
OpenFoodFactsCountry? get _country => ProductQuery.getCountry();
UriProductHelper get _uriHelper =>
ProductQuery.getUriProductHelper(productType: productType);

@override
String getNamespace(String soFar) {
return '${_uriHelper.domain}|tagtype|${tagType.offTag}|${_language.offTag}|${_country?.offTag ?? ''}';
}

@override
Future<List<String>> getSuggestionsFromServer(String soFar) async {
final TagTypeAutocompleter autocompleter = TagTypeAutocompleter(
tagType: tagType,
language: _language,
country: _country,
uriHelper: _uriHelper,
limit: 15,
);
return autocompleter.getSuggestions(soFar);
}
}

/// Implementation for TaxonomyName autocompleters (brands)
class TaxonomyServerSuggestion implements ServerSuggestion {
TaxonomyServerSuggestion({
required this.taxonomyNames,
required this.productType,
});

final List<TaxonomyName> taxonomyNames;
final ProductType productType;

OpenFoodFactsLanguage get _language => OpenFoodFactsLanguage.ENGLISH;
UriProductHelper get _uriHelper =>
ProductQuery.getUriProductHelper(productType: productType);

@override
String getNamespace(String soFar) {
return '${_uriHelper.domain}|taxonomy|${taxonomyNames.map((t) => t.offTag).join(',')}|${_language.offTag}';
}

@override
Future<List<String>> getSuggestionsFromServer(String soFar) async {
final TaxonomyNameAutocompleter autocompleter = TaxonomyNameAutocompleter(
taxonomyNames: taxonomyNames,
language: _language,
uriHelper: _uriHelper,
limit: 25,
fuzziness: Fuzziness.none,
user: ProductQuery.getReadUser(),
);
return autocompleter.getSuggestions(soFar);
}
}

/// Implementation for folksonomy keys autocompleter
class FolksonomyKeysServerSuggestion implements ServerSuggestion {
const FolksonomyKeysServerSuggestion();

UriHelper get _uriHelper => ProductQuery.uriFolksonomyHelper;

@override
String getNamespace(String soFar) {
return '${_uriHelper.host}|folksonomy|keys';
}

@override
Future<List<String>> getSuggestionsFromServer(String soFar) async {
const FolksonomyKeysAutocompleter autocompleter =
FolksonomyKeysAutocompleter(limit: 10);
return autocompleter.getSuggestions(soFar);
}
}

/// Implementation for folksonomy values autocompleter
class FolksonomyValuesServerSuggestion implements ServerSuggestion {
FolksonomyValuesServerSuggestion({required String Function() keyProvider})
: _keyProvider = keyProvider;

final String Function() _keyProvider;

UriHelper get _uriHelper => ProductQuery.uriFolksonomyHelper;

@override
String getNamespace(String soFar) {
final String key = _keyProvider().trim();
return '${_uriHelper.host}|folksonomy|values|$key';
}

@override
Future<List<String>> getSuggestionsFromServer(String soFar) async {
final FolksonomyValuesAutocompleter autocompleter =
FolksonomyValuesAutocompleter(keyProvider: _keyProvider, limit: 10);
return autocompleter.getSuggestions(soFar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/database/dao_autocomplete.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart';
import 'package:smooth_app/helpers/strings_helper.dart';
import 'package:smooth_app/pages/input/debounced_text_editing_controller.dart';
import 'package:smooth_app/pages/input/server_suggestion.dart';
import 'package:smooth_app/pages/product/autocomplete.dart';

/// Autocomplete text field.
Expand All @@ -28,6 +32,7 @@ class SmoothAutocompleteTextField extends StatefulWidget {
this.textStyle,
this.textCapitalization,
this.onSelected,
this.serverSuggestion,
});

final FocusNode focusNode;
Expand All @@ -44,6 +49,7 @@ class SmoothAutocompleteTextField extends StatefulWidget {
final EdgeInsetsGeometry? padding;
final TextStyle? textStyle;
final TextCapitalization? textCapitalization;
final ServerSuggestion? serverSuggestion;

/// Additional specific action when a suggested item is selected.
final Function(String)? onSelected;
Expand Down Expand Up @@ -206,35 +212,48 @@ class _SmoothAutocompleteTextFieldState
return _SearchResults.empty();
}

final DateTime start = DateTime.now();

if (_suggestions[search] != null) {
return _suggestions[search]!;
} else if (widget.manager == null ||
search.length < widget.minLengthForSuggestions) {
} else if (search.length < widget.minLengthForSuggestions) {
_suggestions[search] = _SearchResults.empty();
return _suggestions[search]!;
}

_setLoading(true);

try {
_suggestions[search] = _SearchResults(
await widget.manager!.getSuggestions(search),
);
} catch (_) {}
final List<String> results;

if (_suggestions[search]?.isEmpty ?? true && search == _searchInput) {
// Use serverSuggestion if available
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there cases when it's not available?

if (widget.serverSuggestion != null) {
results = await widget.serverSuggestion!.getSuggestionsFromServer(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a separate method for that, in that page or in a distinct class, like I suggested earlier: "get the suggestions, either from the static, or from the database, or from the server".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SuggestionCache now owns namespace ID cache and orchestrates memory → DB → server lookup

search,
);
} else if (widget.manager != null) {
results = await widget.manager!.getSuggestions(search);
} else {
results = <String>[];
}

_suggestions[search] = _SearchResults(results);

// Store in persistent cache if serverSuggestion is provided and results not empty
if (widget.serverSuggestion != null && results.isNotEmpty) {
final String namespace = widget.serverSuggestion!.getNamespace(search);
final LocalDatabase localDb = context.read<LocalDatabase>();
await DaoAutocomplete(localDb).storeResults(namespace, search, results);
}
} catch (_) {
return _SearchResults.empty();
} finally {
_setLoading(false);
}
Comment thread
Sherley-Sonali marked this conversation as resolved.

if (_searchInput != search &&
start.difference(DateTime.now()).inSeconds > 5) {
// Ignore this request, it's too long and this is not even the current search
return _SearchResults.empty();
} else {
return _suggestions[search] ?? _SearchResults.empty();
if (_suggestions[search]?.isEmpty ?? true && search == _searchInput) {
_setLoading(false);
}

return _suggestions[search]!;
}
}

Expand Down
Loading
Loading