Skip to content

Commit

Permalink
feat: 5405 - 3 queues for background tasks (fast, slow, long haul) (#…
Browse files Browse the repository at this point in the history
…5743)

New file:
* `background_task_queue.dart`: Queues for Background Tasks.

Impacted files:
* `background_task.dart`: now we add tasks to a specific queue
* `background_task_add_other_price.dart`: assigned to `BackgroundTaskQueue.fast`
* `background_task_add_price.dart`: assigned to `BackgroundTaskQueue.slow`
* `background_task_badge.dart`: minor refactoring
* `background_task_crop.dart`: assigned to `BackgroundTaskQueue.fast`
* `background_task_details.dart`: assigned to `BackgroundTaskQueue.fast`
* `background_task_download_products.dart`: assigned to `BackgroundTaskQueue.longHaul`
* `background_task_full_refresh.dart`: assigned to `BackgroundTaskQueue.longHaul`
* `background_task_hunger_games.dart`: assigned to `BackgroundTaskQueue.fast`
* `background_task_image.dart`: assigned to `BackgroundTaskQueue.slow`
* `background_task_language_refresh.dart`: assigned to `BackgroundTaskQueue.longHaul`
* `background_task_manager.dart`: now using new class `BackgroundTaskQueue` in order to use multiple queues
* `background_task_offline.dart`: assigned to `BackgroundTaskQueue.longHaul`
* `background_task_refresh_later.dart`: assigned to `BackgroundTaskQueue.fast`
* `background_task_top_barcodes.dart`: assigned to `BackgroundTaskQueue.longHaul`
* `background_task_unselect.dart`: assigned to `BackgroundTaskQueue.fast`
* `dao_string_list.dart`: added 2 queues
* `local_database.dart`: now running all queues
* `local_database_mock.dart`: minor refactoring
* `offline_tasks_page.dart`: now taking queues into account
* `up_to_date_mixin.dart`: now running all queues
* `user_preferences_page.dart`: now running all queues
  • Loading branch information
monsieurtanuki authored Oct 27, 2024
1 parent 40a8641 commit 69bb171
Show file tree
Hide file tree
Showing 23 changed files with 228 additions and 61 deletions.
7 changes: 6 additions & 1 deletion packages/smooth_app/lib/background/background_task.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_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task_manager.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
Expand Down Expand Up @@ -122,10 +123,14 @@ abstract class BackgroundTask {
@protected
Future<void> addToManager(
final LocalDatabase localDatabase, {
required final BackgroundTaskQueue queue,
final BuildContext? context,
final bool showSnackBar = true,
}) async {
await BackgroundTaskManager.getInstance(localDatabase).add(this);
await BackgroundTaskManager.getInstance(
localDatabase,
queue: queue,
).add(this);
if (context == null || !context.mounted) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_price.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';

Expand Down Expand Up @@ -74,7 +75,11 @@ class BackgroundTaskAddOtherPrice extends BackgroundTaskPrice {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.fast,
);
}

/// Returns a new background task about changing a product.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_image.dart';
import 'package:smooth_app/background/background_task_price.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/background_task_upload.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
Expand Down Expand Up @@ -122,7 +123,11 @@ class BackgroundTaskAddPrice extends BackgroundTaskPrice {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.slow,
);
}

/// Returns a new background task about changing a product.
Expand Down
10 changes: 7 additions & 3 deletions packages/smooth_app/lib/background/background_task_badge.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/database/local_database.dart';

/// Badge about pending background tasks.
Expand All @@ -11,14 +12,17 @@ class BackgroundTaskBadge extends StatelessWidget {
@override
Widget build(BuildContext context) {
final LocalDatabase localDatabase = context.watch<LocalDatabase>();
final List<String> tasks = localDatabase.getAllTaskIds();
if (tasks.isEmpty) {
int count = 0;
for (final BackgroundTaskQueue queue in BackgroundTaskQueue.values) {
count += localDatabase.getAllTaskIds(queue.tagTaskQueue).length;
}
if (count == 0) {
return child;
}
return Badge(
backgroundColor: Colors.blue.shade900,
label: Text(
'${tasks.length}',
'$count',
style: const TextStyle(color: Colors.white),
),
child: child,
Expand Down
7 changes: 6 additions & 1 deletion packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/background/background_task_upload.dart';
import 'package:smooth_app/background/operation_type.dart';
Expand Down Expand Up @@ -83,7 +84,11 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.fast,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/background_task_product_change.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';

Expand Down Expand Up @@ -92,6 +93,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode
localDatabase,
context: context,
showSnackBar: showSnackBar,
queue: BackgroundTaskQueue.fast,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_progressing.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
Expand Down Expand Up @@ -67,7 +68,10 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
downloadFlag,
productType,
);
await task.addToManager(localDatabase);
await task.addToManager(
localDatabase,
queue: BackgroundTaskQueue.longHaul,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_download_products.dart';
import 'package:smooth_app/background/background_task_paged.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/background/work_type.dart';
import 'package:smooth_app/database/dao_product.dart';
Expand Down Expand Up @@ -39,7 +40,11 @@ class BackgroundTaskFullRefresh extends BackgroundTaskPaged {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.longHaul,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/helpers/robotoff_insight_helper.dart';
Expand Down Expand Up @@ -61,7 +62,11 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.fast,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_barcode.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/background_task_refresh_later.dart';
import 'package:smooth_app/background/background_task_upload.dart';
import 'package:smooth_app/background/operation_type.dart';
Expand Down Expand Up @@ -93,7 +94,11 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
await task.addToManager(
localDatabase,
context: context,
queue: BackgroundTaskQueue.slow,
);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/painting.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/background/background_task.dart';
import 'package:smooth_app/background/background_task_queue.dart';
import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/local_database.dart';
Expand Down Expand Up @@ -80,7 +81,10 @@ class BackgroundTaskLanguageRefresh extends BackgroundTask {
excludeBarcodes,
productType,
);
await task.addToManager(localDatabase);
await task.addToManager(
localDatabase,
queue: BackgroundTaskQueue.longHaul,
);
}

@override
Expand Down
Loading

0 comments on commit 69bb171

Please sign in to comment.