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

Add convenience methods to remove action items from the button #240

Merged
merged 1 commit into from
Mar 4, 2020
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
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