Skip to content

Commit

Permalink
fix: fix isARSupportedOnDevice
Browse files Browse the repository at this point in the history
The React code wasn't working and wasn't returning a value properly.
This commit wraps the code in a promise so it can be used
asynchronously.
  • Loading branch information
robertjcolley committed Feb 10, 2024
1 parent eac9454 commit 220847d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 61 deletions.
77 changes: 48 additions & 29 deletions components/Utilities/ViroUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,54 @@ export function polarToCartesianActual(polarcoords: number[]) {
}

import { Platform, NativeModules } from "react-native";
export function isARSupportedOnDevice(
notSupportedCallback: (result?: string | Error) => void,
supportedCallback: (result?: string | Error) => void
) {
if (Platform.OS == "ios") {
NativeModules.VRTARUtils.isARSupported((error: Error, result: any) => {
if (result.isARSupported == true) {
{
supportedCallback();
}
} else {
{
notSupportedCallback();

export interface ViroiOSArSupportResponse {
isARSupported: boolean;
}

export type ViroAndroidArSupportResponse =
/**
* The device is <b>supported</b> by ARCore.
*/
| "SUPPORTED"
/**
* The device is <b>unsupported</b> by ARCore.
*/
| "UNSUPPORTED"
/**
* ARCore support is <b>unknown</b> for this device.
*/
| "UNKNOWN"
/**
* ARCore is still checking for support. This is a temporary state, and the application should
* check again soon.
*/
| "TRANSIENT";

export interface ViroARSupportResponse {
isARSupported: boolean;
}

export function isARSupportedOnDevice() {
return new Promise<ViroARSupportResponse>((resolve, reject) => {
if (Platform.OS == "ios") {
NativeModules.VRTARUtils.isARSupported(
(error: Error, result: ViroiOSArSupportResponse) => {
console.log("[isARSupportedOnDevice]: iOS", { error, result });
if (error) reject(error);
if (result) resolve(result);
reject("AR Support Unknown.");
}
}
});
} else {
NativeModules.VRTARSceneNavigatorModule.isARSupportedOnDevice(
(result: any) => {
if (result == "SUPPORTED") {
{
supportedCallback();
}
} else {
{
notSupportedCallback(result);
}
);
} else {
NativeModules.VRTARSceneNavigatorModule.isARSupportedOnDevice(
(result: ViroAndroidArSupportResponse) => {
console.log("[isARSupportedOnDevice]: Android", { result });
if (result == "SUPPORTED") resolve({ isARSupported: true });
if (result) reject(new Error(result));
reject("AR Support Unknown.");
}
}
);
}
);
}
});
}
26 changes: 25 additions & 1 deletion dist/components/Utilities/ViroUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,28 @@ export declare function polarToCartesian(polarcoords: number[]): number[];
* phi - the yz-plane angle starting from y = 0 degrees
*/
export declare function polarToCartesianActual(polarcoords: number[]): number[];
export declare function isARSupportedOnDevice(notSupportedCallback: (result?: string | Error) => void, supportedCallback: (result?: string | Error) => void): void;
export interface ViroiOSArSupportResponse {
isARSupported: boolean;
}
export type ViroAndroidArSupportResponse =
/**
* The device is <b>supported</b> by ARCore.
*/
"SUPPORTED"
/**
* The device is <b>unsupported</b> by ARCore.
*/
| "UNSUPPORTED"
/**
* ARCore support is <b>unknown</b> for this device.
*/
| "UNKNOWN"
/**
* ARCore is still checking for support. This is a temporary state, and the application should
* check again soon.
*/
| "TRANSIENT";
export interface ViroARSupportResponse {
isARSupported: boolean;
}
export declare function isARSupportedOnDevice(): Promise<ViroARSupportResponse>;
52 changes: 23 additions & 29 deletions dist/components/Utilities/ViroUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,28 @@ function polarToCartesianActual(polarcoords) {
}
exports.polarToCartesianActual = polarToCartesianActual;
const react_native_1 = require("react-native");
function isARSupportedOnDevice(notSupportedCallback, supportedCallback) {
if (react_native_1.Platform.OS == "ios") {
react_native_1.NativeModules.VRTARUtils.isARSupported((error, result) => {
if (result.isARSupported == true) {
{
supportedCallback();
}
}
else {
{
notSupportedCallback();
}
}
});
}
else {
react_native_1.NativeModules.VRTARSceneNavigatorModule.isARSupportedOnDevice((result) => {
if (result == "SUPPORTED") {
{
supportedCallback();
}
}
else {
{
notSupportedCallback(result);
}
}
});
}
function isARSupportedOnDevice() {
return new Promise((resolve, reject) => {
if (react_native_1.Platform.OS == "ios") {
react_native_1.NativeModules.VRTARUtils.isARSupported((error, result) => {
console.log("[isARSupportedOnDevice]: iOS", { error, result });
if (error)
reject(error);
if (result)
resolve(result);
reject("AR Support Unknown.");
});
}
else {
react_native_1.NativeModules.VRTARSceneNavigatorModule.isARSupportedOnDevice((result) => {
console.log("[isARSupportedOnDevice]: Android", { result });
if (result == "SUPPORTED")
resolve({ isARSupported: true });
if (result)
reject(new Error(result));
reject("AR Support Unknown.");
});
}
});
}
exports.isARSupportedOnDevice = isARSupportedOnDevice;
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import { ViroVRSceneNavigator } from "./components/ViroVRSceneNavigator";
import { Viro3DSceneNavigator } from "./components/Viro3DSceneNavigator";
import { ViroTextStyle } from "./components/Styles/ViroTextStyle";
import { ViroStyle } from "./components/Styles/ViroStyle";
import { polarToCartesian, polarToCartesianActual, isARSupportedOnDevice } from "./components/Utilities/ViroUtils";
import { polarToCartesian, polarToCartesianActual, isARSupportedOnDevice, ViroARSupportResponse } from "./components/Utilities/ViroUtils";
import { ViroARCamera } from "./components/AR/ViroARCamera";
import { ViroHoverEvent, ViroClickEvent, ViroClickStateEvent, ViroTouchEvent, ViroScrollEvent, ViroSwipeEvent, ViroFuseEvent, ViroPinchEvent, ViroRotateEvent, ViroDragEvent, ViroPlatformEvent, ViroCollisionEvent, ViroPlatformInfo, ViroCameraTransformEvent, ViroPlatformUpdateEvent, ViroCameraTransform, ViroExitViroEvent, ViroErrorEvent, ViroAnimationStartEvent, ViroAnimationFinishEvent, ViroLoadStartEvent, ViroLoadEndEvent, ViroLoadErrorEvent, ViroVideoBufferStartEvent, ViroVideoBufferEndEvent, ViroVideoUpdateTimeEvent, ViroVideoErrorEvent, ViroVideoFinishEvent, ViroAnimatedComponentStartEvent, ViroAnimatedComponentFinishEvent, ViroARAnchorRemovedEvent, ViroARAnchorUpdatedEvent, ViroARAnchorFoundEvent, ViroAnchor, ViroAnchorFoundMap, ViroAnchorUpdatedMap, ViroPlaneUpdatedMap, ViroPlaneUpdatedEvent, ViroARPlaneSizes, ViroCameraARHitTestEvent, ViroCameraARHitTest, ViroARHitTestResult, ViroARPointCloudUpdateEvent, ViroARPointCloud, ViroTrackingUpdatedEvent, ViroTrackingState, ViroTrackingReason, ViroAmbientLightUpdateEvent, ViroAmbientLightInfo, ViroWorldOrigin, ViroNativeTransformUpdateEvent, ViroControllerStatusEvent, ViroControllerStatus, ViroPortalEnterEvent, ViroPortalExitEvent, ViroSoundFinishEvent, ViroPinchStateTypes, ViroClickStateTypes, ViroRotateStateTypes } from "./components/Types/ViroEvents";
import { ViroSurface } from "./components/ViroSurface";
import { ViroSceneNavigator } from "./components/ViroSceneNavigator";
export { ViroARImageMarker, ViroARObjectMarker, ViroARTrackingTargets, ViroARPlane, ViroARPlaneSelector, ViroARScene, ViroARSceneNavigator, ViroBox, ViroButton, ViroCamera, ViroController, ViroDirectionalLight, ViroFlexView, ViroGeometry, ViroLightingEnvironment, ViroImage, ViroMaterials, ViroARCamera, ViroMaterialVideo, ViroNode, ViroOmniLight, ViroOrbitCamera, ViroParticleEmitter, ViroPolygon, ViroPolyline, ViroPortal, ViroPortalScene, ViroQuad, ViroScene, ViroSurface, ViroSceneNavigator, ViroSkyBox, ViroAnimations, Viro3DObject, Viro360Image, Viro360Video, ViroAnimatedImage, ViroAmbientLight, ViroAnimatedComponent, ViroSound, ViroSoundField, ViroSpatialSound, ViroSphere, ViroSpinner, ViroSpotLight, ViroText, ViroVideo, ViroVRSceneNavigator, Viro3DSceneNavigator, ViroARTrackingReasonConstants, ViroRecordingErrorConstants, ViroTrackingStateConstants, polarToCartesian, polarToCartesianActual, isARSupportedOnDevice, ViroHoverEvent, ViroClickEvent, ViroClickStateEvent, ViroClickStateTypes, ViroTouchEvent, ViroScrollEvent, ViroSwipeEvent, ViroFuseEvent, ViroPinchEvent, ViroPinchStateTypes, ViroRotateEvent, ViroRotateStateTypes, ViroDragEvent, ViroPlatformEvent, ViroCollisionEvent, ViroPlatformInfo, ViroCameraTransformEvent, ViroPlatformUpdateEvent, ViroCameraTransform, ViroExitViroEvent, ViroErrorEvent, ViroAnimationStartEvent, ViroAnimationFinishEvent, ViroLoadStartEvent, ViroLoadEndEvent, ViroLoadErrorEvent, ViroVideoBufferStartEvent, ViroVideoBufferEndEvent, ViroVideoUpdateTimeEvent, ViroVideoErrorEvent, ViroVideoFinishEvent, ViroAnimatedComponentStartEvent, ViroAnimatedComponentFinishEvent, ViroARAnchorRemovedEvent, ViroARAnchorUpdatedEvent, ViroARAnchorFoundEvent, ViroAnchor, ViroAnchorFoundMap, ViroAnchorUpdatedMap, ViroPlaneUpdatedMap, ViroPlaneUpdatedEvent, ViroARPlaneSizes, ViroCameraARHitTestEvent, ViroCameraARHitTest, ViroARHitTestResult, ViroARPointCloudUpdateEvent, ViroARPointCloud, ViroTrackingUpdatedEvent, ViroTrackingState, ViroTrackingReason, ViroAmbientLightUpdateEvent, ViroAmbientLightInfo, ViroWorldOrigin, ViroNativeTransformUpdateEvent, ViroControllerStatusEvent, ViroControllerStatus, ViroPortalEnterEvent, ViroPortalExitEvent, ViroSoundFinishEvent, ViroTextStyle, ViroStyle, };
export { ViroARImageMarker, ViroARObjectMarker, ViroARTrackingTargets, ViroARPlane, ViroARPlaneSelector, ViroARScene, ViroARSceneNavigator, ViroBox, ViroButton, ViroCamera, ViroController, ViroDirectionalLight, ViroFlexView, ViroGeometry, ViroLightingEnvironment, ViroImage, ViroMaterials, ViroARCamera, ViroMaterialVideo, ViroNode, ViroOmniLight, ViroOrbitCamera, ViroParticleEmitter, ViroPolygon, ViroPolyline, ViroPortal, ViroPortalScene, ViroQuad, ViroScene, ViroSurface, ViroSceneNavigator, ViroSkyBox, ViroAnimations, Viro3DObject, Viro360Image, Viro360Video, ViroAnimatedImage, ViroAmbientLight, ViroAnimatedComponent, ViroSound, ViroSoundField, ViroSpatialSound, ViroSphere, ViroSpinner, ViroSpotLight, ViroText, ViroVideo, ViroVRSceneNavigator, Viro3DSceneNavigator, ViroARTrackingReasonConstants, ViroRecordingErrorConstants, ViroTrackingStateConstants, polarToCartesian, polarToCartesianActual, isARSupportedOnDevice, ViroARSupportResponse, ViroHoverEvent, ViroClickEvent, ViroClickStateEvent, ViroClickStateTypes, ViroTouchEvent, ViroScrollEvent, ViroSwipeEvent, ViroFuseEvent, ViroPinchEvent, ViroPinchStateTypes, ViroRotateEvent, ViroRotateStateTypes, ViroDragEvent, ViroPlatformEvent, ViroCollisionEvent, ViroPlatformInfo, ViroCameraTransformEvent, ViroPlatformUpdateEvent, ViroCameraTransform, ViroExitViroEvent, ViroErrorEvent, ViroAnimationStartEvent, ViroAnimationFinishEvent, ViroLoadStartEvent, ViroLoadEndEvent, ViroLoadErrorEvent, ViroVideoBufferStartEvent, ViroVideoBufferEndEvent, ViroVideoUpdateTimeEvent, ViroVideoErrorEvent, ViroVideoFinishEvent, ViroAnimatedComponentStartEvent, ViroAnimatedComponentFinishEvent, ViroARAnchorRemovedEvent, ViroARAnchorUpdatedEvent, ViroARAnchorFoundEvent, ViroAnchor, ViroAnchorFoundMap, ViroAnchorUpdatedMap, ViroPlaneUpdatedMap, ViroPlaneUpdatedEvent, ViroARPlaneSizes, ViroCameraARHitTestEvent, ViroCameraARHitTest, ViroARHitTestResult, ViroARPointCloudUpdateEvent, ViroARPointCloud, ViroTrackingUpdatedEvent, ViroTrackingState, ViroTrackingReason, ViroAmbientLightUpdateEvent, ViroAmbientLightInfo, ViroWorldOrigin, ViroNativeTransformUpdateEvent, ViroControllerStatusEvent, ViroControllerStatus, ViroPortalEnterEvent, ViroPortalExitEvent, ViroSoundFinishEvent, ViroTextStyle, ViroStyle, };
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
polarToCartesian,
polarToCartesianActual,
isARSupportedOnDevice,
ViroARSupportResponse,
} from "./components/Utilities/ViroUtils";
import { ViroARCamera } from "./components/AR/ViroARCamera";
import {
Expand Down Expand Up @@ -185,6 +186,7 @@ export {
polarToCartesianActual,
isARSupportedOnDevice,
// Types
ViroARSupportResponse,
ViroHoverEvent,
ViroClickEvent,
ViroClickStateEvent,
Expand Down

0 comments on commit 220847d

Please sign in to comment.