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

Allow item selection during opening animation #233

Merged
Merged
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
81 changes: 66 additions & 15 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

context("when multiple items are added") {
var action = "not done"
var actionCount = 0

beforeEach {
actionCount = 0
actionButton.addItem(title: "item 1", image: #imageLiteral(resourceName: "Like").withRenderingMode(.alwaysTemplate)) { _ in
action = "done!"
actionCount += 1
}
actionButton.addItem(title: "item 2", image: #imageLiteral(resourceName: "Baloon").withRenderingMode(.alwaysTemplate))
}
Expand All @@ -210,11 +212,19 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(actionButton.buttonState).toEventually(equal(.open))
}

it("opens when tapped twice") {
it("closes when tapped twice") {
actionButton.sendActions(for: .touchUpInside)
actionButton.sendActions(for: .touchUpInside)
expect(actionButton.buttonState) == .opening
expect(actionButton.buttonState).toEventually(equal(.open))
expect(actionButton.buttonState) == .closing
expect(actionButton.buttonState).toEventually(equal(.closed))
}

it("closes when tapped thrice") {
actionButton.sendActions(for: .touchUpInside)
actionButton.sendActions(for: .touchUpInside)
actionButton.sendActions(for: .touchUpInside)
expect(actionButton.buttonState) == .closing
expect(actionButton.buttonState).toEventually(equal(.closed))
}

context("and is opened") {
Expand Down Expand Up @@ -273,7 +283,7 @@ class JJFloatingActionButtonSpec: QuickSpec {

it("does not perform action") {
waitUntil(timeout: 1.5)
expect(action) != "done!"
expect(actionCount) == 0
}
}

Expand All @@ -289,7 +299,7 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

it("performs action") {
expect(action).toEventually(equal("done!"))
expect(actionCount).toEventually(equal(1))
}
}

Expand All @@ -309,7 +319,7 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

it("performs action") {
expect(action).toEventually(equal("done!"))
expect(actionCount).toEventually(equal(1))
}
}
}
Expand All @@ -331,7 +341,7 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

it("performs action") {
expect(action).toEventually(equal("done!"))
expect(actionCount).toEventually(equal(1))
}
}
}
Expand Down Expand Up @@ -365,6 +375,44 @@ class JJFloatingActionButtonSpec: QuickSpec {
it("has eventually state open") {
expect(actionButton.buttonState).toEventually(equal(.open))
}

context("and button is tapped") {
beforeEach {
actionButton.sendActions(for: .touchUpInside)
}

it("closes") {
expect(actionButton.buttonState) == .closing
expect(actionButton.buttonState).toEventually(equal(.closed))
}
}

context("and overlay is tapped") {
beforeEach {
actionButton.overlayView.sendActions(for: .touchUpInside)
}

it("closes") {
expect(actionButton.buttonState) == .closing
expect(actionButton.buttonState).toEventually(equal(.closed))
}
}

context("and item is tapped") {
beforeEach {
let item = actionButton.items[0]
item.sendActions(for: .touchUpInside)
}

it("closes") {
expect(actionButton.buttonState) == .closing
expect(actionButton.buttonState).toEventually(equal(.closed))
}

it("performs action") {
expect(actionCount).toEventually(equal(1))
}
}
}

context("and is closed animated") {
Expand Down Expand Up @@ -424,10 +472,12 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

context("when 1 item is added") {
var action = "not done"
var actionCount = 0

beforeEach {
actionCount = 0
actionButton.addItem(title: "item", image: #imageLiteral(resourceName: "Baloon").withRenderingMode(.alwaysTemplate)) { _ in
action = "done!"
actionCount += 1
}
}

Expand All @@ -445,7 +495,7 @@ class JJFloatingActionButtonSpec: QuickSpec {
}

it("performs action") {
expect(action).toEventually(equal("done!"))
expect(actionCount).toEventually(equal(1))
}
}

Expand Down Expand Up @@ -516,11 +566,12 @@ class JJFloatingActionButtonSpec: QuickSpec {

describe("JJFloatingActionButton using single item initializer") {
var actionButton: JJFloatingActionButton!
var action = "not done"
var actionCount = 0

beforeEach {
actionCount = 0
actionButton = JJFloatingActionButton(image: #imageLiteral(resourceName: "Favourite"), action: { _ in
action = "done!"
actionCount += 1
})
}

Expand All @@ -534,7 +585,7 @@ class JJFloatingActionButtonSpec: QuickSpec {

it("performs action when tapped") {
actionButton.sendActions(for: .touchUpInside)
expect(action).toEventually(equal("done!"))
expect(actionCount).toEventually(equal(1))
}
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/JJFloatingActionButton+Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ import UIKit
openItems(animated: animated, group: animationGroup)

let groupCompletion: () -> Void = {
guard self.buttonState == .opening else {
return
}
self.buttonState = .open
self.delegate?.floatingActionButtonDidOpen?(self)
completion?()
Expand All @@ -80,7 +83,7 @@ import UIKit
/// - SeeAlso: `itemAnimationConfiguration`
///
func close(animated: Bool = true, completion: (() -> Void)? = nil) {
guard buttonState == .open else {
guard buttonState == .open || buttonState == .opening else {
return
}
buttonState = .closing
Expand Down
2 changes: 1 addition & 1 deletion Sources/JJFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ internal extension JJFloatingActionButton {
fileprivate extension JJFloatingActionButton {
@objc func buttonWasTapped() {
switch buttonState {
case .open:
case .open, .opening:
close()

case .closed:
Expand Down