Skip to content

Commit

Permalink
Merge pull request #26 from sansha/master
Browse files Browse the repository at this point in the history
Add possibility to react to notification tap via new delegate
  • Loading branch information
dkcas11 committed Sep 21, 2019
2 parents f95ce76 + 1014602 commit f562c1d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 29 deletions.
13 changes: 11 additions & 2 deletions CRNotifications/Classes/CRNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CRNotificationView: UIView, CRNotification {
}()

private var completion: () -> () = {}

public var onClickDelegate: CRNotificationDelegate?

// MARK: - Init

Expand Down Expand Up @@ -101,7 +101,7 @@ public class CRNotificationView: UIView, CRNotification {

private func setupTargets() {
NotificationCenter.default.addObserver(self, selector: #selector(didRotate), name: UIDevice.orientationDidChangeNotification, object: nil)
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissNotification))
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissNotificationOnTap))
let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.dismissNotification))
swipeRecognizer.direction = .up

Expand Down Expand Up @@ -164,6 +164,15 @@ public class CRNotificationView: UIView, CRNotification {
})
}

@objc internal func dismissNotificationOnTap() {
guard let onClickDelegate = onClickDelegate else {
self.dismissNotification()
return
}
onClickDelegate.onNotificationTap()
self.dismissNotification()
}

/** Animates out the notification **/
@objc internal func dismissNotification() {
hide()
Expand Down
11 changes: 8 additions & 3 deletions CRNotifications/Classes/CRNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

import UIKit

public protocol CRNotificationDelegate {
func onNotificationTap()
}

public class CRNotifications {

// MARK: - Static notification types
Expand All @@ -28,16 +32,16 @@ public class CRNotifications {
Returns the CRNotification that is displayed. Returns nil if the keyWindow is not present.
**/
@discardableResult
public static func showNotification(textColor: UIColor, backgroundColor: UIColor, image: UIImage?, title: String, message: String, dismissDelay: TimeInterval, completion: @escaping () -> () = {}) -> CRNotification? {
public static func showNotification(textColor: UIColor, backgroundColor: UIColor, image: UIImage?, title: String, message: String, dismissDelay: TimeInterval, delegate: CRNotificationDelegate? = nil, completion: @escaping () -> () = {}) -> CRNotification? {
let notificationDefinition = CRNotificationTypeDefinition(textColor: textColor, backgroundColor: backgroundColor, image: image)
return showNotification(type: notificationDefinition, title: title, message: message, dismissDelay: dismissDelay, completion: completion)
return showNotification(type: notificationDefinition, title: title, message: message, dismissDelay: dismissDelay, delegate: delegate, completion: completion)
}

/** Shows a CRNotification from a CRNotificationType.
Returns the CRNotification that is displayed. Returns nil if the keyWindow is not present.
**/
@discardableResult
public static func showNotification(type: CRNotificationType, title: String, message: String, dismissDelay: TimeInterval, completion: @escaping () -> () = {}) -> CRNotification? {
public static func showNotification(type: CRNotificationType, title: String, message: String, dismissDelay: TimeInterval, delegate: CRNotificationDelegate? = nil, completion: @escaping () -> () = {}) -> CRNotification? {
let view = CRNotificationView()

view.setBackgroundColor(color: type.backgroundColor)
Expand All @@ -47,6 +51,7 @@ public class CRNotifications {
view.setMessage(message: message)
view.setDismisTimer(delay: dismissDelay)
view.setCompletionBlock(completion)
view.onClickDelegate = delegate

guard let window = UIApplication.shared.keyWindow else {
print("Failed to show CRNotification. No keywindow available.")
Expand Down
36 changes: 24 additions & 12 deletions CRNotificationsExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="cgU-iY-bff">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="cgU-iY-bff">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -21,8 +21,8 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="i48-R5-0AR">
<rect key="frame" x="166" y="266.5" width="44" height="34"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="i48-R5-0AR">
<rect key="frame" x="165" y="201" width="44" height="34"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
<state key="normal" title="Error">
<color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -31,8 +31,8 @@
<action selector="showError:" destination="BYZ-38-t0r" eventType="touchUpInside" id="qR3-WY-8rT"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VH0-uo-A2x">
<rect key="frame" x="155.5" y="207.5" width="64" height="34"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="VH0-uo-A2x">
<rect key="frame" x="155" y="142" width="64" height="34"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
<state key="normal" title="Succes">
<color key="titleColor" red="0.0" green="1" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -41,8 +41,8 @@
<action selector="showSuccess:" destination="BYZ-38-t0r" eventType="touchUpInside" id="b5q-2Q-u3E"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="d4B-VL-ogg">
<rect key="frame" x="171" y="325.5" width="34" height="34"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="d4B-VL-ogg">
<rect key="frame" x="170" y="260" width="34" height="34"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
<state key="normal" title="Info">
<color key="titleColor" red="0.29803922770000002" green="0.29803922770000002" blue="0.29803922770000002" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -52,14 +52,24 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="81M-2e-SRi">
<rect key="frame" x="119" y="493.5" width="137" height="30"/>
<rect key="frame" x="119" y="587" width="137" height="30"/>
<state key="normal" title="Next ViewController"/>
<connections>
<action selector="nextViewControllerButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="cmw-Tl-541"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ehu-8b-b9O">
<rect key="frame" x="153.5" y="384.5" width="68" height="34"/>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="hhs-yf-cKk">
<rect key="frame" x="161" y="378.5" width="52" height="34"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
<state key="normal" title="onTap">
<color key="titleColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</state>
<connections>
<action selector="showWithOnTapFeature:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Wmy-w2-2Kh"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ehu-8b-b9O">
<rect key="frame" x="153" y="319" width="68" height="34"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="18"/>
<state key="normal" title="Custom">
<color key="titleColor" red="0.66799789669999998" green="0.47512125970000002" blue="0.25860109930000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -73,10 +83,12 @@
<constraints>
<constraint firstItem="i48-R5-0AR" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" constant="-50" id="5go-4j-WZe"/>
<constraint firstItem="d4B-VL-ogg" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="96u-dy-my2"/>
<constraint firstItem="81M-2e-SRi" firstAttribute="top" secondItem="ehu-8b-b9O" secondAttribute="bottom" constant="75" id="BTd-Gy-ZOQ"/>
<constraint firstItem="ehu-8b-b9O" firstAttribute="top" secondItem="d4B-VL-ogg" secondAttribute="bottom" constant="25" id="Dra-t6-fOc"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="81M-2e-SRi" secondAttribute="bottom" constant="50" id="NT0-vX-gIX"/>
<constraint firstItem="VH0-uo-A2x" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="R76-q6-Zkw"/>
<constraint firstItem="i48-R5-0AR" firstAttribute="top" secondItem="VH0-uo-A2x" secondAttribute="bottom" constant="25" id="VhZ-Dk-Z1I"/>
<constraint firstItem="hhs-yf-cKk" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="WTO-s4-s2n"/>
<constraint firstItem="hhs-yf-cKk" firstAttribute="top" secondItem="ehu-8b-b9O" secondAttribute="bottom" constant="25" id="YHL-Oh-pPM"/>
<constraint firstItem="i48-R5-0AR" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="h90-dx-Fsh"/>
<constraint firstItem="d4B-VL-ogg" firstAttribute="top" secondItem="i48-R5-0AR" secondAttribute="bottom" constant="25" id="rB0-1T-9GM"/>
<constraint firstItem="81M-2e-SRi" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="wM7-Sy-LIk"/>
Expand Down
13 changes: 12 additions & 1 deletion CRNotificationsExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class ViewController: UIViewController {
CRNotifications.showNotification(type: customNotification, title: "Did you know?", message: "This notification is very long and resizes itself in order to fit the size of the view. These last words exceed the limit.", dismissDelay: 3)
}

@IBAction func nextViewControllerButton(_ sender: Any) {
@IBAction func showWithOnTapFeature(_ sender: Any) {
CRNotifications.showNotification(type: CRNotifications.info, title: "Did you know?", message: "Tap on this notification to show another notification", dismissDelay: 3, delegate: self)
}

@IBAction func nextViewControllerButton(_ sender: Any) {
navigationController?.pushViewController(PushViewController(), animated: true)
}
}
Expand All @@ -42,3 +46,10 @@ fileprivate struct CustomCRNotification: CRNotificationType {
var image: UIImage?
}

extension ViewController: CRNotificationDelegate {
func onNotificationTap(type: CRNotificationType, title: String?, message: String?) {
CRNotifications.showNotification(type: CRNotifications.success, title: "Success!", message: "You successfully tapped on the previous notification. Title was \(title ?? "no title")", dismissDelay: 3, completion: {
print("Successfully executed this print when the notification disappeared.")
})
}
}
20 changes: 16 additions & 4 deletions Pods/CRNotifications/CRNotifications/Classes/CRNotification.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f562c1d

Please sign in to comment.