|
| 1 | +import 'package:flutter/widgets.dart'; |
| 2 | +import 'package:immich_mobile/constants/enums.dart'; |
| 3 | +import 'package:immich_mobile/domain/models/album/album.model.dart'; |
| 4 | +import 'package:immich_mobile/presentation/widgets/action_buttons/archive_action_button.widget.dart'; |
| 5 | +import 'package:immich_mobile/presentation/widgets/action_buttons/delete_action_button.widget.dart'; |
| 6 | +import 'package:immich_mobile/presentation/widgets/action_buttons/delete_local_action_button.widget.dart'; |
| 7 | +import 'package:immich_mobile/presentation/widgets/action_buttons/delete_permanent_action_button.widget.dart'; |
| 8 | +import 'package:immich_mobile/presentation/widgets/action_buttons/download_action_button.widget.dart'; |
| 9 | +import 'package:immich_mobile/presentation/widgets/action_buttons/like_activity_action_button.widget.dart'; |
| 10 | +import 'package:immich_mobile/presentation/widgets/action_buttons/move_to_lock_folder_action_button.widget.dart'; |
| 11 | +import 'package:immich_mobile/presentation/widgets/action_buttons/remove_from_album_action_button.widget.dart'; |
| 12 | +import 'package:immich_mobile/presentation/widgets/action_buttons/remove_from_lock_folder_action_button.widget.dart'; |
| 13 | +import 'package:immich_mobile/presentation/widgets/action_buttons/share_action_button.widget.dart'; |
| 14 | +import 'package:immich_mobile/presentation/widgets/action_buttons/share_link_action_button.widget.dart'; |
| 15 | +import 'package:immich_mobile/presentation/widgets/action_buttons/trash_action_button.widget.dart'; |
| 16 | +import 'package:immich_mobile/presentation/widgets/action_buttons/unarchive_action_button.widget.dart'; |
| 17 | +import 'package:immich_mobile/presentation/widgets/action_buttons/upload_action_button.widget.dart'; |
| 18 | +import 'package:immich_mobile/domain/models/asset/base_asset.model.dart'; |
| 19 | + |
| 20 | +class ActionButtonContext { |
| 21 | + final BaseAsset asset; |
| 22 | + final bool isOwner; |
| 23 | + final bool isArchived; |
| 24 | + final bool isTrashEnabled; |
| 25 | + final bool isInLockedView; |
| 26 | + final RemoteAlbum? currentAlbum; |
| 27 | + final ActionSource source; |
| 28 | + |
| 29 | + const ActionButtonContext({ |
| 30 | + required this.asset, |
| 31 | + required this.isOwner, |
| 32 | + required this.isArchived, |
| 33 | + required this.isTrashEnabled, |
| 34 | + required this.isInLockedView, |
| 35 | + required this.currentAlbum, |
| 36 | + required this.source, |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +enum ActionButtonType { |
| 41 | + share, |
| 42 | + shareLink, |
| 43 | + archive, |
| 44 | + unarchive, |
| 45 | + download, |
| 46 | + trash, |
| 47 | + deletePermanent, |
| 48 | + delete, |
| 49 | + moveToLockFolder, |
| 50 | + removeFromLockFolder, |
| 51 | + deleteLocal, |
| 52 | + upload, |
| 53 | + removeFromAlbum, |
| 54 | + likeActivity; |
| 55 | + |
| 56 | + bool shouldShow(ActionButtonContext context) { |
| 57 | + return switch (this) { |
| 58 | + ActionButtonType.share => true, |
| 59 | + ActionButtonType.shareLink => |
| 60 | + !context.isInLockedView && // |
| 61 | + context.asset.hasRemote, |
| 62 | + ActionButtonType.archive => |
| 63 | + context.isOwner && // |
| 64 | + !context.isInLockedView && // |
| 65 | + context.asset.hasRemote && // |
| 66 | + !context.isArchived, |
| 67 | + ActionButtonType.unarchive => |
| 68 | + context.isOwner && // |
| 69 | + !context.isInLockedView && // |
| 70 | + context.asset.hasRemote && // |
| 71 | + context.isArchived, |
| 72 | + ActionButtonType.download => |
| 73 | + !context.isInLockedView && // |
| 74 | + context.asset.hasRemote && // |
| 75 | + !context.asset.hasLocal, |
| 76 | + ActionButtonType.trash => |
| 77 | + context.isOwner && // |
| 78 | + !context.isInLockedView && // |
| 79 | + context.asset.hasRemote && // |
| 80 | + context.isTrashEnabled, |
| 81 | + ActionButtonType.deletePermanent => |
| 82 | + context.isOwner && // |
| 83 | + context.asset.hasRemote && // |
| 84 | + !context.isTrashEnabled || |
| 85 | + context.isInLockedView, |
| 86 | + ActionButtonType.delete => |
| 87 | + context.isOwner && // |
| 88 | + !context.isInLockedView && // |
| 89 | + context.asset.hasRemote, |
| 90 | + ActionButtonType.moveToLockFolder => |
| 91 | + context.isOwner && // |
| 92 | + !context.isInLockedView && // |
| 93 | + context.asset.hasRemote, |
| 94 | + ActionButtonType.removeFromLockFolder => |
| 95 | + context.isOwner && // |
| 96 | + context.isInLockedView && // |
| 97 | + context.asset.hasRemote, |
| 98 | + ActionButtonType.deleteLocal => |
| 99 | + !context.isInLockedView && // |
| 100 | + context.asset.storage == AssetState.local, |
| 101 | + ActionButtonType.upload => |
| 102 | + !context.isInLockedView && // |
| 103 | + context.asset.storage == AssetState.local, |
| 104 | + ActionButtonType.removeFromAlbum => |
| 105 | + context.isOwner && // |
| 106 | + !context.isInLockedView && // |
| 107 | + context.currentAlbum != null, |
| 108 | + ActionButtonType.likeActivity => |
| 109 | + !context.isInLockedView && |
| 110 | + context.currentAlbum != null && |
| 111 | + context.currentAlbum!.isActivityEnabled && |
| 112 | + context.currentAlbum!.isShared, |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + Widget buildButton(ActionButtonContext context) { |
| 117 | + return switch (this) { |
| 118 | + ActionButtonType.share => ShareActionButton(source: context.source), |
| 119 | + ActionButtonType.shareLink => ShareLinkActionButton(source: context.source), |
| 120 | + ActionButtonType.archive => ArchiveActionButton(source: context.source), |
| 121 | + ActionButtonType.unarchive => UnArchiveActionButton(source: context.source), |
| 122 | + ActionButtonType.download => DownloadActionButton(source: context.source), |
| 123 | + ActionButtonType.trash => TrashActionButton(source: context.source), |
| 124 | + ActionButtonType.deletePermanent => DeletePermanentActionButton(source: context.source), |
| 125 | + ActionButtonType.delete => DeleteActionButton(source: context.source), |
| 126 | + ActionButtonType.moveToLockFolder => MoveToLockFolderActionButton(source: context.source), |
| 127 | + ActionButtonType.removeFromLockFolder => RemoveFromLockFolderActionButton(source: context.source), |
| 128 | + ActionButtonType.deleteLocal => DeleteLocalActionButton(source: context.source), |
| 129 | + ActionButtonType.upload => UploadActionButton(source: context.source), |
| 130 | + ActionButtonType.removeFromAlbum => RemoveFromAlbumActionButton( |
| 131 | + albumId: context.currentAlbum!.id, |
| 132 | + source: context.source, |
| 133 | + ), |
| 134 | + ActionButtonType.likeActivity => const LikeActivityActionButton(), |
| 135 | + }; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +class ActionButtonBuilder { |
| 140 | + static const List<ActionButtonType> _actionTypes = [ |
| 141 | + ActionButtonType.share, |
| 142 | + ActionButtonType.shareLink, |
| 143 | + ActionButtonType.likeActivity, |
| 144 | + ActionButtonType.archive, |
| 145 | + ActionButtonType.unarchive, |
| 146 | + ActionButtonType.download, |
| 147 | + ActionButtonType.trash, |
| 148 | + ActionButtonType.deletePermanent, |
| 149 | + ActionButtonType.delete, |
| 150 | + ActionButtonType.moveToLockFolder, |
| 151 | + ActionButtonType.removeFromLockFolder, |
| 152 | + ActionButtonType.deleteLocal, |
| 153 | + ActionButtonType.upload, |
| 154 | + ActionButtonType.removeFromAlbum, |
| 155 | + ]; |
| 156 | + |
| 157 | + static List<Widget> build(ActionButtonContext context) { |
| 158 | + return _actionTypes.where((type) => type.shouldShow(context)).map((type) => type.buildButton(context)).toList(); |
| 159 | + } |
| 160 | +} |
0 commit comments