Skip to content

Commit

Permalink
feat: #1145 - added a preference for "test env host" (#1204)
Browse files Browse the repository at this point in the history
Impacted files:
* `product_query.dart`: improved method `setQueryType` in order to include the new "test env host" preference
* `user_preferences.dart`: added getter/setter for `String` in dev mode
* `user_preferences_dev_mode.dart`: added a dialog for "test env host"
  • Loading branch information
monsieurtanuki authored Mar 13, 2022
1 parent 4ba2bec commit b16d050
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ class UserPreferences extends ChangeNotifier {
_sharedPreferences.setInt(tag, index);

int? getDevModeIndex(final String tag) => _sharedPreferences.getInt(tag);

Future<void> setDevModeString(final String tag, final String value) async =>
_sharedPreferences.setString(tag, value);

String? getDevModeString(final String tag) =>
_sharedPreferences.getString(tag);
}
18 changes: 12 additions & 6 deletions packages/smooth_app/lib/database/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ abstract class ProductQuery {
);

/// Sets the query type according to the current [UserPreferences]
static void setQueryType(final UserPreferences userPreferences) =>
OpenFoodAPIConfiguration.globalQueryType = userPreferences
.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) ??
true
? QueryType.PROD
: QueryType.TEST;
static void setQueryType(final UserPreferences userPreferences) {
OpenFoodAPIConfiguration.globalQueryType = userPreferences
.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) ??
true
? QueryType.PROD
: QueryType.TEST;
final String? testEnvHost = userPreferences
.getDevModeString(UserPreferencesDevMode.userPreferencesTestEnvHost);
if (testEnvHost != null) {
OpenFoodAPIConfiguration.uriTestHost = testEnvHost;
}
}

static List<ProductField> get fields => <ProductField>[
ProductField.NAME,
Expand Down
42 changes: 41 additions & 1 deletion packages/smooth_app/lib/pages/user_preferences_dev_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
);

static const String userPreferencesFlagProd = '__devWorkingOnProd';
static const String userPreferencesTestEnvHost = '__testEnvHost';
static const String userPreferencesFlagUseMLKit = '__useMLKit';
static const String userPreferencesFlagLenientMatching = '__lenientMatching';
static const String userPreferencesFlagAdditionalButton =
'__additionalButtonOnProductPage';
static const String userPreferencesEnumScanMode = '__scanMode';

final TextEditingController _textFieldController = TextEditingController();

@override
bool isCollapsedByDefault() => true;

Expand Down Expand Up @@ -84,7 +87,8 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
},
),
ListTile(
title: const Text('Switch between openfoodfacts.org and .net'),
title: const Text(
'Switch between openfoodfacts.org (PROD) and test env'),
subtitle: Text(
'Current query type is ${OpenFoodAPIConfiguration.globalQueryType}',
),
Expand All @@ -95,6 +99,13 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
setState(() {});
},
),
ListTile(
title: const Text('Test env parameters'),
subtitle: Text(
'Current base URL of test env is ${OpenFoodAPIConfiguration.uriScheme}://${OpenFoodAPIConfiguration.uriTestHost}/',
),
onTap: () async => _changeTestEnvHost(),
),
SwitchListTile(
title: const Text('Use ML Kit'),
subtitle: const Text('then you have to restart this app'),
Expand Down Expand Up @@ -221,6 +232,35 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
},
),
];

Future<void> _changeTestEnvHost() async {
_textFieldController.text =
userPreferences.getDevModeString(userPreferencesTestEnvHost) ??
OpenFoodAPIConfiguration.uriTestHost;
final bool? result = await showDialog<bool>(
context: context,
builder: (final BuildContext context) => AlertDialog(
title: const Text('Test Env Host'),
content: TextField(controller: _textFieldController),
actions: <Widget>[
TextButton(
child: const Text('Cancel'),
onPressed: () => Navigator.pop(context, false),
),
ElevatedButton(
child: const Text('OK'),
onPressed: () => Navigator.pop(context, true),
),
],
),
);
if (result == true) {
await userPreferences.setDevModeString(
userPreferencesTestEnvHost, _textFieldController.text);
ProductQuery.setQueryType(userPreferences);
setState(() {});
}
}
}

enum DevModeScanMode {
Expand Down

0 comments on commit b16d050

Please sign in to comment.