Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachment selection protocol 6.1.0 #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Sources/Plugins/AttachmentManager/AttachmentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ open class AttachmentManager: NSObject, InputPlugin {
case image(UIImage)
case url(URL)
case data(Data)
case attachment(AttachmentPicked)

@available(*, deprecated, message: ".other(AnyObject) has been depricated as of 2.0.0")
case other(AnyObject)
Expand All @@ -54,7 +55,7 @@ open class AttachmentManager: NSObject, InputPlugin {
}()

/// The attachments that the managers holds
private(set) public var attachments = [Attachment]() { didSet { reloadData() } }
public var attachments = [Attachment]() { didSet { reloadData() } }

/// A flag you can use to determine if you want the manager to be always visible
open var isPersistent = false { didSet { attachmentView.reloadData() } }
Expand Down Expand Up @@ -102,6 +103,8 @@ open class AttachmentManager: NSObject, InputPlugin {
attachment = .url(url)
} else if let data = object as? Data {
attachment = .data(data)
} else if let attachmentPicked = object as? AttachmentPicked {
attachment = .attachment(attachmentPicked)
} else {
return false
}
Expand Down Expand Up @@ -153,6 +156,10 @@ extension AttachmentManager: UICollectionViewDataSource, UICollectionViewDelegat
if indexPath.row == attachments.count {
delegate?.attachmentManager(self, didSelectAddAttachmentAt: indexPath.row)
delegate?.attachmentManager(self, shouldBecomeVisible: attachments.count > 0 || isPersistent)
} else {
let attachment = attachments[indexPath.row]
delegate?.attachmentManager(self, didSelectAttachment: attachment)

}
}

Expand Down Expand Up @@ -191,6 +198,31 @@ extension AttachmentManager: UICollectionViewDataSource, UICollectionViewDelegat
cell.imageView.tintColor = tintColor
cell.deleteButton.backgroundColor = tintColor
return cell
case .attachment(let attach):
if attach.fileType == "IMAGE" {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageAttachmentCell.reuseIdentifier, for: indexPath) as? ImageAttachmentCell else {
fatalError()
}
cell.attachment = attachment
cell.indexPath = indexPath
cell.manager = self
cell.imageView.image = UIImage(data: attach.pickedFile)
cell.deleteButton.backgroundColor = tintColor

return cell
} else {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CustomAttachmentCell.reuseIdentifier, for: indexPath) as? CustomAttachmentCell else {
fatalError()
}
cell.attachment = attachment
cell.indexPath = indexPath
cell.manager = self
cell.attachmentLabel.text = attach.fileName
cell.deleteButton.backgroundColor = tintColor

return cell
}

default:
return collectionView.dequeueReusableCell(withReuseIdentifier: AttachmentCell.reuseIdentifier, for: indexPath) as! AttachmentCell
}
Expand Down
20 changes: 20 additions & 0 deletions Sources/Plugins/AttachmentManager/Models/AttachmentPicked.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// File.swift
//
//
// Created by Bruno Antoninho on 07/06/2022.
//

import Foundation

open class AttachmentPicked {

open var fileType: String!
open var fileName: String!
open var fileSize: Int!
open var mimeType: String!
open var blob: String!
open var pickedFile: Data!

public init() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public protocol AttachmentManagerDelegate: AnyObject {
/// - manager: The AttachmentManager
/// - attachments: The index of the AddAttachmentCell
func attachmentManager(_ manager: AttachmentManager, didSelectAddAttachmentAt index: Int)

/// Notifys when an AttachmentCell was selected
///
/// - Parameters:
/// - manager: The AttachmentManager
/// - attachments: The index of the AddAttachmentCell
func attachmentManager(_ manager: AttachmentManager, didSelectAttachment attachment: AttachmentManager.Attachment)
}

public extension AttachmentManagerDelegate {
Expand All @@ -78,4 +85,6 @@ public extension AttachmentManagerDelegate {
func attachmentManager(_ manager: AttachmentManager, didReloadTo attachments: [AttachmentManager.Attachment]) {}

func attachmentManager(_ manager: AttachmentManager, didSelectAddAttachmentAt index: Int) {}

func attachmentManager(_ manager: AttachmentManager, didSelectAttachment attachment: AttachmentManager.Attachment) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,6 @@ open class AttachmentCollectionView: UICollectionView {
setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .vertical)
register(AttachmentCell.self, forCellWithReuseIdentifier: AttachmentCell.reuseIdentifier)
register(ImageAttachmentCell.self, forCellWithReuseIdentifier: ImageAttachmentCell.reuseIdentifier)
register(CustomAttachmentCell.self, forCellWithReuseIdentifier: CustomAttachmentCell.reuseIdentifier)
}
}
84 changes: 84 additions & 0 deletions Sources/Plugins/AttachmentManager/Views/CustomAttachmentCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// CustomAttachmentCell.swift
//
//
// Created by Bruno Antoninho on 08/06/2022.
//

import UIKit

open class CustomAttachmentCell: AttachmentCell {

override open class var reuseIdentifier: String {
return "CustomAttachmentCell"
}

// MARK: - Properties

open lazy var attachmentImage: UIImageView = { [weak self] in
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "documentIcon")
imageView.tintColor = UIColor(red: 0.682, green: 0.682, blue: 0.698, alpha: 1)
return imageView
}()

open lazy var attachmentLabel: UILabel = { [weak self] in
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 10, weight: .semibold)
label.textColor = UIColor(red: 0.682, green: 0.682, blue: 0.698, alpha: 1)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()


private var attachmentImageLayoutSet: NSLayoutConstraintSet?
private var attachmentLabelLayoutSet: NSLayoutConstraintSet?

// MARK: - Initialization

public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}

open override func prepareForReuse() {
super.prepareForReuse()
attachmentLabel.text = ""
}

// MARK: - Setup

private func setup() {

setupSubviews()
setupConstraints()
}

private func setupSubviews() {
containerView.addSubview(attachmentImage)
containerView.addSubview(attachmentLabel)
}

private func setupConstraints() {

attachmentImageLayoutSet = NSLayoutConstraintSet(
centerX: attachmentImage.centerXAnchor.constraint(equalTo: containerView.centerXAnchor, constant: 0),
centerY: attachmentImage.centerYAnchor.constraint(equalTo: containerView.centerYAnchor, constant: -5),
width: attachmentImage.widthAnchor.constraint(equalToConstant: 26),
height: attachmentImage.heightAnchor.constraint(equalToConstant: 29)
).activate()

attachmentLabelLayoutSet = NSLayoutConstraintSet(
left: attachmentLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10),
right: attachmentLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -10),
centerX: attachmentLabel.centerXAnchor.constraint(equalTo: containerView.centerXAnchor, constant: 0),
centerY: attachmentLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor, constant: 20)
).activate()
}
}