1// Copyright 2015-present 650 Industries. All rights reserved.
2
3#import "EXReactAppManager.h"
4#import "EXAppLoaderExpoUpdates.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 = [[EXAppLoaderExpoUpdates 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    return self.appLoader.manifest.rawID;
62  }
63  return nil;
64}
65
66- (NSString *)description
67{
68  return [NSString stringWithFormat:@"EXKernelAppRecord %p:\n  url: %@\n  experience id: %@",
69          self,
70          self.appLoader.manifestUrl,
71          (self.experienceId) ? self.experienceId : @"(none)"];
72}
73
74@end
75
76