forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyFeedbackWidget.m
113 lines (92 loc) · 4.02 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
// 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 kCountlyReservedEventPrefix = @"[CLY]_"; //NOTE: This will be used with feedback type.
NSString* const kCountlyFBKeyClosed = @"closed";
@interface CountlyFeedbackWidget ()
@property (nonatomic) CLYFeedbackWidgetType type;
@property (nonatomic) NSString* ID;
@property (nonatomic) NSString* name;
@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
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
__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];
NSURLRequest* request = [self displayRequest];
[webView loadRequest:request];
CLYButton* dismissButton = [CLYButton dismissAlertButton];
dismissButton.onClick = ^(id sender)
{
[webVC dismissViewControllerAnimated:YES completion:^
{
webVC = nil;
}];
[self recordReservedEventForDismissing];
};
[webVC.view addSubview:dismissButton];
[dismissButton positionToTopRightConsideringStatusBar];
[CountlyCommon.sharedInstance tryPresentingViewController:webVC];
}
- (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
{
if (!CountlyConsentManager.sharedInstance.consentForFeedback)
return;
NSString* eventName = [kCountlyReservedEventPrefix stringByAppendingString:self.type];
NSMutableDictionary* segmentation = NSMutableDictionary.new;
segmentation[kCountlyFBKeyPlatform] = CountlyDeviceInfo.osName;
segmentation[kCountlyFBKeyAppVersion] = CountlyDeviceInfo.appVersion;
segmentation[kCountlyFBKeyClosed] = @1;
segmentation[kCountlyFBKeyWidgetID] = self.ID;
[Countly.sharedInstance recordReservedEvent:eventName segmentation:segmentation];
}
- (NSString *)description
{
NSString *customDescription = [NSString stringWithFormat:@"\rID: %@, Type: %@ \rName: %@", self.ID, self.type, self.name];
return [[super description] stringByAppendingString:customDescription];
}
#endif
@end