Skip to content

Commit

Permalink
fix: update how toastWindow is obtained
Browse files Browse the repository at this point in the history
  • Loading branch information
vonovak committed Aug 1, 2023
1 parent 1cf07ed commit eaf6a83
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
21 changes: 13 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ export default function App() {
style={{ backgroundColor: 'white' }}
>
<View style={styles.container}>
<Button
title={'simple toast'}
onPress={() => {
Toast.show('This is a toast.', Toast.SHORT);
}}
/>
<Button
onPress={() => {
setModalVisible(true);
Expand All @@ -59,15 +65,10 @@ export default function App() {
);
}, 500);
}}
title="Show Modal"
title="toast on top of Modal"
/>
{/*<Text>{JSON.stringify(Toast, null, 2)}</Text>*/}
<Button
title={'simple toast'}
onPress={() => {
Toast.show('This is a toast.', Toast.SHORT);
}}
/>

<Button
title={'styled toast'}
onPress={() => {
Expand All @@ -94,7 +95,11 @@ export default function App() {
onPress={() => {
Alert.alert('this is an alert');
setTimeout(() => {
Toast.showWithGravity('This is an alert toast.', 5, Toast.TOP);
Toast.showWithGravity(
'This is an alert toast.',
5,
Toast.CENTER,
);
}, 100);
}}
/>
Expand Down
41 changes: 36 additions & 5 deletions ios/RNToastViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,31 @@ @interface RNToastViewController ()

@implementation RNToastViewController

- (UIWindow *)toastWindow {
// presenting directly from RCTSharedApplication().keyWindow won't work for Alerts
// which is why we have our own VC

- (UIWindow *)toastWindow
{
if (_toastWindow == nil) {
_toastWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
_toastWindow.rootViewController = [UIViewController new];
_toastWindow.windowLevel = UIWindowLevelAlert + 2;
_toastWindow.userInteractionEnabled = NO;
_toastWindow = [self getUIWindowFromScene];

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

if (_toastWindow) {
_toastWindow.rootViewController = [UIViewController new];
_toastWindow.windowLevel = UIWindowLevelAlert + 1;
_toastWindow.userInteractionEnabled = NO;
}
}

return _toastWindow;
}

Expand All @@ -36,4 +54,17 @@ - (void)hide {
_toastWindow = nil;
}

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

@end

0 comments on commit eaf6a83

Please sign in to comment.