-
Notifications
You must be signed in to change notification settings - Fork 244
/
CountlyFeedbackWidget.m
290 lines (242 loc) · 10.8 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 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";
CLYFeedbackWidgetType const CLYFeedbackWidgetTypeRating = @"rating";
NSString* const kCountlyReservedEventSurvey = @"[CLY]_survey";
NSString* const kCountlyReservedEventNPS = @"[CLY]_nps";
NSString* const kCountlyReservedEventRating = @"[CLY]_star_rating";
NSString* const kCountlyFBKeyClosed = @"closed";
NSString* const kCountlyFBKeyShown = @"shown";
@interface CountlyFeedbackWidget ()
@property (nonatomic) CLYFeedbackWidgetType type;
@property (nonatomic) NSString* ID;
@property (nonatomic) NSString* name;
@property (nonatomic) NSArray<NSString*>* tags;
@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"];
feedback.tags = dictionary[@"tg"];
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);
[self presentWithCallback:^(WidgetState widgetState) {
if(appearBlock && widgetState == WIDGET_APPEARED) {
appearBlock();
}
if(dismissBlock && widgetState == WIDGET_CLOSED) {
dismissBlock();
}
}];
}
- (void)presentWithCallback:(WidgetCallback) widgetCallback;
{
CLY_LOG_I(@"%s %@", __FUNCTION__, widgetCallback);
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
__block CLYInternalViewController* webVC = CLYInternalViewController.new;
webVC.view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
webVC.modalPresentationStyle = UIModalPresentationCustom;
// Configure WKWebView with non-persistent data store
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
WKWebView* webView = [[WKWebView alloc] initWithFrame:webVC.view.bounds configuration:configuration];
webView.layer.shadowColor = UIColor.blackColor.CGColor;
webView.layer.shadowOpacity = 0.5;
webView.layer.shadowOffset = CGSizeMake(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:^
{
CLY_LOG_D(@"Feedback widget dismissed. Widget ID: %@, Name: %@", self.ID, self.name);
if (widgetCallback)
widgetCallback(WIDGET_CLOSED);
webVC = nil;
}];
[self recordReservedEventForDismissing];
};
[webView addSubview:dismissButton];
[dismissButton positionToTopRight];
[CountlyCommon.sharedInstance tryPresentingViewController:webVC withCompletion:^{
CLY_LOG_D(@"Feedback widget presented. Widget ID: %@, Name: %@", self.ID, self.name);
if(widgetCallback)
widgetCallback(WIDGET_APPEARED);
}];
}
- (void)getWidgetData:(void (^)(NSDictionary * __nullable widgetData, NSError * __nullable error))completionHandler
{
CLY_LOG_I(@"%s %@", __FUNCTION__, completionHandler);
if (!CountlyServerConfig.sharedInstance.networkingEnabled)
{
CLY_LOG_D(@"'getWidgetData' is aborted: SDK Networking is disabled from server config!");
return;
}
NSURLSessionTask* task = [CountlyCommon.sharedInstance.URLSession 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 = [queryString stringByAppendingFormat:@"&%@=%@",
kCountlyAppVersionKey, CountlyDeviceInfo.appVersion];
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 {
// Create the base URL with endpoint and feedback type
NSMutableString *URL = [NSMutableString stringWithFormat:@"%@%@/%@",
CountlyConnectionManager.sharedInstance.host,
kCountlyEndpointFeedback,
self.type];
// Create a dictionary for query parameters
NSDictionary *queryParams = @{
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,
kCountlyAppVersionKey: CountlyDeviceInfo.appVersion,
};
// Create the query string
NSMutableArray *queryItems = [NSMutableArray array];
[queryParams enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[queryItems addObject:[NSString stringWithFormat:@"%@=%@", key, obj]];
}];
NSString *queryString = [queryItems componentsJoinedByString:@"&"];
// Append checksum to the query string
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
// Add the query string to the URL
[URL appendFormat:@"?%@", queryString];
// Create custom parameters
NSDictionary *customParams = @{@"tc": @"1"};
// Create JSON data from custom parameters
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:customParams options:0 error:&error];
if (!jsonData) {
NSLog(@"Failed to serialize JSON: %@", error);
} else {
NSString *customString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// Append the custom parameter to the URL
[URL appendFormat:@"&custom=%@", customString.cly_URLEscaped];
}
// Create and return the NSURLRequest
return [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
}
- (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;
else if ([self.type isEqualToString:CLYFeedbackWidgetTypeRating])
eventName = kCountlyReservedEventRating;
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: %@ \rTags: %@", self.ID, self.type, self.name, self.tags];
return [[super description] stringByAppendingString:customDescription];
}
#endif
@end