1// Copyright 2015-present 650 Industries. All rights reserved. 2 3@import UIKit; 4 5#import "EXAppDelegate.h" 6#import "EXAppViewController.h" 7#import "EXButtonView.h" 8#import "EXHomeAppManager.h" 9#import "EXHomeDiagnosticsViewController.h" 10#import "EXKernel.h" 11#import "EXAppLoader.h" 12#import "EXKernelAppRecord.h" 13#import "EXKernelAppRegistry.h" 14#import "EXKernelDevKeyCommands.h" 15#import "EXKernelLinkingManager.h" 16#import "EXKernelServiceRegistry.h" 17#import "EXMenuGestureRecognizer.h" 18#import "EXMenuViewController.h" 19#import "EXRootViewController.h" 20#import "EXMenuWindow.h" 21 22NSString * const kEXHomeDisableNuxDefaultsKey = @"EXKernelDisableNuxDefaultsKey"; 23NSString * const kEXHomeIsNuxFinishedDefaultsKey = @"EXHomeIsNuxFinishedDefaultsKey"; 24 25NS_ASSUME_NONNULL_BEGIN 26 27@interface EXRootViewController () <EXAppBrowserController> 28 29@property (nonatomic, strong) EXMenuViewController *menuViewController; 30@property (nonatomic, assign) BOOL isMenuVisible; 31@property (nonatomic, assign) BOOL isAnimatingMenu; 32@property (nonatomic, assign) BOOL isAnimatingAppTransition; 33@property (nonatomic, strong) EXButtonView *btnMenu; 34@property (nonatomic, strong) EXMenuWindow *menuWindow; 35 36@end 37 38@implementation EXRootViewController 39 40- (instancetype)init 41{ 42 if (self = [super init]) { 43 [EXKernel sharedInstance].browserController = self; 44 [[NSNotificationCenter defaultCenter] addObserver:self 45 selector:@selector(_updateMenuButtonBehavior) 46 name:kEXKernelDidChangeMenuBehaviorNotification 47 object:nil]; 48 [self _maybeResetNuxState]; 49 } 50 return self; 51} 52 53- (void)viewDidLoad 54{ 55 [super viewDidLoad]; 56 _btnMenu = [[EXButtonView alloc] init]; 57 _btnMenu.hidden = YES; 58 [self.view addSubview:_btnMenu]; 59 EXMenuGestureRecognizer *menuGestureRecognizer = [[EXMenuGestureRecognizer alloc] initWithTarget:self action:@selector(_onMenuGestureRecognized:)]; 60 [((EXAppDelegate *)[UIApplication sharedApplication].delegate).window addGestureRecognizer:menuGestureRecognizer]; 61} 62 63- (void)viewWillLayoutSubviews 64{ 65 [super viewWillLayoutSubviews]; 66 _btnMenu.frame = CGRectMake(0, 0, 48.0f, 48.0f); 67 _btnMenu.center = CGPointMake(self.view.frame.size.width - 36.0f, self.view.frame.size.height - 72.0f); 68 [self.view bringSubviewToFront:_btnMenu]; 69} 70 71#pragma mark - EXViewController 72 73- (void)createRootAppAndMakeVisible 74{ 75 EXHomeAppManager *homeAppManager = [[EXHomeAppManager alloc] init]; 76 EXAppLoader *homeAppLoader = [[EXAppLoader alloc] initWithLocalManifest:[EXHomeAppManager bundledHomeManifest]]; 77 EXKernelAppRecord *homeAppRecord = [[EXKernelAppRecord alloc] initWithAppLoader:homeAppLoader appManager:homeAppManager]; 78 [[EXKernel sharedInstance].appRegistry registerHomeAppRecord:homeAppRecord]; 79 [self moveAppToVisible:homeAppRecord]; 80} 81 82#pragma mark - EXAppBrowserController 83 84- (void)moveAppToVisible:(EXKernelAppRecord *)appRecord 85{ 86 [self _foregroundAppRecord:appRecord]; 87} 88 89- (void)toggleMenuWithCompletion:(void (^ _Nullable)(void))completion 90{ 91 [self setIsMenuVisible:!_isMenuVisible completion:completion]; 92} 93 94- (void)setIsMenuVisible:(BOOL)isMenuVisible completion:(void (^ _Nullable)(void))completion 95{ 96 if (!_menuViewController) { 97 _menuViewController = [[EXMenuViewController alloc] init]; 98 } 99 if (isMenuVisible != _isMenuVisible) { 100 if (!_isAnimatingMenu) { 101 _isMenuVisible = isMenuVisible; 102 [self _animateMenuToVisible:_isMenuVisible completion:completion]; 103 } 104 } else { 105 completion(); 106 } 107} 108 109- (void)showDiagnostics 110{ 111 __weak typeof(self) weakSelf = self; 112 [self setIsMenuVisible:NO completion:^{ 113 __strong typeof(weakSelf) strongSelf = weakSelf; 114 if (strongSelf) { 115 EXHomeDiagnosticsViewController *vcDiagnostics = [[EXHomeDiagnosticsViewController alloc] init]; 116 [strongSelf presentViewController:vcDiagnostics animated:NO completion:nil]; 117 } 118 }]; 119} 120 121- (void)showQRReader 122{ 123 [self moveHomeToVisible]; 124 [[self _getHomeAppManager] showQRReader]; 125} 126 127- (void)moveHomeToVisible 128{ 129 __weak typeof(self) weakSelf = self; 130 [self setIsMenuVisible:NO completion:^{ 131 __strong typeof(weakSelf) strongSelf = weakSelf; 132 if (strongSelf) { 133 [strongSelf moveAppToVisible:[EXKernel sharedInstance].appRegistry.homeAppRecord]; 134 135 if (strongSelf.isMenuVisible) { 136 [strongSelf setIsMenuVisible:NO completion:nil]; 137 } 138 } 139 }]; 140} 141 142- (void)refreshVisibleApp 143{ 144 // this is different from Util.reload() 145 // because it can work even on an errored app record (e.g. with no manifest, or with no running bridge). 146 [self setIsMenuVisible:NO completion:nil]; 147 EXKernelAppRecord *visibleApp = [EXKernel sharedInstance].visibleApp; 148 [[EXKernel sharedInstance] logAnalyticsEvent:@"RELOAD_EXPERIENCE" forAppRecord:visibleApp]; 149 NSURL *urlToRefresh = visibleApp.appLoader.manifestUrl; 150 [[EXKernel sharedInstance] createNewAppWithUrl:urlToRefresh initialProps:nil]; 151} 152 153- (void)addHistoryItemWithUrl:(NSURL *)manifestUrl manifest:(NSDictionary *)manifest 154{ 155 [[self _getHomeAppManager] addHistoryItemWithUrl:manifestUrl manifest:manifest]; 156} 157 158- (void)getIsValidHomeManifestToOpen:(NSDictionary *)manifest completion:(void (^)(BOOL isValid))completion 159{ 160 [[self _getHomeAppManager] getIsValidHomeManifestToOpen:manifest completion:completion]; 161} 162 163- (void)getHistoryUrlForExperienceId:(NSString *)experienceId completion:(void (^)(NSString *))completion 164{ 165 return [[self _getHomeAppManager] getHistoryUrlForExperienceId:experienceId completion:completion]; 166} 167 168- (void)setIsNuxFinished:(BOOL)isFinished 169{ 170 [[NSUserDefaults standardUserDefaults] setBool:isFinished forKey:kEXHomeIsNuxFinishedDefaultsKey]; 171 [[NSUserDefaults standardUserDefaults] synchronize]; 172} 173 174- (BOOL)isNuxFinished 175{ 176 return [[NSUserDefaults standardUserDefaults] boolForKey:kEXHomeIsNuxFinishedDefaultsKey]; 177} 178 179- (void)appDidFinishLoadingSuccessfully:(EXKernelAppRecord *)appRecord 180{ 181 // show nux if needed 182 if (!self.isNuxFinished 183 && appRecord == [EXKernel sharedInstance].visibleApp 184 && appRecord != [EXKernel sharedInstance].appRegistry.homeAppRecord 185 && !self.isMenuVisible) { 186 [self setIsMenuVisible:YES completion:nil]; 187 } 188 189 // check button availability when any new app loads 190 [self _updateMenuButtonBehavior]; 191} 192 193#pragma mark - internal 194 195- (void)_foregroundAppRecord:(EXKernelAppRecord *)appRecord 196{ 197 if (_isAnimatingAppTransition) { 198 return; 199 } 200 EXAppViewController *viewControllerToShow = appRecord.viewController; 201 EXAppViewController *viewControllerToHide; 202 if (viewControllerToShow != self.contentViewController) { 203 _isAnimatingAppTransition = YES; 204 if (self.contentViewController) { 205 viewControllerToHide = (EXAppViewController *)self.contentViewController; 206 } 207 if (viewControllerToShow) { 208 [viewControllerToShow willMoveToParentViewController:self]; 209 [self.view addSubview:viewControllerToShow.view]; 210 } 211 212 __weak typeof(self) weakSelf = self; 213 void (^transitionFinished)(void) = ^{ 214 __strong typeof(weakSelf) strongSelf = weakSelf; 215 if (strongSelf) { 216 if (viewControllerToHide) { 217 [viewControllerToHide willMoveToParentViewController:nil]; 218 [viewControllerToHide.view removeFromSuperview]; 219 [viewControllerToHide didMoveToParentViewController:nil]; 220 221 // dismisses all modals that are presented by the app 222 [viewControllerToHide dismissViewControllerAnimated:NO completion:nil]; 223 } 224 if (viewControllerToShow) { 225 [viewControllerToShow didMoveToParentViewController:strongSelf]; 226 strongSelf.contentViewController = viewControllerToShow; 227 } 228 [strongSelf.view setNeedsLayout]; 229 strongSelf.isAnimatingAppTransition = NO; 230 if (strongSelf.delegate) { 231 [strongSelf.delegate viewController:strongSelf didNavigateAppToVisible:appRecord]; 232 } 233 } 234 }; 235 236 BOOL animated = (viewControllerToHide && viewControllerToShow); 237 if (animated) { 238 if (viewControllerToHide.contentView) { 239 viewControllerToHide.contentView.transform = CGAffineTransformIdentity; 240 viewControllerToHide.contentView.alpha = 1.0f; 241 } 242 if (viewControllerToShow.contentView) { 243 viewControllerToShow.contentView.transform = CGAffineTransformMakeScale(1.1f, 1.1f); 244 viewControllerToShow.contentView.alpha = 0; 245 } 246 [UIView animateWithDuration:0.3f animations:^{ 247 if (viewControllerToHide.contentView) { 248 viewControllerToHide.contentView.transform = CGAffineTransformMakeScale(0.95f, 0.95f); 249 viewControllerToHide.contentView.alpha = 0.5f; 250 } 251 if (viewControllerToShow.contentView) { 252 viewControllerToShow.contentView.transform = CGAffineTransformIdentity; 253 viewControllerToShow.contentView.alpha = 1.0f; 254 } 255 } completion:^(BOOL finished) { 256 transitionFinished(); 257 }]; 258 } else { 259 transitionFinished(); 260 } 261 } 262} 263 264- (void)_animateMenuToVisible:(BOOL)visible completion:(void (^ _Nullable)(void))completion 265{ 266 _isAnimatingMenu = YES; 267 __weak typeof(self) weakSelf = self; 268 if (visible) { 269 [_menuViewController willMoveToParentViewController:self]; 270 271 if (_menuWindow == nil) { 272 _menuWindow = [[EXMenuWindow alloc] init]; 273 } 274 275 [_menuWindow setFrame:self.view.frame]; 276 [_menuWindow addSubview:_menuViewController.view]; 277 [_menuWindow makeKeyAndVisible]; 278 279 _menuViewController.view.alpha = 0.0f; 280 _menuViewController.view.transform = CGAffineTransformMakeScale(1.1f, 1.1f); 281 [UIView animateWithDuration:0.1f animations:^{ 282 _menuViewController.view.alpha = 1.0f; 283 _menuViewController.view.transform = CGAffineTransformIdentity; 284 } completion:^(BOOL finished) { 285 __strong typeof(weakSelf) strongSelf = weakSelf; 286 if (strongSelf) { 287 strongSelf.isAnimatingMenu = NO; 288 [strongSelf.menuViewController didMoveToParentViewController:self]; 289 if (completion) { 290 completion(); 291 } 292 } 293 }]; 294 } else { 295 _menuViewController.view.alpha = 1.0f; 296 [UIView animateWithDuration:0.1f animations:^{ 297 _menuViewController.view.alpha = 0.0f; 298 } completion:^(BOOL finished) { 299 __strong typeof(weakSelf) strongSelf = weakSelf; 300 if (strongSelf) { 301 strongSelf.isAnimatingMenu = NO; 302 [strongSelf.menuViewController willMoveToParentViewController:nil]; 303 [strongSelf.menuViewController.view removeFromSuperview]; 304 [strongSelf.menuViewController didMoveToParentViewController:nil]; 305 _menuWindow = nil; 306 if (completion) { 307 completion(); 308 } 309 } 310 }]; 311 } 312} 313 314- (EXHomeAppManager *)_getHomeAppManager 315{ 316 return (EXHomeAppManager *)[EXKernel sharedInstance].appRegistry.homeAppRecord.appManager; 317} 318 319- (void)_maybeResetNuxState 320{ 321 // used by appetize: optionally disable nux 322 BOOL disableNuxDefaultsValue = [[NSUserDefaults standardUserDefaults] boolForKey:kEXHomeDisableNuxDefaultsKey]; 323 if (disableNuxDefaultsValue) { 324 [self setIsNuxFinished:YES]; 325 [[NSUserDefaults standardUserDefaults] removeObjectForKey:kEXHomeDisableNuxDefaultsKey]; 326 } 327} 328 329- (void)_updateMenuButtonBehavior 330{ 331 BOOL shouldShowButton = [[EXKernelDevKeyCommands sharedInstance] isLegacyMenuButtonAvailable]; 332 dispatch_async(dispatch_get_main_queue(), ^{ 333 _btnMenu.hidden = !shouldShowButton; 334 }); 335} 336 337- (void)_onMenuGestureRecognized:(EXMenuGestureRecognizer *)sender 338{ 339 if (sender.state == UIGestureRecognizerStateEnded) { 340 [[EXKernel sharedInstance] switchTasks]; 341 } 342} 343 344@end 345 346NS_ASSUME_NONNULL_END 347