From 652e5b444fcafcf67d4d83b63b6578e245a487be Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 21 Feb 2018 14:40:58 +0100 Subject: [PATCH] implement isInitialized and isMounted --- ARKit.js | 13 +++++++------ ios/RCTARKit.h | 4 ++-- ios/RCTARKit.m | 15 ++++++++++++++- ios/RCTARKitManager.m | 13 +++++++++++++ startup.js | 7 ++++++- 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/ARKit.js b/ARKit.js index e9c8e85..7854b32 100644 --- a/ARKit.js +++ b/ARKit.js @@ -159,13 +159,12 @@ Object.keys(ARKitManager).forEach(key => { ARKit[key] = ARKitManager[key]; }); -const addDefaultsToSnapShotFunc = funcName => ({ - target = 'cameraRoll', - format = 'png' -} = {}) => ARKitManager[funcName]({ target, format }); +const addDefaultsToSnapShotFunc = funcName => ( + { target = 'cameraRoll', format = 'png' } = {}, +) => ARKitManager[funcName]({ target, format }); -ARKit.snapshot = addDefaultsToSnapShotFunc("snapshot"); -ARKit.snapshotCamera = addDefaultsToSnapShotFunc("snapshotCamera"); +ARKit.snapshot = addDefaultsToSnapShotFunc('snapshot'); +ARKit.snapshotCamera = addDefaultsToSnapShotFunc('snapshotCamera'); ARKit.exportModel = presetId => { const id = presetId || generateId(); @@ -197,6 +196,8 @@ ARKit.propTypes = { onTapOnPlaneUsingExtent: PropTypes.func, onTapOnPlaneNoExtent: PropTypes.func, onEvent: PropTypes.func, + isMounted: PropTypes.func, + isInitialized: PropTypes.func, }; const RCTARKit = requireNativeComponent('RCTARKit', ARKit); diff --git a/ios/RCTARKit.h b/ios/RCTARKit.h index 6d418a9..b6b97d7 100644 --- a/ios/RCTARKit.h +++ b/ios/RCTARKit.h @@ -21,6 +21,7 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error @interface RCTARKit : UIView + (instancetype)sharedInstance; ++ (bool)isInitialized; - (instancetype)initWithARView:(ARSCNView *)arView; @@ -72,8 +73,7 @@ typedef void (^RCTARKitReject)(NSString *code, NSString *message, NSError *error - (NSDictionary *)readCamera; - (NSDictionary* )getCurrentLightEstimation; - (NSArray * )getCurrentDetectedFeaturePoints; - - +- (bool)isMounted; #pragma mark - Delegates - (void)renderer:(id )renderer didRenderScene:(SCNScene *)scene atTime:(NSTimeInterval)time; diff --git a/ios/RCTARKit.m b/ios/RCTARKit.m index 5577297..bb7a099 100644 --- a/ios/RCTARKit.m +++ b/ios/RCTARKit.m @@ -36,9 +36,14 @@ void dispatch_once_on_main_thread(dispatch_once_t *predicate, @implementation RCTARKit +static RCTARKit *instance = nil; + ++ (bool)isInitialized { + return instance !=nil; +} + (instancetype)sharedInstance { - static RCTARKit *instance = nil; + static dispatch_once_t onceToken; dispatch_once_on_main_thread(&onceToken, ^{ @@ -47,9 +52,15 @@ + (instancetype)sharedInstance { instance = [[self alloc] initWithARView:arView]; } }); + return instance; } +- (bool)isMounted { + + return self.superview != nil; +} + - (instancetype)initWithARView:(ARSCNView *)arView { if ((self = [super init])) { self.arView = arView; @@ -85,6 +96,8 @@ - (instancetype)initWithARView:(ARSCNView *)arView { return self; } + + - (void)layoutSubviews { [super layoutSubviews]; //NSLog(@"setting view bounds %@", NSStringFromCGRect(self.bounds)); diff --git a/ios/RCTARKitManager.m b/ios/RCTARKitManager.m index a5ff7d8..ba501a2 100644 --- a/ios/RCTARKitManager.m +++ b/ios/RCTARKitManager.m @@ -139,6 +139,19 @@ - (NSDictionary *)constantsToExport resolve(@{}); } +RCT_EXPORT_METHOD(isInitialized:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { + resolve(@([ARKit isInitialized])); +} + +RCT_EXPORT_METHOD(isMounted:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) { + if( [ARKit isInitialized]) { + dispatch_async(dispatch_get_main_queue(), ^{ + resolve(@([[ARKit sharedInstance] isMounted])); + }); + } else { + resolve(@(NO)); + } +} RCT_EXPORT_METHOD( hitTestPlanes: (NSDictionary *)pointDict diff --git a/startup.js b/startup.js index 4899c34..e1f0eba 100644 --- a/startup.js +++ b/startup.js @@ -6,5 +6,10 @@ export default () => { // when reloading the app, the scene should be cleared. // on prod, this usually does not happen, but you can reload the app in develop mode // without clearing, this would result in inconsistency - ARKitManager.clearScene(); + ARKitManager.isInitialized().then(isInitialized => { + console.log('was already initialized on startup', isInitialized); + if (isInitialized) { + ARKitManager.clearScene(); + } + }); };