|
5 | 5 | // Please visit www.count.ly for more information. |
6 | 6 |
|
7 | 7 | #import "CountlyCommon.h" |
| 8 | +#import "CountlyWebViewManager.h" |
8 | 9 | #if (TARGET_OS_IOS) |
9 | 10 | #import <WebKit/WebKit.h> |
10 | 11 | #endif |
@@ -58,108 +59,105 @@ - (void)present |
58 | 59 | - (void)presentWithAppearBlock:(void(^ __nullable)(void))appearBlock andDismissBlock:(void(^ __nullable)(void))dismissBlock |
59 | 60 | { |
60 | 61 | CLY_LOG_I(@"%s %@ %@", __FUNCTION__, appearBlock, dismissBlock); |
61 | | - [self presentWithCallback:^(WidgetState widgetState) { |
| 62 | + id widgetCallback = ^(WidgetState widgetState) { |
62 | 63 | if(appearBlock && widgetState == WIDGET_APPEARED) { |
63 | 64 | appearBlock(); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | if(dismissBlock && widgetState == WIDGET_CLOSED) { |
67 | 68 | dismissBlock(); |
68 | 69 | } |
69 | | - }]; |
| 70 | + }; |
| 71 | + |
| 72 | + if (self.widgetVersion) { |
| 73 | + [self presentWidget_new:widgetCallback]; |
| 74 | + } else { |
| 75 | + [self presentWithCallback:widgetCallback]; |
| 76 | + } |
| 77 | + |
70 | 78 | } |
71 | 79 |
|
72 | | -- (void)presentWithCallback:(WidgetCallback) widgetCallback; |
| 80 | +- (void)presentWidget_new:(WidgetCallback) widgetCallback; |
73 | 81 | { |
74 | 82 | CLY_LOG_I(@"%s %@", __FUNCTION__, widgetCallback); |
75 | 83 | if (!CountlyConsentManager.sharedInstance.consentForFeedback) |
76 | 84 | return; |
77 | 85 |
|
78 | | - BOOL isWidgetOld = ![self.widgetVersion length]; |
| 86 | + CGSize size = [CountlyCommon.sharedInstance getWindowSize]; |
79 | 87 |
|
80 | | - _webVC = CLYInternalViewController.new; |
81 | | - _widgetCallback = widgetCallback; |
| 88 | + dispatch_async(dispatch_get_main_queue(), ^ { |
| 89 | + CGRect frame = CGRectMake(0.0, 0.0, size.width, size.height); |
| 90 | + |
| 91 | + // Log the URL and the frame |
| 92 | + CLY_LOG_I(@"%s, Placement frame: %@", __FUNCTION__, NSStringFromCGRect(frame)); |
| 93 | + |
| 94 | + CountlyWebViewManager* webViewManager = CountlyWebViewManager.new; |
| 95 | + [webViewManager createWebViewWithURL:[self generateWidgetURL] frame:frame appearBlock:^ |
| 96 | + { |
| 97 | + CLY_LOG_I(@"%s, Webview appeared", __FUNCTION__); |
| 98 | + if(widgetCallback) |
| 99 | + widgetCallback(WIDGET_APPEARED); |
| 100 | + } dismissBlock:^ |
| 101 | + { |
| 102 | + CLY_LOG_I(@"%s, Webview dismissed", __FUNCTION__); |
| 103 | + if (widgetCallback) |
| 104 | + widgetCallback(WIDGET_CLOSED); |
| 105 | + [self recordReservedEventForDismissing]; |
| 106 | + }]; |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +- (void)presentWithCallback:(WidgetCallback) widgetCallback; |
| 111 | +{ |
| 112 | + CLY_LOG_I(@"%s %@", __FUNCTION__, widgetCallback); |
| 113 | + if (!CountlyConsentManager.sharedInstance.consentForFeedback) |
| 114 | + return; |
82 | 115 |
|
83 | | - if (isWidgetOld) { |
84 | | - _webVC.view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4]; |
85 | | - _webVC.modalPresentationStyle = UIModalPresentationCustom; |
86 | | - } else { |
87 | | - _webVC.modalPresentationStyle = UIModalPresentationOverFullScreen; |
88 | | - _webVC.view.bounds = UIScreen.mainScreen.bounds; |
89 | | - _webVC.view.backgroundColor = [UIColor clearColor]; |
| 116 | + if (self.widgetVersion) { |
| 117 | + [self presentWidget_new:widgetCallback]; |
| 118 | + return; |
90 | 119 | } |
91 | 120 |
|
| 121 | + __block CLYInternalViewController* webVC = CLYInternalViewController.new; |
| 122 | + webVC.view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4]; |
| 123 | + webVC.modalPresentationStyle = UIModalPresentationCustom; |
| 124 | + |
92 | 125 | // Configure WKWebView with non-persistent data store |
93 | 126 | WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; |
94 | 127 | configuration.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore]; |
95 | | - WKWebView* webView = [[WKWebView alloc] initWithFrame:_webVC.view.bounds configuration:configuration]; |
96 | | - webView.navigationDelegate = self; |
97 | | - |
98 | | - if (isWidgetOld) { |
99 | | - webView.layer.shadowColor = UIColor.blackColor.CGColor; |
100 | | - webView.layer.shadowOpacity = 0.5; |
101 | | - webView.layer.shadowOffset = CGSizeMake(0.0f, 5.0f); |
102 | | - webView.layer.masksToBounds = NO; |
103 | | - } |
| 128 | + WKWebView* webView = [[WKWebView alloc] initWithFrame:webVC.view.bounds configuration:configuration]; |
| 129 | + webView.layer.shadowColor = UIColor.blackColor.CGColor; |
| 130 | + webView.layer.shadowOpacity = 0.5; |
| 131 | + webView.layer.shadowOffset = CGSizeMake(0.0f, 5.0f); |
| 132 | + webView.layer.masksToBounds = NO; |
104 | 133 |
|
105 | 134 | webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
106 | | - [_webVC.view addSubview:webView]; |
107 | | - _webVC.webView = webView; |
108 | | - NSURLRequest* request = [self displayRequest]; |
| 135 | + [webVC.view addSubview:webView]; |
| 136 | + webVC.webView = webView; |
| 137 | + NSURLRequest* request = [NSURLRequest requestWithURL:[self generateWidgetURL]]; |
109 | 138 | [webView loadRequest:request]; |
110 | 139 |
|
111 | | - |
112 | | - if (isWidgetOld) { |
113 | | - CLYButton* dismissButton = [CLYButton dismissAlertButton]; |
114 | | - dismissButton.onClick = ^(id sender) |
| 140 | + CLYButton* dismissButton = [CLYButton dismissAlertButton]; |
| 141 | + dismissButton.onClick = ^(id sender) |
| 142 | + { |
| 143 | + [webVC dismissViewControllerAnimated:YES completion:^ |
115 | 144 | { |
116 | | - [self closeFeedbackWidget]; |
117 | | - }; |
118 | | - [webView addSubview:dismissButton]; |
119 | | - [dismissButton positionToTopRight]; |
120 | | - } |
121 | | - |
122 | | - [CountlyCommon.sharedInstance tryPresentingViewController:self.webVC withCompletion:^{ |
| 145 | + CLY_LOG_D(@"Feedback widget dismissed. Widget ID: %@, Name: %@", self.ID, self.name); |
| 146 | + if (widgetCallback) |
| 147 | + widgetCallback(WIDGET_CLOSED); |
| 148 | + webVC = nil; |
| 149 | + }]; |
| 150 | + [self recordReservedEventForDismissing]; |
| 151 | + }; |
| 152 | + [webView addSubview:dismissButton]; |
| 153 | + [dismissButton positionToTopRight]; |
| 154 | + [CountlyCommon.sharedInstance tryPresentingViewController:webVC withCompletion:^{ |
123 | 155 | CLY_LOG_D(@"Feedback widget presented. Widget ID: %@, Name: %@", self.ID, self.name); |
124 | 156 | if(widgetCallback) |
125 | 157 | widgetCallback(WIDGET_APPEARED); |
126 | 158 | }]; |
127 | 159 | } |
128 | 160 |
|
129 | | -- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { |
130 | | - NSString *url = navigationAction.request.URL.absoluteString; |
131 | | - |
132 | | - if ([url hasPrefix:@"https://countly_action_event"] && [url containsString:@"cly_widget_command=1"]) { |
133 | | - if ([url containsString:@"close=1"]) { |
134 | | - [self closeFeedbackWidget]; |
135 | | - } |
136 | | - |
137 | | - decisionHandler(WKNavigationActionPolicyCancel); |
138 | | - } else { |
139 | | - decisionHandler(WKNavigationActionPolicyAllow); |
140 | | - } |
141 | | -} |
142 | | - |
143 | | -- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { |
144 | | - CLY_LOG_I(@"%s Web view has started loading", __FUNCTION__); |
145 | | -} |
146 | | - |
147 | | -- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { |
148 | | - CLY_LOG_I(@"%s Web view has finished loading", __FUNCTION__); |
149 | | -} |
150 | | - |
151 | | -- (void)closeFeedbackWidget{ |
152 | | - [self.webVC dismissViewControllerAnimated:YES completion:^ |
153 | | - { |
154 | | - CLY_LOG_D(@"Feedback widget dismissed. Widget ID: %@, Name: %@", self.ID, self.name); |
155 | | - if (self.widgetCallback) |
156 | | - self.widgetCallback(WIDGET_CLOSED); |
157 | | - self.webVC = nil; |
158 | | - self.widgetCallback = nil; |
159 | | - }]; |
160 | | - [self recordReservedEventForDismissing]; |
161 | | -} |
162 | | - |
163 | 161 | - (void)getWidgetData:(void (^)(NSDictionary * __nullable widgetData, NSError * __nullable error))completionHandler |
164 | 162 | { |
165 | 163 | CLY_LOG_I(@"%s %@", __FUNCTION__, completionHandler); |
@@ -248,7 +246,7 @@ - (NSURLRequest *)dataRequest |
248 | 246 | } |
249 | 247 | } |
250 | 248 |
|
251 | | -- (NSURLRequest *)displayRequest { |
| 249 | +- (NSURL *)generateWidgetURL { |
252 | 250 | // Create the base URL with endpoint and feedback type |
253 | 251 | NSMutableString *URL = [NSMutableString stringWithFormat:@"%@%@/%@", |
254 | 252 | CountlyConnectionManager.sharedInstance.host, |
@@ -296,8 +294,7 @@ - (NSURLRequest *)displayRequest { |
296 | 294 | [URL appendFormat:@"&custom=%@", customString.cly_URLEscaped]; |
297 | 295 | } |
298 | 296 |
|
299 | | - // Create and return the NSURLRequest |
300 | | - return [NSURLRequest requestWithURL:[NSURL URLWithString:URL]]; |
| 297 | + return [NSURL URLWithString:URL]; |
301 | 298 | } |
302 | 299 |
|
303 | 300 |
|
|
0 commit comments