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

Remove Singleton Only Presenter. Allow multiple presenters. #299

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
59 changes: 32 additions & 27 deletions Sources/Extended/NVActivityIndicatorPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,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

/**
Expand Down Expand Up @@ -263,8 +266,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)

Copy link

Choose a reason for hiding this comment

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

Lines should not have trailing whitespace.

containerView.backgroundColor = activityData.backgroundColor
containerView.restorationIdentifier = restorationIdentifier
containerView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -286,7 +291,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
Expand All @@ -299,41 +304,41 @@ 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)
let trailingConstraint = NSLayoutConstraint(item: presentingView, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: presentingView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: presentingView, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .bottom, multiplier: 1, constant: 0)

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()
}

Copy link

Choose a reason for hiding this comment

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

Lines should not have 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
}

Copy link

Choose a reason for hiding this comment

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

Lines should not have trailing whitespace.


Copy link

Choose a reason for hiding this comment

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

Lines should not have trailing whitespace.

Copy link

Choose a reason for hiding this comment

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

Limit vertical whitespace to a single empty line. Currently 2.

}
}
#endif
32 changes: 20 additions & 12 deletions Sources/Extended/NVActivityIndicatorViewable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,21 @@
// SOFTWARE.
//

#if canImport(UIKit)
import UIKit
import NVActivityIndicatorView

/**
* UIViewController conforms this protocol to be able to display NVActivityIndicatorView as UI blocker.
*
* This extends abilities of UIViewController to display and remove UI blocker.
*/
@available(*, deprecated, message: "")
public protocol NVActivityIndicatorViewable {}
public protocol NVActivityIndicatorViewable {
var activityIndicatorPresenter: NVActivityIndicatorPresenter { get set }
}

@available(*, deprecated, message: "")
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.
Expand All @@ -58,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,
Expand All @@ -81,16 +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) {
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) {
self.activityIndicatorPresenter.stopAnimating(fadeOutAnimation)
}
}
#endif