Skip to content

Commit

Permalink
feat: openfoodfacts#848 - dev mode - item that switches the QueryType…
Browse files Browse the repository at this point in the history
… to PROD and DEV

Impacted files:
* `knowledge_panels_query.dart`: not using systematically `QueryType.PROD` anymore
* `main.dart`: at init time, sets the correct query type from preferences
* `product_query.dart`: new method `setQueryType` that uses the preferences to set the query type
* `user_preferences_dev_mode.dart`: added an item that switches the `QueryType` to `PROD` and `DEV`
  • Loading branch information
monsieurtanuki committed Jan 4, 2022
1 parent dd4338a commit ef4b21a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/smooth_app/lib/database/knowledge_panels_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/QueryType.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:smooth_app/database/product_query.dart';

class KnowledgePanelsQuery {
Expand All @@ -18,6 +18,9 @@ class KnowledgePanelsQuery {
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
);
return OpenFoodAPIClient.getKnowledgePanels(configuration, QueryType.PROD);
return OpenFoodAPIClient.getKnowledgePanels(
configuration,
OpenFoodAPIConfiguration.globalQueryType,
);
}
}
11 changes: 11 additions & 0 deletions packages/smooth_app/lib/database/product_query.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:openfoodfacts/utils/QueryType.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/pages/user_preferences_dev_mode.dart';

abstract class ProductQuery {
/// Returns the global language for API queries.
Expand Down Expand Up @@ -39,6 +42,14 @@ abstract class ProductQuery {
comment: 'Test user for project smoothie',
);

/// 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 List<ProductField> get fields => <ProductField>[
ProductField.NAME,
ProductField.BRANDS,
Expand Down
1 change: 1 addition & 0 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class _SmoothAppState extends State<SmoothApp> {
ProductQuery.setCountry(_userPreferences.userCountryCode);
_localDatabase = await LocalDatabase.getLocalDatabase();
_themeProvider = ThemeProvider(_userPreferences);
ProductQuery.setQueryType(_userPreferences);
}

@override
Expand Down
20 changes: 20 additions & 0 deletions packages/smooth_app/lib/pages/user_preferences_dev_mode.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/pages/abstract_user_preferences.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';

Expand Down Expand Up @@ -28,6 +30,8 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
themeData: themeData,
);

static const String userPreferencesFlagProd = '__devWorkingOnProd';

@override
bool isCollapsedByDefault() => true;

Expand All @@ -51,7 +55,11 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
ListTile(
title: const Text('Remove dev mode'),
onTap: () async {
// resetting back to "no dev mode"
await userPreferences.setDevMode(0);
// resetting back to PROD
await userPreferences.setFlag(userPreferencesFlagProd, true);
ProductQuery.setQueryType(userPreferences);
setState(() {});
},
),
Expand All @@ -64,5 +72,17 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
setState(() {});
},
),
ListTile(
title: const Text('switch query type'),
subtitle: Text(
'current value is ${OpenFoodAPIConfiguration.globalQueryType}',
),
onTap: () async {
await userPreferences.setFlag(userPreferencesFlagProd,
!(userPreferences.getFlag(userPreferencesFlagProd) ?? true));
ProductQuery.setQueryType(userPreferences);
setState(() {});
},
),
];
}

0 comments on commit ef4b21a

Please sign in to comment.