Skip to content

Commit

Permalink
Don't force the product type with custom URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k committed Jan 29, 2025
1 parent f83b9e0 commit ab31535
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/smooth_app/lib/data_models/login_result.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/query/product_query.dart';

/// How did the login attempt work?
Expand Down Expand Up @@ -34,12 +36,19 @@ class LoginResult {
text.startsWith('Failed host lookup: ');

/// Checks credentials. Returns null if OK, or an error message.
static Future<LoginResult> getLoginResult(final User user) async {
static Future<LoginResult> getLoginResult(
final User user,
final UserPreferences userPreferences,
) async {
try {
final bool prodUrl = userPreferences
.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) ??
true;

final LoginStatus? loginStatus = await OpenFoodAPIClient.login2(
user,
uriHelper: ProductQuery.getUriProductHelper(
productType: ProductType.food,
productType: prodUrl ? ProductType.food : null,
),
);
if (loginStatus == null) {
Expand Down
14 changes: 11 additions & 3 deletions packages/smooth_app/lib/data_models/user_management_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/login_result.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/dao_secured_string.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/services/smooth_services.dart';
Expand All @@ -14,8 +15,14 @@ class UserManagementProvider with ChangeNotifier {
static const String _COOKIE = 'user_cookie';

/// Checks credentials and conditionally saves them.
Future<LoginResult> login(final User user) async {
final LoginResult loginResult = await LoginResult.getLoginResult(user);
Future<LoginResult> login(
final User user,
final UserPreferences preferences,
) async {
final LoginResult loginResult = await LoginResult.getLoginResult(
user,
preferences,
);
if (loginResult.type != LoginResultType.successful) {
return loginResult;
}
Expand Down Expand Up @@ -101,7 +108,7 @@ class UserManagementProvider with ChangeNotifier {

/// Check if the user is still logged in and the credentials are still valid
/// If not, the user is logged out
Future<void> checkUserLoginValidity() async {
Future<void> checkUserLoginValidity(UserPreferences preferences) async {
if (!ProductQuery.isLoggedIn()) {
return;
}
Expand All @@ -111,6 +118,7 @@ class UserManagementProvider with ChangeNotifier {
userId: user.userId,
password: user.password,
),
preferences,
);

if (loginResult.type == LoginResultType.unsuccessful) {
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Future<bool> _init1() async {
daoString: DaoString(_localDatabase),
);
ProductQuery.setQueryType(_userPreferences);
UserManagementProvider().checkUserLoginValidity();
UserManagementProvider().checkUserLoginValidity(_userPreferences);

await AnalyticsHelper.linkPreferences(_userPreferences);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class _LoginPageState extends State<LoginPage> with TraceableClientMixin {
userId: userIdController.text,
password: passwordController.text,
),
context.read<UserPreferences>(),
);
if (!context.mounted) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:smooth_app/generic_lib/loading_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_text_form_field.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/helpers/user_management_helper.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/smooth_app_bar.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';
Expand Down Expand Up @@ -331,6 +332,11 @@ class _SignUpPageState extends State<SignUpPage> with TraceableClientMixin {
userId: _userController.trimmedText,
password: _password1Controller.text,
);
final bool prodUrl = context
.read<UserPreferences>()
.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) ??
true;

final SignUpStatus? status = await LoadingDialog.run<SignUpStatus>(
context: context,
future: OpenFoodAPIClient.register(
Expand All @@ -342,7 +348,7 @@ class _SignUpPageState extends State<SignUpPage> with TraceableClientMixin {
country: ProductQuery.getCountry(),
language: ProductQuery.getLanguage(),
uriHelper: ProductQuery.getUriProductHelper(
productType: ProductType.food,
productType: prodUrl ? ProductType.food : null,
),
),
title: appLocalisations.sign_up_page_action_doing_it,
Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ abstract class ProductQuery {
);
}

static String getProductTypeFromDomain(UriProductHelper uriProductHelper) {
return uriProductHelper.domain;
}

static List<ProductField> get fields => const <ProductField>[
ProductField.NAME,
ProductField.NAME_ALL_LANGUAGES,
Expand Down

0 comments on commit ab31535

Please sign in to comment.