Skip to content

Commit

Permalink
Merge pull request #371 from Countly/resize_me_action
Browse files Browse the repository at this point in the history
Resize me action
  • Loading branch information
turtledreams authored Jan 22, 2025
2 parents 54cefb4 + befa327 commit 7de7ef5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 32 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## XX.XX.XX
* Added dynamic resizing functionality for the content zone
* Improved management of content zone size for better responsiveness
* Added a config option to content (setZoneTimerInterval) to set content zone timer. (Experimental!)

## 24.7.9
Expand Down
57 changes: 38 additions & 19 deletions CountlyContentBuilderInternal.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,49 @@ - (NSURLRequest *)fetchContentsRequest
return request;
}

- (NSString *)resolutionJson {
//TODO: check why area is not clickable and safearea things
CGRect screenBounds = [UIScreen mainScreen].bounds;
if (@available(iOS 11.0, *)) {
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;

if (top) {
screenBounds.origin.y += top + 5;
screenBounds.size.height -= top + 5;
} else {
screenBounds.origin.y += 20.0;
screenBounds.size.height -= 20.0;
- (CGSize)getWindowSize {
CGSize size = CGSizeZero;

// Attempt to retrieve the size from the connected scenes (for modern apps)
if (@available(iOS 13.0, *)) {
NSSet<UIScene *> *scenes = [[UIApplication sharedApplication] connectedScenes];
for (UIScene *scene in scenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
UIWindow *window = windowScene.windows.firstObject;
if (window) {
size = window.bounds.size;
return size; // Return immediately if we find a valid size
}
}
}
}

// Fallback for legacy apps using AppDelegate
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
if ([appDelegate respondsToSelector:@selector(window)]) {
UIWindow *legacyWindow = [appDelegate performSelector:@selector(window)];
if (legacyWindow) {
size = legacyWindow.bounds.size;
}
} else {
screenBounds.origin.y += 20.0;
screenBounds.size.height -= 20.0;
}

return size;
}

- (NSString *)resolutionJson {
//TODO: check why area is not clickable and safearea things
CGSize size = [self getWindowSize];

CGFloat width = screenBounds.size.width;
CGFloat height = screenBounds.size.height;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);

CGFloat lHpW = isLandscape ? size.height : size.width;
CGFloat lWpH = isLandscape ? size.width : size.height;

NSDictionary *resolutionDict = @{
@"portrait": @{@"height": @(height), @"width": @(width)},
@"landscape": @{@"height": @(width), @"width": @(height)}
@"portrait": @{@"height": @(lWpH), @"width": @(lHpW)},
@"landscape": @{@"height": @(lHpW), @"width": @(lWpH)}
};

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resolutionDict options:0 error:nil];
Expand Down
11 changes: 0 additions & 11 deletions CountlyWebViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ - (void)createWebViewWithURL:(NSURL *)url
UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController;
CGRect backgroundFrame = rootViewController.view.bounds;

if (@available(iOS 11.0, *)) {
CGFloat top = UIApplication.sharedApplication.keyWindow.safeAreaInsets.top;
backgroundFrame.origin.y += top ? top + 5 : 20.0;
backgroundFrame.size.height -= top ? top + 5 : 20.0;
} else {
backgroundFrame.origin.y += 20.0;
backgroundFrame.size.height -= 20.0;
}

self.backgroundView = [[PassThroughBackgroundView alloc] initWithFrame:backgroundFrame];
self.backgroundView.backgroundColor = [UIColor clearColor];
[rootViewController.view addSubview:self.backgroundView];
Expand Down Expand Up @@ -297,8 +288,6 @@ - (void)resizeWebViewWithJSONString:(NSString *)jsonString {
}];
}



- (void)closeWebView {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.dismissBlock) {
Expand Down
78 changes: 76 additions & 2 deletions PassThroughBackgroundView.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ @implementation PassThroughBackgroundView
- (instancetype)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
}
#if (TARGET_OS_IOS)
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIDeviceOrientationDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIScreenModeDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleScreenChange) name:UIApplicationDidBecomeActiveNotification object:nil];
#endif
return self;
}

Expand All @@ -31,6 +34,77 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
return NO;
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];

if (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass) {
[self adjustWebViewForTraitCollection:self.traitCollection];
}
}

- (void)adjustWebViewForTraitCollection:(UITraitCollection *)traitCollection {
if (traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
[self handleScreenChange];
}
}

CGSize getWindowSize(void) {
CGSize size = CGSizeZero;

// Attempt to retrieve the size from the connected scenes (for modern apps)
if (@available(iOS 13.0, *)) {
NSSet<UIScene *> *scenes = [[UIApplication sharedApplication] connectedScenes];
for (UIScene *scene in scenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
UIWindow *window = windowScene.windows.firstObject;
if (window) {
size = window.bounds.size;
return size; // Return immediately if we find a valid size
}
}
}
}

// Fallback for legacy apps using AppDelegate
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
if ([appDelegate respondsToSelector:@selector(window)]) {
UIWindow *legacyWindow = [appDelegate performSelector:@selector(window)];
if (legacyWindow) {
size = legacyWindow.bounds.size;
}
}

return size;
}

- (void)handleScreenChange {
// Execute after a short delay to ensure properties are updated
dispatch_async(dispatch_get_main_queue(), ^{
[self updateWindowSize];
});
}

- (void)updateWindowSize {
CGSize size = getWindowSize();
CGFloat width = size.width;
CGFloat height = size.height;

NSString *postMessage = [NSString stringWithFormat:
@"javascript:window.postMessage({type: 'resize', width: %f, height: %f}, '*');",
width,
height];
[self.webView evaluateJavaScript:postMessage completionHandler:^(id result, NSError *err) {
if (err != nil) {
CLY_LOG_E(@"[PassThroughBackgroundView] updateWindowSize, %@", err);
}
}];
}

// Always remove observers when the view is deallocated
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
#endif

0 comments on commit 7de7ef5

Please sign in to comment.