Skip to content

Commit

Permalink
Stop manually manage viewControllers store, Iterate layout instead (#…
Browse files Browse the repository at this point in the history
…4991)

* Stop manually manage viewControllers store, Iterate layout instead

* Update RNNCommandsHandlerTest.m
  • Loading branch information
yogevbd authored Apr 17, 2019
1 parent 9d7d7f4 commit 275304c
Show file tree
Hide file tree
Showing 28 changed files with 311 additions and 433 deletions.
2 changes: 0 additions & 2 deletions lib/ios/RNNBridgeManager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import <Foundation/Foundation.h>
#import <React/RCTBridge.h>
#import "RNNBridgeManagerDelegate.h"
#import "RNNStore.h"

typedef UIViewController * (^RNNExternalViewCreator)(NSDictionary* props, RCTBridge* bridge);

Expand All @@ -12,6 +11,5 @@ typedef UIViewController * (^RNNExternalViewCreator)(NSDictionary* props, RCTBri
- (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback;

@property (readonly, nonatomic, strong) RCTBridge *bridge;
@property (readonly, nonatomic, strong) RNNStore *store;

@end
11 changes: 5 additions & 6 deletions lib/ios/RNNBridgeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@interface RNNBridgeManager() <RCTBridgeDelegate>

@property (nonatomic, strong, readwrite) RCTBridge *bridge;
@property (nonatomic, strong, readwrite) RNNStore *store;
@property (nonatomic, strong, readwrite) RNNExternalComponentStore *store;
@property (nonatomic, strong, readwrite) RNNReactComponentRegistry *componentRegistry;

@end
Expand All @@ -25,7 +25,7 @@ @implementation RNNBridgeManager {
RCTBridge* _bridge;
UIWindow* _mainWindow;

RNNStore* _store;
RNNExternalComponentStore* _store;

RNNCommandsHandler* _commandsHandler;
}
Expand All @@ -37,7 +37,7 @@ - (instancetype)initWithJsCodeLocation:(NSURL *)jsCodeLocation launchOptions:(NS
_launchOptions = launchOptions;
_delegate = delegate;

_store = [RNNStore new];
_store = [RNNExternalComponentStore new];
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:_launchOptions];


Expand Down Expand Up @@ -82,7 +82,7 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
_componentRegistry = [[RNNReactComponentRegistry alloc] initWithCreator:rootViewCreator];
RNNControllerFactory *controllerFactory = [[RNNControllerFactory alloc] initWithRootViewCreator:rootViewCreator eventEmitter:eventEmitter store:_store componentRegistry:_componentRegistry andBridge:bridge];

_commandsHandler = [[RNNCommandsHandler alloc] initWithStore:_store controllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new] mainWindow:_mainWindow];
_commandsHandler = [[RNNCommandsHandler alloc] initWithControllerFactory:controllerFactory eventEmitter:eventEmitter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:[RNNOverlayManager new] mainWindow:_mainWindow];
RNNBridgeModule *bridgeModule = [[RNNBridgeModule alloc] initWithCommandsHandler:_commandsHandler];

return [@[bridgeModule,eventEmitter] arrayByAddingObjectsFromArray:[self extraModulesFromDelegate]];
Expand All @@ -91,12 +91,11 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
# pragma mark - JavaScript & Bridge Notifications

- (void)onJavaScriptWillLoad {
[_store clean];
[_componentRegistry clear];
}

- (void)onJavaScriptLoaded {
[_store setReadyToReceiveCommands:true];
[_commandsHandler setReadyToReceiveCommands:true];
[[_bridge moduleForClass:[RNNEventEmitter class]] sendOnAppLaunched];
}

Expand Down
5 changes: 3 additions & 2 deletions lib/ios/RNNCommandsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#import <UIKit/UIKit.h>

#import "RNNControllerFactory.h"
#import "RNNStore.h"
#import "RNNModalManager.h"
#import "RNNNavigationStackManager.h"
#import "RNNOverlayManager.h"

@interface RNNCommandsHandler : NSObject

- (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow;
- (instancetype)initWithControllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow;

@property (nonatomic) BOOL readyToReceiveCommands;

- (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion;

Expand Down
25 changes: 12 additions & 13 deletions lib/ios/RNNCommandsHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "UIViewController+RNNOptions.h"
#import "React/RCTI18nUtil.h"
#import "UIViewController+LayoutProtocol.h"
#import "RNNLayoutManager.h"

static NSString* const setRoot = @"setRoot";
static NSString* const setStackRoot = @"setStackRoot";
Expand All @@ -31,17 +32,15 @@ @interface RNNCommandsHandler() <RNNModalManagerDelegate>

@implementation RNNCommandsHandler {
RNNControllerFactory *_controllerFactory;
RNNStore *_store;
RNNModalManager* _modalManager;
RNNOverlayManager* _overlayManager;
RNNNavigationStackManager* _stackManager;
RNNEventEmitter* _eventEmitter;
UIWindow* _mainWindow;
}

- (instancetype)initWithStore:(RNNStore*)store controllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
- (instancetype)initWithControllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
self = [super init];
_store = store;
_controllerFactory = controllerFactory;
_eventEmitter = eventEmitter;
_modalManager = modalManager;
Expand Down Expand Up @@ -87,7 +86,7 @@ - (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:
- (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
[self assertReady];

UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[_store findComponentForId:componentId];
UIViewController<RNNLayoutProtocol>* vc = (UIViewController<RNNLayoutProtocol>*)[RNNLayoutManager findComponentForId:componentId];
RNNNavigationOptions* newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] || [vc isKindOfClass:[RNNRootViewController class]]) {
[CATransaction begin];
Expand Down Expand Up @@ -115,10 +114,10 @@ - (void)push:(NSString*)componentId commandId:(NSString*)commandId layout:(NSDic
[self assertReady];

UIViewController *newVc = [_controllerFactory createLayout:layout];
UIViewController *fromVC = [_store findComponentForId:componentId];
UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];

if ([[newVc.resolveOptions.preview.reactTag getWithDefaultValue:@(0)] floatValue] > 0) {
UIViewController* vc = [_store findComponentForId:componentId];
UIViewController* vc = [RNNLayoutManager findComponentForId:componentId];

if([vc isKindOfClass:[RNNRootViewController class]]) {
RNNRootViewController* rootVc = (RNNRootViewController*)vc;
Expand Down Expand Up @@ -177,7 +176,7 @@ - (void)setStackRoot:(NSString*)componentId commandId:(NSString*)commandId child
[viewController renderTreeAndWait:NO perform:nil];
}
RNNNavigationOptions* options = [childViewControllers.lastObject getCurrentChild].resolveOptions;
UIViewController *fromVC = [_store findComponentForId:componentId];
UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
__weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
[_stackManager setStackChildren:childViewControllers fromViewController:fromVC animated:[options.animations.setStackRoot.enable getWithDefaultValue:YES] completion:^{
[weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot commandId:commandId params:@{@"componentId": componentId}];
Expand All @@ -188,7 +187,7 @@ - (void)setStackRoot:(NSString*)componentId commandId:(NSString*)commandId child
- (void)pop:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];

RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
RNNRootViewController *vc = (RNNRootViewController*)[RNNLayoutManager findComponentForId:componentId];
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc overrideOptions:options];

Expand All @@ -214,7 +213,7 @@ - (void)pop:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(

- (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
RNNRootViewController *vc = (RNNRootViewController*)[RNNLayoutManager findComponentForId:componentId];
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc overrideOptions:options];

Expand All @@ -226,7 +225,7 @@ - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions

- (void)popToRoot:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNRootViewController *vc = (RNNRootViewController*)[_store findComponentForId:componentId];
RNNRootViewController *vc = (RNNRootViewController*)[RNNLayoutManager findComponentForId:componentId];
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc overrideOptions:options];

Expand Down Expand Up @@ -261,7 +260,7 @@ - (void)showModal:(NSDictionary*)layout commandId:(NSString *)commandId completi
- (void)dismissModal:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
[self assertReady];

UIViewController *modalToDismiss = (UIViewController *)[_store findComponentForId:componentId];
UIViewController *modalToDismiss = (UIViewController *)[RNNLayoutManager findComponentForId:componentId];

if (!modalToDismiss.isModal) {
[RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
Expand Down Expand Up @@ -310,7 +309,7 @@ - (void)showOverlay:(NSDictionary *)layout commandId:(NSString*)commandId comple

- (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
[self assertReady];
UIViewController* viewController = [_store findComponentForId:componentId];
UIViewController* viewController = [RNNLayoutManager findComponentForId:componentId];
if (viewController) {
[_overlayManager dismissOverlay:viewController];
[_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
Expand All @@ -323,7 +322,7 @@ - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId com
#pragma mark - private

- (void)assertReady {
if (!_store.isReadyToReceiveCommands) {
if (!self.readyToReceiveCommands) {
[[NSException exceptionWithName:@"BridgeNotLoadedError"
reason:@"Bridge not yet loaded! Send commands after Navigation.events().onAppLaunched() has been called."
userInfo:nil]
Expand Down
4 changes: 2 additions & 2 deletions lib/ios/RNNControllerFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RNNRootViewCreator.h"
#import "RNNStore.h"
#import "RNNExternalComponentStore.h"
#import "RNNEventEmitter.h"
#import "RNNReactComponentRegistry.h"
#import "RNNNavigationOptions.h"
Expand All @@ -11,7 +11,7 @@

-(instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
eventEmitter:(RNNEventEmitter*)eventEmitter
store:(RNNStore *)store
store:(RNNExternalComponentStore *)store
componentRegistry:(RNNReactComponentRegistry *)componentRegistry
andBridge:(RCTBridge*)bridge;

Expand Down
6 changes: 2 additions & 4 deletions lib/ios/RNNControllerFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@implementation RNNControllerFactory {
id<RNNRootViewCreator> _creator;
RNNStore *_store;
RNNExternalComponentStore *_store;
RCTBridge *_bridge;
RNNReactComponentRegistry* _componentRegistry;
}
Expand All @@ -28,7 +28,7 @@ @implementation RNNControllerFactory {

- (instancetype)initWithRootViewCreator:(id <RNNRootViewCreator>)creator
eventEmitter:(RNNEventEmitter*)eventEmitter
store:(RNNStore *)store
store:(RNNExternalComponentStore *)store
componentRegistry:(RNNReactComponentRegistry *)componentRegistry
andBridge:(RCTBridge *)bridge {

Expand Down Expand Up @@ -107,8 +107,6 @@ - (UIViewController *)fromTree:(NSDictionary*)json {
@throw [NSException exceptionWithName:@"UnknownControllerType" reason:[@"Unknown controller type " stringByAppendingString:node.type] userInfo:nil];
}

result.store = _store;

return result;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/ios/RNNExternalComponentStore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "ReactNativeNavigation.h"
#import "RNNLayoutInfo.h"

@interface RNNExternalComponentStore : NSObject

- (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback;
- (UIViewController *)getExternalComponent:(RNNLayoutInfo *)layoutInfo bridge:(RCTBridge *)bridge;

@end
26 changes: 26 additions & 0 deletions lib/ios/RNNExternalComponentStore.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import "RNNExternalComponentStore.h"

@interface RNNExternalComponentStore ()

@end

@implementation RNNExternalComponentStore {
NSMutableDictionary* _externalComponentCreators;
}

-(instancetype)init {
self = [super init];
_externalComponentCreators = [NSMutableDictionary new];
return self;
}

- (void)registerExternalComponent:(NSString *)name callback:(RNNExternalViewCreator)callback {
[_externalComponentCreators setObject:[callback copy] forKey:name];
}

- (UIViewController *)getExternalComponent:(RNNLayoutInfo *)layoutInfo bridge:(RCTBridge *)bridge {
RNNExternalViewCreator creator = [_externalComponentCreators objectForKey:layoutInfo.name];
return creator(layoutInfo.props, bridge);
}

@end
9 changes: 9 additions & 0 deletions lib/ios/RNNLayoutManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface RNNLayoutManager : NSObject

+ (UIViewController *)findComponentForId:(NSString *)componentId;

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

#import "RNNLayoutManager.h"
#import "RNNLayoutProtocol.h"
#import "UIViewController+LayoutProtocol.h"

@implementation RNNLayoutManager

+ (UIViewController *)findComponentForId:(NSString *)componentId {
for (UIWindow* window in UIApplication.sharedApplication.windows) {
UIViewController* result = [self findChildComponentForParent:window.rootViewController ForId:componentId];
if (result) {
return result;
}
}

return nil;
}

+ (UIViewController *)findChildComponentForParent:(UIViewController *)parentViewController ForId:(NSString *)componentId {
if ([parentViewController.layoutInfo.componentId isEqualToString:componentId]) {
return parentViewController;
}

if (parentViewController.presentedViewController) {
if ([parentViewController.presentedViewController.layoutInfo.componentId isEqualToString:componentId]) {
return parentViewController.presentedViewController;
}

UIViewController* modalResult = [self findChildComponentForParent:parentViewController.presentedViewController ForId:componentId];
if (modalResult) {
return modalResult;
}

}

for (UIViewController* childVC in parentViewController.childViewControllers) {
UIViewController* result = [self findChildComponentForParent:childVC ForId:componentId];
if (result) {
return result;
}
}

return nil;
}


@end
5 changes: 4 additions & 1 deletion lib/ios/RNNModalManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RNNStore.h"

typedef void (^RNNTransitionCompletionBlock)(void);
typedef void (^RNNTransitionWithComponentIdCompletionBlock)(NSString *componentId);
typedef void (^RNNTransitionRejectionBlock)(NSString *code, NSString *message, NSError *error);

@protocol RNNModalManagerDelegate <NSObject>

Expand Down
1 change: 0 additions & 1 deletion lib/ios/RNNOverlayManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "RNNStore.h"
#import "RNNOverlayWindow.h"

@interface RNNOverlayManager : NSObject
Expand Down
1 change: 0 additions & 1 deletion lib/ios/RNNReactComponentRegistry.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#import <Foundation/Foundation.h>
#import "RNNReactView.h"
#import "RNNComponentOptions.h"
#import "RNNStore.h"
#import "RNNRootViewCreator.h"

@interface RNNReactComponentRegistry : NSObject
Expand Down
Loading

0 comments on commit 275304c

Please sign in to comment.