diff --git a/FlowCrypt/Controllers/Compose/ComposeViewController.swift b/FlowCrypt/Controllers/Compose/ComposeViewController.swift index 1b5a4f007..592b7ee95 100644 --- a/FlowCrypt/Controllers/Compose/ComposeViewController.swift +++ b/FlowCrypt/Controllers/Compose/ComposeViewController.swift @@ -402,7 +402,11 @@ extension ComposeViewController { AttachmentNode( input: .init( composeAttachment: contextToSend.attachments[index] - ) + ), + onDeleteTap: { [weak self] in + self?.contextToSend.attachments.safeRemove(at: index) + self?.node.reloadSections(IndexSet(integer: 2), with: .automatic) + } ) } } diff --git a/FlowCryptCommon/Extensions/CollectionExtensions.swift b/FlowCryptCommon/Extensions/CollectionExtensions.swift index 6505b39a4..a4ab99fa7 100644 --- a/FlowCryptCommon/Extensions/CollectionExtensions.swift +++ b/FlowCryptCommon/Extensions/CollectionExtensions.swift @@ -41,6 +41,13 @@ public extension Array { } } +public extension Array { + mutating func safeRemove(at index: Int) { + if !self.indices.contains(index) { return } + self.remove(at: index) + } +} + public extension Array where Element == String { func firstCaseInsensitive(_ stringToCompare: String) -> Element? { first(where: { $0.caseInsensitiveCompare(stringToCompare) == .orderedSame }) diff --git a/FlowCryptUI/Nodes/AttachmentNode.swift b/FlowCryptUI/Nodes/AttachmentNode.swift index 6a71dd7a4..97730ab0d 100644 --- a/FlowCryptUI/Nodes/AttachmentNode.swift +++ b/FlowCryptUI/Nodes/AttachmentNode.swift @@ -21,14 +21,18 @@ public final class AttachmentNode: CellNode { private let imageNode = ASImageNode() private let buttonNode = ASButtonNode() private let borderNode = ASDisplayNode() + private let deleteButtonNode = ASButtonNode() private var onDownloadTap: (() -> Void)? + private var onDeleteTap: (() -> Void)? public init( input: Input, - onDownloadTap: (() -> Void)? = nil + onDownloadTap: (() -> Void)? = nil, + onDeleteTap: (() -> Void)? = nil ) { self.onDownloadTap = onDownloadTap + self.onDeleteTap = onDeleteTap super.init() automaticallyManagesSubnodes = true borderNode.borderWidth = 1.0 @@ -38,7 +42,7 @@ public final class AttachmentNode: CellNode { imageNode.tintColor = .gray buttonNode.tintColor = .gray - + deleteButtonNode.setImage(UIImage(named: "cancel")?.tinted(.gray), for: .normal) imageNode.image = UIImage(named: "paperclip")?.tinted(.gray) buttonNode.setImage(UIImage(named: "download")?.tinted(.gray), for: .normal) titleNode.attributedText = input.name @@ -46,11 +50,18 @@ public final class AttachmentNode: CellNode { buttonNode.addTarget(self, action: #selector(onDownloadButtonTap), forControlEvents: .touchUpInside) buttonNode.isHidden = onDownloadTap == nil + + deleteButtonNode.addTarget(self, action: #selector(onDeleteButtonTap), forControlEvents: .touchUpInside) + deleteButtonNode.isHidden = onDeleteTap == nil } @objc private func onDownloadButtonTap() { onDownloadTap?() } + + @objc private func onDeleteButtonTap() { + onDeleteTap?() + } public override func layoutSpecThatFits(_: ASSizeRange) -> ASLayoutSpec { let verticalStack = ASStackLayoutSpec.vertical() @@ -65,7 +76,7 @@ public final class AttachmentNode: CellNode { spacing: 10, justifyContent: .start, alignItems: .center, - children: [imageNode, verticalStack, buttonNode] + children: [imageNode, verticalStack, buttonNode, deleteButtonNode] ) let borderInset = UIEdgeInsets.side(8)