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
10@import EXManifests;
11
12NSString *kEXKernelBridgeDidForegroundNotification = @"EXKernelBridgeDidForegroundNotification";
13NSString *kEXKernelBridgeDidBackgroundNotification = @"EXKernelBridgeDidBackgroundNotification";
14
15@implementation EXKernelAppRecord
16
17- (instancetype)initWithManifestUrl:(NSURL *)manifestUrl initialProps:(NSDictionary *)initialProps
18{
19  if (self = [super init]) {
20    _appManager = [[EXReactAppManager alloc] initWithAppRecord:self initialProps:initialProps];
21    _appLoader = [[EXAppLoaderExpoUpdates alloc] initWithManifestUrl:manifestUrl];
22    _viewController = [[EXAppViewController alloc] initWithAppRecord:self];
23    _timeCreated = [NSDate date];
24  }
25  return self;
26}
27
28- (instancetype)initWithAppLoader:(EXAbstractLoader *)customAppLoader appManager:(EXReactAppManager *)customAppManager
29{
30  if (self = [super init]) {
31    _appManager = customAppManager;
32    _appManager.appRecord = self;
33    _appLoader = customAppLoader;
34    _viewController = [[EXAppViewController alloc] initWithAppRecord:self];
35    _timeCreated = [NSDate date];
36  }
37  return self;
38}
39
40- (EXKernelAppRecordStatus)status
41{
42  if (_appLoader.status == kEXAppLoaderStatusError) {
43    return kEXKernelAppRecordStatusError;
44  }
45  if (_appManager && _appManager.status == kEXReactAppManagerStatusError && _appLoader.status == kEXAppLoaderStatusHasManifestAndBundle) {
46    return kEXKernelAppRecordStatusError;
47  }
48  if (_appManager && _appManager.isBridgeRunning) {
49    return kEXKernelAppRecordStatusRunning;
50  }
51  if (_appLoader.status == kEXAppLoaderStatusHasManifestAndBundle) {
52    return kEXKernelAppRecordStatusBridgeLoading;
53  }
54  if (_appLoader.status != kEXAppLoaderStatusNew) {
55    return kEXKernelAppRecordStatusDownloading;
56  }
57  return kEXKernelAppRecordStatusNew;
58}
59
60- (NSString * _Nullable)scopeKey
61{
62  if (self.appLoader && self.appLoader.manifest) {
63    return self.appLoader.manifest.scopeKey;
64  }
65  return nil;
66}
67
68- (NSString *)description
69{
70  return [NSString stringWithFormat:@"EXKernelAppRecord %p:\n  url: %@\n  experience scope key: %@",
71          self,
72          self.appLoader.manifestUrl,
73          self.scopeKey ?: @"(none)"];
74}
75
76@end
77
78