Handle.swift 3.94 KB
Newer Older
Aral Balkan's avatar
Aral Balkan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//////////////////////////////////////////////////////////////////////////////////////////
//
//  Handle
//
//  Provide a simple, human interface for handling notifications.
//
//  Copyright © 2015 Aral Balkan.
//  Released with ♥ by Ind.ie under the MIT License.
//
//////////////////////////////////////////////////////////////////////////////////////////

import Foundation

let 📡:NSNotificationCenter = NSNotificationCenter.defaultCenter()

public typealias NotificationBlock = (NSNotification!) -> Void


19
20
21
// Utility method: destroy a notification handler
public func destroyNotificationHandler(inout handler:NotificationHandler?)
{
22
    handler?.remove()
23
24
25
26
    handler = nil
}


Aral Balkan's avatar
Aral Balkan committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// MARK: - Class
//


public class NotificationHandler:NSObject
{
    var handler:NotificationBlock
    
    init(handler:NotificationBlock)
    {
        self.handler = handler
    }
    
    public func handlerProxy(notification:NSNotification!)
    {
        handler(notification)
    }
    
46
47
48
    // Note: This is no longer public. Please use the global destroyNotificationHandler() function
    // to properly dispose of notification handlers in your apps.
    internal func remove()
Aral Balkan's avatar
Aral Balkan committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    {
        📡.removeObserver(self)
    }
}


////////////////////////////////////////////////////////////////////////////////
//
// MARK: - Closure-based API (preferred)
//
////////////////////////////////////////////////////////////////////////////////


//
// MARK: - Handling notifications
//
// (When you no longer need the returned notification handler, call its
// remove method. For example, you can define the notification handler
// in a view controller’s viewWillAppear() method and remove it in its
// viewWillDisappear() method.
//


public func handle(notificationName:String, with block:NotificationBlock) -> NotificationHandler
{
    //
    // Instead of using the block-based methods of NSNotificationCenter, create an observer
    // object and return that for easy removing of the observer, etc.
    //
    let observer = NotificationHandler(handler: block)
    📡.addObserver(observer, selector: "handlerProxy:", name: notificationName, object: nil)
    return observer
}


public func handle(notificationName:String, from object:AnyObject?, with block:NotificationBlock) -> NotificationHandler
{
    let observer = NotificationHandler(handler: block)
    📡.addObserver(observer, selector: "handlerProxy:", name: notificationName, object: object)
    return observer
}


//
// MARK: - Posting notifications
//


public func post(notificationName:String, from object:AnyObject?, with userInfo:[NSObject : AnyObject]?)
{
    // println("About to post: \(notificationName) notification from \(object) with userInfo: \(userInfo)")
    📡.post(notificationName, from: object, with: userInfo)
}


public func post(notificationName:String, from object:AnyObject?)
{
    📡.post(notificationName, from: object, with: /* userInfo as */ nil)
}


public func post(notificationName:String)
{
    📡.post(notificationName)
}


////////////////////////////////////////////////////////////////////////////////
//
// MARK: - Selector-based API
//
////////////////////////////////////////////////////////////////////////////////


//
// MARK: - Handling notifications
//

public func handle(notificationName:String, with selector:Selector, on observer:AnyObject)
{
    📡.addObserver(observer, selector: selector, name: notificationName, object: nil)
}


public func handle(notificationName:String, from object:AnyObject?, with selector:Selector, on observer:AnyObject)
{
    📡.addObserver(observer, selector: selector, name: notificationName, object: object)
}


//
140
// MARK: - Removing notification handlers.
Aral Balkan's avatar
Aral Balkan committed
141
142
//

143
public func remove(observer observer:AnyObject, forNotificationNamed name:String?)
Aral Balkan's avatar
Aral Balkan committed
144
145
146
147
148
{
    📡.removeObserver(observer, name: name, object: nil)
}


149
public func remove(observer observer:AnyObject, forNotificationNamed name:String?, from object:AnyObject?)
Aral Balkan's avatar
Aral Balkan committed
150
151
152
{
    📡.removeObserver(observer, name: name, object: object)
}