Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PrajeetShrestha committed Jun 18, 2021
1 parent 8597d1c commit 5d52e78
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions Sources/WittyMessageCenter/WittyMessageCenter.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,100 @@
struct WittyMessageCenter {
var text = "Hello, World!"
//
// MessageCenter.swift
// PSArsenal
//
// Created by Prajeet Shrestha on 01/06/2021.
//

import Foundation
///
///A class to simplify usage of NSNotificationCenter
///
/// **Example:**
///
/// **1. Initialize**
///
/// let messageCenter = MessageCenter()
///
/// **2.0 Send Notification**
///
/// //2.1. Send Notification without data
/// messageCenter.send(name: .homePageFetchComplete)
///
/// //2.2 Send Notification with data
/// messageCenter.send(name: .homePageFetchComplete, ["name": "Prajeet"])
///
/// **3.0 Observe Notification**
///
/// //3.1 Observe without data
/// messageCenter.observe(name: .homePageFetchComplete) {[weak self] _ in
/// self?.lblMessage.text = "Got Notification"
/// }
///
/// //3.2 Observe with data
/// messageCenter.observe(name: .homePageFetchComplete) {[weak self] data in
/// self?.lblMessage.text = "Got Notification"
///
/// **Notes**
///
/// 1. Can cause cyclic reference.
/// 2. There will be a retention cycle, if callback methods capture strong references and you don't call remove() method.
/// 3. You can use weak reference in callback closures to prevent retention cycle.
/// 4. If you use weak references then you don't need to call remove() method explicitly.

public class MessageCenter {
private static let notificationCenter = NotificationCenter()
public var scope:String = ""
public typealias CallBack = (_ data:[AnyHashable:Any]?) -> Void

public init() {}

private var callbackStore = [String : CallBack]()

/// Observe Notification in NSNotificationCenter.default
/// - Parameters:
/// - name: Name of a notification as string
/// - callback: Closure to be executed upon receiving notificaitons
///
///callback will also return a userinfo as data from dictionary

public func observe(name:String, callback:@escaping CallBack) {

MessageCenter.notificationCenter.addObserver (self,
selector: #selector(self.notificationCallback),
name: NSNotification.Name(name),
object: nil)
callbackStore[name] = callback
}


/// Sends notification through NSNotificationCenter.default
/// - Parameters:
/// - name: Name of notification as string
/// - data: Data to be passed as user info in Notification Object
public func send(name:String, data:[String:AnyObject]? = nil) {
MessageCenter.notificationCenter.post(name: NSNotification.Name(name),
object: nil, userInfo:data)
}

/// Method to remove notification observer
public func remove() {
callbackStore.removeAll()
MessageCenter.notificationCenter.removeObserver(self)
}

@objc private func notificationCallback(notification: Notification) {
if let callback = callbackStore[notification.name.rawValue] {
callback(notification.userInfo)
}
}

deinit {
//print("MessageCenter deinit from: \(scope)")
remove()
}
}

public protocol Notifiable {
var messageCenter:MessageCenter { get }
func observeNotifications()
}

0 comments on commit 5d52e78

Please sign in to comment.