forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyFeedbacks.m
466 lines (373 loc) · 16.3 KB
/
CountlyFeedbacks.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// CountlyFeedbacks.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
@interface CountlyFeedbackWidget ()
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
@end
@interface CountlyFeedbacks ()
#if (TARGET_OS_IOS)
@property (nonatomic) UIAlertController* alertController;
@property (nonatomic, copy) void (^ratingCompletion)(NSInteger);
#endif
@end
NSString* const kCountlyReservedEventStarRating = @"[CLY]_star_rating";
NSString* const kCountlyStarRatingStatusSessionCountKey = @"kCountlyStarRatingStatusSessionCountKey";
NSString* const kCountlyStarRatingStatusHasEverAskedAutomatically = @"kCountlyStarRatingStatusHasEverAskedAutomatically";
NSString* const kCountlyFBKeyPlatform = @"platform";
NSString* const kCountlyFBKeyAppVersion = @"app_version";
NSString* const kCountlyFBKeyRating = @"rating";
NSString* const kCountlyFBKeyWidgetID = @"widget_id";
NSString* const kCountlyFBKeyID = @"_id";
NSString* const kCountlyFBKeyTargetDevices = @"target_devices";
NSString* const kCountlyFBKeyPhone = @"phone";
NSString* const kCountlyFBKeyTablet = @"tablet";
NSString* const kCountlyFBKeyFeedback = @"feedback";
const CGFloat kCountlyStarRatingButtonSize = 40.0;
@implementation CountlyFeedbacks
#if (TARGET_OS_IOS)
{
UIButton* btn_star[5];
}
+ (instancetype)sharedInstance
{
if (!CountlyCommon.sharedInstance.hasStarted)
return nil;
static CountlyFeedbacks* s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
NSString* langDesignator = [NSLocale.preferredLanguages.firstObject substringToIndex:2];
NSDictionary* dictMessage =
@{
@"en": @"How would you rate the app?",
@"tr": @"Uygulamayı nasıl değerlendirirsiniz?",
@"ja": @"あなたの評価を教えてください。",
@"zh": @"请告诉我你的评价。",
@"ru": @"Как бы вы оценили приложение?",
@"cz": @"Jak hodnotíte aplikaci?",
@"lv": @"Kā Jūs novērtētu šo lietotni?",
@"bn": @"আপনি কিভাবে এই এপ্লিক্যাশনটি মূল্যায়ন করবেন?",
@"hi": @"आप एप्लीकेशन का मूल्यांकन कैसे करेंगे?",
};
self.message = dictMessage[langDesignator];
if (!self.message)
self.message = dictMessage[@"en"];
}
return self;
}
#pragma mark - Star Rating
- (void)showDialog:(void(^)(NSInteger rating))completion
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
self.ratingCompletion = completion;
self.alertController = [UIAlertController alertControllerWithTitle:@" " message:self.message preferredStyle:UIAlertControllerStyleAlert];
CLYButton* dismissButton = [CLYButton dismissAlertButton];
dismissButton.onClick = ^(id sender)
{
[self.alertController dismissViewControllerAnimated:YES completion:^
{
[self finishWithRating:0];
}];
};
[self.alertController.view addSubview:dismissButton];
[dismissButton positionToTopRight];
CLYInternalViewController* cvc = CLYInternalViewController.new;
[cvc setPreferredContentSize:(CGSize){kCountlyStarRatingButtonSize * 5, kCountlyStarRatingButtonSize * 1.5}];
[cvc.view addSubview:[self starView]];
@try
{
[self.alertController setValue:cvc forKey:@"contentViewController"];
}
@catch (NSException* exception)
{
COUNTLY_LOG(@"UIAlertController's contentViewController can not be set: \n%@", exception);
}
[CountlyCommon.sharedInstance tryPresentingViewController:self.alertController];
}
- (void)checkForStarRatingAutoAsk
{
if (!self.sessionCount)
return;
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
NSMutableDictionary* status = [CountlyPersistency.sharedInstance retrieveStarRatingStatus].mutableCopy;
if (self.disableAskingForEachAppVersion && status[kCountlyStarRatingStatusHasEverAskedAutomatically])
return;
NSString* keyForAppVersion = [kCountlyStarRatingStatusSessionCountKey stringByAppendingString:CountlyDeviceInfo.appVersion];
NSInteger sessionCountSoFar = [status[keyForAppVersion] integerValue];
sessionCountSoFar++;
if (self.sessionCount == sessionCountSoFar)
{
COUNTLY_LOG(@"Asking for star-rating as session count reached specified limit %d ...", (int)self.sessionCount);
[self showDialog:self.ratingCompletionForAutoAsk];
status[kCountlyStarRatingStatusHasEverAskedAutomatically] = @YES;
}
status[keyForAppVersion] = @(sessionCountSoFar);
[CountlyPersistency.sharedInstance storeStarRatingStatus:status];
}
- (UIView *)starView
{
UIView* vw_star = [UIView.alloc initWithFrame:(CGRect){0, 0, kCountlyStarRatingButtonSize * 5, kCountlyStarRatingButtonSize}];
vw_star.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
for (int i = 0; i < 5; i++)
{
btn_star[i] = [UIButton.alloc initWithFrame:(CGRect){i * kCountlyStarRatingButtonSize, 0, kCountlyStarRatingButtonSize, kCountlyStarRatingButtonSize}];
btn_star[i].titleLabel.font = [UIFont fontWithName:@"Helvetica" size:28];
[btn_star[i] setTitle:@"★" forState:UIControlStateNormal];
[btn_star[i] setTitleColor:[self passiveStarColor] forState:UIControlStateNormal];
[btn_star[i] addTarget:self action:@selector(onClick_star:) forControlEvents:UIControlEventTouchUpInside];
[vw_star addSubview:btn_star[i]];
}
return vw_star;
}
- (void)setMessage:(NSString *)message
{
if (!message)
return;
_message = message;
}
- (void)onClick_star:(id)sender
{
UIColor* color = [self activeStarColor];
NSInteger rating = 0;
for (int i = 0; i < 5; i++)
{
[btn_star[i] setTitleColor:color forState:UIControlStateNormal];
if (btn_star[i] == sender)
{
color = [self passiveStarColor];
rating = i + 1;
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
[self.alertController dismissViewControllerAnimated:YES completion:^{ [self finishWithRating:rating]; }];
});
}
- (void)finishWithRating:(NSInteger)rating
{
if (self.ratingCompletion)
self.ratingCompletion(rating);
if (rating != 0)
{
NSMutableDictionary* segmentation = NSMutableDictionary.new;
segmentation[kCountlyFBKeyPlatform] = CountlyDeviceInfo.osName;
segmentation[kCountlyFBKeyAppVersion] = CountlyDeviceInfo.appVersion;
segmentation[kCountlyFBKeyRating] = @(rating);
[Countly.sharedInstance recordReservedEvent:kCountlyReservedEventStarRating segmentation:segmentation];
}
self.alertController = nil;
self.ratingCompletion = nil;
}
- (UIColor *)activeStarColor
{
return [UIColor colorWithRed:253/255.0 green:148/255.0 blue:38/255.0 alpha:1];
}
- (UIColor *)passiveStarColor
{
return [UIColor colorWithWhite:178/255.0 alpha:1];
}
#pragma mark - Feedbacks (Ratings) (Legacy Feedback Widget)
- (void)checkFeedbackWidgetWithID:(NSString *)widgetID completionHandler:(void (^)(NSError * error))completionHandler
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
if (!widgetID.length)
return;
NSURLRequest* feedbackWidgetCheckRequest = [self widgetCheckURLRequest:widgetID];
NSURLSessionTask* task = [NSURLSession.sharedSession dataTaskWithRequest:feedbackWidgetCheckRequest completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSDictionary* widgetInfo = nil;
if (!error)
{
widgetInfo = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (!error)
{
NSMutableDictionary* userInfo = widgetInfo.mutableCopy;
if (![widgetInfo[kCountlyFBKeyID] isEqualToString:widgetID])
{
userInfo[NSLocalizedDescriptionKey] = [NSString stringWithFormat:@"Feedback widget with ID %@ is not available.", widgetID];
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorFeedbackWidgetNotAvailable userInfo:userInfo];
}
else if (![self isDeviceTargetedByWidget:widgetInfo])
{
userInfo[NSLocalizedDescriptionKey] = [NSString stringWithFormat:@"Feedback widget with ID %@ does not include this device in target devices list.", widgetID];
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorFeedbackWidgetNotTargetedForDevice userInfo:userInfo];
}
}
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (completionHandler)
completionHandler(error);
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^
{
[self presentFeedbackWidgetWithID:widgetID completionHandler:completionHandler];
});
}];
[task resume];
}
- (void)presentFeedbackWidgetWithID:(NSString *)widgetID completionHandler:(void (^)(NSError * error))completionHandler
{
__block CLYInternalViewController* webVC = CLYInternalViewController.new;
webVC.view.backgroundColor = UIColor.whiteColor;
webVC.view.bounds = UIScreen.mainScreen.bounds;
webVC.modalPresentationStyle = UIModalPresentationCustom;
WKWebView* webView = [WKWebView.alloc initWithFrame:webVC.view.bounds];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[webVC.view addSubview:webView];
NSURL* widgetDisplayURL = [self widgetDisplayURL:widgetID];
[webView loadRequest:[NSURLRequest requestWithURL:widgetDisplayURL]];
CLYButton* dismissButton = [CLYButton dismissAlertButton];
dismissButton.onClick = ^(id sender)
{
[webVC dismissViewControllerAnimated:YES completion:^
{
if (completionHandler)
completionHandler(nil);
webVC = nil;
}];
};
[webVC.view addSubview:dismissButton];
[dismissButton positionToTopRightConsideringStatusBar];
[CountlyCommon.sharedInstance tryPresentingViewController:webVC];
}
- (NSURLRequest *)widgetCheckURLRequest:(NSString *)widgetID
{
NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials];
queryString = [queryString stringByAppendingFormat:@"&%@=%@", kCountlyFBKeyWidgetID, widgetID];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSString* serverOutputFeedbackWidgetEndpoint = [CountlyConnectionManager.sharedInstance.host stringByAppendingFormat:@"%@%@%@",
kCountlyEndpointO,
kCountlyEndpointFeedback,
kCountlyEndpointWidget];
if (CountlyConnectionManager.sharedInstance.alwaysUsePOST)
{
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverOutputFeedbackWidgetEndpoint]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString cly_dataUTF8];
return request.copy;
}
else
{
NSString* withQueryString = [serverOutputFeedbackWidgetEndpoint stringByAppendingFormat:@"?%@", queryString];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:withQueryString]];
return request;
}
}
- (NSURL *)widgetDisplayURL:(NSString *)widgetID
{
NSString* queryString = [CountlyConnectionManager.sharedInstance queryEssentials];
queryString = [queryString stringByAppendingFormat:@"&%@=%@&%@=%@",
kCountlyFBKeyWidgetID, widgetID,
kCountlyFBKeyAppVersion, CountlyDeviceInfo.appVersion];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSString* URLString = [NSString stringWithFormat:@"%@%@?%@",
CountlyConnectionManager.sharedInstance.host,
kCountlyEndpointFeedback,
queryString];
return [NSURL URLWithString:URLString];
}
- (BOOL)isDeviceTargetedByWidget:(NSDictionary *)widgetInfo
{
BOOL isTablet = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
BOOL isPhone = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
BOOL isTabletTargeted = [widgetInfo[kCountlyFBKeyTargetDevices][kCountlyFBKeyTablet] boolValue];
BOOL isPhoneTargeted = [widgetInfo[kCountlyFBKeyTargetDevices][kCountlyFBKeyPhone] boolValue];
return ((isTablet && isTabletTargeted) || (isPhone && isPhoneTargeted));
}
#pragma mark - Feedbacks (Surveys, NPS)
- (void)getFeedbackWidgets:(void (^)(NSArray <CountlyFeedbackWidget *> *feedbackWidgets, NSError *error))completionHandler
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
if (CountlyDeviceInfo.sharedInstance.isDeviceIDTemporary)
return;
NSURLSessionTask* task = [NSURLSession.sharedSession dataTaskWithRequest:[self feedbacksRequest] completionHandler:^(NSData* data, NSURLResponse* response, NSError* error)
{
NSDictionary *feedbacksResponse = nil;
if (!error)
{
feedbacksResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
if (!error)
{
if (((NSHTTPURLResponse*)response).statusCode != 200)
{
NSMutableDictionary* userInfo = feedbacksResponse.mutableCopy;
userInfo[NSLocalizedDescriptionKey] = @"Feedbacks general API error";
error = [NSError errorWithDomain:kCountlyErrorDomain code:CLYErrorFeedbacksGeneralAPIError userInfo:userInfo];
}
}
if (error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (completionHandler)
completionHandler(nil, error);
});
return;
}
NSMutableArray* feedbacks = NSMutableArray.new;
NSArray* rawFeedbackObjects = feedbacksResponse[@"result"];
for (NSDictionary * feedbackDict in rawFeedbackObjects)
{
CountlyFeedbackWidget *feedback = [CountlyFeedbackWidget createWithDictionary:feedbackDict];
if (feedback)
[feedbacks addObject:feedback];
}
dispatch_async(dispatch_get_main_queue(), ^
{
if (completionHandler)
completionHandler([NSArray arrayWithArray:feedbacks], nil);
});
}];
[task resume];
}
- (NSURLRequest *)feedbacksRequest
{
NSString* queryString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&%@=%@",
kCountlyQSKeyMethod, kCountlyFBKeyFeedback,
kCountlyQSKeyAppKey, CountlyConnectionManager.sharedInstance.appKey.cly_URLEscaped,
kCountlyQSKeyDeviceID, CountlyDeviceInfo.sharedInstance.deviceID.cly_URLEscaped,
kCountlyQSKeySDKName, CountlyCommon.sharedInstance.SDKName,
kCountlyQSKeySDKVersion, CountlyCommon.sharedInstance.SDKVersion];
queryString = [CountlyConnectionManager.sharedInstance appendChecksum:queryString];
NSMutableString* URL = CountlyConnectionManager.sharedInstance.host.mutableCopy;
[URL appendString:kCountlyEndpointO];
[URL appendString:kCountlyEndpointSDK];
if (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;
}
}
#endif
@end