Skip to content

Commit

Permalink
Add convenience methods to remove action items from the button (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjochen committed Mar 4, 2020
1 parent f1a126a commit d8d7aae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
53 changes: 53 additions & 0 deletions Example/Tests/JJFloatingActionButtonSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,59 @@ class JJFloatingActionButtonSpec: QuickSpec {
expect(superview) == snapshot()
}
}

context("and all but one item are removed") {
beforeEach {
for (index, item) in actionButton.items.enumerated() {
if index > 0 {
actionButton.removeItem(item)
}
}
}

it("has the correct number of items") {
expect(actionButton.items.count) == 1
}

it("looks correct") {
expect(superview) == snapshot()
}

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

context("and the first item is removed") {
var first: JJActionItem!
var result: JJActionItem?
beforeEach {
first = actionButton.items.first
result = actionButton.removeItem(first)
}

it("returns the first item") {
expect(result).notTo(beNil())
expect(result) == first
}
}

context("and an item not from the list is removed") {
var result: JJActionItem?
beforeEach {
let item = JJActionItem()
result = actionButton.removeItem(item)
}

it("has the correct number of items") {
expect(actionButton.items.count) == 2
}

it("returns nil") {
expect(result).to(beNil())
}
}
}

context("when 1 item is added") {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 24 additions & 3 deletions Sources/JJFloatingActionButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,32 @@ import UIKit
///
/// - Parameter item: The action item.
///
/// - Returns: The item that was add. Its configuration can be changed after it has been added.
///
func addItem(_ item: JJActionItem) {
items.append(item) // this will call `didSet` of `items`
configureButtonImage()
}

/// Remove an action item from the list of items.
///
/// - Parameter item: The action item.
///
/// - Returns: The item that was removed. `nil` if `item` was not found.
///
@discardableResult func removeItem(_ item: JJActionItem) -> JJActionItem? {
guard let index = items.firstIndex(of: item) else {
return nil
}
return removeItem(at: index)
}

/// Remove and returns the action item at the specified position in the list of items.
///
/// - Parameter index: The index of the action item. `index` must
/// be a valid index of the list of items.
///
/// - Returns: The item that was removed.
///
@discardableResult func removeItem(at index: Int) -> JJActionItem {
return items.remove(at: index)
}

/// Calls the given closure on each item that is or was added to the floating action button.
Expand Down

0 comments on commit d8d7aae

Please sign in to comment.