forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNNComponentViewController.m
171 lines (139 loc) · 6.27 KB
/
RNNComponentViewController.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
#import "RNNComponentViewController.h"
#import "RNNAnimationsTransitionDelegate.h"
#import "UIViewController+LayoutProtocol.h"
@implementation RNNComponentViewController
@synthesize previewCallback;
- (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNComponentViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNComponentPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
self = [super initWithLayoutInfo:layoutInfo creator:creator options:options defaultOptions:defaultOptions presenter:presenter eventEmitter:eventEmitter childViewControllers:nil];
self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.resolveOptions.customTransition];
return self;
}
- (void)setDefaultOptions:(RNNNavigationOptions *)defaultOptions {
_defaultOptions = defaultOptions;
[_presenter setDefaultOptions:defaultOptions];
}
- (void)overrideOptions:(RNNNavigationOptions *)options {
[self.options overrideOptions:options];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_presenter applyOptions:self.resolveOptions];
[self.parentViewController onChildWillAppear];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self componentDidAppear];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self componentDidDisappear];
}
- (void)loadView {
[self renderReactViewIfNeeded];
}
- (void)render {
if (!self.waitForRender)
[self readyForPresentation];
[self renderReactViewIfNeeded];
}
- (void)renderReactViewIfNeeded {
if (!self.isViewLoaded) {
self.view = [self.creator createRootView:self.layoutInfo.name
rootViewId:self.layoutInfo.componentId
ofType:RNNComponentTypeComponent
reactViewReadyBlock:^{
[self->_presenter renderComponents:self.resolveOptions perform:^{
[self readyForPresentation];
}];
}];
} else {
[self readyForPresentation];
}
}
- (UIViewController *)getCurrentChild {
return nil;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
[self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
text:searchController.searchBar.text
isFocused:searchController.searchBar.isFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
}
-(BOOL)isCustomTransitioned {
return self.resolveOptions.customTransition.animations != nil;
}
- (BOOL)prefersStatusBarHidden {
return [_presenter isStatusBarVisibility:self.navigationController resolvedOptions:self.resolveOptions];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return [_presenter getStatusBarStyle:[self resolveOptions]];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
RNNComponentViewController* vc = (RNNComponentViewController*)viewController;
if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
navigationController.delegate = nil;
}
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController*)fromVC
toViewController:(UIViewController*)toVC {
if (self.animator) {
return self.animator;
} else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
} else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
} else {
return nil;
}
return nil;
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
return self.previewController;
}
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
if (self.previewCallback) {
self.previewCallback(self);
}
}
- (void)onActionPress:(NSString *)id {
[_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
}
- (UIPreviewAction *) convertAction:(NSDictionary *)action {
NSString *actionId = action[@"id"];
NSString *actionTitle = action[@"title"];
UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
if ([action[@"style"] isEqualToString:@"selected"]) {
actionStyle = UIPreviewActionStyleSelected;
} else if ([action[@"style"] isEqualToString:@"destructive"]) {
actionStyle = UIPreviewActionStyleDestructive;
}
return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
[self onActionPress:actionId];
}];
}
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
NSMutableArray *actions = [[NSMutableArray alloc] init];
for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
UIPreviewAction *action = [self convertAction:previewAction];
NSDictionary *actionActions = previewAction[@"actions"];
if (actionActions.count > 0) {
NSMutableArray *group = [[NSMutableArray alloc] init];
for (NSDictionary *previewGroupAction in actionActions) {
[group addObject:[self convertAction:previewGroupAction]];
}
UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
[actions addObject:actionGroup];
} else {
[actions addObject:action];
}
}
return actions;
}
-(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
[self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
}
@end