From 7306d3cedfd119c23af888f68aa04a9c0fe66b89 Mon Sep 17 00:00:00 2001 From: Ming Ming Date: Tue, 3 Oct 2023 22:28:35 +0800 Subject: [PATCH] Move object utilities to np_common --- .../person/content_provider/recognize.dart | 1 + app/lib/object_extension.dart | 28 ++++++------------- app/lib/widget/archive_browser.dart | 1 + app/lib/widget/enhanced_photo_browser.dart | 1 + app/lib/widget/home_photos.dart | 1 + app/lib/widget/home_search.dart | 1 + app/lib/widget/network_thumbnail.dart | 2 +- app/lib/widget/trashbin_browser.dart | 1 + np_common/lib/object_util.dart | 26 +++++++++++++++++ 9 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 np_common/lib/object_util.dart diff --git a/app/lib/entity/person/content_provider/recognize.dart b/app/lib/entity/person/content_provider/recognize.dart index f9a9cd44..a92fbaee 100644 --- a/app/lib/entity/person/content_provider/recognize.dart +++ b/app/lib/entity/person/content_provider/recognize.dart @@ -7,6 +7,7 @@ import 'package:nc_photos/entity/person.dart'; import 'package:nc_photos/entity/recognize_face.dart'; import 'package:nc_photos/entity/recognize_face_item.dart'; import 'package:nc_photos/object_extension.dart'; +import 'package:np_common/object_util.dart'; import 'package:to_string/to_string.dart'; part 'recognize.g.dart'; diff --git a/app/lib/object_extension.dart b/app/lib/object_extension.dart index 62ca1470..0b541c7c 100644 --- a/app/lib/object_extension.dart +++ b/app/lib/object_extension.dart @@ -1,26 +1,16 @@ import 'dart:async'; +import 'package:np_common/object_util.dart'; extension ObjectExtension on T { - /// Run [fn] with this, and return this - T apply(void Function(T obj) fn) { - fn(this); - return this; - } + /// Deprecated, use [also] + T apply(void Function(T obj) fn) => also(fn); - /// Run [fn] with this, and return this - Future applyFuture(FutureOr Function(T obj) fn) async { - await fn(this); - return this; - } + /// Deprecated, use [alsoFuture] + Future applyFuture(FutureOr Function(T obj) fn) => alsoFuture(fn); - /// Run [fn] with this, and return the results of [fn] - U run(U Function(T obj) fn) => fn(this); + /// Deprecated, use [let] + U run(U Function(T obj) fn) => let(fn); - /// Run [fn] with this, and return the results of [fn] - Future runFuture(FutureOr Function(T obj) fn) async { - return await fn(this); - } - - /// Cast this as U, or null if this is not an object of U - U? as() => this is U ? this as U : null; + /// Deprecated, use [letFuture] + Future runFuture(FutureOr Function(T obj) fn) => letFuture(fn); } diff --git a/app/lib/widget/archive_browser.dart b/app/lib/widget/archive_browser.dart index 153a9dbe..d68a1d2e 100644 --- a/app/lib/widget/archive_browser.dart +++ b/app/lib/widget/archive_browser.dart @@ -29,6 +29,7 @@ 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_common/object_util.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 e08e257f..a70ba942 100644 --- a/app/lib/widget/enhanced_photo_browser.dart +++ b/app/lib/widget/enhanced_photo_browser.dart @@ -27,6 +27,7 @@ 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_common/object_util.dart'; import 'package:np_platform_permission/np_platform_permission.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 e07d2b10..6e41f2d4 100644 --- a/app/lib/widget/home_photos.dart +++ b/app/lib/widget/home_photos.dart @@ -52,6 +52,7 @@ 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_common/object_util.dart'; import 'package:np_platform_image_processor/np_platform_image_processor.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 33c40b80..22247aca 100644 --- a/app/lib/widget/home_search.dart +++ b/app/lib/widget/home_search.dart @@ -38,6 +38,7 @@ 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_common/object_util.dart'; import 'package:np_ui/np_ui.dart'; part 'home_search.g.dart'; diff --git a/app/lib/widget/network_thumbnail.dart b/app/lib/widget/network_thumbnail.dart index 0fad4192..e86b71fc 100644 --- a/app/lib/widget/network_thumbnail.dart +++ b/app/lib/widget/network_thumbnail.dart @@ -7,8 +7,8 @@ import 'package:nc_photos/cache_manager_util.dart'; import 'package:nc_photos/entity/file_descriptor.dart'; import 'package:nc_photos/k.dart' as k; import 'package:nc_photos/np_api_util.dart'; -import 'package:nc_photos/object_extension.dart'; import 'package:nc_photos/widget/cached_network_image_mod.dart' as mod; +import 'package:np_common/object_util.dart'; /// A square thumbnail widget for a file class NetworkRectThumbnail extends StatelessWidget { diff --git a/app/lib/widget/trashbin_browser.dart b/app/lib/widget/trashbin_browser.dart index c6486144..5dd05ade 100644 --- a/app/lib/widget/trashbin_browser.dart +++ b/app/lib/widget/trashbin_browser.dart @@ -31,6 +31,7 @@ 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'; +import 'package:np_common/object_util.dart'; part 'trashbin_browser.g.dart'; diff --git a/np_common/lib/object_util.dart b/np_common/lib/object_util.dart new file mode 100644 index 00000000..c6ba8fda --- /dev/null +++ b/np_common/lib/object_util.dart @@ -0,0 +1,26 @@ +import 'dart:async'; + +extension ObjectExtension on T { + /// Run [fn] with this, and return this + T also(void Function(T obj) fn) { + fn(this); + return this; + } + + /// Run [fn] with this, and return this + Future alsoFuture(FutureOr Function(T obj) fn) async { + await fn(this); + return this; + } + + /// Run [fn] with this, and return the results of [fn] + U let(U Function(T obj) fn) => fn(this); + + /// Run [fn] with this, and return the results of [fn] + Future letFuture(FutureOr Function(T obj) fn) async { + return await fn(this); + } + + /// Cast this as U, or null if this is not an object of U + U? as() => this is U ? this as U : null; +}