Skip to content

Commit

Permalink
1.2.0: Mutable items
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrommcarrasco committed Jul 23, 2019
1 parent 47a6a55 commit 10921bf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Hover (1.0.2)
- Hover (1.2.0)

DEPENDENCIES:
- Hover (from `..`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: ".."

SPEC CHECKSUMS:
Hover: 047bb7a44fdf9e49bd201ceec84241e0893012ef
Hover: 4d6d452c3f903162730289058e316319e1213566

PODFILE CHECKSUM: 4464a752690793fe72e3febd146ffc7ffb897004

Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/Hover.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Hover.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'Hover'
spec.version = '1.1.0'
spec.version = '1.2.0'
spec.license = { :type => 'MIT', :file => 'LICENSE' }
spec.homepage = 'https://github.com/pedrommcarrasco/Hover'
spec.authors = { 'Pedro Carrasco' => 'https://twitter.com/pedrommcarrasco' }
Expand Down
12 changes: 7 additions & 5 deletions Hover/Extensions/UIStackView+Add.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import UIKit
// MARK: - Add
extension UIStackView {

func add(arrangedViews: UIView...) {
arrangedViews.forEach {
self.addArrangedSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
func add(arrangedViews: UIView..., hidden: Bool = false) {
add(arrangedViews: arrangedViews, hidden: hidden)
}

func add(arrangedViews: [UIView], hidden: Bool = false) {
Expand All @@ -25,4 +22,9 @@ extension UIStackView {
$0.alpha = hidden ? 0.0 : 1.0
}
}
func removeAll() {
arrangedSubviews.forEach {
$0.removeFromSuperview()
}
}
}
28 changes: 18 additions & 10 deletions Hover/UI/HoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public class HoverView: UIView {

// MARK: Outlets
private let button: HoverButton
private let itemViews: [HoverItemView]
private var itemViews = [HoverItemView]() {
didSet {
animateState(to: false)
itemsStackView.removeAll()
itemsStackView.add(arrangedViews: itemViews, hidden: true)
adapt(to: currentAnchor)
itemViews.forEach { $0.onTap = onTapInButton }
}
}
private let itemsStackView: UIStackView = .create {
$0.spacing = Constant.interItemSpacing
$0.axis = .vertical
Expand All @@ -48,6 +56,12 @@ public class HoverView: UIView {
private var stackViewYConstraint = NSLayoutConstraint()

// MARK: Properties
public var items = [HoverItem]() {
didSet {
itemViews = items.reversed().map { HoverItemView(with: $0, configuration: configuration.itemConfiguration) }
}
}

public var isEnabled = true {
didSet {
UIView.animate(withDuration: Constant.enableAnimationDuration) {
Expand All @@ -65,7 +79,6 @@ public class HoverView: UIView {
// MARK: Private Properties
private let anchors: [Anchor]
private let configuration: HoverConfiguration
private let items: [HoverItem]
private let panRecognizer = UIPanGestureRecognizer()
private var state: State = .none
private var isOpen = false
Expand All @@ -81,7 +94,7 @@ public class HoverView: UIView {
}

// MARK: Lifecycle
public init(with configuration: HoverConfiguration = .init(), items: [HoverItem] = .init()) {
public init(with configuration: HoverConfiguration = HoverConfiguration(), items: [HoverItem] = []) {

guard !configuration.allowedPositions.isEmpty else {
fatalError("`allowedPositions` can't be empty")
Expand All @@ -93,11 +106,11 @@ public class HoverView: UIView {
fatalError("`allowedPositions` must contain the `initalPosition`")
}

defer { self.items = items }

self.currentAnchor = currentAnchor
self.configuration = configuration
self.button = HoverButton(with: configuration.color, image: configuration.image, imageSizeRatio: configuration.imageSizeRatio)
self.itemViews = items.reversed().map { HoverItemView(with: $0, configuration: configuration.itemConfiguration) }
self.items = items
super.init(frame: .zero)
configure()
}
Expand Down Expand Up @@ -136,7 +149,6 @@ private extension HoverView {
func addSubviews() {
add(layoutGuides: anchors.map { $0.guide })
add(views: dimView, itemsStackView, button)
itemsStackView.add(arrangedViews: itemViews, hidden: true)
}

func defineConstraints() {
Expand All @@ -161,17 +173,13 @@ private extension HoverView {
dimView.trailingAnchor.constraint(equalTo: trailingAnchor)
]
)

adapt(to: currentAnchor)
}

func setupSubviews() {
dimView.backgroundColor = configuration.dimColor

button.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(onPan(from:))))
button.addTarget(self, action: #selector(onTapInButton), for: .touchUpInside)

itemViews.forEach { $0.onTap = onTapInButton }
}
}

Expand Down

0 comments on commit 10921bf

Please sign in to comment.