1// Copyright 2015-present 650 Industries. All rights reserved. 2 3#import "EXReactAppManager.h" 4#import "EXAppLoader.h" 5#import "EXKernelAppRecord.h" 6#import "EXAppViewController.h" 7 8#import <React/RCTUtils.h> 9 10NSString *kEXKernelBridgeDidForegroundNotification = @"EXKernelBridgeDidForegroundNotification"; 11NSString *kEXKernelBridgeDidBackgroundNotification = @"EXKernelBridgeDidBackgroundNotification"; 12 13@implementation EXKernelAppRecord 14 15- (instancetype)initWithManifestUrl:(NSURL *)manifestUrl initialProps:(NSDictionary *)initialProps 16{ 17 if (self = [super init]) { 18 _appManager = [[EXReactAppManager alloc] initWithAppRecord:self initialProps:initialProps]; 19 _appLoader = [[EXAppLoader alloc] initWithManifestUrl:manifestUrl]; 20 _viewController = [[EXAppViewController alloc] initWithAppRecord:self]; 21 _timeCreated = [NSDate date]; 22 } 23 return self; 24} 25 26- (instancetype)initWithAppLoader:(EXAppLoader *)customAppLoader appManager:(EXReactAppManager *)customAppManager 27{ 28 if (self = [super init]) { 29 _appManager = customAppManager; 30 _appManager.appRecord = self; 31 _appLoader = customAppLoader; 32 _viewController = [[EXAppViewController alloc] initWithAppRecord:self]; 33 _timeCreated = [NSDate date]; 34 } 35 return self; 36} 37 38- (EXKernelAppRecordStatus)status 39{ 40 if (_appLoader.status == kEXAppLoaderStatusError) { 41 return kEXKernelAppRecordStatusError; 42 } 43 if (_appManager && _appManager.status == kEXReactAppManagerStatusError && _appLoader.status == kEXAppLoaderStatusHasManifestAndBundle) { 44 return kEXKernelAppRecordStatusError; 45 } 46 if (_appManager && _appManager.isBridgeRunning) { 47 return kEXKernelAppRecordStatusRunning; 48 } 49 if (_appLoader.status == kEXAppLoaderStatusHasManifestAndBundle) { 50 return kEXKernelAppRecordStatusBridgeLoading; 51 } 52 if (_appLoader.status != kEXAppLoaderStatusNew) { 53 return kEXKernelAppRecordStatusDownloading; 54 } 55 return kEXKernelAppRecordStatusNew; 56} 57 58- (NSString * _Nullable)experienceId 59{ 60 if (self.appLoader && self.appLoader.manifest) { 61 id experienceIdJsonValue = self.appLoader.manifest[@"id"]; 62 if (experienceIdJsonValue) { 63 RCTAssert([experienceIdJsonValue isKindOfClass:[NSString class]], @"Manifest contains an id which is not a string: %@", experienceIdJsonValue); 64 return experienceIdJsonValue; 65 } 66 } 67 return nil; 68} 69 70- (NSString *)description 71{ 72 return [NSString stringWithFormat:@"EXKernelAppRecord %p:\n url: %@\n experience id: %@", 73 self, 74 self.appLoader.manifestUrl, 75 (self.experienceId) ? self.experienceId : @"(none)"]; 76} 77 78@end 79 80