瀏覽代碼

[mob] Add Ente popup menu widget (#1562)

## Description

## Tests
Neeraj Gupta 1 年之前
父節點
當前提交
90baf7efb9
共有 2 個文件被更改,包括 129 次插入236 次删除
  1. 38 0
      mobile/lib/ui/common/popup_item.dart
  2. 91 236
      mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart

+ 38 - 0
mobile/lib/ui/common/popup_item.dart

@@ -0,0 +1,38 @@
+import 'package:flutter/material.dart';
+
+class EntePopupMenuItem<T> extends PopupMenuItem<T> {
+  final String label;
+  final IconData? icon;
+  final Widget? iconWidget;
+
+  EntePopupMenuItem(
+    this.label, {
+    required T value,
+    this.icon,
+    this.iconWidget,
+    Key? key,
+  })  : assert(
+          icon != null || iconWidget != null,
+          'Either icon or iconWidget must be provided.',
+        ),
+        assert(
+          !(icon != null && iconWidget != null),
+          'Only one of icon or iconWidget can be provided.',
+        ),
+        super(
+          value: value,
+          key: key,
+          child: Row(
+            children: [
+              if (iconWidget != null)
+                iconWidget
+              else if (icon != null)
+                Icon(icon),
+              const Padding(
+                padding: EdgeInsets.all(8),
+              ),
+              Text(label),
+            ],
+          ), // Initially empty, will be populated in build
+        );
+}

+ 91 - 236
mobile/lib/ui/viewer/gallery/gallery_app_bar_widget.dart

@@ -24,6 +24,7 @@ import 'package:photos/services/collections_service.dart';
 import 'package:photos/services/sync_service.dart';
 import 'package:photos/services/update_service.dart';
 import 'package:photos/ui/actions/collection/collection_sharing_actions.dart';
+import "package:photos/ui/common/popup_item.dart";
 import 'package:photos/ui/components/action_sheet_widget.dart';
 import 'package:photos/ui/components/buttons/button_widget.dart';
 import 'package:photos/ui/components/models/button_type.dart';
@@ -319,263 +320,117 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
         ),
       );
     }
-    final List<PopupMenuItem<AlbumPopupAction>> items = [];
-    if (galleryType.canRename()) {
-      items.add(
-        PopupMenuItem(
+    final List<EntePopupMenuItem<AlbumPopupAction>> items = [];
+    items.addAll([
+      if (galleryType.canRename())
+        EntePopupMenuItem(
+          isQuickLink
+              ? S.of(context).convertToAlbum
+              : S.of(context).renameAlbum,
           value: AlbumPopupAction.rename,
-          child: Row(
-            children: [
-              Icon(isQuickLink ? Icons.photo_album_outlined : Icons.edit),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                isQuickLink
-                    ? S.of(context).convertToAlbum
-                    : S.of(context).renameAlbum,
-              ),
-            ],
-          ),
+          icon: isQuickLink ? Icons.photo_album_outlined : Icons.edit,
         ),
-      );
-    }
-    if (galleryType.canSetCover()) {
-      items.add(
-        PopupMenuItem(
+      if (galleryType.canSetCover())
+        EntePopupMenuItem(
+          S.of(context).setCover,
           value: AlbumPopupAction.setCover,
-          child: Row(
-            children: [
-              const Icon(Icons.image_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(S.of(context).setCover),
-            ],
-          ),
+          icon: Icons.image_outlined,
         ),
-      );
-    }
-    if (galleryType.showMap()) {
-      items.add(
-        PopupMenuItem(
+      if (galleryType.showMap())
+        EntePopupMenuItem(
+          S.of(context).map,
           value: AlbumPopupAction.map,
-          child: Row(
-            children: [
-              const Icon(Icons.map_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(S.of(context).map),
-            ],
-          ),
+          icon: Icons.map_outlined,
         ),
-      );
-    }
-
-    if (galleryType.canSort()) {
-      items.add(
-        PopupMenuItem(
+      if (galleryType.canSort())
+        EntePopupMenuItem(
+          S.of(context).sortAlbumsBy,
           value: AlbumPopupAction.sort,
-          child: Row(
-            children: [
-              const Icon(Icons.sort_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                S.of(context).sortAlbumsBy,
-              ),
-            ],
-          ),
+          icon: Icons.sort_outlined,
         ),
-      );
-    }
-
-    if (galleryType == GalleryType.uncategorized) {
-      items.add(
-        PopupMenuItem(
+      if (galleryType == GalleryType.uncategorized)
+        EntePopupMenuItem(
+          S.of(context).cleanUncategorized,
           value: AlbumPopupAction.cleanUncategorized,
-          child: Row(
-            children: [
-              const Icon(Icons.crop_original_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(S.of(context).cleanUncategorized),
-            ],
-          ),
+          icon: Icons.crop_original_outlined,
         ),
-      );
-    }
-    if (galleryType.canPin()) {
-      items.add(
-        PopupMenuItem(
+      if (galleryType.canPin())
+        EntePopupMenuItem(
+          widget.collection!.isPinned
+              ? S.of(context).unpinAlbum
+              : S.of(context).pinAlbum,
           value: AlbumPopupAction.pinAlbum,
-          child: Row(
-            children: [
-              widget.collection!.isPinned
-                  ? const Icon(CupertinoIcons.pin_slash)
-                  : Transform.rotate(
-                      angle: 45 * math.pi / 180, // rotate by 45 degrees
-                      child: const Icon(CupertinoIcons.pin),
-                    ),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                widget.collection!.isPinned
-                    ? S.of(context).unpinAlbum
-                    : S.of(context).pinAlbum,
-              ),
-            ],
-          ),
+          iconWidget: widget.collection!.isPinned
+              ? const Icon(CupertinoIcons.pin_slash)
+              : Transform.rotate(
+                  angle: 45 * math.pi / 180, // rotate by 45 degrees
+                  child: const Icon(CupertinoIcons.pin),
+                ),
         ),
-      );
-    }
+    ]);
     final bool isArchived = widget.collection?.isArchived() ?? false;
     final bool isHidden = widget.collection?.isHidden() ?? false;
-    // Do not show archive option for favorite collection. If collection is
-    // already archived, allow user to unarchive that collection.
-    if (isArchived || (galleryType.canArchive() && !isHidden)) {
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.ownedArchive,
-          child: Row(
-            children: [
-              Icon(isArchived ? Icons.unarchive : Icons.archive_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                isArchived
-                    ? S.of(context).unarchiveAlbum
-                    : S.of(context).archiveAlbum,
-              ),
-            ],
+
+    items.addAll(
+      [
+        // Do not show archive option for favorite collection. If collection is
+        // already archived, allow user to unarchive that collection.
+        if (isArchived || (galleryType.canArchive() && !isHidden))
+          EntePopupMenuItem(
+            value: AlbumPopupAction.ownedArchive,
+            isArchived
+                ? S.of(context).unarchiveAlbum
+                : S.of(context).archiveAlbum,
+            icon: isArchived ? Icons.unarchive : Icons.archive_outlined,
           ),
-        ),
-      );
-    }
-    if (!isArchived && galleryType.canHide()) {
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.ownedHide,
-          child: Row(
-            children: [
-              Icon(
-                isHidden
-                    ? Icons.visibility_outlined
-                    : Icons.visibility_off_outlined,
-              ),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                isHidden ? S.of(context).unhide : S.of(context).hide,
-              ),
-            ],
+        if (!isArchived && galleryType.canHide())
+          EntePopupMenuItem(
+            value: AlbumPopupAction.ownedHide,
+            isHidden ? S.of(context).unhide : S.of(context).hide,
+            icon: isHidden
+                ? Icons.visibility_outlined
+                : Icons.visibility_off_outlined,
           ),
-        ),
-      );
-    }
-    if (widget.collection != null && isInternalUser) {
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.playOnTv,
-          child: Row(
-            children: [
-              const Icon(Icons.tv_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(context.l10n.playOnTv),
-            ],
+        if (widget.collection != null && isInternalUser)
+          EntePopupMenuItem(
+            value: AlbumPopupAction.playOnTv,
+            context.l10n.playOnTv,
+            icon: Icons.tv_outlined,
           ),
-        ),
-      );
-    }
-
-    if (galleryType.canDelete()) {
-      items.add(
-        PopupMenuItem(
-          value: isQuickLink
-              ? AlbumPopupAction.removeLink
-              : AlbumPopupAction.delete,
-          child: Row(
-            children: [
-              Icon(
-                isQuickLink
-                    ? Icons.remove_circle_outline
-                    : Icons.delete_outline,
-              ),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                isQuickLink
-                    ? S.of(context).removeLink
-                    : S.of(context).deleteAlbum,
-              ),
-            ],
+        if (galleryType.canDelete())
+          EntePopupMenuItem(
+            isQuickLink ? S.of(context).removeLink : S.of(context).deleteAlbum,
+            value: isQuickLink
+                ? AlbumPopupAction.removeLink
+                : AlbumPopupAction.delete,
+            icon: isQuickLink
+                ? Icons.remove_circle_outline
+                : Icons.delete_outline,
           ),
-        ),
-      );
-    }
-
-    if (galleryType == GalleryType.sharedCollection) {
-      final bool hasShareeArchived = widget.collection!.hasShareeArchived();
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.sharedArchive,
-          child: Row(
-            children: [
-              Icon(
-                hasShareeArchived ? Icons.unarchive : Icons.archive_outlined,
-              ),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(
-                hasShareeArchived
-                    ? S.of(context).unarchiveAlbum
-                    : S.of(context).archiveAlbum,
-              ),
-            ],
+        if (galleryType == GalleryType.sharedCollection)
+          EntePopupMenuItem(
+            widget.collection!.hasShareeArchived()
+                ? S.of(context).unarchiveAlbum
+                : S.of(context).archiveAlbum,
+            value: AlbumPopupAction.sharedArchive,
+            icon: widget.collection!.hasShareeArchived()
+                ? Icons.unarchive
+                : Icons.archive_outlined,
           ),
-        ),
-      );
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.leave,
-          child: Row(
-            children: [
-              const Icon(Icons.logout),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(S.of(context).leaveAlbum),
-            ],
+        if (galleryType == GalleryType.sharedCollection)
+          EntePopupMenuItem(
+            S.of(context).leaveAlbum,
+            value: AlbumPopupAction.leave,
+            icon: Icons.logout,
           ),
-        ),
-      );
-    }
-    if (galleryType == GalleryType.localFolder) {
-      items.add(
-        PopupMenuItem(
-          value: AlbumPopupAction.freeUpSpace,
-          child: Row(
-            children: [
-              const Icon(Icons.delete_sweep_outlined),
-              const Padding(
-                padding: EdgeInsets.all(8),
-              ),
-              Text(S.of(context).freeUpDeviceSpace),
-            ],
+        if (galleryType == GalleryType.localFolder)
+          EntePopupMenuItem(
+            S.of(context).freeUpDeviceSpace,
+            value: AlbumPopupAction.freeUpSpace,
+            icon: Icons.delete_sweep_outlined,
           ),
-        ),
-      );
-    }
+      ],
+    );
     if (items.isNotEmpty) {
       actions.add(
         PopupMenuButton(