forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountlyFeedbackWidget.m
229 lines (186 loc) · 7.92 KB
/
CountlyFeedbackWidget.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// CountlyFeedbackWidget.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
#if (TARGET_OS_IOS)
#import <WebKit/WebKit.h>
#endif
CLYFeedbackWidgetType const CLYFeedbackWidgetTypeSurvey = @"survey";
CLYFeedbackWidgetType const CLYFeedbackWidgetTypeNPS = @"nps";
NSString* const kCountlyReservedEventSurvey = @"[CLY]_survey";
NSString* const kCountlyReservedEventNPS = @"[CLY]_nps";
NSString* const kCountlyFBKeyClosed = @"closed";
NSString* const kCountlyFBKeyShown = @"shown";
@interface CountlyFeedbackWidget ()
@property (nonatomic) CLYFeedbackWidgetType type;
@property (nonatomic) NSString* ID;
@property (nonatomic) NSString* name;
@property (nonatomic) NSDictionary* data;
@end
@implementation CountlyFeedbackWidget
#if (TARGET_OS_IOS)
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary
{
CountlyFeedbackWidget *feedback = CountlyFeedbackWidget.new;
feedback.ID = dictionary[kCountlyFBKeyID];
feedback.type = dictionary[@"type"];
feedback.name = dictionary[@"name"];
return feedback;
}
- (void)present
{
CLY_LOG_I(@"%s", __FUNCTION__);
[self presentWithAppearBlock:nil andDismissBlock:nil];
}
- (void)presentWithAppearBlock:(void(^ __nullable)(void))appearBlock andDismissBlock:(void(^ __nullable)(void))dismissBlock;
{
CLY_LOG_I(@"%s %@ %@", __FUNCTION__, appearBlock, dismissBlock);
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
__block CLYInternalViewController* webVC = CLYInternalViewController.new;
webVC.view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
webVC.modalPresentationStyle = UIModalPresentationCustom;
WKWebView* webView = [WKWebView.alloc initWithFrame:webVC.view.bounds];
webView.layer.shadowColor = UIColor.blackColor.CGColor;
webView.layer.shadowOpacity = 0.5;
webView.layer.shadowOffset = (CGSize){0.0f, 5.0f};
webView.layer.masksToBounds = NO;
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webVC.view addSubview:webView];
webVC.webView = webView;
NSURLRequest* request = [self displayRequest];
[webView loadRequest:request];
CLYButton* dismissButton = [CLYButton dismissAlertButton];
dismissButton.onClick = ^(id sender)
{
[webVC dismissViewControllerAnimated:YES completion:^
{
if (dismissBlock)
dismissBlock();
webVC = nil;
}];
[self recordReservedEventForDismissing];
};
[webView addSubview:dismissButton];
[dismissButton positionToTopRight];
[CountlyCommon.sharedInstance tryPresentingViewController:webVC withCompletion:appearBlock];
}
- (void)getWidgetData:(void (^)(NSDictionary * __nullable widgetData, NSError * __nullable error))completionHandler
{
CLY_LOG_I(@"%s %@", __FUNCTION__, completionHandler);
NSURLSessionTask* task = [NSURLSession.sharedSession dataTaskWithRequest:[self dataRequest] completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSDictionary *widgetData = nil;
if (!error)
{
widgetData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (!error)
{
if (((NSHTTPURLResponse*)response).statusCode != 200)
{
NSMutableDictionary* userInfo = widgetData.mutableCopy;
userInfo[NSLocalizedDescriptionKey] = @"Feedbacks general API error";
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorFeedbacksGeneralAPIError userInfo:userInfo];
}
}
self.data = widgetData;
dispatch_async(dispatch_get_main_queue(), ^
{
if (completionHandler)
completionHandler(widgetData, error);
});
}];
[task resume];
}
- (void)recordResult:(NSDictionary * __nullable)result
{
CLY_LOG_I(@"%s %@", __FUNCTION__, result);
if (!result)
[self recordReservedEventForDismissing];
else
[self recordReservedEventWithSegmentation:result];
}
- (NSURLRequest *)dataRequest
{
NSString* queryString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
kCountlyQSKeySDKName, CountlyCommon.sharedInstance.SDKName,
kCountlyQSKeySDKVersion, CountlyCommon.sharedInstance.SDKVersion,
kCountlyFBKeyAppVersion, CountlyDeviceInfo.appVersion,
kCountlyFBKeyPlatform, CountlyDeviceInfo.osName,
kCountlyFBKeyShown, @"1",
kCountlyFBKeyWidgetID, self.ID];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSMutableString* URL = CountlyConnectionManager.sharedInstance.host.mutableCopy;
[URL appendString:kCountlyEndpointO];
[URL appendString:kCountlyEndpointSurveys];
NSString* feedbackTypeEndpoint = [@"/" stringByAppendingString:self.type];
[URL appendString:feedbackTypeEndpoint];
[URL appendString:kCountlyEndpointWidget];
if (queryString.length > kCountlyGETRequestMaxLength || CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
[URL appendFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
return request;
}
}
- (NSURLRequest *)displayRequest
{
NSString* queryString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
kCountlyQSKeyAppKey, CountlyConnectionManager.sharedInstance.appKey.cly_URLEscaped,
kCountlyQSKeyDeviceID, CountlyDeviceInfo.sharedInstance.deviceID.cly_URLEscaped,
kCountlyQSKeySDKName, CountlyCommon.sharedInstance.SDKName,
kCountlyQSKeySDKVersion, CountlyCommon.sharedInstance.SDKVersion,
kCountlyFBKeyAppVersion, CountlyDeviceInfo.appVersion,
kCountlyFBKeyPlatform, CountlyDeviceInfo.osName,
kCountlyFBKeyWidgetID, self.ID];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSMutableString* URL = CountlyConnectionManager.sharedInstance.host.mutableCopy;
[URL appendString:kCountlyEndpointFeedback];
NSString* feedbackTypeEndpoint = [@"/" stringByAppendingString:self.type];
[URL appendString:feedbackTypeEndpoint];
[URL appendFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
return request;
}
- (void)recordReservedEventForDismissing
{
[self recordReservedEventWithSegmentation:@{kCountlyFBKeyClosed: @1}];
}
- (void)recordReservedEventWithSegmentation:(NSDictionary *)segm
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
NSString* eventName = nil;
if ([self.type isEqualToString:CLYFeedbackWidgetTypeSurvey])
eventName = kCountlyReservedEventSurvey;
else if ([self.type isEqualToString:CLYFeedbackWidgetTypeNPS])
eventName = kCountlyReservedEventNPS;
if (!eventName)
{
CLY_LOG_W(@"Unsupported feedback widget type! Event will not be recorded!");
return;
}
NSMutableDictionary* segmentation = segm.mutableCopy;
segmentation[kCountlyFBKeyPlatform] = CountlyDeviceInfo.osName;
segmentation[kCountlyFBKeyAppVersion] = CountlyDeviceInfo.appVersion;
segmentation[kCountlyFBKeyWidgetID] = self.ID;
[Countly.sharedInstance recordReservedEvent:eventName segmentation:segmentation];
[CountlyConnectionManager.sharedInstance sendEvents];
}
- (NSString *)description
{
NSString *customDescription = [NSString stringWithFormat:@"\rID: %@, Type: %@ \rName: %@", self.ID, self.type, self.name];
return [[super description] stringByAppendingString:customDescription];
}
#endif
@end