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

Revert "Refactor toast window" #78

Merged
merged 1 commit into from
Mar 20, 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: 6 additions & 5 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,17 +106,18 @@ - (void)_show:(NSString *)msg

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

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

void (^completion)(BOOL) = ^(BOOL didTap) {
[weakView removeFromSuperview];
[window setHidden:YES];
[controller hide];
};
// 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: 10 additions & 0 deletions ios/RNToastViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import <UIKit/UIKit.h>

@interface RNToastViewController : NSObject

@property(nonatomic, strong) UIWindow *toastWindow;

- (void)show;
- (void)hide;

@end
54 changes: 54 additions & 0 deletions ios/RNToastViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#import "RNToastViewController.h"
#import <React/RCTUtils.h>
#import "RNToastWindow.h"

@implementation RNToastViewController

- (UIWindow *)toastWindow
{
if (_toastWindow == nil) {
_toastWindow = [self getUIWindowFromScene];

if (_toastWindow == nil) {
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
_toastWindow = [[RNToastWindow alloc] initWithFrame:keyWindow.bounds];
} else {
// keyWindow is nil, so we cannot create and initialize _toastWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}
}

return _toastWindow;
}

- (void)show {
[self.toastWindow setHidden:NO];
}

- (void)hide {
[_toastWindow setHidden:YES];

if (@available(iOS 13, *)) {
_toastWindow.windowScene = nil;
}

_toastWindow = nil;
}

- (UIWindow *)getUIWindowFromScene
{
if (@available(iOS 13.0, *)) {
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive &&
[scene isKindOfClass:[UIWindowScene class]]) {
return [[RNToastWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
}
}
}
return nil;
}

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

@implementation RNToastWindow

- (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];
}
}
}
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");
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.windowLevel = UIWindowLevelAlert + 1;
}
if (self) {
[self setHidden:NO];
return self;
}

- (instancetype)initWithWindowScene:(UIWindowScene *)windowScene {
if (self = [super initWithWindowScene:windowScene]) {
self.windowLevel = UIWindowLevelAlert + 1;
}
return self;
Expand All @@ -35,11 +22,4 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
else return hitView;
}

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

@end
Loading