Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor toast window #74

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ios/RNSimpleToast.mm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#import "RNSimpleToast.h"

#import "UIView+Toast.h"
#import "RNToastViewController.h"
#import <React/RCTConvert.h>
#import "RNToastView.h"
#import "RNToastWindow.h"

static double defaultPositionId = 2.0;

Expand Down Expand Up @@ -106,18 +106,17 @@ - (void)_show:(NSString *)msg

NSString *positionString = RNToastPositionMap[@(position)] ?: CSToastPositionBottom;
dispatch_async(dispatch_get_main_queue(), ^{
RNToastViewController *controller = [RNToastViewController new];
[controller show];
UIWindow *window = [RNToastWindow new];
BOOL kbdAvoidEnabled = [CSToastPositionBottom isEqualToString:positionString];
UIView *view = [[RNToastView alloc] initWithFrame:controller.toastWindow.bounds kbdHeight:self->_kbdHeight kbdAvoidEnabled:kbdAvoidEnabled];
[controller.toastWindow addSubview:view];
UIView *view = [[RNToastView alloc] initWithFrame:window.bounds kbdHeight:self->_kbdHeight kbdAvoidEnabled:kbdAvoidEnabled];
[window addSubview:view];
UIView __weak *weakView = view;

UIView *toast = [view toastViewForMessage:msg title:nil image:nil style:style];

void (^completion)(BOOL) = ^(BOOL didTap) {
[weakView removeFromSuperview];
[controller hide];
[window setHidden:YES];
};
// CSToastManager state is shared among toasts, and is used when toast is shown
// so modifications to it should happen in the dispatch_get_main_queue block
Expand Down
10 changes: 0 additions & 10 deletions ios/RNToastViewController.h

This file was deleted.

54 changes: 0 additions & 54 deletions ios/RNToastViewController.m

This file was deleted.

36 changes: 28 additions & 8 deletions ios/RNToastWindow.m
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
#import "RNToastWindow.h"
#import <React/RCTUtils.h>

@implementation RNToastWindow

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.windowLevel = UIWindowLevelAlert + 1;
- (instancetype)init
{
if (@available(iOS 13.0, *)) {
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive &&
[scene isKindOfClass:[UIWindowScene class]]) {
self = [super initWithWindowScene:(UIWindowScene *)scene];
}
}
}
return self;
}

- (instancetype)initWithWindowScene:(UIWindowScene *)windowScene {
if (self = [super initWithWindowScene:windowScene]) {
if (!self) {
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
self = [super initWithFrame:keyWindow.bounds];
} else {
// keyWindow is nil, so we cannot create and initialize _toastWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}
if (self) {
[self setHidden:NO];
self.windowLevel = UIWindowLevelAlert + 1;
}
return self;
Expand All @@ -22,4 +35,11 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
else return hitView;
}

- (void)dealloc
{
if (@available(iOS 13, *)) {
self.windowScene = nil;
}
}

@end
Loading