Skip to content

Commit b16193e

Browse files
feat: move everything to view
1 parent 4b032f4 commit b16193e

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

CountlyWebViewManager.m

-38
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ - (void)createWebViewWithURL:(NSURL *)url
4343
NSURLRequest *request = [NSURLRequest requestWithURL:url];
4444
[webView loadRequest:request];
4545

46-
#if (TARGET_OS_IOS)
47-
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIDeviceOrientationDidChangeNotification object:nil];
48-
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIScreenModeDidChangeNotification object:nil];
49-
#endif
50-
5146
CLYButton *dismissButton = [CLYButton dismissAlertButton:@"X"];
5247
[self configureDismissButton:dismissButton forWebView:webView];
5348

@@ -64,35 +59,6 @@ - (void)createWebViewWithURL:(NSURL *)url
6459
}];
6560
}
6661

67-
- (void)handleScreenChange {
68-
CGRect screenBounds = [UIScreen mainScreen].bounds;
69-
if (@available(iOS 11.0, *)) {
70-
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
71-
72-
if (top) {
73-
screenBounds.origin.y += top + 5;
74-
screenBounds.size.height -= top + 5;
75-
} else {
76-
screenBounds.origin.y += 20.0;
77-
screenBounds.size.height -= 20.0;
78-
}
79-
} else {
80-
screenBounds.origin.y += 20.0;
81-
screenBounds.size.height -= 20.0;
82-
}
83-
84-
CGFloat width = screenBounds.size.width;
85-
CGFloat height = screenBounds.size.height;
86-
87-
NSString *postMessage = [NSString stringWithFormat:
88-
@"javascript:window.postMessage({type: 'resize', width: %f, height: %f}, '*');",
89-
width,
90-
height];
91-
NSURL *uri = [NSURL URLWithString:postMessage];
92-
NSURLRequest *request = [NSURLRequest requestWithURL:uri];
93-
[self.backgroundView.webView loadRequest:request];
94-
}
95-
9662
- (void)configureWebView:(WKWebView *)webView {
9763
webView.layer.shadowColor = UIColor.blackColor.CGColor;
9864
webView.layer.shadowOpacity = 0.5;
@@ -332,10 +298,6 @@ - (void)resizeWebViewWithJSONString:(NSString *)jsonString {
332298
}
333299

334300
- (void)closeWebView {
335-
#if (TARGET_OS_IOS)
336-
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
337-
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenModeDidChangeNotification object:nil];
338-
#endif
339301
dispatch_async(dispatch_get_main_queue(), ^{
340302
if (self.dismissBlock) {
341303
self.dismissBlock();

PassThroughBackgroundView.m

+52-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ @implementation PassThroughBackgroundView
1414
- (instancetype)initWithFrame:(CGRect)frame {
1515

1616
self = [super initWithFrame:frame];
17-
if (self) {
18-
}
17+
#if (TARGET_OS_IOS)
18+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIDeviceOrientationDidChangeNotification object:nil];
19+
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIScreenModeDidChangeNotification object:nil];
20+
#endif
1921
return self;
2022
}
2123

@@ -31,6 +33,54 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
3133
return NO;
3234
}
3335

36+
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
37+
[super traitCollectionDidChange:previousTraitCollection];
38+
39+
if (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass) {
40+
[self adjustWebViewForTraitCollection:self.traitCollection];
41+
}
42+
}
43+
44+
- (void)adjustWebViewForTraitCollection:(UITraitCollection *)traitCollection {
45+
if (traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
46+
[self handleScreenChange];
47+
}
48+
}
49+
50+
- (void)handleScreenChange {
51+
CGRect screenBounds = [UIScreen mainScreen].bounds;
52+
if (@available(iOS 11.0, *)) {
53+
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
54+
55+
if (top) {
56+
screenBounds.origin.y += top + 5;
57+
screenBounds.size.height -= top + 5;
58+
} else {
59+
screenBounds.origin.y += 20.0;
60+
screenBounds.size.height -= 20.0;
61+
}
62+
} else {
63+
screenBounds.origin.y += 20.0;
64+
screenBounds.size.height -= 20.0;
65+
}
66+
67+
CGFloat width = screenBounds.size.width;
68+
CGFloat height = screenBounds.size.height;
69+
70+
NSString *postMessage = [NSString stringWithFormat:
71+
@"javascript:window.postMessage({type: 'resize', width: %f, height: %f}, '*');",
72+
width,
73+
height];
74+
NSURL *uri = [NSURL URLWithString:postMessage];
75+
NSURLRequest *request = [NSURLRequest requestWithURL:uri];
76+
[self.webView loadRequest:request];
77+
}
78+
79+
80+
// Always remove observers when the view is deallocated
81+
- (void)dealloc {
82+
[[NSNotificationCenter defaultCenter] removeObserver:self];
83+
}
3484

3585
@end
3686
#endif

0 commit comments

Comments
 (0)