forked from openfoodfacts/smooth-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 5741 - respecting the "search products" access limitations
New file: * `search_products_manager.dart`: Management of "search products" access limitations. Impacted files: * `background_task_download_products.dart`: refactored using `SearchProductsManager` * `background_task_language_refresh.dart`: refactored using `SearchProductsManager` * `background_task_top_barcodes.dart`: refactored using `SearchProductsManager` * `lazy_counter.dart`: refactored using `SearchProductsManager` * `paged_product_query.dart`: refactored using `SearchProductsManager` * `product_list_page.dart`: refactored using `SearchProductsManager` * `product_refresher.dart`: refactored using `SearchProductsManager` * `pubspec.lock`: wtf
- Loading branch information
1 parent
e3fa4f4
commit 3ba9a48
Showing
9 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/smooth_app/lib/query/search_products_manager.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
|
||
/// Type of "search products" action. | ||
enum SearchProductsType { | ||
/// For heavy background tasks like offline downloads. | ||
background, | ||
|
||
/// For live user interactions in the app. | ||
live, | ||
|
||
/// For counts like "number of products I edited". | ||
count; | ||
|
||
/// General threshold: 10 requests per minute. | ||
static final TooManyRequestsManager _generalRequestManager = | ||
TooManyRequestsManager( | ||
maxCount: 10, | ||
duration: const Duration(minutes: 1), | ||
); | ||
|
||
/// Specific threshold for background tasks. | ||
static final TooManyRequestsManager _backgroundRequestManager = | ||
TooManyRequestsManager( | ||
maxCount: 3, | ||
duration: const Duration(minutes: 1), | ||
); | ||
|
||
/// Specific threshold for count tasks. | ||
/// | ||
/// For the record there are currently 4 counts displayed. | ||
static final TooManyRequestsManager _countRequestManager = | ||
TooManyRequestsManager( | ||
maxCount: 4, | ||
duration: const Duration(minutes: 1), | ||
); | ||
|
||
Future<void> _specificWaitIfNeeded() async => switch (this) { | ||
SearchProductsType.background => | ||
_backgroundRequestManager.waitIfNeeded(), | ||
SearchProductsType.count => _countRequestManager.waitIfNeeded(), | ||
SearchProductsType.live => null, | ||
}; | ||
|
||
Future<void> waitIfNeeded() async { | ||
await _specificWaitIfNeeded(); | ||
await _generalRequestManager.waitIfNeeded(); | ||
} | ||
} | ||
|
||
/// Management of "search products" access limitations. | ||
class SearchProductsManager { | ||
SearchProductsManager._(); | ||
|
||
static Future<SearchResult> searchProducts( | ||
final User? user, | ||
final AbstractQueryConfiguration configuration, { | ||
required final UriProductHelper uriHelper, | ||
required final SearchProductsType type, | ||
}) async { | ||
await type.waitIfNeeded(); | ||
return OpenFoodAPIClient.searchProducts( | ||
user, | ||
configuration, | ||
uriHelper: uriHelper, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters