forked from urbanairship/ios-gimbal-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUAGimbalAdapter.m
134 lines (93 loc) · 3.84 KB
/
UAGimbalAdapter.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
/* Copyright 2016 Urban Airship and Contributors */
#import "UAGimbalAdapter.h"
#import <Gimbal/Gimbal.h>
@import AirshipKit;
@interface UAGimbalAdapter() <GMBLPlaceManagerDelegate>
@property (nonatomic, assign, getter=isStarted) BOOL started;
@end
NSString *const GimbalSource = @"Gimbal";
// NSUserDefault Keys
NSString *const GimbalAlertViewKey = @"gmbl_hide_bt_power_alert_view";
NSString *const AdapterStartedKey = @"com.urbanairship.gimbal.started";
NSString *const GimbalKey = @"com.urbanairship.gimbal.key";
@implementation UAGimbalAdapter
static id _sharedObject = nil;
+ (void)load {
[[UAGimbalAdapter shared] restore];
}
- (instancetype)init {
self = [super init];
if (self) {
self.placeManager = [[GMBLPlaceManager alloc] init];
// Hide the power alert by default
if (![[NSUserDefaults standardUserDefaults] valueForKey:GimbalAlertViewKey]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:GimbalAlertViewKey];
}
}
return self;
}
- (void)dealloc {
self.placeManager.delegate = nil;
}
+ (instancetype)shared {
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (BOOL)isBluetoothPoweredOffAlertEnabled {
return ![[NSUserDefaults standardUserDefaults] boolForKey:GimbalAlertViewKey];
}
- (void)setBluetoothPoweredOffAlertEnabled:(BOOL)bluetoothPoweredOffAlertEnabled {
[[NSUserDefaults standardUserDefaults] setBool:!bluetoothPoweredOffAlertEnabled
forKey:GimbalAlertViewKey];
}
- (void)startWithGimbalAPIKey:(NSString *)gimbalAPIKey {
if (self.isStarted) {
return;
}
[Gimbal setAPIKey:gimbalAPIKey options:nil];
self.placeManager.delegate = self;
[GMBLPlaceManager startMonitoring];
self.started = YES;
[[NSUserDefaults standardUserDefaults] setBool:YES
forKey:AdapterStartedKey];
[[NSUserDefaults standardUserDefaults] setObject:gimbalAPIKey
forKey:GimbalKey];
UA_LDEBUG(@"Started Gimbal Adapter.");
}
- (void)stop {
if (!self.isStarted) {
return;
}
[GMBLPlaceManager stopMonitoring];
self.placeManager.delegate = nil;
self.started = NO;
[[NSUserDefaults standardUserDefaults] setBool:NO
forKey:AdapterStartedKey];
UA_LDEBUG(@"Stopped Gimbal Adapter.");
}
- (void)restore {
NSString *apiKey = [[NSUserDefaults standardUserDefaults] stringForKey:GimbalKey];
BOOL started = [[NSUserDefaults standardUserDefaults] boolForKey:AdapterStartedKey];
if (apiKey && started) {
[[UAGimbalAdapter shared] startWithGimbalAPIKey:apiKey];
}
}
#pragma mark -
#pragma mark Gimbal places callbacks
- (void)placeManager:(GMBLPlaceManager *)manager didBeginVisit:(GMBLVisit *)visit {
UA_LDEBUG(@"Entered a Gimbal Place: %@ on the following date: %@", visit.place.name, visit.arrivalDate);
UARegionEvent *regionEvent = [UARegionEvent regionEventWithRegionID:visit.place.identifier
source:GimbalSource
boundaryEvent:UABoundaryEventEnter];
[[UAirship shared].analytics addEvent:regionEvent];
}
- (void)placeManager:(GMBLPlaceManager *)manager didEndVisit:(GMBLVisit *)visit {
UA_LDEBUG(@"Exited a Gimbal Place: %@ Entrance date:%@ Exit Date:%@", visit.place.name, visit.arrivalDate, visit.departureDate);
UARegionEvent *regionEvent = [UARegionEvent regionEventWithRegionID:visit.place.identifier
source:GimbalSource
boundaryEvent:UABoundaryEventExit];
}
@end