diff --git a/StickerSlack/SwiftUI/DownloadedView.swift b/StickerSlack/SwiftUI/DownloadedView.swift index 873c5da..9d903b6 100644 --- a/StickerSlack/SwiftUI/DownloadedView.swift +++ b/StickerSlack/SwiftUI/DownloadedView.swift @@ -45,7 +45,8 @@ struct DownloadedView: View { items: emojiHoarder.downloadedStickersArr, pack: nil, width: 75, - style: .plainWithMenu + style: .plainWithMenu, + edit: false ) // GeometryReader { geo in // ScrollView { diff --git a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift index 2c9f4b7..6792b8b 100644 --- a/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift +++ b/StickerSlack/SwiftUI/EmojiPacks/EmojiPackDetailView.swift @@ -77,7 +77,8 @@ struct EmojiPackDetailView: View { items: pack.items, pack: pack, width: 75, - style: .full + style: .full, + edit: edit ) GeometryReader { geo in diff --git a/StickerSlack/SwiftUI/SettingsView.swift b/StickerSlack/SwiftUI/SettingsView.swift index b525e17..442aebc 100644 --- a/StickerSlack/SwiftUI/SettingsView.swift +++ b/StickerSlack/SwiftUI/SettingsView.swift @@ -118,7 +118,8 @@ struct SettingsView: View { items: hoarder.emojis.prefix(1000).map { $0.name }, pack: nil, width: 50, - style: .plainWithMenu + style: .plainWithMenu, + edit: false ) } label: { Label("grid view", systemImage: "square.grid.2x2.fill") diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift index 892f648..1333363 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionView.swift @@ -16,6 +16,8 @@ struct EmojiCollectionView: UIViewRepresentable { let pack: EmojiPack? let width: CGFloat let style: EmojiCollectionView.Style + var edit: Bool + var onRemove: ((String) -> Void)? func makeUIView(context: Context) -> UICollectionView { let collectionView = context.coordinator as UICollectionViewController @@ -30,6 +32,8 @@ struct EmojiCollectionView: UIViewRepresentable { context.coordinator.hoarder = hoarder context.coordinator.items = items context.coordinator.pack = pack + context.coordinator.edit = edit + context.coordinator.onRemove = onRemove uiView.reloadData() } @@ -39,7 +43,9 @@ struct EmojiCollectionView: UIViewRepresentable { items: items, pack: pack, width: width, - style: style + style: style, + edit: edit, + onRemove: onRemove ) } @@ -55,6 +61,8 @@ struct EmojiCollectionView: UIViewRepresentable { var pack: EmojiPack? var width: CGFloat var style: EmojiCollectionView.Style + var edit: Bool + var onRemove: ((String) -> Void)? var layout = UICollectionViewFlowLayout() @@ -63,7 +71,9 @@ struct EmojiCollectionView: UIViewRepresentable { items: [String], pack: EmojiPack?, width: CGFloat, - style: EmojiCollectionView.Style + style: EmojiCollectionView.Style, + edit: Bool, + onRemove: ((String) -> Void)? ) { self.hoarder = hoarder self.items = items @@ -73,6 +83,8 @@ struct EmojiCollectionView: UIViewRepresentable { } self.width = width self.style = style + self.edit = edit + self.onRemove = onRemove layout.minimumInteritemSpacing = 8 @@ -102,6 +114,7 @@ struct EmojiCollectionView: UIViewRepresentable { cell = collectionView.dequeueReusableCell(withReuseIdentifier: "plain", for: indexPath) as! PlainEmojiCollectionViewCell case .full: cell = collectionView.dequeueReusableCell(withReuseIdentifier: "full", for: indexPath) as! EmojiCollectionViewCell + (cell as! EmojiCollectionViewCell).setEdit(to: edit) } guard !hoarder.trie.dict.isEmpty else { return cell } @@ -109,6 +122,7 @@ struct EmojiCollectionView: UIViewRepresentable { guard let emoji = hoarder.trie.dict[emojiName] else { return cell } cell.configure(with: hoarder, emoji: emoji) + return cell } diff --git a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift index 8af968d..2281ed0 100644 --- a/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift +++ b/StickerSlack/UIKit/CollectionView/EmojiCollectionViewCell.swift @@ -11,6 +11,8 @@ import SwiftUI class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { let label = UILabel() + let deleteButton = UIButton(type: .custom) + var edit: Bool = false override init(frame: CGRect) { super.init(frame: frame) @@ -25,6 +27,14 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), ]) + + deleteButton.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal) + deleteButton.imageView?.contentMode = .scaleAspectFit + deleteButton.configuration = .plain() + contentView.addSubview(deleteButton) + deleteButton.tintColor = .systemRed + deleteButton.clipsToBounds = false + deleteButton.translatesAutoresizingMaskIntoConstraints = false } required init?(coder: NSCoder) { @@ -38,10 +48,18 @@ class EmojiCollectionViewCell: PlainEmojiCollectionViewCell { override func configure(with: EmojiHoarder, emoji: Emoji) { super.configure(with: with, emoji: emoji) label.text = emoji.UIName + contentView.bringSubviewToFront(deleteButton) - if label.superview == nil { - contentView.addSubview(label) - } + NSLayoutConstraint.activate([ + deleteButton.topAnchor.constraint(equalTo: hostingController!.view.topAnchor, constant: -16), + deleteButton.leadingAnchor.constraint(equalTo: hostingController!.view.leadingAnchor, constant: -16), + deleteButton.heightAnchor.constraint(equalToConstant: 32), + deleteButton.widthAnchor.constraint(equalToConstant: 32) + ]) + } + + func setEdit(to newValue: Bool) { + self.edit = newValue } override func prepareForReuse() {