From 0ca85f2598d49658ca4c5a8d505344df02c1460a Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Sun, 27 Aug 2023 22:09:10 +0800 Subject: [PATCH] Refactor: extract async utilities to its own package --- app/lib/entity/sqlite/database.dart | 2 +- app/lib/future_extension.dart | 3 -- app/lib/mobile/android/activity.dart | 2 +- app/lib/service.dart | 2 +- app/lib/widget/archive_browser.dart | 2 +- app/lib/widget/enhanced_photo_browser.dart | 2 +- app/lib/widget/home_photos.dart | 2 +- app/lib/widget/home_search.dart | 2 +- app/lib/widget/tag_picker_dialog.dart | 5 ++-- app/lib/widget/trashbin_browser.dart | 2 +- app/pubspec.lock | 7 +++++ app/pubspec.yaml | 2 ++ app/test/mock_type.dart | 4 +-- np_async/.gitignore | 30 +++++++++++++++++++ np_async/.metadata | 10 +++++++ np_async/analysis_options.yaml | 1 + np_async/lib/np_async.dart | 5 ++++ {app/lib => np_async/lib/src}/async_util.dart | 0 .../lib/src}/compute_queue.dart | 0 .../lib => np_async/lib/src}/future_util.dart | 4 +++ np_async/pubspec.yaml | 19 ++++++++++++ 21 files changed, 90 insertions(+), 16 deletions(-) delete mode 100644 app/lib/future_extension.dart create mode 100644 np_async/.gitignore create mode 100644 np_async/.metadata create mode 100644 np_async/analysis_options.yaml create mode 100644 np_async/lib/np_async.dart rename {app/lib => np_async/lib/src}/async_util.dart (100%) rename {app/lib => np_async/lib/src}/compute_queue.dart (100%) rename {app/lib => np_async/lib/src}/future_util.dart (89%) create mode 100644 np_async/pubspec.yaml diff --git a/app/lib/entity/sqlite/database.dart b/app/lib/entity/sqlite/database.dart index 78a1ec22..ee3610b7 100644 --- a/app/lib/entity/sqlite/database.dart +++ b/app/lib/entity/sqlite/database.dart @@ -8,12 +8,12 @@ import 'package:nc_photos/entity/sqlite/files_query_builder.dart'; import 'package:nc_photos/entity/sqlite/isolate_util.dart'; import 'package:nc_photos/entity/sqlite/table.dart'; import 'package:nc_photos/entity/sqlite/type_converter.dart'; -import 'package:nc_photos/future_extension.dart'; import 'package:nc_photos/iterable_extension.dart'; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/mobile/platform.dart' if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform; import 'package:nc_photos/object_extension.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:np_collection/np_collection.dart'; import 'package:np_platform_lock/np_platform_lock.dart'; diff --git a/app/lib/future_extension.dart b/app/lib/future_extension.dart deleted file mode 100644 index 89dd5677..00000000 --- a/app/lib/future_extension.dart +++ /dev/null @@ -1,3 +0,0 @@ -extension FutureNotNullExtension on Future { - Future notNull() async => (await this)!; -} diff --git a/app/lib/mobile/android/activity.dart b/app/lib/mobile/android/activity.dart index d873dbc4..092102ad 100644 --- a/app/lib/mobile/android/activity.dart +++ b/app/lib/mobile/android/activity.dart @@ -1,5 +1,5 @@ import 'package:flutter/services.dart'; -import 'package:nc_photos/future_extension.dart'; +import 'package:np_async/np_async.dart'; class Activity { static Future consumeInitialRoute() => diff --git a/app/lib/service.dart b/app/lib/service.dart index 5b73da8d..ea3fe6d5 100644 --- a/app/lib/service.dart +++ b/app/lib/service.dart @@ -18,11 +18,11 @@ import 'package:nc_photos/entity/file_util.dart' as file_util; import 'package:nc_photos/entity/pref.dart'; import 'package:nc_photos/event/event.dart'; import 'package:nc_photos/event/native_event.dart'; -import 'package:nc_photos/future_extension.dart'; import 'package:nc_photos/language_util.dart' as language_util; import 'package:nc_photos/reverse_geocoder.dart'; import 'package:nc_photos/use_case/update_missing_metadata.dart'; import 'package:nc_photos_plugin/nc_photos_plugin.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; part 'service.g.dart'; diff --git a/app/lib/widget/archive_browser.dart b/app/lib/widget/archive_browser.dart index e6881d65..153a9dbe 100644 --- a/app/lib/widget/archive_browser.dart +++ b/app/lib/widget/archive_browser.dart @@ -7,7 +7,6 @@ import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/scan_account_dir.dart'; -import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/debug_util.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/entity/file.dart'; @@ -28,6 +27,7 @@ import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart'; import 'package:nc_photos/widget/selection_app_bar.dart'; import 'package:nc_photos/widget/viewer.dart'; import 'package:nc_photos/widget/zoom_menu_button.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; part 'archive_browser.g.dart'; diff --git a/app/lib/widget/enhanced_photo_browser.dart b/app/lib/widget/enhanced_photo_browser.dart index c890e45c..fa6a29e0 100644 --- a/app/lib/widget/enhanced_photo_browser.dart +++ b/app/lib/widget/enhanced_photo_browser.dart @@ -5,7 +5,6 @@ import 'package:logging/logging.dart'; import 'package:nc_photos/app_init.dart' as app_init; import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/scan_local_dir.dart'; -import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/entity/file_util.dart' as file_util; import 'package:nc_photos/entity/local_file.dart'; @@ -25,6 +24,7 @@ import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util; import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart'; import 'package:nc_photos/widget/selection_app_bar.dart'; import 'package:nc_photos_plugin/nc_photos_plugin.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:np_collection/np_collection.dart'; import 'package:np_platform_util/np_platform_util.dart'; diff --git a/app/lib/widget/home_photos.dart b/app/lib/widget/home_photos.dart index 72015c88..bd4cbbfe 100644 --- a/app/lib/widget/home_photos.dart +++ b/app/lib/widget/home_photos.dart @@ -14,7 +14,6 @@ import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/progress.dart'; import 'package:nc_photos/bloc/scan_account_dir.dart'; -import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/controller/account_controller.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/download_handler.dart'; @@ -51,6 +50,7 @@ import 'package:nc_photos/widget/selection_app_bar.dart'; import 'package:nc_photos/widget/settings.dart'; import 'package:nc_photos/widget/viewer.dart'; import 'package:nc_photos/widget/zoom_menu_button.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:np_platform_util/np_platform_util.dart'; import 'package:visibility_detector/visibility_detector.dart'; diff --git a/app/lib/widget/home_search.dart b/app/lib/widget/home_search.dart index c20f16c0..33c40b80 100644 --- a/app/lib/widget/home_search.dart +++ b/app/lib/widget/home_search.dart @@ -8,7 +8,6 @@ import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/search.dart'; -import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/download_handler.dart'; import 'package:nc_photos/entity/file.dart'; @@ -37,6 +36,7 @@ import 'package:nc_photos/widget/search_landing.dart'; import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart'; import 'package:nc_photos/widget/selection_app_bar.dart'; import 'package:nc_photos/widget/viewer.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:np_ui/np_ui.dart'; diff --git a/app/lib/widget/tag_picker_dialog.dart b/app/lib/widget/tag_picker_dialog.dart index 4ee2598c..4a45c684 100644 --- a/app/lib/widget/tag_picker_dialog.dart +++ b/app/lib/widget/tag_picker_dialog.dart @@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_typeahead/flutter_typeahead.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; -import 'package:nc_photos/async_util.dart' as async_util; import 'package:nc_photos/bloc/list_tag.dart'; import 'package:nc_photos/bloc/search_suggestion.dart'; import 'package:nc_photos/entity/tag.dart'; @@ -11,6 +10,7 @@ import 'package:nc_photos/exception_util.dart' as exception_util; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/snack_bar_manager.dart'; import 'package:nc_photos/widget/dialog_scaffold.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_string/np_string.dart'; class TagPickerDialog extends StatefulWidget { @@ -137,8 +137,7 @@ class _TagPickerDialogState extends State { Future> _onSearch(String pattern) async { _suggestionBloc.add(SearchSuggestionBlocSearchEvent(pattern.toCi())); await Future.delayed(const Duration(milliseconds: 250)); - await async_util - .wait(() => _suggestionBloc.state is! SearchSuggestionBlocLoading); + await wait(() => _suggestionBloc.state is! SearchSuggestionBlocLoading); if (_suggestionBloc.state is SearchSuggestionBlocSuccess) { return _suggestionBloc.state.results; } else { diff --git a/app/lib/widget/trashbin_browser.dart b/app/lib/widget/trashbin_browser.dart index b48dd696..c6486144 100644 --- a/app/lib/widget/trashbin_browser.dart +++ b/app/lib/widget/trashbin_browser.dart @@ -8,7 +8,6 @@ import 'package:logging/logging.dart'; import 'package:nc_photos/account.dart'; import 'package:nc_photos/app_localizations.dart'; import 'package:nc_photos/bloc/ls_trashbin.dart'; -import 'package:nc_photos/compute_queue.dart'; import 'package:nc_photos/debug_util.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/entity/file.dart'; @@ -30,6 +29,7 @@ import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart'; import 'package:nc_photos/widget/selection_app_bar.dart'; import 'package:nc_photos/widget/trashbin_viewer.dart'; import 'package:nc_photos/widget/zoom_menu_button.dart'; +import 'package:np_async/np_async.dart'; import 'package:np_codegen/np_codegen.dart'; part 'trashbin_browser.g.dart'; diff --git a/app/pubspec.lock b/app/pubspec.lock index 94fc97c1..ff30859f 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -948,6 +948,13 @@ packages: relative: true source: path version: "0.0.1" + np_async: + dependency: "direct main" + description: + path: "../np_async" + relative: true + source: path + version: "0.0.1" np_codegen: dependency: "direct main" description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index cef598b7..757b6848 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -99,6 +99,8 @@ dependencies: path: ../plugin np_api: path: ../np_api + np_async: + path: ../np_async np_codegen: path: ../codegen np_common: diff --git a/app/test/mock_type.dart b/app/test/mock_type.dart index f26c7ff8..48611d1c 100644 --- a/app/test/mock_type.dart +++ b/app/test/mock_type.dart @@ -18,7 +18,7 @@ import 'package:nc_photos/entity/share.dart'; import 'package:nc_photos/entity/sharee.dart'; import 'package:nc_photos/entity/tag.dart'; import 'package:nc_photos/exception_event.dart'; -import 'package:nc_photos/future_util.dart' as future_util; +import 'package:np_async/np_async.dart'; import 'package:np_common/or_null.dart'; import 'package:np_string/np_string.dart'; import 'package:path/path.dart' as path_lib; @@ -63,7 +63,7 @@ class MockAlbumMemoryRepo extends MockAlbumRepo { @override getAll(Account account, List albumFiles) async* { - final results = await future_util.waitOr( + final results = await waitOr( albumFiles.map((f) => get(account, f)), (error, stackTrace) => ExceptionEvent(error, stackTrace), ); diff --git a/np_async/.gitignore b/np_async/.gitignore new file mode 100644 index 00000000..96486fd9 --- /dev/null +++ b/np_async/.gitignore @@ -0,0 +1,30 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. +/pubspec.lock +**/doc/api/ +.dart_tool/ +.packages +build/ diff --git a/np_async/.metadata b/np_async/.metadata new file mode 100644 index 00000000..788b91db --- /dev/null +++ b/np_async/.metadata @@ -0,0 +1,10 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled and should not be manually edited. + +version: + revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf + channel: stable + +project_type: package diff --git a/np_async/analysis_options.yaml b/np_async/analysis_options.yaml new file mode 100644 index 00000000..f92d2567 --- /dev/null +++ b/np_async/analysis_options.yaml @@ -0,0 +1 @@ +include: package:np_lints/np.yaml diff --git a/np_async/lib/np_async.dart b/np_async/lib/np_async.dart new file mode 100644 index 00000000..2311b2de --- /dev/null +++ b/np_async/lib/np_async.dart @@ -0,0 +1,5 @@ +library np_async; + +export 'src/async_util.dart'; +export 'src/compute_queue.dart'; +export 'src/future_util.dart'; diff --git a/app/lib/async_util.dart b/np_async/lib/src/async_util.dart similarity index 100% rename from app/lib/async_util.dart rename to np_async/lib/src/async_util.dart diff --git a/app/lib/compute_queue.dart b/np_async/lib/src/compute_queue.dart similarity index 100% rename from app/lib/compute_queue.dart rename to np_async/lib/src/compute_queue.dart diff --git a/app/lib/future_util.dart b/np_async/lib/src/future_util.dart similarity index 89% rename from app/lib/future_util.dart rename to np_async/lib/src/future_util.dart index 1f9e9805..7cd6d1c2 100644 --- a/app/lib/future_util.dart +++ b/np_async/lib/src/future_util.dart @@ -2,6 +2,10 @@ import 'dart:async'; import 'package:np_collection/np_collection.dart'; +extension FutureNotNullExtension on Future { + Future notNull() async => (await this)!; +} + Future> waitOr( Iterable> futures, T Function(Object error, StackTrace? stackTrace) onError, diff --git a/np_async/pubspec.yaml b/np_async/pubspec.yaml new file mode 100644 index 00000000..44884c20 --- /dev/null +++ b/np_async/pubspec.yaml @@ -0,0 +1,19 @@ +name: np_async +description: A new Flutter package project. +version: 0.0.1 +homepage: +publish_to: none + +environment: + sdk: '>=2.19.6 <3.0.0' + flutter: ">=3.3.0" + +dependencies: + flutter: + sdk: flutter + np_collection: + path: ../np_collection + +dev_dependencies: + np_lints: + path: ../np_lints