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 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
75 changes: 64 additions & 11 deletions NVActivityIndicatorView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
attributes = {
LastSwiftMigration = 9999;
LastSwiftUpdateCheck = 1130;
LastUpgradeCheck = 9999;
LastUpgradeCheck = 1240;
TargetAttributes = {
1F1836DC248CEFC200583430 = {
CreatedOnToolsVersion = 11.3.1;
Expand Down Expand Up @@ -857,7 +857,7 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = NVActivityIndicatorView.xcodeproj/NVActivityIndicatorViewExtended_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.1.0;
Expand All @@ -872,7 +872,7 @@
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = NVActivityIndicatorViewExtended;
TVOS_DEPLOYMENT_TARGET = 9.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Release;
Expand Down Expand Up @@ -901,18 +901,45 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_NS_ASSERTIONS = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
"SWIFT_PACKAGE=1",
"DEBUG=1",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "$(inherited) -DXcode";
Expand All @@ -930,16 +957,42 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = s;
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
"SWIFT_PACKAGE=1",
);
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MACOSX_DEPLOYMENT_TARGET = 10.10;
OTHER_SWIFT_FLAGS = "$(inherited) -DXcode";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -963,7 +1016,7 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = NVActivityIndicatorView.xcodeproj/NVActivityIndicatorView_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.1.0;
Expand All @@ -978,7 +1031,7 @@
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = NVActivityIndicatorView;
TVOS_DEPLOYMENT_TARGET = 9.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Debug;
Expand All @@ -994,7 +1047,7 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = NVActivityIndicatorView.xcodeproj/NVActivityIndicatorView_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.1.0;
Expand All @@ -1009,7 +1062,7 @@
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = NVActivityIndicatorView;
TVOS_DEPLOYMENT_TARGET = 9.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Release;
Expand All @@ -1025,7 +1078,7 @@
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = NVActivityIndicatorView.xcodeproj/NVActivityIndicatorViewExtended_Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) $(TOOLCHAIN_DIR)/usr/lib/swift/macosx";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.1.0;
Expand All @@ -1040,7 +1093,7 @@
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
TARGET_NAME = NVActivityIndicatorViewExtended;
TVOS_DEPLOYMENT_TARGET = 9.0;
TVOS_DEPLOYMENT_TARGET = 12.0;
WATCHOS_DEPLOYMENT_TARGET = 2.0;
};
name = Debug;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1130"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "9999"
LastUpgradeVersion = "1240"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
63 changes: 34 additions & 29 deletions Sources/Extended/NVActivityIndicatorPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import UIKit
import NVActivityIndicatorView

/// Class packages information used to display UI blocker.
@available(*, deprecated, message: "")
//@available(*, deprecated, message: "")
public final class ActivityData {
/// Size of activity indicator view.
let size: CGSize
Expand Down Expand Up @@ -178,7 +178,7 @@ private struct NVActivityIndicatorPresenterStateStopped: NVActivityIndicatorPres
}

/// Presenter that displays NVActivityIndicatorView as UI blocker.
@available(*, deprecated, message: "")
//@available(*, deprecated, message: "")
public final class NVActivityIndicatorPresenter {
fileprivate enum State: NVActivityIndicatorPresenterState {
case waitingToStart
Expand Down 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
31 changes: 20 additions & 11 deletions Sources/Extended/NVActivityIndicatorViewable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
// SOFTWARE.
//

#if canImport(UIKit)
import UIKit
import NVActivityIndicatorView

Expand All @@ -34,14 +33,14 @@ import NVActivityIndicatorView
*
* 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 +57,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 +80,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