From 2a0c7d1e56f89beb5f5bed7a83e442de544dcd12 Mon Sep 17 00:00:00 2001 From: Aman Raj Singh Mourya Date: Tue, 10 Sep 2024 13:44:00 +0530 Subject: [PATCH] [mob][photos] Refractor code --- mobile/lib/ui/tabs/shared/empty_state.dart | 51 +----------------- .../gallery/collect_photos_card_widget.dart | 54 ++----------------- mobile/lib/utils/collection_util.dart | 49 +++++++++++++++++ 3 files changed, 54 insertions(+), 100 deletions(-) create mode 100644 mobile/lib/utils/collection_util.dart diff --git a/mobile/lib/ui/tabs/shared/empty_state.dart b/mobile/lib/ui/tabs/shared/empty_state.dart index 97f88b88f5..b69d64b96d 100644 --- a/mobile/lib/ui/tabs/shared/empty_state.dart +++ b/mobile/lib/ui/tabs/shared/empty_state.dart @@ -1,66 +1,19 @@ import "package:flutter/material.dart"; -import "package:intl/intl.dart"; -import "package:logging/logging.dart"; import "package:photos/core/event_bus.dart"; import "package:photos/events/tab_changed_event.dart"; import "package:photos/generated/l10n.dart"; -import "package:photos/models/collection/collection.dart"; -import "package:photos/models/collection/collection_items.dart"; -import "package:photos/services/collections_service.dart"; import "package:photos/theme/ente_theme.dart"; import 'package:photos/ui/collections/collection_action_sheet.dart'; import 'package:photos/ui/components/buttons/button_widget.dart'; import "package:photos/ui/components/empty_state_item_widget.dart"; import "package:photos/ui/components/models/button_type.dart"; -import "package:photos/ui/viewer/gallery/collection_page.dart"; -import "package:photos/utils/dialog_util.dart"; -import "package:photos/utils/navigation_util.dart"; +import "package:photos/utils/collection_util.dart"; import "package:photos/utils/share_util.dart"; import "package:photos/utils/toast_util.dart"; class SharedEmptyStateWidget extends StatelessWidget { const SharedEmptyStateWidget({super.key}); - Future _onTapCollectEventPhotos(BuildContext context) async { - final String currentDate = - DateFormat('MMMM d, yyyy').format(DateTime.now()); - final result = await showTextInputDialog( - context, - title: "Name the album", - submitButtonLabel: S.of(context).create, - hintText: S.of(context).enterAlbumName, - alwaysShowSuccessState: false, - initialValue: currentDate, - textCapitalization: TextCapitalization.words, - onSubmit: (String text) async { - // indicates user cancelled the rename request - if (text.trim() == "") { - return; - } - - try { - final Collection c = - await CollectionsService.instance.createAlbum(text); - // ignore: unawaited_futures - routeToPage( - context, - CollectionPage( - isFromCollectPhotos: true, - CollectionWithThumbnail(c, null), - ), - ); - } catch (e, s) { - Logger("Collect event photos from CollectPhotosCardWidget") - .severe("Failed to rename album", e, s); - rethrow; - } - }, - ); - if (result is Exception) { - await showGenericErrorDialog(context: context, error: result); - } - } - @override Widget build(BuildContext context) { final textTheme = getEnteTextTheme(context); @@ -128,7 +81,7 @@ class SharedEmptyStateWidget extends StatelessWidget { labelText: S.of(context).collectEventPhotos, icon: Icons.add_photo_alternate_outlined, onTap: () async { - await _onTapCollectEventPhotos(context); + await onTapCollectEventPhotos(context); }, ), const SizedBox(height: 6), diff --git a/mobile/lib/ui/viewer/gallery/collect_photos_card_widget.dart b/mobile/lib/ui/viewer/gallery/collect_photos_card_widget.dart index 888b18da5b..46e816b407 100644 --- a/mobile/lib/ui/viewer/gallery/collect_photos_card_widget.dart +++ b/mobile/lib/ui/viewer/gallery/collect_photos_card_widget.dart @@ -1,17 +1,9 @@ import "package:figma_squircle/figma_squircle.dart"; import 'package:flutter/material.dart'; -import "package:intl/intl.dart"; -import "package:logging/logging.dart"; -import "package:photos/generated/l10n.dart"; -import "package:photos/models/collection/collection.dart"; -import "package:photos/models/collection/collection_items.dart"; -import "package:photos/services/collections_service.dart"; import "package:photos/theme/ente_theme.dart"; import "package:photos/ui/components/buttons/button_widget.dart"; import "package:photos/ui/components/models/button_type.dart"; -import "package:photos/ui/viewer/gallery/collection_page.dart"; -import "package:photos/utils/dialog_util.dart"; -import "package:photos/utils/navigation_util.dart"; +import "package:photos/utils/collection_util.dart"; class CollectPhotosCardWidget extends StatefulWidget { const CollectPhotosCardWidget({super.key}); @@ -22,46 +14,6 @@ class CollectPhotosCardWidget extends StatefulWidget { } class _CollectPhotosCardWidgetState extends State { - Future _onTapCreateAlbum() async { - final String currentDate = - DateFormat('MMMM d, yyyy').format(DateTime.now()); - final result = await showTextInputDialog( - context, - title: "Name the album", - submitButtonLabel: S.of(context).create, - hintText: S.of(context).enterAlbumName, - alwaysShowSuccessState: false, - initialValue: currentDate, - textCapitalization: TextCapitalization.words, - onSubmit: (String text) async { - // indicates user cancelled the rename request - if (text.trim() == "") { - return; - } - - try { - final Collection c = - await CollectionsService.instance.createAlbum(text); - // ignore: unawaited_futures - routeToPage( - context, - CollectionPage( - isFromCollectPhotos: true, - CollectionWithThumbnail(c, null), - ), - ); - } catch (e, s) { - Logger("CollectPhotosCardWidget") - .severe("Failed to rename album", e, s); - rethrow; - } - }, - ); - if (result is Exception) { - await showGenericErrorDialog(context: context, error: result); - } - } - @override Widget build(BuildContext context) { final textTheme = getEnteTextTheme(context); @@ -94,7 +46,7 @@ class _CollectPhotosCardWidgetState extends State { ), ), GestureDetector( - onTap: () => _onTapCreateAlbum(), + onTap: () => onTapCollectEventPhotos(context), child: Padding( padding: const EdgeInsets.fromLTRB(16, 16, 16, 24), child: Container( @@ -144,7 +96,7 @@ class _CollectPhotosCardWidgetState extends State { icon: Icons.add_photo_alternate_outlined, shouldShowSuccessConfirmation: false, shouldSurfaceExecutionStates: false, - onTap: () => _onTapCreateAlbum(), + onTap: () => onTapCollectEventPhotos(context), ), ], ), diff --git a/mobile/lib/utils/collection_util.dart b/mobile/lib/utils/collection_util.dart new file mode 100644 index 0000000000..0190c10624 --- /dev/null +++ b/mobile/lib/utils/collection_util.dart @@ -0,0 +1,49 @@ +import "package:flutter/material.dart"; +import "package:intl/intl.dart"; +import "package:logging/logging.dart"; +import "package:photos/generated/l10n.dart"; +import "package:photos/models/collection/collection.dart"; +import "package:photos/models/collection/collection_items.dart"; +import "package:photos/services/collections_service.dart"; +import "package:photos/ui/viewer/gallery/collection_page.dart"; +import "package:photos/utils/dialog_util.dart"; +import "package:photos/utils/navigation_util.dart"; + +Future onTapCollectEventPhotos(BuildContext context) async { + final String currentDate = DateFormat('MMMM d, yyyy').format(DateTime.now()); + final result = await showTextInputDialog( + context, + title: "Name the album", + submitButtonLabel: S.of(context).create, + hintText: S.of(context).enterAlbumName, + alwaysShowSuccessState: false, + initialValue: currentDate, + textCapitalization: TextCapitalization.words, + onSubmit: (String text) async { + // indicates user cancelled the rename request + if (text.trim() == "") { + return; + } + + try { + final Collection c = + await CollectionsService.instance.createAlbum(text); + // ignore: unawaited_futures + routeToPage( + context, + CollectionPage( + isFromCollectPhotos: true, + CollectionWithThumbnail(c, null), + ), + ); + } catch (e, s) { + Logger("Collect event photos from CollectPhotosCardWidget") + .severe("Failed to rename album", e, s); + rethrow; + } + }, + ); + if (result is Exception) { + await showGenericErrorDialog(context: context, error: result); + } +}