Skip to content

Commit fb78459

Browse files
authored
feat: remove unneeded view controller (#74)
* refactor: toast window * fix: window level
1 parent 500f882 commit fb78459

File tree

4 files changed

+33
-78
lines changed

4 files changed

+33
-78
lines changed

ios/RNSimpleToast.mm

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#import "RNSimpleToast.h"
22

33
#import "UIView+Toast.h"
4-
#import "RNToastViewController.h"
54
#import <React/RCTConvert.h>
65
#import "RNToastView.h"
6+
#import "RNToastWindow.h"
77

88
static double defaultPositionId = 2.0;
99

@@ -106,18 +106,17 @@ - (void)_show:(NSString *)msg
106106

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

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

118117
void (^completion)(BOOL) = ^(BOOL didTap) {
119118
[weakView removeFromSuperview];
120-
[controller hide];
119+
[window setHidden:YES];
121120
};
122121
// CSToastManager state is shared among toasts, and is used when toast is shown
123122
// so modifications to it should happen in the dispatch_get_main_queue block

ios/RNToastViewController.h

-10
This file was deleted.

ios/RNToastViewController.m

-54
This file was deleted.

ios/RNToastWindow.m

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
#import "RNToastWindow.h"
2+
#import <React/RCTUtils.h>
23

34
@implementation RNToastWindow
45

5-
- (instancetype)initWithFrame:(CGRect)frame {
6-
if (self = [super initWithFrame:frame]) {
7-
self.windowLevel = UIWindowLevelAlert + 1;
6+
- (instancetype)init
7+
{
8+
if (@available(iOS 13.0, *)) {
9+
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
10+
if (scene.activationState == UISceneActivationStateForegroundActive &&
11+
[scene isKindOfClass:[UIWindowScene class]]) {
12+
self = [super initWithWindowScene:(UIWindowScene *)scene];
13+
}
14+
}
815
}
9-
return self;
10-
}
11-
12-
- (instancetype)initWithWindowScene:(UIWindowScene *)windowScene {
13-
if (self = [super initWithWindowScene:windowScene]) {
16+
if (!self) {
17+
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
18+
if (keyWindow) {
19+
self = [super initWithFrame:keyWindow.bounds];
20+
} else {
21+
// keyWindow is nil, so we cannot create and initialize _toastWindow
22+
NSLog(@"Unable to create alert window: keyWindow is nil");
23+
}
24+
}
25+
if (self) {
26+
[self setHidden:NO];
1427
self.windowLevel = UIWindowLevelAlert + 1;
1528
}
1629
return self;
@@ -22,4 +35,11 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
2235
else return hitView;
2336
}
2437

38+
- (void)dealloc
39+
{
40+
if (@available(iOS 13, *)) {
41+
self.windowScene = nil;
42+
}
43+
}
44+
2545
@end

0 commit comments

Comments
 (0)