Skip to content

Commit

Permalink
feat: 6025 - settings for the folksonomy server (#6027)
Browse files Browse the repository at this point in the history
Impacted files:
* `folksonomy_provider.dart`: used a specific folksonomy server instead of systematically the prod server
* `product_query.dart`: specific folksonomy server
* `user_preferences_dev_mode.dart`: settings for the specific folksonomy server
  • Loading branch information
monsieurtanuki authored Dec 12, 2024
1 parent 5757e9b commit 736f08c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
36 changes: 25 additions & 11 deletions packages/smooth_app/lib/pages/folksonomy/folksonomy_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/query/product_query.dart';

class FolksonomyProvider extends ChangeNotifier {
FolksonomyProvider(this.barcode) {
Expand Down Expand Up @@ -30,6 +31,7 @@ class FolksonomyProvider extends ChangeNotifier {
await FolksonomyAPIClient.getAuthenticationToken(
username: user.userId,
password: user.password,
uriHelper: ProductQuery.uriFolksonomyHelper,
);

if (token.isError) {
Expand All @@ -48,7 +50,10 @@ class FolksonomyProvider extends ChangeNotifier {

Future<void> fetchProductTags() async {
try {
_productTags = await FolksonomyAPIClient.getProductTags(barcode: barcode);
_productTags = await FolksonomyAPIClient.getProductTags(
barcode: barcode,
uriHelper: ProductQuery.uriFolksonomyHelper,
);
_isLoading = false;
} catch (e) {
_error = e.toString();
Expand All @@ -62,7 +67,12 @@ class FolksonomyProvider extends ChangeNotifier {
final String bearerToken = await getBearerToken();
// to-do: The addProduct tag method does not yet have a way to add a comment.
await FolksonomyAPIClient.addProductTag(
barcode: barcode, key: key, value: value, bearerToken: bearerToken);
barcode: barcode,
key: key,
value: value,
bearerToken: bearerToken,
uriHelper: ProductQuery.uriFolksonomyHelper,
);
_productTags?[key] = ProductTag(
barcode: barcode,
key: key,
Expand All @@ -85,11 +95,13 @@ class FolksonomyProvider extends ChangeNotifier {

final int newVersion = (_productTags?[key]?.version ?? 0) + 1;
await FolksonomyAPIClient.updateProductTag(
barcode: barcode,
key: key,
value: newValue,
version: newVersion,
bearerToken: bearerToken);
barcode: barcode,
key: key,
value: newValue,
version: newVersion,
bearerToken: bearerToken,
uriHelper: ProductQuery.uriFolksonomyHelper,
);
_productTags?[key] = ProductTag(
barcode: barcode,
key: key,
Expand All @@ -111,10 +123,12 @@ class FolksonomyProvider extends ChangeNotifier {
final String bearerToken = await getBearerToken();
final int tagVersion = _productTags?[key]?.version ?? 0;
await FolksonomyAPIClient.deleteProductTag(
barcode: barcode,
key: key,
version: tagVersion,
bearerToken: bearerToken);
barcode: barcode,
key: key,
version: tagVersion,
bearerToken: bearerToken,
uriHelper: ProductQuery.uriFolksonomyHelper,
);
_productTags?.remove(key);
} catch (e) {
_error = e.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
static const String userPreferencesFlagProd = '__devWorkingOnProd';
static const String userPreferencesFlagPriceProd = '__devWorkingOnPricesProd';
static const String userPreferencesTestEnvDomain = '__testEnvHost';
static const String userPreferencesFolksonomyHost = '__folksonomyHost';
static const String userPreferencesFlagEditIngredients = '__editIngredients';
static const String userPreferencesFlagHideFolksonomy = '__hideFolksonomy';
static const String userPreferencesFlagBoostedComparison =
Expand Down Expand Up @@ -292,6 +293,14 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
],
),
),
const UserPreferencesItemSection(
label: 'Folksonomy Server configuration',
),
UserPreferencesItemTile(
title: 'Folksonomy host',
subtitle: ProductQuery.uriFolksonomyHelper.host,
onTap: () async => _changeFolksonomyHost(),
),
UserPreferencesItemSection(
label: appLocalizations.dev_mode_section_news,
),
Expand Down Expand Up @@ -534,4 +543,30 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
ProductQuery.setQueryType(userPreferences);
}
}

Future<void> _changeFolksonomyHost() async {
_textFieldController.text = ProductQuery.uriFolksonomyHelper.host;
final String? result = await showDialog<String>(
context: context,
builder: (final BuildContext context) => SmoothAlertDialog(
title: 'Folksonomy host',
body: TextField(controller: _textFieldController),
negativeAction: SmoothActionButton(
text: appLocalizations.cancel,
onPressed: () => Navigator.pop(context),
),
positiveAction: SmoothActionButton(
text: appLocalizations.okay,
onPressed: () => Navigator.pop(context, _textFieldController.text),
),
),
);
if (result != null) {
await userPreferences.setDevModeString(
userPreferencesFolksonomyHost,
result,
);
ProductQuery.setQueryType(userPreferences);
}
}
}
9 changes: 9 additions & 0 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ abstract class ProductQuery {
/// Product helper only for prices.
static late UriProductHelper uriPricesHelper;

/// Product helper only for Folksonomy.
static late UriHelper uriFolksonomyHelper;

static bool isLoggedIn() => OpenFoodAPIConfiguration.globalUser != null;

/// Sets the query type according to the current [UserPreferences]
Expand All @@ -187,6 +190,12 @@ abstract class ProductQuery {
uriPricesHelper = getProductHelper(
UserPreferencesDevMode.userPreferencesFlagPriceProd,
);
uriFolksonomyHelper = UriHelper(
host: userPreferences.getDevModeString(
UserPreferencesDevMode.userPreferencesFolksonomyHost,
) ??
uriHelperFolksonomyProd.host,
);
}

/// Returns the standard test env, or the custom test env if relevant.
Expand Down

0 comments on commit 736f08c

Please sign in to comment.