forked from alxn1/wjoy
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNotificationCenter.m
165 lines (132 loc) · 4.57 KB
/
NotificationCenter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// NotificationCenter.m
// WJoy
//
// Created by alxn1 on 27.07.12.
// Copyright 2012 alxn1. All rights reserved.
//
#import <Wiimote/Wiimote.h>
#import <UpdateChecker/UAppUpdateChecker.h>
#import "NotificationCenter.h"
@interface NotificationCenter (PrivatePart)
- (id)initInternal;
- (void)onDiscoveryBegin;
- (void)onDiscoveryEnd;
- (void)onDeviceConnected;
- (void)onDeviceBatteryStateChanged:(NSNotification*)notification;
- (void)onDeviceDisconnected;
@end
@implementation NotificationCenter
+ (void)start
{
[[NotificationCenter alloc] initInternal];
}
- (BOOL)userNotificationCenter:(UserNotificationCenter*)center
shouldDeliverNotification:(UserNotification*)notification
{
return YES;
}
- (void)userNotificationCenter:(UserNotificationCenter*)center
notificationClicked:(UserNotification*)notification
{
NSString *url = [[notification userInfo] objectForKey:@"URL"];
if(url != nil)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:url]];
}
@end
@implementation NotificationCenter (PrivatePart)
- (id)init
{
[[super init] release];
return nil;
}
- (id)initInternal
{
self = [super init];
if(self == nil)
return nil;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDiscoveryBegin)
name:WiimoteBeginDiscoveryNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDiscoveryEnd)
name:WiimoteEndDiscoveryNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceConnected)
name:WiimoteConnectedNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceBatteryStateChanged:)
name:WiimoteBatteryLevelUpdatedNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceDisconnected)
name:WiimoteDisconnectedNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onCheckNewVersionFinished:)
name:UAppUpdateCheckerDidFinishNotification
object:nil];
[UserNotificationCenter setDelegate:self];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)onDiscoveryBegin
{
[UserNotificationCenter
deliver:[UserNotification
userNotificationWithTitle:@"Discovery Mode On"
text:@"Press the red pairing button on your Nintendo device, or click the WJoy icon and enable ."]];
}
- (void)onDiscoveryEnd
{
[UserNotificationCenter
deliver:[UserNotification
userNotificationWithTitle:@"Discovery Mode Off"
text:@"Click the WJoy icon to re-enable discovery."]];
}
- (void)onDeviceConnected
{
[UserNotificationCenter
deliver:[UserNotification
userNotificationWithTitle:@"Connected"
text:@"A device connected."]];
}
- (void)onDeviceBatteryStateChanged:(NSNotification*)notification
{
Wiimote *device = [notification object];
if([device userInfo] != nil ||
![device isBatteryLevelLow])
{
return;
}
[device setUserInfo:[NSDictionary dictionary]];
[UserNotificationCenter
deliver:[UserNotification
userNotificationWithTitle:@"Battery is Low"
text:@"Check the remaining battery of your devices."]];
}
- (void)onDeviceDisconnected
{
[UserNotificationCenter
deliver:[UserNotification
userNotificationWithTitle:@"Disconnected"
text:@"A device disconnected."]];
}
- (void)onCheckNewVersionFinished:(NSNotification*)notification
{
return;
}
@end