Skip to content

Commit

Permalink
Handle hidden items (#52)
Browse files Browse the repository at this point in the history
* handle button animations in separate classes

* handle item animations in separate classes

* do not include items that are hidden or have user interaction disabled

* handle hidden items everywhere else

* change open variables to public
  • Loading branch information
jjochen committed Dec 24, 2017
1 parent 08761f1 commit 73e9c4d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
17 changes: 17 additions & 0 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Expand Up @@ -298,6 +298,23 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(actionButton.imageView.image).toEventually(equal(actionButton.defaultButtonImage))
}
}

context("plus one hidden item") {
var hiddenItem: JJActionItem!
beforeEach {
hiddenItem = actionButton.addItem(title: "hidden item", image: #imageLiteral(resourceName: "Like").withRenderingMode(.alwaysTemplate))
hiddenItem.isHidden = true
}

it("it will not be added") {
expect(actionButton.enabledItems).toNot(contain(hiddenItem))
}

it("looks correct when opened") {
actionButton.open(animated: false)
expect(superview) == snapshot()
}
}
}

context("when 1 item is added") {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 19 additions & 8 deletions JJFloatingActionButton/Classes/JJFloatingActionButton.swift
Expand Up @@ -306,12 +306,12 @@ import UIKit
/// The round background view of the floating action button.
/// Read only.
///
@objc open fileprivate(set) lazy var circleView: JJCircleView = lazyCircleView()
@objc public fileprivate(set) lazy var circleView: JJCircleView = lazyCircleView()

/// The image view of the floating action button.
/// Read only.
///
@objc open fileprivate(set) lazy var imageView: UIImageView = lazyImageView()
@objc public fileprivate(set) lazy var imageView: UIImageView = lazyImageView()

public override init(frame: CGRect) {
super.init(frame: frame)
Expand Down Expand Up @@ -420,6 +420,8 @@ public extension JJFloatingActionButton {
/// - Parameter animated: When true, button will be opened with an animation. Default is `true`.
/// - Parameter completion: Will be handled upon completion. Default is `nil`.
///
/// - Remark: Hidden items and items that have user interaction disabled are omitted.
///
@objc func open(animated: Bool = true, completion: (() -> Void)? = nil) {
guard buttonState == .closed else {
return
Expand Down Expand Up @@ -499,6 +501,15 @@ public extension JJFloatingActionButton {
groupCompletion()
}
}

/// All items that will be shown when floating action button ist opened.
/// This excludes hidden items and items that have user interaction disabled.
///
@objc public var enabledItems: [JJActionItem] {
return items.filter { item -> Bool in
!item.isHidden && item.isUserInteractionEnabled
}
}
}

// MARK: - UIControl
Expand Down Expand Up @@ -527,7 +538,7 @@ extension JJFloatingActionButton {

fileprivate extension JJFloatingActionButton {
func setup() {
backgroundColor = UIColor.clear
backgroundColor = .clear
clipsToBounds = false
isUserInteractionEnabled = true
addTarget(self, action: #selector(buttonWasTapped), for: .touchUpInside)
Expand Down Expand Up @@ -580,7 +591,7 @@ fileprivate extension JJFloatingActionButton {
}

var currentButtonImage: UIImage? {
if isSingleActionButton, let image = items.first?.imageView.image {
if isSingleActionButton, let image = enabledItems.first?.imageView.image {
return image
}

Expand All @@ -598,7 +609,7 @@ fileprivate extension JJFloatingActionButton {
}

var isSingleActionButton: Bool {
return handleSingleActionDirectly && items.count == 1
return handleSingleActionDirectly && enabledItems.count == 1
}
}

Expand Down Expand Up @@ -661,7 +672,7 @@ fileprivate extension JJFloatingActionButton {
var currentItemAnimation: JJItemAnimation {
let itemAnimation: JJItemAnimation
itemAnimation = JJItemPopAnimation(actionButton: self,
items: items,
items: enabledItems,
itemSizeRatio: itemSizeRatio,
interItemSpacing: interItemSpacing)

Expand Down Expand Up @@ -697,12 +708,12 @@ fileprivate extension JJFloatingActionButton {
}

func handleSingleActionOrOpen() {
guard !items.isEmpty else {
guard !enabledItems.isEmpty else {
return
}

if isSingleActionButton {
let item = items.first
let item = enabledItems.first
item?.action?(item!)
} else {
open()
Expand Down

0 comments on commit 73e9c4d

Please sign in to comment.