Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Fixed a bug causing a tag view to not send tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nekonora committed Sep 7, 2019
1 parent d9124be commit 5011bda
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 78 deletions.
36 changes: 11 additions & 25 deletions Example/TaggerKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,22 @@ class ViewController: UIViewController {
// testCollection.customSpacing = 20.0 // Spacing between cells
// testCollection.customBackgroundColor = UIColor.red // Background of cells

productTags = TKCollectionView(action: .removeTag, receiver: nil)
allTags = TKCollectionView(action: .addTag, receiver: productTags)
productTags = TKCollectionView(tags: [
"Tech", "Design", "Writing", "Social Media"
],
action: .removeTag,
receiver: nil)

// These are the tags already added by the user, give an aray of strings to the collection
productTags.tags = ["Tech", "Design", "Writing", "Social Media"]

// These are intended to be all the tags the user has added in the app, which are going to be filtered
allTags.tags = ["Cars", "Skateboard", "Freetime", "Humor", "Travel", "Music", "Places", "Journalism", "Music", "Sports"]

/*
We set this collection's action to .removeTag,
becasue these are supposed to be the tags the user has already added
*/
productTags.action = .removeTag
allTags = TKCollectionView(tags: [
"Cars", "Skateboard", "Freetime", "Humor", "Travel", "Music", "Places", "Journalism", "Music", "Sports"
],
action: .addTag,
receiver: productTags)

// Set the current controller as the delegate of both collections
productTags.delegate = self
allTags.delegate = self

// "testCollection" takes the tags sent by "searchCollection"
allTags.receiver = productTags

// The tags in "searchCollection" are going to be added, so we set the action to addTag
allTags.action = .addTag

// Set the sender and receiver of the TextField
addTagsTextField.sender = allTags
addTagsTextField.receiver = productTags
Expand All @@ -67,12 +58,7 @@ class ViewController: UIViewController {
// MARK: - Extension to TKCollectionViewDelegate

extension ViewController: TKCollectionViewDelegate {

/*
These methods come from UIViewController now conforming to TKCollectionViewDelegate,
You use these to do whatever you want when a tag is added or removed (e.g. save to file, etc)
*/


func tagIsBeingAdded(name: String?) {
// Example: save testCollection.tags to UserDefault
print("added \(name!)")
Expand Down
26 changes: 0 additions & 26 deletions TaggerKit/Classes/Source/CollectionView/TKCollectionView+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,6 @@ extension TKCollectionView: UICollectionViewDataSource {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TKCell", for: indexPath) as? TKTagCell else { return UICollectionViewCell() }
return cell
}

public func addNewTag(named tag: String) {
guard let receiver = self.receiver, !tag.isEmpty, receiver.tags.contains(tag) else { return }

receiver.tags.insert(tag, at: 0)

let indexPath = IndexPath(item: 0, section: 0)

receiver.tagsCollectionView.performBatchUpdates({
receiver.tagsCollectionView.insertItems(at: [indexPath])
}, completion: nil)
}

public func removeOldTag(named tag: String) {
guard tags.contains(tag) else { return }

if let index = tags.firstIndex(of: tag) {
tags.remove(at: index)

let indexPath = IndexPath(item: index, section: 0)

tagsCollectionView.performBatchUpdates({
self.tagsCollectionView?.deleteItems(at: [indexPath])
}, completion: nil)
}
}
}

// MARK: - Extension to UICollectionViewDelegate
Expand Down
64 changes: 50 additions & 14 deletions TaggerKit/Classes/Source/CollectionView/TKCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public class TKCollectionView: UIViewController {
// MARK: - Class properties

/// The actual collectionView inside the controller, where every cell is a tag
public var tagsCollectionView : UICollectionView!
public var tagsCollectionView: UICollectionView!

/// The custom cell layout of the single cell
public var tagCellLayout : TagCellLayout!
public var tagCellLayout: TagCellLayout!

/// If tags in the collection are given an action of type "add", a receiver can be automatically binded on this property
public var receiver : TKCollectionView?
public var receiver: TKCollectionView?

/// A controller that confromed to be a delegate for the tags collection view
public var delegate : TKCollectionViewDelegate?
public var delegate: TKCollectionViewDelegate?

lazy var oneLineHeight: CGFloat = { customFont.pointSize * 2 }()

Expand All @@ -64,11 +64,12 @@ public class TKCollectionView: UIViewController {

// MARK: - Lifecycle methods

public convenience init(action: ActionType, receiver: TKCollectionView?) {
public convenience init(tags: [String], action: ActionType, receiver: TKCollectionView?) {
self.init()

self.action = action
self.receiver = receiver
self.tags = tags
}

public override func viewDidLoad() {
Expand All @@ -83,21 +84,56 @@ public class TKCollectionView: UIViewController {
tagsCollectionView.frame = view.bounds
}

// MARK - Public Methods

/// Adds a tag to the tag view
/// - Parameter tag: the name of the tag to add
public func addNewTag(named tag: String) {
guard
receiver != nil,
!tag.isEmpty
else { return }

if receiver!.tags.contains(tag) {
return
} else {
receiver!.tags.insert(tag, at: 0)
let indexPath = IndexPath(item: 0, section: 0)
receiver!.tagsCollectionView.performBatchUpdates({
receiver!.tagsCollectionView.insertItems(at: [indexPath])
}, completion: nil)
}
}

/// Removes a tag from the tag view
/// - Parameter tag: the name of the tag to remove
public func removeOldTag(named tag: String) {
guard tags.contains(tag) else { return }

if let index = tags.firstIndex(of: tag) {
tags.remove(at: index)

let indexPath = IndexPath(item: index, section: 0)

tagsCollectionView.performBatchUpdates({
self.tagsCollectionView?.deleteItems(at: [indexPath])
}, completion: nil)
}
}

// MARK: - Class Methods

private func setupView() {
tagCellLayout = TagCellLayout(alignment: .left, delegate: self)
tagCellLayout.delegate = self
tagCellLayout = TagCellLayout(alignment: .left, delegate: self)
tagCellLayout.delegate = self

tagsCollectionView = UICollectionView(frame: view.bounds, collectionViewLayout: tagCellLayout)
tagsCollectionView.dataSource = self
tagsCollectionView.delegate = self
tagsCollectionView.alwaysBounceVertical = true
tagsCollectionView.backgroundColor = UIColor.clear
tagsCollectionView = UICollectionView(frame: view.bounds, collectionViewLayout: tagCellLayout)
tagsCollectionView.dataSource = self
tagsCollectionView.delegate = self
tagsCollectionView.alwaysBounceVertical = true
tagsCollectionView.backgroundColor = UIColor.clear
tagsCollectionView.register(TKTagCell.self, forCellWithReuseIdentifier: "TKCell")

view.addSubview(tagsCollectionView)
}

}

11 changes: 0 additions & 11 deletions TaggerKit/Classes/Source/Extensions/UIViewController+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,8 @@ public extension UIViewController {
view.removeFromSuperview()
removeFromParent()
}

}

// MARK: - Extension to TKCollectionViewDelegate

//extension UIViewController: TKCollectionViewDelegate {
//
// @objc open func tagIsBeingAdded(name: String?) { }
//
// @objc open func tagIsBeingRemoved(name: String?) { }
//
//}

extension UIBezierPath {

func shapeImage(view: UIView) -> UIImage? {
Expand Down
4 changes: 2 additions & 2 deletions TaggerKit/Classes/Source/TextField/TKTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class TKTextField: UITextField {
// Objects to operate - obviously should not be the same

/// A collection view of tags that can be sent to a receiver collection
public var sender : TKCollectionView? { didSet { allTags = sender?.tags } }
public var sender: TKCollectionView? { didSet { allTags = sender?.tags } }

/// A collection view of tags that receives tags from other senders
public var receiver : TKCollectionView?
public var receiver: TKCollectionView?

var allTags: [String]!

Expand Down

0 comments on commit 5011bda

Please sign in to comment.