Skip to content

Commit

Permalink
Add button view delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenyneu committed Jun 9, 2015
1 parent 33b474d commit 0d6802e
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Dodo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
7E49464F1B2666DF00915DB3 /* DodoIcons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E49464D1B26664B00915DB3 /* DodoIcons.swift */; };
7E4946501B26705F00915DB3 /* Icons.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E49464B1B2665FF00915DB3 /* Icons.xcassets */; };
7E4946521B267E6B00915DB3 /* happy.png in Resources */ = {isa = PBXBuildFile; fileRef = 7E4946511B267E6B00915DB3 /* happy.png */; };
7E4946541B26942000915DB3 /* DodoButtonViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E4946531B26942000915DB3 /* DodoButtonViewDelegate.swift */; };
7E4946551B26942C00915DB3 /* DodoButtonViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E4946531B26942000915DB3 /* DodoButtonViewDelegate.swift */; };
7E4946581B26955600915DB3 /* ButtonViewDelegateMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E4946571B26955600915DB3 /* ButtonViewDelegateMock.swift */; };
7EC51DA41B215C070070019F /* DodoAnimationsShow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EC51DA31B215C070070019F /* DodoAnimationsShow.swift */; };
7EC51DA61B215C9D0070019F /* DodoAnimationsHide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EC51DA51B215C9D0070019F /* DodoAnimationsHide.swift */; };
7EC51DA71B215D450070019F /* DodoAnimationsShow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EC51DA31B215C070070019F /* DodoAnimationsShow.swift */; };
Expand Down Expand Up @@ -135,6 +138,8 @@
7E49464B1B2665FF00915DB3 /* Icons.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Icons.xcassets; sourceTree = "<group>"; };
7E49464D1B26664B00915DB3 /* DodoIcons.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DodoIcons.swift; sourceTree = "<group>"; };
7E4946511B267E6B00915DB3 /* happy.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = happy.png; sourceTree = "<group>"; };
7E4946531B26942000915DB3 /* DodoButtonViewDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DodoButtonViewDelegate.swift; sourceTree = "<group>"; };
7E4946571B26955600915DB3 /* ButtonViewDelegateMock.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ButtonViewDelegateMock.swift; sourceTree = "<group>"; };
7EC51DA31B215C070070019F /* DodoAnimationsShow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DodoAnimationsShow.swift; sourceTree = "<group>"; };
7EC51DA51B215C9D0070019F /* DodoAnimationsHide.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DodoAnimationsHide.swift; sourceTree = "<group>"; };
7EDF785F1B2105F300A9597E /* Dodo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Dodo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -235,6 +240,14 @@
path = Icons;
sourceTree = "<group>";
};
7E4946561B26954500915DB3 /* Mocks */ = {
isa = PBXGroup;
children = (
7E4946571B26955600915DB3 /* ButtonViewDelegateMock.swift */,
);
path = Mocks;
sourceTree = "<group>";
};
7EDF78551B2105F300A9597E = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -290,6 +303,7 @@
7EDF78B21B2106CD00A9597E /* Buttons */,
7EDF78B41B2106CD00A9597E /* Helpers */,
7EDF78B61B2106CD00A9597E /* Image */,
7E4946561B26954500915DB3 /* Mocks */,
7EDF78BD1B2106CD00A9597E /* Style */,
7EDF78C11B2106CD00A9597E /* TestHelpers */,
7EDF78C71B2106CD00A9597E /* Utils */,
Expand Down Expand Up @@ -325,6 +339,7 @@
children = (
7EDF78811B21068800A9597E /* DodoButtonOnTap.swift */,
7EDF78821B21068800A9597E /* DodoButtonView.swift */,
7E4946531B26942000915DB3 /* DodoButtonViewDelegate.swift */,
);
path = Buttons;
sourceTree = "<group>";
Expand Down Expand Up @@ -659,6 +674,7 @@
7EDF78A61B21068800A9597E /* DodoButtonStyle.swift in Sources */,
7EDF78B01B21068800A9597E /* TegAutolayoutConstraints.swift in Sources */,
7EDF78A31B21068800A9597E /* DodoBarDefaultStyles.swift in Sources */,
7E4946541B26942000915DB3 /* DodoButtonViewDelegate.swift in Sources */,
7EDF78B11B21068800A9597E /* DodoColor.swift in Sources */,
7EDF78A91B21068800A9597E /* DodoPresets.swift in Sources */,
7EC51DA41B215C070070019F /* DodoAnimationsShow.swift in Sources */,
Expand All @@ -682,6 +698,8 @@
7EDF78EE1B21072900A9597E /* DodoToolbar.swift in Sources */,
7EDF78D61B2106CD00A9597E /* XCTestCase+Helpers.swift in Sources */,
7EDF78D21B2106CD00A9597E /* DodoTests.swift in Sources */,
7E4946581B26955600915DB3 /* ButtonViewDelegateMock.swift in Sources */,
7E4946551B26942C00915DB3 /* DodoButtonViewDelegate.swift in Sources */,
7EDF78E21B21071500A9597E /* DodoButtonDefaultStyles.swift in Sources */,
7E49464F1B2666DF00915DB3 /* DodoIcons.swift in Sources */,
7EDF78E91B21072200A9597E /* OnTap.swift in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Dodo/Buttons/DodoButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit

class DodoButtonView: UIImageView {
private let style: DodoButtonStyle
weak var delegate: DodoButtonViewDelegate?
var onTap: OnTap?

init(style: DodoButtonStyle) {
Expand All @@ -17,6 +18,7 @@ class DodoButtonView: UIImageView {

// Create button views for given button styles.
static func createMany(styles: [DodoButtonStyle]) -> [DodoButtonView] {

if !haveButtons(styles) { return [] }

return styles.map { style in
Expand Down
3 changes: 3 additions & 0 deletions Dodo/Buttons/DodoButtonViewDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public protocol DodoButtonViewDelegate: class {
func buttonDelegateDidTap(buttonStyle: DodoButtonStyle)
}
9 changes: 8 additions & 1 deletion Dodo/Dodo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

final public class Dodo {
final public class Dodo: DodoButtonViewDelegate {
private weak var superview: UIView!
private var hideTimer: MoaTimer?

Expand Down Expand Up @@ -91,6 +91,7 @@ final public class Dodo {
var bar = DodoToolbar(witStyle: style)
setupHideOnTap(bar)
bar.layoutGuide = style.bar.locationTop ? topLayoutGuide : bottomLayoutGuide
bar.buttonViewDelegate = self
bar.show(inSuperview: superview, withMessage: message)
}

Expand Down Expand Up @@ -138,4 +139,10 @@ final public class Dodo {
}
}
}

// MARK: - DodoButtonViewDelegate

public func buttonDelegateDidTap(buttonStyle: DodoButtonStyle) {

}
}
4 changes: 4 additions & 0 deletions Dodo/DodoToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import UIKit
class DodoToolbar: UIView {
var layoutGuide: UILayoutSupport?
var style: DodoStyle
weak var buttonViewDelegate: DodoButtonViewDelegate?

convenience init(witStyle style: DodoStyle) {
self.init(frame: CGRect())
Expand Down Expand Up @@ -101,12 +102,15 @@ class DodoToolbar: UIView {
// MARK: - Buttons

private func createButtons() -> [DodoButtonView] {
precondition(buttonViewDelegate != nil, "Button view delegate can not be nil")
let buttonStyles = [style.leftButton, style.rightButton]

let buttonViews = DodoButtonView.createMany(buttonStyles)

for (index, button) in enumerate(buttonViews) {
addSubview(button)
button.doLayout(onLeftSide: index == 0)
button.delegate = buttonViewDelegate

if style.bar.debugMode {
button.backgroundColor = UIColor.yellowColor()
Expand Down
13 changes: 13 additions & 0 deletions Dodo/Style/DodoButtonDefaultStyles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct DodoButtonDefaultStyles {
onTap = _onTap
size = _size
tintColor = _tintColor
hideOnTap = _hideOnTap
}


Expand Down Expand Up @@ -84,4 +85,16 @@ public struct DodoButtonDefaultStyles {

/// Button tint color. When set it is used to replace the image colors.
public static var tintColor = _tintColor


// ---------------------------


private static let _hideOnTap = false

/// When true it hides the bar when the button is tapped
public static var hideOnTap = _hideOnTap


// ---------------------------
}
17 changes: 17 additions & 0 deletions Dodo/Style/DodoButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class DodoButtonStyle {
_size = nil
_horizontalMarginToBar = nil
_tintColor = nil
_hideOnTap = nil
}

// -----------------------------
Expand Down Expand Up @@ -130,4 +131,20 @@ public class DodoButtonStyle {
_tintColor = newValue
}
}


// -----------------------------

private var _hideOnTap: Bool?

/// When true it hides the bar when the button is tapped
public var hideOnTap: Bool {
get {
return _hideOnTap ?? parent?.hideOnTap ?? DodoButtonDefaultStyles.hideOnTap
}

set {
_hideOnTap = newValue
}
}
}
3 changes: 3 additions & 0 deletions DodoTests/Buttons/DodoButtonViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import XCTest

class DodoButtonViewTests: XCTestCase {

var buttonDelegate: ButtonViewDelegateMock!

override func setUp() {
super.setUp()

DodoStyle.resetDefaultStyles()
buttonDelegate = ButtonViewDelegateMock()
}

// MARK: - Create many
Expand Down
3 changes: 3 additions & 0 deletions DodoTests/DodoToolbarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ class DodoToolbarTests: XCTestCase {
var superview: UIView!
var animationShowCompleted = false
var animationHideCompleted = false
var buttonDelegate: ButtonViewDelegateMock!

override func setUp() {
super.setUp()

buttonDelegate = ButtonViewDelegateMock()
DodoPresets.resetAll()
obj = DodoToolbar()
obj.buttonViewDelegate = buttonDelegate
superview = UIView()

// Mock animation
Expand Down
5 changes: 5 additions & 0 deletions DodoTests/Mocks/ButtonViewDelegateMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ButtonViewDelegateMock: DodoButtonViewDelegate {
func buttonDelegateDidTap(buttonStyle: DodoButtonStyle) {

}
}

0 comments on commit 0d6802e

Please sign in to comment.