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

Feature/different origins #4

Merged
merged 6 commits into from
Oct 5, 2015
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
4 changes: 4 additions & 0 deletions Demo/WhisperDemo/WhisperDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2977865E1BC30BEF0050D397 /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2977865D1BC30BEF0050D397 /* DetailViewController.swift */; settings = {ASSET_TAGS = (); }; };
29AD429E1BBC2BD2004292F1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29AD429D1BBC2BD2004292F1 /* AppDelegate.swift */; };
29AD42A01BBC2BD2004292F1 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29AD429F1BBC2BD2004292F1 /* ViewController.swift */; };
29AD42A51BBC2BD2004292F1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 29AD42A41BBC2BD2004292F1 /* Assets.xcassets */; };
Expand All @@ -15,6 +16,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2977865D1BC30BEF0050D397 /* DetailViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = "<group>"; };
29AD429A1BBC2BD2004292F1 /* WhisperDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WhisperDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
29AD429D1BBC2BD2004292F1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
29AD429F1BBC2BD2004292F1 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -69,6 +71,7 @@
children = (
29AD429D1BBC2BD2004292F1 /* AppDelegate.swift */,
29AD429F1BBC2BD2004292F1 /* ViewController.swift */,
2977865D1BC30BEF0050D397 /* DetailViewController.swift */,
29AD42A41BBC2BD2004292F1 /* Assets.xcassets */,
29AD42A61BBC2BD2004292F1 /* LaunchScreen.storyboard */,
29AD42A91BBC2BD2004292F1 /* Info.plist */,
Expand Down Expand Up @@ -205,6 +208,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2977865E1BC30BEF0050D397 /* DetailViewController.swift in Sources */,
29AD42A01BBC2BD2004292F1 /* ViewController.swift in Sources */,
29AD429E1BBC2BD2004292F1 /* AppDelegate.swift in Sources */,
);
Expand Down
Binary file not shown.
31 changes: 31 additions & 0 deletions Demo/WhisperDemo/WhisperDemo/DetailViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import UIKit

class DetailViewController: UIViewController {

lazy var containerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.grayColor()

return view
}()

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor(red:0.86, green:0.86, blue:0.86, alpha:1)
title = "Whisper detail".uppercaseString

guard let navigationController = navigationController else { return }

navigationController.navigationBar.addSubview(containerView)
containerView.frame = CGRect(x: 0,
y: navigationController.navigationBar.frame.maxY - UIApplication.sharedApplication().statusBarFrame.height,
width: UIScreen.mainScreen().bounds.width, height: 100)
}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)

containerView.removeFromSuperview()
}
}
106 changes: 81 additions & 25 deletions Demo/WhisperDemo/WhisperDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,75 @@ class ViewController: UIViewController {
return label
}()

lazy var presentButton: UIButton = {
lazy var presentButton: UIButton = { [unowned self] in
let button = UIButton()
button.addTarget(self, action: "presentButtonDidPress:", forControlEvents: .TouchUpInside)
button.setTitle("Present", forState: .Normal)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.layer.borderColor = UIColor.blackColor().CGColor
button.layer.borderWidth = 1.5
button.layer.cornerRadius = 7.5
button.setTitle("Present and silent", forState: .Normal)

return button
}()

var shouldChange = false
lazy var showButton: UIButton = { [unowned self] in
let button = UIButton()
button.addTarget(self, action: "showButtonDidPress:", forControlEvents: .TouchUpInside)
button.setTitle("Show", forState: .Normal)

return button
}()

lazy var presentPermanentButton: UIButton = { [unowned self] in
let button = UIButton()
button.addTarget(self, action: "presentPermanentButtonDidPress:", forControlEvents: .TouchUpInside)
button.setTitle("Present permanent Whisper", forState: .Normal)

return button
}()

lazy var newControllerButton: UIBarButtonItem = { [unowned self] in
let button = UIBarButtonItem()
button.title = "Next"
button.target = self
button.action = "nextButtonDidPress"

return button
}()

lazy var containerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.grayColor()

return view
}()

override func viewDidLoad() {
super.viewDidLoad()

title = "Whisper".uppercaseString
view.backgroundColor = UIColor.whiteColor()

navigationItem.rightBarButtonItem = newControllerButton
navigationItem.backBarButtonItem = UIBarButtonItem(title: nil, style: .Plain, target: nil, action: nil)

view.addSubview(scrollView)
for subview in [icon, titleLabel, presentButton] { scrollView.addSubview(subview) }
for subview in [icon, titleLabel, presentButton, showButton, presentPermanentButton] { scrollView.addSubview(subview) }

for button in [presentButton, showButton, presentPermanentButton] {
button.setTitleColor(UIColor.grayColor(), forState: .Normal)
button.layer.borderColor = UIColor.grayColor().CGColor
button.layer.borderWidth = 1.5
button.layer.cornerRadius = 7.5
}

guard let navigationController = navigationController else { return }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RamonGilabert @zenangst I don't like the early return in the middle of the func, still feel that guard is our next map that we're using in the wrong way. It should be used for error handling, to throw errors, or for early return in the beginning of the functions. What do you guys think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah I agree, it needs to fit into the context for it to make sense. We should be more careful with it because it could end up being a real bitch to debug.

I like it at the start of a method because that helps describe the methods requirements.


navigationController.navigationBar.addSubview(containerView)
containerView.frame = CGRect(x: 0,
y: navigationController.navigationBar.frame.maxY - UIApplication.sharedApplication().statusBarFrame.height,
width: UIScreen.mainScreen().bounds.width, height: 75)
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
title = "Whisper".uppercaseString
}

override func viewDidAppear(animated: Bool) {
Expand All @@ -56,21 +103,30 @@ class ViewController: UIViewController {

func presentButtonDidPress(button: UIButton) {
guard let navigationController = self.navigationController else { return }
let message = Message(title: "Sup", color: UIColor.redColor())
let secondMessage = Message(title: "Changing all the things", color: UIColor.blackColor())
let message = Message(title: "This message will silent in 3 seconds.", color: UIColor(red:0.89, green:0.09, blue:0.44, alpha:1))

Whisper(message, to: navigationController, action: .Present)
Silent(navigationController, after: 3)
}

if shouldChange {
//Silent(navigationController, after: 3)
let controller = ViewController()
navigationController.pushViewController(controller, animated: true)
} else {
Whisper(message, to: navigationController, action: .Present)
}
func showButtonDidPress(button: UIButton) {
guard let navigationController = self.navigationController else { return }

let message = Message(title: "Showing all the things.", color: UIColor.blackColor())
Whisper(message, to: navigationController)
}

func presentPermanentButtonDidPress(button: UIButton) {
guard let navigationController = self.navigationController else { return }

// if shouldChange { Silent(navigationController, after: 3) }
let message = Message(title: "This is a permanent Whisper.", color: UIColor(red:0.87, green:0.34, blue:0.05, alpha:1))
Whisper(message, to: navigationController, action: .Present)
}

shouldChange = !shouldChange
func nextButtonDidPress() {
let controller = DetailViewController()
title = ""
navigationController?.pushViewController(controller, animated: true)
}

// MARK - Configuration
Expand All @@ -80,11 +136,11 @@ class ViewController: UIViewController {
let totalSize = UIScreen.mainScreen().bounds
let originY = navigationHeight + UIApplication.sharedApplication().statusBarFrame.height

scrollView.frame = CGRect(x: 0, y: originY,
width: totalSize.width, height: totalSize.height - originY)
titleLabel.frame.origin = CGPoint(x: (totalSize.width - titleLabel.frame.width) / 2, y: totalSize.height / 2 - 150)
presentButton.frame = CGRect(x: 50, y: titleLabel.frame.origin.y + titleLabel.frame.size.height + 75,
width: totalSize.width - 100, height: 50)
scrollView.frame = CGRect(x: 0, y: originY, width: totalSize.width, height: totalSize.height - originY)
titleLabel.frame.origin = CGPoint(x: (totalSize.width - titleLabel.frame.width) / 2, y: totalSize.height / 2 - 200)
presentButton.frame = CGRect(x: 50, y: titleLabel.frame.maxY + 75, width: totalSize.width - 100, height: 50)
showButton.frame = CGRect(x: 50, y: presentButton.frame.maxY + 15, width: totalSize.width - 100, height: 50)
presentPermanentButton.frame = CGRect(x: 50, y: showButton.frame.maxY + 15, width: totalSize.width - 100, height: 50)
}
}

51 changes: 47 additions & 4 deletions Source/WhisperFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ class WhisperFactory: NSObject {
if !containsWhisper {
whisperView = WhisperView(height: navigationController.navigationBar.frame.height, message: message)
whisperView.frame.size.height = 0
for subview in whisperView.transformViews { subview.frame.origin.y = -20 }
var maximumY = navigationController.navigationBar.frame.height
- UIApplication.sharedApplication().statusBarFrame.height
for subview in whisperView.transformViews {
subview.frame.origin.y = -10
subview.alpha = 0
}

for subview in navigationController.navigationBar.subviews {
if subview.frame.maxY > maximumY { maximumY = subview.frame.maxY }
}

whisperView.frame.origin.y = maximumY
navigationController.navigationBar.addSubview(whisperView)
}

Expand Down Expand Up @@ -86,7 +97,10 @@ class WhisperFactory: NSObject {

UIView.animateWithDuration(AnimationTiming.movement, animations: {
self.whisperView.frame.size.height = WhisperView.Dimensions.height
for subview in self.whisperView.transformViews { subview.frame.origin.y = 0 }
for subview in self.whisperView.transformViews {
subview.frame.origin.y = 0
subview.alpha = 1
}
})
}

Expand All @@ -95,7 +109,10 @@ class WhisperFactory: NSObject {

UIView.animateWithDuration(AnimationTiming.movement, animations: {
self.whisperView.frame.size.height = WhisperView.Dimensions.height
for subview in self.whisperView.transformViews { subview.frame.origin.y = 0 }
for subview in self.whisperView.transformViews {
subview.frame.origin.y = 0
subview.alpha = 1
}
}, completion: { _ in
self.delayTimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self,
selector: "delayFired:", userInfo: nil, repeats: false)
Expand Down Expand Up @@ -123,7 +140,10 @@ class WhisperFactory: NSObject {

UIView.animateWithDuration(AnimationTiming.movement, animations: {
self.whisperView.frame.size.height = 0
for subview in self.whisperView.transformViews { subview.frame.origin.y = -20 }
for subview in self.whisperView.transformViews {
subview.frame.origin.y = -10
subview.alpha = 0
}
}, completion: { _ in
self.whisperView.removeFromSuperview()
})
Expand Down Expand Up @@ -152,6 +172,15 @@ class WhisperFactory: NSObject {
navigationController.navigationBar.addSubview(whisperView)
whisperView.frame.size.height = 0

var maximumY = navigationController.navigationBar.frame.height
- UIApplication.sharedApplication().statusBarFrame.height

for subview in navigationController.navigationBar.subviews {
if subview.frame.maxY > maximumY { maximumY = subview.frame.maxY }
}

whisperView.frame.origin.y = maximumY

action == .Present ? presentView() : showView()
}

Expand Down Expand Up @@ -187,6 +216,20 @@ class WhisperFactory: NSObject {

extension WhisperFactory: UINavigationControllerDelegate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zenangst @RamonGilabert With the name like WhisperFactory I'm expecting to see creation of objects, but it's not the purpose of this class. Might be only me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factory |ˈfakt(ə)rē|
noun (pl. factories)
1 a building or group of buildings where goods are manufactured or assembled chiefly by machine.

We manufacture Whispers, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about Factory pattern.


func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
var maximumY = navigationController.navigationBar.frame.maxY - UIApplication.sharedApplication().statusBarFrame.height

for subview in navigationController.navigationBar.subviews {
if subview is WhisperView { navigationController.navigationBar.bringSubviewToFront(subview) }

if subview.frame.maxY > maximumY && !(subview is WhisperView) {
maximumY = subview.frame.maxY
}
}

whisperView.frame.origin.y = maximumY
}

func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {

for subview in navigationController.navigationBar.subviews where subview is WhisperView {
Expand Down