Skip to content

Commit

Permalink
Allow item selection during opening animation (jjochen#233)
Browse files Browse the repository at this point in the history
* Close Fix

Fixes close function when the menu is opening.

* Close Fix

Fixes close function when the menu is opening.

* add relevant tests for taps during during opening animation

* prevent opening completion block when opening animation is interrupted

* add test for tap during during closing animation

* improve tests for actions

Co-authored-by: Martin Claesson <[email protected]>
  • Loading branch information
2 people authored and zalexej committed Mar 6, 2020
1 parent 032d854 commit 6a8d9da
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
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 @@ -62,6 +62,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 @@ -82,7 +85,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

0 comments on commit 6a8d9da

Please sign in to comment.