Skip to content

Commit

Permalink
Merge pull request #2 from odigeoteam/develop
Browse files Browse the repository at this point in the history
ActioBarManager Callbacks
  • Loading branch information
ndleon09 authored Sep 15, 2016
2 parents aa10d74 + 9232de6 commit 4fcc891
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 55 deletions.
8 changes: 4 additions & 4 deletions ActionBarManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/* Begin PBXBuildFile section */
2564E6311D88524500A9DC3E /* ActionBarManager.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2564E6271D88524500A9DC3E /* ActionBarManager.framework */; };
2564E6361D88524500A9DC3E /* ActionBarManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2564E6351D88524500A9DC3E /* ActionBarManagerTests.swift */; };
2564E6361D88524500A9DC3E /* ActionBarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2564E6351D88524500A9DC3E /* ActionBarTests.swift */; };
2564E6381D88524500A9DC3E /* ActionBarManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 2564E62A1D88524500A9DC3E /* ActionBarManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
2564E6431D88526D00A9DC3E /* ActionBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2564E6411D88526D00A9DC3E /* ActionBar.swift */; };
2564E6441D88526D00A9DC3E /* ActionBarManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2564E6421D88526D00A9DC3E /* ActionBarManager.swift */; };
Expand All @@ -31,7 +31,7 @@
2564E62A1D88524500A9DC3E /* ActionBarManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ActionBarManager.h; sourceTree = "<group>"; };
2564E62B1D88524500A9DC3E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2564E6301D88524500A9DC3E /* ActionBarManagerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ActionBarManagerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2564E6351D88524500A9DC3E /* ActionBarManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionBarManagerTests.swift; sourceTree = "<group>"; };
2564E6351D88524500A9DC3E /* ActionBarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionBarTests.swift; sourceTree = "<group>"; };
2564E6371D88524500A9DC3E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2564E6411D88526D00A9DC3E /* ActionBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionBar.swift; sourceTree = "<group>"; };
2564E6421D88526D00A9DC3E /* ActionBarManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ActionBarManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,7 +98,7 @@
2564E6341D88524500A9DC3E /* ActionBarManagerTests */ = {
isa = PBXGroup;
children = (
2564E6351D88524500A9DC3E /* ActionBarManagerTests.swift */,
2564E6351D88524500A9DC3E /* ActionBarTests.swift */,
2564E6371D88524500A9DC3E /* Info.plist */,
);
path = ActionBarManagerTests;
Expand Down Expand Up @@ -327,7 +327,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2564E6361D88524500A9DC3E /* ActionBarManagerTests.swift in Sources */,
2564E6361D88524500A9DC3E /* ActionBarTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
19 changes: 9 additions & 10 deletions ActionBarManager/ActionBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public enum Direction {

public protocol ActionBarDelegate {

func actionBar(_ actionBar: ActionBar, direction: Direction)
func actionBar(_ actionBar: ActionBar, doneButtonPressed doneButtonItem: UIBarButtonItem)
func directionButtonPressed(actionBar: ActionBar, direction: Direction)
func doneButtonPressed(actionBar: ActionBar, barButtonItem: UIBarButtonItem)
}

open class ActionBar: UIToolbar {
Expand All @@ -26,13 +26,12 @@ open class ActionBar: UIToolbar {
public var actionBarDelegate: ActionBarDelegate!

public init(delegate: ActionBarDelegate) {

super.init(frame: CGRect.zero)

sizeToFit()

actionBarDelegate = delegate

setup()
}

Expand All @@ -54,15 +53,15 @@ open class ActionBar: UIToolbar {
items = [previousButtonItem, spacer, nextButtonItem, flexible, doneButton]
}

@objc func handleActionBarDone(_ item: UIBarButtonItem) {
actionBarDelegate.actionBar(self, doneButtonPressed: item)
@objc func handleActionBarDone(item: UIBarButtonItem) {
actionBarDelegate.doneButtonPressed(actionBar: self, barButtonItem: item)
}

@objc func previousHandler(_ sender: UIBarButtonItem) {
actionBarDelegate.actionBar(self, direction: .previous)
@objc func previousHandler(sender: UIBarButtonItem) {
actionBarDelegate.directionButtonPressed(actionBar: self, direction: .previous)
}

@objc func nextHandler(_ sender: UIBarButtonItem) {
actionBarDelegate.actionBar(self, direction: .next)
@objc func nextHandler(sender: UIBarButtonItem) {
actionBarDelegate.directionButtonPressed(actionBar: self, direction: .next)
}
}
19 changes: 15 additions & 4 deletions ActionBarManager/ActionBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,34 @@
import Foundation
import TableViewKit

public typealias DirectionButtonPressedCallback = (IndexPath, Direction) -> ()
public typealias DoneButtonPressedCallback = () -> ()

open class ActionBarManager: ActionBarDelegate {

let manager: TableViewManager

public var onDirectionButtonPressed: DirectionButtonPressedCallback?
public var onDoneButtonPressedCallback: DoneButtonPressedCallback?

public init(manager: TableViewManager) {
self.manager = manager
}

public func actionBar(_ actionBar: ActionBar, direction: Direction) {
guard let indexPath = indexPathForResponder(forDirection: direction) else { return }
public func doneButtonPressed(actionBar: ActionBar, barButtonItem: UIBarButtonItem) {
onDoneButtonPressedCallback?()
}

public func directionButtonPressed(actionBar: ActionBar, direction: Direction) {

guard let indexPath = indexPathForResponder(forDirection: direction) as IndexPath? else { return }

manager.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
manager.tableView.cellForRow(at: indexPath)?.becomeFirstResponder()

onDirectionButtonPressed?(indexPath, direction)
}

public func actionBar(_ actionBar: ActionBar, doneButtonPressed doneButtonItem: UIBarButtonItem) { }

fileprivate func indexPathForResponder(forDirection direction: Direction) -> IndexPath? {

func isFirstResponder(item: Item) -> Bool {
Expand Down
36 changes: 0 additions & 36 deletions ActionBarManagerTests/ActionBarManagerTests.swift

This file was deleted.

78 changes: 78 additions & 0 deletions ActionBarManagerTests/ActionBarTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// ActionBarManagerTests.swift
// ActionBarManagerTests
//
// Created by Nelson Dominguez Leon on 13/09/16.
// Copyright © 2016 ODIGEO. All rights reserved.
//

import XCTest
@testable import ActionBarManager

class ActionBarObserver: ActionBarDelegate {

var doneButtonPressed: Bool?
var directionButtonPressed: Direction?

func doneButtonPressed(actionBar: ActionBar, barButtonItem: UIBarButtonItem) {
doneButtonPressed = true
}

func directionButtonPressed(actionBar: ActionBar, direction: Direction) {
directionButtonPressed = direction
}
}

class ActionBarTests: XCTestCase {

var delegate: ActionBarObserver!
var actionbar: ActionBar!

override func setUp() {
super.setUp()

delegate = ActionBarObserver()
actionbar = ActionBar(delegate: delegate)
}

override func tearDown() {

delegate = nil
actionbar = nil

super.tearDown()
}

func testActionBarPrevious() {

let previous = actionbar.items?.first
XCTAssertNotNil(previous)

actionbar.previousHandler(sender: previous!)

XCTAssertNotNil(delegate.directionButtonPressed)
XCTAssert(delegate.directionButtonPressed! == .previous)
}

func testActionBarNext() {

let next = actionbar.items?[2]
XCTAssertNotNil(next)

actionbar.nextHandler(sender: next!)

XCTAssertNotNil(delegate.directionButtonPressed)
XCTAssert(delegate.directionButtonPressed! == .next)
}

func testActionBarDone() {

let done = actionbar.items?.last
XCTAssertNotNil(done)

actionbar.handleActionBarDone(item: done!)

XCTAssertNotNil(delegate.doneButtonPressed)
XCTAssert(delegate.doneButtonPressed! == true)
}
}

0 comments on commit 4fcc891

Please sign in to comment.