Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Spellchecker for OCR screens #5409

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,14 @@
"@dev_mode_hide_ecoscore_title": {
"description": "User dev preferences - Disable Ecoscore - Title"
},
"dev_mode_spellchecker_for_ocr_title": "Use a spellchecker for OCR screens",
"@dev_mode_spellchecker_for_ocr_title": {
"description": "User dev preferences - Enable Spellchecker on OCR screens - Title"
},
"dev_mode_spellchecker_for_ocr_subtitle": "(Ingredients and packaging)",
"@dev_mode_spellchecker_for_ocr_subtitle": {
"description": "User dev preferences - Enable Spellchecker on OCR screens - Subtitle"
},
"search_history_item_edit_tooltip": "Reuse and edit this search",
"@search_history_item_edit_tooltip": {
"description": "A tooltip to explain the Pen button near a search term -> it allows to reuse the item"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
static const String userPreferencesFlagAccessibilityEmoji =
'__accessibilityEmoji';
static const String userPreferencesFlagUserOrderedKP = '__userOrderedKP';
static const String userPreferencesFlagSpellCheckerOnOcr =
'__spellcheckerOcr';

final TextEditingController _textFieldController = TextEditingController();

Expand Down Expand Up @@ -325,6 +327,17 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
_showSuccessMessage();
},
),
UserPreferencesItemSwitch(
title: appLocalizations.dev_mode_spellchecker_for_ocr_title,
subtitle: appLocalizations.dev_mode_spellchecker_for_ocr_subtitle,
value:
userPreferences.getFlag(userPreferencesFlagSpellCheckerOnOcr) ??
false,
onChanged: (bool value) async => userPreferences.setFlag(
userPreferencesFlagSpellCheckerOnOcr,
value,
),
),
UserPreferencesItemSection(
label: appLocalizations.dev_mode_section_experimental_features,
),
Expand Down
65 changes: 54 additions & 11 deletions packages/smooth_app/lib/pages/product/edit_ocr/edit_ocr_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:shimmer/shimmer.dart';
import 'package:smooth_app/background/background_task_details.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/up_to_date_mixin.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/transient_file.dart';
Expand All @@ -13,13 +16,16 @@ import 'package:smooth_app/generic_lib/loading_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/picture_not_found.dart';
import 'package:smooth_app/helpers/analytics_helper.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/helpers/provider_helper.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/preferences/user_preferences_dev_mode.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/edit_ocr/ocr_helper.dart';
import 'package:smooth_app/pages/product/explanation_widget.dart';
import 'package:smooth_app/pages/product/multilingual_helper.dart';
import 'package:smooth_app/pages/product/product_image_button.dart';
import 'package:smooth_app/themes/smooth_theme_colors.dart';
import 'package:smooth_app/themes/theme_provider.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';

part 'edit_ocr_main_action.dart';
Expand Down Expand Up @@ -295,17 +301,54 @@ class _EditOcrPageState extends State<EditOcrPage> with UpToDateMixin {
state: _extractState(transientFile),
),
const SizedBox(height: MEDIUM_SPACE),
TextField(
controller: _controller,
decoration: InputDecoration(
fillColor: Colors.white.withOpacity(0.2),
filled: true,
enabledBorder: const OutlineInputBorder(
borderRadius: ANGULAR_BORDER_RADIUS,
),
),
maxLines: null,
textInputAction: TextInputAction.newline,
ConsumerFilter<UserPreferences>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks a bit complex to me. A context.watch<UserPreferences>() would do the job, wouldn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A context.watch redraws the full Widget (that's a giant flaw in the code right now).
Here, it will only rebuild the field.

Copy link
Contributor

Choose a reason for hiding this comment

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

A context.watch redraws the full Widget

In that specific case, every time the user preferences change, right? Doesn't happen that often.
If really that bothers you, what about a specific private class with a watch for the text field?

Tbh I'm getting confused with all that mix of provider, consumer, consumerfilter, changenotifier and so on.
Confusion leads to low maintainability and bugs. It wouldn't be a problem if there were many reviewers with expertise in flutter. If we want to be able to integrate more contributors, the less flutter-specific we code, the better we are. My 2 cents.

buildWhen: (
UserPreferences? previousValue,
UserPreferences currentValue,
) {
return previousValue?.getFlag(UserPreferencesDevMode
.userPreferencesFlagSpellCheckerOnOcr) !=
currentValue.getFlag(UserPreferencesDevMode
.userPreferencesFlagSpellCheckerOnOcr);
},
builder: (
BuildContext context,
UserPreferences prefs,
Widget? child,
) {
final ThemeData theme = Theme.of(context);

return Theme(
data: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(
onSurface: context
.read<ThemeProvider>()
.isDarkMode(context)
? Colors.white
: Colors.black,
),
),
child: TextField(
controller: _controller,
decoration: InputDecoration(
fillColor: Colors.white.withOpacity(0.2),
filled: true,
enabledBorder: const OutlineInputBorder(
borderRadius: ANGULAR_BORDER_RADIUS,
),
),
maxLines: null,
textInputAction: TextInputAction.newline,
spellCheckConfiguration: (prefs.getFlag(
UserPreferencesDevMode
.userPreferencesFlagSpellCheckerOnOcr) ??
false) &&
(Platform.isAndroid || Platform.isIOS)
? const SpellCheckConfiguration()
: const SpellCheckConfiguration.disabled(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
: const SpellCheckConfiguration.disabled(),
: null,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

null and SpellCheckConfiguration.disabled() have different meanings here:

  • null = inherits from the config
  • SpellCheckConfiguration.disabled() = I don't want it at all

Copy link
Contributor

Choose a reason for hiding this comment

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

null and SpellCheckConfiguration.disabled()

I understand your point.
The thing is that in the app we already have tons of TextField, with implicitly null spellCheckConfigurations (or "inherits from the config").
I would say that this field should behave as before and should behave as the other TextFields of the app. Unless the user selects the new dev mode switch. Therefore null.

),
);
},
),
const SizedBox(height: SMALL_SPACE),
ExplanationWidget(
Expand Down
Loading