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: 4941 - refresh products when switching app language #5016

Merged
merged 12 commits into from
Feb 24, 2024

Conversation

monsieurtanuki
Copy link
Contributor

What

  • New background task that re-downloads all the products that are not in the app language, with a page size of 20, ordered by decreasing interest (recently last accessed first).
  • This task is called when the user changes the app language.

Screenshots

Even the pictures are different!

app in French after an app switch to Italian
Screenshot_1706367088 Screenshot_1706367050

For the record, those are the barcodes I used that have different names/images in Italian/French:

  • 3046920029759
  • 8002270014901
  • 20267605
  • 5411188124689
  • 4056489216162
  • 5411188112709

Fixes bug(s)

Part of

Files

New file:

  • background_task_language_refresh.dart: Background task about downloading products to translate.

Impacted files:

  • background_task_manager.dart: minor refactoring
  • dao_product.dart: new method getTopProductsToTranslate
  • local_database.dart: minor refactoring
  • offline_tasks_page.dart: minor refactoring
  • operation_type.dart: new operation type languageRefresh
  • product_refresher.dart: minor refactoring
  • up_to_date_mixin.dart: minor refactoring
  • up_to_date_product_provider.dart: minor refactoring
  • user_preferences_language_selector.dart: now calling new task BackgroundTaskLanguageRefresh when changing language

Special thanks to @LuanRoger for the "last accessed timestamp" idea!

New file:
* `background_task_language_refresh.dart`: Background task about downloading products to translate.

Impacted files:
* `background_task_manager.dart`: minor refactoring
* `dao_product.dart`: new method `getTopProductsToTranslate`
* `local_database.dart`: minor refactoring
* `offline_tasks_page.dart`: minor refactoring
* `operation_type.dart`: new operation type `languageRefresh`
* `product_refresher.dart`: minor refactoring
* `up_to_date_mixin.dart`: minor refactoring
* `up_to_date_product_provider.dart`: minor refactoring
* `user_preferences_language_selector.dart`: now calling new task `BackgroundTaskLanguageRefresh` when changing language
@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2024

Codecov Report

Attention: Patch coverage is 0% with 75 lines in your changes are missing coverage. Please review.

Project coverage is 9.51%. Comparing base (4d9c7fc) to head (ff79443).
Report is 33 commits behind head on develop.

Files Patch % Lines
...b/background/background_task_language_refresh.dart 0.00% 46 Missing ⚠️
packages/smooth_app/lib/database/dao_product.dart 0.00% 11 Missing ⚠️
...th_app/lib/background/background_task_manager.dart 0.00% 6 Missing ⚠️
...references/user_preferences_language_selector.dart 0.00% 6 Missing ⚠️
...ages/smooth_app/lib/background/operation_type.dart 0.00% 3 Missing ⚠️
...s/smooth_app/lib/data_models/up_to_date_mixin.dart 0.00% 1 Missing ⚠️
...ckages/smooth_app/lib/database/local_database.dart 0.00% 1 Missing ⚠️
...kages/smooth_app/lib/pages/offline_tasks_page.dart 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           develop   #5016      +/-   ##
==========================================
- Coverage     9.54%   9.51%   -0.04%     
==========================================
  Files          325     327       +2     
  Lines        16411   16495      +84     
==========================================
+ Hits          1567    1569       +2     
- Misses       14844   14926      +82     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Future<void> preExecute(final LocalDatabase localDatabase) async {}

@override
bool get hasImmediateNextTask => true;
Copy link
Member

Choose a reason for hiding this comment

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

It's not always the case. I'm again doing a mobile review so I can't double-check the implications but I guess it's not causing any problems, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bool get hasImmediateNextTask => true;

@M123-dev When we page our background tasks, task A will deal with only say 10 products and at the end will say "I know there are more products to deal with and I create task B". In that case we need the background manager to immediately restart, to deal with task B.
In case we have no more products to deal with, no additional task will be created and the manager will see an empty task queue, and do nothing.

As opposed to standard one-shot tasks (e.g. product detail change), that are not somehow chained to other tasks to be run immediately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm again doing a mobile review so I can't double-check the implications

Many thanks @M123-dev, that's not the easiest PR to review on mobile ;)

Comment on lines +127 to +129
// ...and remove barcodes we actually found on the server.
for (final Product product in searchResult.products!) {
newExcludeBarcodes.remove(product.barcode);
Copy link
Member

Choose a reason for hiding this comment

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

Say we try to refresh a product that was removed in the time before the refresh, then we'll have a lopped until the Cannot refresh language exception

Let's remove the ones we queried, not the ones we received

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's remove the ones we queried, not the ones we received

First we add all the barcodes we expected, and then remove all the barcodes we received. In the end we exclude the barcodes we expected but haven't received.
Here we're dealing with an exclusion barcode list: we need to tell the next task "I have already tried this barcode but received nothing".

    // ...add the new barcodes...
    newExcludeBarcodes.addAll(barcodes);
    // ...and remove barcodes we actually found on the server.
    for (final Product product in searchResult.products!) {
      newExcludeBarcodes.remove(product.barcode);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@M123-dev Just to clarify a bit:

  1. we run a query in the local database - "which products are not yet in the expected language?"
  2. we maintain a list of "excluded" barcodes - barcodes we already tried to download but were not returned by the server
  3. the products returned by the server won't get listed again by the (1) query, as they are now flagged with the expected language
  4. and then we go back to (1) query in the next background task, looking for products not in the expected language and not queried yet.

Copy link
Member

Choose a reason for hiding this comment

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

Oh okay, yeah makes more sense reading it now

Copy link
Member

@M123-dev M123-dev left a comment

Choose a reason for hiding this comment

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

Thanks a lot @monsieurtanuki

@monsieurtanuki monsieurtanuki merged commit f98bce1 into openfoodfacts:develop Feb 24, 2024
6 checks passed
@monsieurtanuki
Copy link
Contributor Author

Thank you very much @M123-dev for your review!

@monsieurtanuki
Copy link
Contributor Author

@teolemon Had the opportunity to test the automatic product refresh when the user changes the app language?

@teolemon
Copy link
Member

Yes, it seems to work well. 👍

  • When it comes to internationalisation, the next step is sensible default country on product addition, and fixing the fact that the app forgets the language of the front image for the rest of the addition
  • For refresh, the target is refresh everything to Nutriscore v2, with the ability to test it for privileged users before going public.

@monsieurtanuki
Copy link
Contributor Author

When it comes to internationalisation, the next step is

  • sensible default country on product addition

We don't have a default country, do we? We have countries where the product was erm... produced and where the product is sold. Which one would that be?
Do you rather mean default language?

  • and fixing the fact that the app forgets the language of the front image for the rest of the addition

That could be somehow already solved by #5025 (and incidentally later by #4771), cf. LanguagePriority.
Could you please test again, and precise which use-cases are problematic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants