forked from wix-incubator/react-native-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCManager.m
executable file
·221 lines (183 loc) · 5.67 KB
/
RCCManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#import "RCCManager.h"
#import "RCTBridge.h"
#import "RCTRedBox.h"
#import <Foundation/Foundation.h>
@interface RCCManager() <RCTBridgeDelegate>
@property (nonatomic, strong) NSMutableDictionary *modulesRegistry;
@property (nonatomic, strong) RCTBridge *sharedBridge;
@property (nonatomic, strong) NSURL *bundleURL;
@end
@implementation RCCManager
+ (instancetype)sharedInstance
{
static RCCManager *sharedInstance = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken,^{
if (sharedInstance == nil)
{
sharedInstance = [[RCCManager alloc] init];
}
});
return sharedInstance;
}
+ (instancetype)sharedIntance
{
return [RCCManager sharedInstance];
}
- (instancetype)init
{
self = [super init];
if (self)
{
self.modulesRegistry = [@{} mutableCopy];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTReloadNotification object:nil];
}
return self;
}
-(void)clearModuleRegistry
{
[self.modulesRegistry removeAllObjects];
}
-(void)onRNReload
{
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = nil;
[self clearModuleRegistry];
}
-(void)registerController:(UIViewController*)controller componentId:(NSString*)componentId componentType:(NSString*)componentType
{
if (controller == nil || componentId == nil)
{
return;
}
NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
if (componentsDic == nil)
{
componentsDic = [@{} mutableCopy];
self.modulesRegistry[componentType] = componentsDic;
}
/*
TODO: we really want this error, but we need to unregister controllers when they dealloc
if (componentsDic[componentId])
{
[self.sharedBridge.redBox showErrorMessage:[NSString stringWithFormat:@"Controllers: controller with id %@ is already registered. Make sure all of the controller id's you use are unique.", componentId]];
}
*/
componentsDic[componentId] = controller;
}
-(void)unregisterController:(UIViewController*)vc
{
if (vc == nil) return;
for (NSString *key in [self.modulesRegistry allKeys])
{
NSMutableDictionary *componentsDic = self.modulesRegistry[key];
for (NSString *componentID in [componentsDic allKeys])
{
UIViewController *tmpVc = componentsDic[componentID];
if (tmpVc == vc)
{
[componentsDic removeObjectForKey:componentID];
}
}
}
}
-(id)getControllerWithId:(NSString*)componentId componentType:(NSString*)componentType
{
if (componentId == nil)
{
return nil;
}
id component = nil;
NSMutableDictionary *componentsDic = self.modulesRegistry[componentType];
if (componentsDic != nil)
{
component = componentsDic[componentId];
}
return component;
}
-(void)initBridgeWithBundleURL:(NSURL *)bundleURL
{
[self initBridgeWithBundleURL :bundleURL launchOptions:nil];
}
-(void)initBridgeWithBundleURL:(NSURL *)bundleURL launchOptions:(NSDictionary *)launchOptions
{
if (self.sharedBridge) return;
self.bundleURL = bundleURL;
self.sharedBridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
[self showSplashScreen];
}
-(void)showSplashScreen
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
UIView *splashView = nil;
NSString* launchStoryBoard = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UILaunchStoryboardName"];
if (launchStoryBoard != nil)
{//load the splash from the storyboard that's defined in the info.plist as the LaunchScreen
@try
{
splashView = [[NSBundle mainBundle] loadNibNamed:launchStoryBoard owner:self options:nil][0];
if (splashView != nil)
{
splashView.frame = CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height);
}
}
@catch(NSException *e)
{
splashView = nil;
}
}
else
{//load the splash from the DEfault image or from LaunchImage in the xcassets
CGFloat screenHeight = screenBounds.size.height;
NSString* imageName = @"Default";
if (screenHeight == 568)
imageName = [imageName stringByAppendingString:@"-568h"];
else if (screenHeight == 667)
imageName = [imageName stringByAppendingString:@"-667h"];
else if (screenHeight == 736)
imageName = [imageName stringByAppendingString:@"-736h"];
//xcassets LaunchImage files
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil)
{
imageName = @"LaunchImage";
if (screenHeight == 480)
imageName = [imageName stringByAppendingString:@"-700"];
if (screenHeight == 568)
imageName = [imageName stringByAppendingString:@"-700-568h"];
else if (screenHeight == 667)
imageName = [imageName stringByAppendingString:@"-800-667h"];
else if (screenHeight == 736)
imageName = [imageName stringByAppendingString:@"-800-Portrait-736h"];
image = [UIImage imageNamed:imageName];
}
if (image != nil)
{
splashView = [[UIImageView alloc] initWithImage:image];
}
}
if (splashView != nil)
{
UIViewController *splashVC = [[UIViewController alloc] init];
splashVC.view = splashView;
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.window.rootViewController = splashVC;
[appDelegate.window makeKeyAndVisible];
}
}
-(RCTBridge*)getBridge
{
return self.sharedBridge;
}
-(UIWindow*)getAppWindow
{
UIApplication *app = [UIApplication sharedApplication];
UIWindow *window = (app.keyWindow != nil) ? app.keyWindow : app.windows[0];
return window;
}
#pragma mark - RCTBridgeDelegate methods
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
return self.bundleURL;
}
@end