-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Remove Singleton Only Presenter. Allow multiple presenters. #299
Changes from 5 commits
0054bc1
7106c33
4d456a9
71ce15f
6fbddce
abd0692
43fa684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,8 +223,11 @@ public final class NVActivityIndicatorPresenter { | |
/// Current status of animation, read-only. | ||
public var isAnimating: Bool { return state == .animating || state == .waitingToStop } | ||
|
||
private init() {} | ||
|
||
public private(set) weak var presentingView: UIView? | ||
public private(set) weak var presentedView: UIView? | ||
public required init(presentingView: UIView? = nil) { | ||
self.presentingView = presentingView | ||
} | ||
// MARK: - Public interface | ||
|
||
/** | ||
|
@@ -259,8 +262,10 @@ public final class NVActivityIndicatorPresenter { | |
// MARK: - Helpers | ||
|
||
fileprivate func show(with activityData: ActivityData, _ fadeInAnimation: FadeInAnimation?) { | ||
let containerView = UIView(frame: UIScreen.main.bounds) | ||
|
||
guard let presentingView = self.presentingView ?? UIApplication.shared.keyWindow else { return } | ||
let containerViewFrame = self.presentingView?.bounds ?? UIScreen.main.bounds | ||
let containerView = UIView(frame: containerViewFrame) | ||
|
||
containerView.backgroundColor = activityData.backgroundColor | ||
containerView.restorationIdentifier = restorationIdentifier | ||
containerView.translatesAutoresizingMaskIntoConstraints = false | ||
|
@@ -282,7 +287,7 @@ public final class NVActivityIndicatorPresenter { | |
let yConstraint = NSLayoutConstraint(item: containerView, attribute: .centerY, relatedBy: .equal, toItem: activityIndicatorView, attribute: .centerY, multiplier: 1, constant: 0) | ||
|
||
containerView.addConstraints([xConstraint, yConstraint]) | ||
}()) | ||
}()) | ||
|
||
messageLabel.font = activityData.messageFont | ||
messageLabel.textColor = activityData.textColor | ||
|
@@ -295,40 +300,40 @@ public final class NVActivityIndicatorPresenter { | |
let trailingConstraint = NSLayoutConstraint(item: containerView, attribute: .trailing, relatedBy: .equal, toItem: messageLabel, attribute: .trailing, multiplier: 1, constant: 8) | ||
|
||
containerView.addConstraints([leadingConstraint, trailingConstraint]) | ||
}()) | ||
}()) | ||
({ | ||
let spacingConstraint = NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: activityIndicatorView, attribute: .bottom, multiplier: 1, constant: activityData.messageSpacing) | ||
|
||
containerView.addConstraint(spacingConstraint) | ||
}()) | ||
}()) | ||
|
||
guard let keyWindow = UIApplication.shared.keyWindow else { return } | ||
|
||
keyWindow.addSubview(containerView) | ||
presentingView.addSubview(containerView) | ||
|
||
// Add constraints for `containerView`. | ||
({ | ||
let leadingConstraint = NSLayoutConstraint(item: keyWindow, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1, constant: 0) | ||
let trailingConstraint = NSLayoutConstraint(item: keyWindow, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1, constant: 0) | ||
let topConstraint = NSLayoutConstraint(item: keyWindow, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 0) | ||
let bottomConstraint = NSLayoutConstraint(item: keyWindow, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .bottom, multiplier: 1, constant: 0) | ||
|
||
keyWindow.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint]) | ||
}()) | ||
let leadingConstraint = NSLayoutConstraint(item: presentingView, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1, constant: 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 188 characters (line_length) |
||
let trailingConstraint = NSLayoutConstraint(item: presentingView, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1, constant: 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 191 characters (line_length) |
||
let topConstraint = NSLayoutConstraint(item: presentingView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 176 characters (line_length) |
||
let bottomConstraint = NSLayoutConstraint(item: presentingView, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .bottom, multiplier: 1, constant: 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 185 characters (line_length) |
||
|
||
presentingView.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint]) | ||
}()) | ||
self.presentedView = containerView | ||
} | ||
|
||
fileprivate func hide(_ fadeOutAnimation: FadeOutAnimation?) { | ||
for window in UIApplication.shared.windows { | ||
for item in window.subviews | ||
where item.restorationIdentifier == restorationIdentifier { | ||
if let fadeOutAnimation = fadeOutAnimation { | ||
fadeOutAnimation(item) { | ||
item.removeFromSuperview() | ||
} | ||
} else { | ||
item.removeFromSuperview() | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
if let fadeOutAnimation = fadeOutAnimation, | ||
let presentedView = self.presentedView { | ||
fadeOutAnimation(presentedView) { | ||
self.presentedView?.removeFromSuperview() | ||
self.presentedView = nil | ||
} | ||
} else { | ||
self.presentedView?.removeFromSuperview() | ||
self.presentedView = nil | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,14 @@ import UIKit | |
* | ||
* This extends abilities of UIViewController to display and remove UI blocker. | ||
*/ | ||
public protocol NVActivityIndicatorViewable {} | ||
public protocol NVActivityIndicatorViewable { | ||
var activityIndicatorPresenter: NVActivityIndicatorPresenter { get set } | ||
} | ||
|
||
public extension NVActivityIndicatorViewable where Self: UIViewController { | ||
public extension NVActivityIndicatorViewable { | ||
|
||
/// Current status of animation, read-only. | ||
var isAnimating: Bool { return NVActivityIndicatorPresenter.sharedInstance.isAnimating } | ||
var isActivityIndicatorAnimating: Bool { return self.activityIndicatorPresenter.isAnimating } | ||
|
||
/** | ||
Display UI blocker. | ||
|
@@ -54,7 +56,7 @@ public extension NVActivityIndicatorViewable where Self: UIViewController { | |
- parameter minimumDisplayTime: minimum display time of UI blocker. | ||
- parameter fadeInAnimation: fade in animation. | ||
*/ | ||
func startAnimating( | ||
func showActivityIndicator( | ||
_ size: CGSize? = nil, | ||
message: String? = nil, | ||
messageFont: UIFont? = nil, | ||
|
@@ -77,15 +79,26 @@ public extension NVActivityIndicatorViewable where Self: UIViewController { | |
backgroundColor: backgroundColor, | ||
textColor: textColor) | ||
|
||
NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData, fadeInAnimation) | ||
self.showActivityIndicator(activityData: activityData, fadeInAnimation: fadeInAnimation) | ||
} | ||
/** | ||
Display UI blocker. | ||
|
||
Appropriate NVActivityIndicatorView.DEFAULT_* values are used for omitted params. | ||
|
||
- parameter size: Information used to display UIBlocker | ||
- parameter fadeInAnimation: fade in animation. | ||
*/ | ||
func showActivityIndicator(activityData: ActivityData, fadeInAnimation: FadeInAnimation? = NVActivityIndicatorView.DEFAULT_FADE_IN_ANIMATION) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 147 characters (line_length) |
||
self.activityIndicatorPresenter.startAnimating(activityData, fadeInAnimation) | ||
} | ||
|
||
/** | ||
Remove UI blocker. | ||
|
||
- parameter fadeOutAnimation: fade out animation. | ||
*/ | ||
func stopAnimating(_ fadeOutAnimation: FadeOutAnimation? = NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION) { | ||
NVActivityIndicatorPresenter.sharedInstance.stopAnimating(fadeOutAnimation) | ||
func hideActivityIndicator(_ fadeOutAnimation: FadeOutAnimation? = NVActivityIndicatorView.DEFAULT_FADE_OUT_ANIMATION) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line Length Violation: Line should be 120 characters or less: currently 124 characters (line_length) |
||
self.activityIndicatorPresenter.stopAnimating(fadeOutAnimation) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)