Skip to content

Commit

Permalink
Fixes #438 (remove button not working)
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Jan 9, 2020
1 parent 7c2bb71 commit 44e275b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
29 changes: 18 additions & 11 deletions Source/SelectionsGallery/YPSelectionsGalleryCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
import UIKit
import Stevia

public protocol YPSelectionsGalleryCellDelegate: class {
func selectionsGalleryCellDidTapRemove(cell: YPSelectionsGalleryCell)
}

public class YPSelectionsGalleryCell: UICollectionViewCell {

weak var delegate: YPSelectionsGalleryCellDelegate?
let imageView = UIImageView()
let editIcon = UIView()
let editSquare = UIView()
let removeButton = UIButton()

override init(frame: CGRect) {
super.init(frame: frame)

sv(
imageView,
editIcon,
editSquare
editSquare,
removeButton
)

imageView.fillContainer()
Expand All @@ -30,6 +37,8 @@ public class YPSelectionsGalleryCell: UICollectionViewCell {
editSquare.CenterY == editIcon.CenterY
editSquare.CenterX == editIcon.CenterX

removeButton.top(12).trailing(12)

layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.2
layer.shadowOffset = CGSize(width: 4, height: 7)
Expand All @@ -47,27 +56,25 @@ public class YPSelectionsGalleryCell: UICollectionViewCell {
v.layer.borderWidth = 1
v.layer.borderColor = UIColor.ypLabel.cgColor
}
removeButton.setImage(YPConfig.icons.removeImage, for: .normal)

removeButton.addTarget(self, action: #selector(removeButtonTapped), for: .touchUpInside)
}

@objc
func removeButtonTapped() {
delegate?.selectionsGalleryCellDidTapRemove(cell: self)
}

func setEditable(_ editable: Bool) {
self.editIcon.isHidden = !editable
self.editSquare.isHidden = !editable
}

func addRemoveButton(target: Any?, action: Selector) {
let image = YPConfig.icons.removeImage
let removeButton = UIButton(frame: CGRect(origin: CGPoint.zero, size: image.size))
removeButton.setBackgroundImage(image, for: UIControl.State())
sv(removeButton)
removeButton.top(12).trailing(12)
removeButton.addTarget(target, action: action, for: .touchUpInside)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


public override var isHighlighted: Bool {
didSet {
UIView.animate(withDuration: 0.5,
Expand Down
23 changes: 9 additions & 14 deletions Source/SelectionsGallery/YPSelectionsGalleryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

public class YPSelectionsGalleryVC: UIViewController {
public class YPSelectionsGalleryVC: UIViewController, YPSelectionsGalleryCellDelegate {

override public var prefersStatusBarHidden: Bool { return YPConfig.hidesStatusBar }

Expand Down Expand Up @@ -63,15 +63,13 @@ public class YPSelectionsGalleryVC: UIViewController {
didFinishHandler?(self, items)
}

@objc
private func removeButtonDidClick(sender: UIButton) {
guard let cell = sender.superview as? UICollectionViewCell, let indexPath = v.collectionView.indexPath(for: cell) else {
return
public func selectionsGalleryCellDidTapRemove(cell: YPSelectionsGalleryCell) {
if let indexPath = v.collectionView.indexPath(for: cell) {
items.remove(at: indexPath.row)
v.collectionView.performBatchUpdates({
v.collectionView.deleteItems(at: [indexPath])
}, completion: { _ in })
}
items.remove(at: indexPath.row)
v.collectionView.performBatchUpdates({
v.collectionView.deleteItems(at: [indexPath])
}, completion: { _ in })
}
}

Expand All @@ -87,6 +85,7 @@ extension YPSelectionsGalleryVC: UICollectionViewDataSource {
for: indexPath) as? YPSelectionsGalleryCell else {
return UICollectionViewCell()
}
cell.delegate = self
let item = items[indexPath.row]
switch item {
case .photo(let photo):
Expand All @@ -96,11 +95,7 @@ extension YPSelectionsGalleryVC: UICollectionViewDataSource {
cell.imageView.image = video.thumbnail
cell.setEditable(YPConfig.showsVideoTrimmer)
}

if !YPConfig.gallery.hidesRemoveButton {
cell.addRemoveButton(target: self, action: #selector(removeButtonDidClick(sender:)))
}

cell.removeButton.isHidden = YPConfig.gallery.hidesRemoveButton
return cell
}
}
Expand Down

0 comments on commit 44e275b

Please sign in to comment.