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

Add a new max width setting #43

Merged
merged 3 commits into from
Jul 19, 2019
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
17 changes: 12 additions & 5 deletions Example/UINotifications-Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
isa = PBXContainerItemProxy;
containerPortal = 5078C8321F18D9FC006EB23F /* UINotifications.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = 5078C7931F18CAF5006EB23F;
remoteGlobalIDString = 502C139F206E5F230082CE37;
remoteInfo = UINotifications;
};
/* End PBXContainerItemProxy section */
Expand Down Expand Up @@ -146,19 +146,20 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0910;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = WeTransfer;
TargetAttributes = {
50042BCD1F18BC85007209B7 = {
CreatedOnToolsVersion = 8.3.3;
DevelopmentTeam = 4QMDKC8VLJ;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 50042BC91F18BC85007209B7 /* Build configuration list for PBXProject "UINotifications-Example" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down Expand Up @@ -271,6 +272,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
Expand All @@ -281,13 +283,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = 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_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -328,6 +332,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
Expand All @@ -338,13 +343,15 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = 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_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -385,7 +392,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.wetransfer.UINotifications-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -400,7 +407,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.wetransfer.UINotifications-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
11 changes: 8 additions & 3 deletions Example/UINotifications-Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,19 @@ enum NotificationStyle: UINotificationStyle {
}

/// The height of the notification which applies on the notification view.
var height: UINotificationHeight {
var height: UINotification.Height {
switch self {
case .success:
return UINotificationHeight.navigationBar
return .navigationBar
case .failure:
return UINotificationHeight.statusBar
return .statusBar
}
}

/// Use this to set a max width to the notification view.
var maxWidth: CGFloat? {
return nil
}

/// When `true`, the notification is swipeable and tappable.
var interactive: Bool {
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ UINotificationCenter.current.show(notification: notification, dismissTrigger: di
```swift
import UINotifications

enum CustomNotificationStyle: UINotificationStyle {
enum NotificationStyle: UINotificationStyle {
case success
case failure

Expand Down Expand Up @@ -98,14 +98,19 @@ enum CustomNotificationStyle: UINotificationStyle {
}

/// The height of the notification which applies on the notification view.
var height: UINotificationHeight {
var height: UINotification.Height {
switch self {
case .success:
return UINotificationHeight.navigationBar
return .navigationBar
case .failure:
return UINotificationHeight.statusBar
return .statusBar
}
}

/// Use this to set a max width to the notification view.
var maxWidth: CGFloat? {
return nil
}

/// When `true`, the notification is swipeable and tappable.
var interactive: Bool {
Expand Down
3 changes: 3 additions & 0 deletions Sources/UINotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public protocol UINotificationStyle {

/// The height of the notification which applies on the notification view.
var height: UINotification.Height { get }

/// The max width of the notification which applies on the notification view. Defaults to current screen width if set to `nil`.
var maxWidth: CGFloat? { get }

/// When `true`, the notification is swipeable and tappable.
var interactive: Bool { get }
Expand Down
15 changes: 11 additions & 4 deletions Sources/UINotificationPresentationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ public final class UINotificationPresentationContext {

notificationView.topConstraint = notificationViewTopConstraint

let constraints = [
notificationView.leftAnchor.constraint(equalTo: containerViewController.view.leftAnchor),
notificationView.rightAnchor.constraint(equalTo: containerViewController.view.rightAnchor),
var constraints = [
notificationView.leftAnchor.constraint(equalTo: containerViewController.view.leftAnchor).usingPriority(.defaultHigh),
notificationView.rightAnchor.constraint(equalTo: containerViewController.view.rightAnchor).usingPriority(.defaultHigh),
notificationView.heightAnchor.constraint(equalToConstant: notification.style.height.value),
notificationViewTopConstraint
]


if let maxWidth = notification.style.maxWidth {
constraints.append(contentsOf: [
notificationView.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth),
notificationView.centerXAnchor.constraint(equalTo: containerViewController.view.centerXAnchor)
])
}

NSLayoutConstraint.activate(constraints)
containerViewController.view.layoutIfNeeded()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct UINotificationSystemStyle: UINotificationStyle {
public var titleTextColor: UIColor = UIColor.black
public var subtitleTextColor: UIColor = UIColor.darkGray
public var height: UINotification.Height = .navigationBar
public var maxWidth: CGFloat?
public var interactive: Bool = true
public var chevronImage: UIImage?

Expand Down
2 changes: 1 addition & 1 deletion Sources/UINotificationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ extension UINotificationView: UINotificationDelegate {
}
}

fileprivate extension NSLayoutConstraint {
extension NSLayoutConstraint {

/// Returns the constraint sender with the passed priority.
///
Expand Down
26 changes: 21 additions & 5 deletions UINotificationsTests/UINotificationDefaultElementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ final class UINotificationDefaultElementsTests: UINotificationTestCase {
var subtitleTextColor: UIColor = UIColor.black
var backgroundColor: UIColor = UIColor.white
var height: UINotification.Height {
return UINotification.Height.custom(height: self.customHeight)
guard let customHeight = customHeight else { return .navigationBar }
return .custom(height: customHeight)
}
var interactive: Bool = true
var chevronImage: UIImage?

let customHeight: CGFloat

init(customHeight: CGFloat) {
let customHeight: CGFloat?
let maxWidth: CGFloat?

init(customHeight: CGFloat? = nil, maxWidth: CGFloat? = nil) {
self.customHeight = customHeight
self.maxWidth = maxWidth
}
}

Expand Down Expand Up @@ -73,7 +76,20 @@ final class UINotificationDefaultElementsTests: UINotificationTestCase {

waitFor(notificationCenter.currentPresenter?.presentationContext.notificationView.frame.size.height == customHeight, timeout: 5.0, description: "Custom height should be applied to the view")
}


/// When passing a notification style with a max width, this should be applied to the presented view.
func testNotificationViewMaxWidth() {
let notificationCenter = UINotificationCenter()
notificationCenter.isDuplicateQueueingAllowed = true
notificationCenter.presenterType = MockPresenter.self
let customWidth: CGFloat = 100
let notification = UINotification(content: UINotificationContent(title: "test"), style: CustomStyle(maxWidth: customWidth))

notificationCenter.show(notification: notification)

waitFor(notificationCenter.currentPresenter?.presentationContext.notificationView.frame.size.width == customWidth, timeout: 5.0, description: "Max width should be applied to the view")
}

/// When using the manual dismiss trigger, the notification should only dismiss after manually called.
func testManualDismissTrigger() {
let notificationCenter = UINotificationCenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private struct LargeChevronStyle: UINotificationStyle {
var height: UINotification.Height {
return UINotification.Height.navigationBar
}
var maxWidth: CGFloat?
var interactive: Bool = true
var chevronImage: UIImage? {
let bundle = Bundle(for: UINotificationViewTests.self)
Expand Down