Skip to content

Commit 81edc30

Browse files
authored
refactor: remove view controller (vonovak#63)
1 parent de95e06 commit 81edc30

File tree

4 files changed

+25
-37
lines changed

4 files changed

+25
-37
lines changed

example/ios/Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ PODS:
266266
- React-jsinspector (0.71.12)
267267
- React-logger (0.71.12):
268268
- glog
269-
- react-native-simple-toast (2.0.1):
269+
- react-native-simple-toast (3.0.1):
270270
- React-Core
271271
- Toast (~> 4)
272272
- React-perflogger (0.71.12)
@@ -496,7 +496,7 @@ SPEC CHECKSUMS:
496496
React-jsiexecutor: 509cd947c28834614808ce056ee8eb700a0662aa
497497
React-jsinspector: ec4dcbfb1f4e72f04f826a0301eceee5fa7ca540
498498
React-logger: 35538accacf2583693fbc3ee8b53e69a1776fcee
499-
react-native-simple-toast: ba0299fee44a1b52fcf9669ff11d4a2278c9c9ab
499+
react-native-simple-toast: 0c7d14bcad288b5b83ae4b0eea65651b4ca829b0
500500
React-perflogger: 75b0e25075c67565a830985f3c373e2eae5389e0
501501
React-RCTActionSheet: a0c3e916b327e297d124d9ebe8b0c721840ee04d
502502
React-RCTAnimation: 3da7025801d7bf0f8cfd94574d6278d5b82a8b88

ios/RNSimpleToast.mm

+18-20
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,22 @@ - (void)_show:(NSString *)msg
107107
NSString *positionString = RNToastPositionMap[@(position)] ?: CSToastPositionBottom;
108108
dispatch_async(dispatch_get_main_queue(), ^{
109109
RNToastViewController *controller = [RNToastViewController new];
110-
[controller show:^() {
111-
UIView *view = [self getToastView:controller];
112-
UIView __weak *weakView = view;
113-
RNToastViewController __weak *weakController = controller;
114-
115-
UIView *toast = [view toastViewForMessage:msg title:nil image:nil style:style];
116-
117-
void (^completion)(BOOL) = ^(BOOL didTap) {
118-
[weakView removeFromSuperview];
119-
[weakController hide];
120-
};
121-
if (!CGPointEqualToPoint(offset, CGPointZero)) {
122-
CGPoint centerWithOffset = [self getCenterWithOffset:offset view:view toast:toast position:positionString];
123-
[view showToast:toast duration:duration position:[NSValue valueWithCGPoint:centerWithOffset] completion:completion];
124-
} else {
125-
[view showToast:toast duration:duration position:positionString completion:completion];
126-
}
127-
}];
110+
[controller show];
111+
UIView *view = [self getToastView:controller];
112+
UIView __weak *weakView = view;
113+
114+
UIView *toast = [view toastViewForMessage:msg title:nil image:nil style:style];
115+
116+
void (^completion)(BOOL) = ^(BOOL didTap) {
117+
[weakView removeFromSuperview];
118+
[controller hide];
119+
};
120+
if (!CGPointEqualToPoint(offset, CGPointZero)) {
121+
CGPoint centerWithOffset = [self getCenterWithOffset:offset view:view toast:toast position:positionString];
122+
[view showToast:toast duration:duration position:[NSValue valueWithCGPoint:centerWithOffset] completion:completion];
123+
} else {
124+
[view showToast:toast duration:duration position:positionString completion:completion];
125+
}
128126
});
129127
}
130128

@@ -160,8 +158,8 @@ - (CGPoint)rnToast_centerPointForPosition:(NSString *)gravity withToast:(UIView
160158
return CGPointMake(view.bounds.size.width / 2.0, (view.bounds.size.height - (toast.frame.size.height / 2.0)) - bottomPadding);
161159
}
162160

163-
- (UIView *)getToastView:(UIViewController *)ctrl {
164-
UIView *rootView = ctrl.view;
161+
- (UIView *)getToastView:(RNToastViewController *)ctrl {
162+
UIView *rootView = ctrl.toastWindow;
165163
CGRect bounds = rootView.bounds;
166164
bounds.size.height -= _kbdHeight;
167165

ios/RNToastViewController.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#import <UIKit/UIKit.h>
22

3-
@interface RNToastViewController : UIViewController
3+
@interface RNToastViewController : NSObject
44

5-
- (void)show:(void (^)(void))completion;
5+
@property(nonatomic, strong) UIWindow *toastWindow;
6+
7+
- (void)show;
68
- (void)hide;
79

810
@end

ios/RNToastViewController.m

+1-13
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,8 @@
22
#import "RNToastViewController.h"
33
#import <React/RCTUtils.h>
44

5-
@interface RNToastViewController ()
6-
7-
@property(nonatomic, strong) UIWindow *toastWindow;
8-
9-
@end
10-
115
@implementation RNToastViewController
126

13-
// presenting directly from RCTSharedApplication().keyWindow won't work for Alerts
14-
// which is why we have our own VC
15-
167
- (UIWindow *)toastWindow
178
{
189
if (_toastWindow == nil) {
@@ -29,7 +20,6 @@ - (UIWindow *)toastWindow
2920
}
3021

3122
if (_toastWindow) {
32-
_toastWindow.rootViewController = [UIViewController new];
3323
_toastWindow.windowLevel = UIWindowLevelAlert + 1;
3424
_toastWindow.userInteractionEnabled = NO;
3525
}
@@ -38,10 +28,8 @@ - (UIWindow *)toastWindow
3828
return _toastWindow;
3929
}
4030

41-
- (void)show:(void (^)(void))completion {
42-
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
31+
- (void)show {
4332
[self.toastWindow setHidden:NO];
44-
[self.toastWindow.rootViewController presentViewController:self animated:NO completion:completion];
4533
}
4634

4735
- (void)hide {

0 commit comments

Comments
 (0)