From 1d17b31de3960a45b7f653e3d9b0d26085c8dd18 Mon Sep 17 00:00:00 2001 From: riderodd Date: Thu, 12 May 2022 18:50:20 +0200 Subject: [PATCH] Fixed podspec for release + forces commands IDs in Int on android --- android/build.gradle | 2 +- .../com/reactnativearviewer/ArViewerView.kt | 33 +++++++++++++++---- .../ArViewerViewManager.kt | 21 +++++++++--- ....podspec => react-native-ar-viewer.podspec | 2 +- src/index.tsx | 7 ++-- 5 files changed, 48 insertions(+), 17 deletions(-) rename ArViewer.podspec => react-native-ar-viewer.podspec (93%) diff --git a/android/build.gradle b/android/build.gradle index 83fa054..adba0ec 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -125,5 +125,5 @@ dependencies { // noinspection GradleDynamicVersion api 'com.facebook.react:react-native:+' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'io.github.sceneview:arsceneview:0.5.2' + implementation 'io.github.sceneview:arsceneview:0.6.0' } diff --git a/android/src/main/java/com/reactnativearviewer/ArViewerView.kt b/android/src/main/java/com/reactnativearviewer/ArViewerView.kt index acde7fe..eefdeb4 100644 --- a/android/src/main/java/com/reactnativearviewer/ArViewerView.kt +++ b/android/src/main/java/com/reactnativearviewer/ArViewerView.kt @@ -1,8 +1,6 @@ package com.reactnativearviewer -import android.Manifest import android.content.Context -import android.content.pm.PackageManager import android.graphics.Bitmap import android.os.Handler import android.os.HandlerThread @@ -20,6 +18,7 @@ import io.github.sceneview.ar.arcore.ArSession import io.github.sceneview.ar.node.ArModelNode import io.github.sceneview.ar.node.EditableTransform import io.github.sceneview.math.Position +import io.github.sceneview.math.Rotation import io.github.sceneview.node.Node import io.github.sceneview.utils.FrameTime import java.io.ByteArrayOutputStream @@ -51,8 +50,6 @@ open class ArViewerView @JvmOverloads constructor( */ private var allowTransform = mutableSetOf() - - /** * Start the loading of a GLB model URI */ @@ -61,6 +58,13 @@ open class ArViewerView @JvmOverloads constructor( Log.d("ARview model", "detaching") modelNode.detachAnchor() modelNode.destroy() + val event = Arguments.createMap() + val reactContext = context as ReactContext + reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent( + id, + "onModelRemoved", + event + ) } Log.d("ARview model", "loading") modelSrc = src @@ -82,11 +86,19 @@ open class ArViewerView @JvmOverloads constructor( } ) modelNode.editableTransforms = allowTransform - + modelNode.onDetachedFromScene.add { this.onChildRemoved(modelNode) } + modelNode.name = "mainModel" context.checkSelfPermission("CAMERA") } + /** + * Rotate the model with the requested angle + */ + fun rotateModel(pitch: Number, yaw: Number, roll:Number) { + this.modelNode.rotation = Rotation(pitch.toFloat(), yaw.toFloat(), roll.toFloat()) + } + /** * Remove the model from the view and reset plane detection */ @@ -149,6 +161,7 @@ open class ArViewerView @JvmOverloads constructor( * Hide the planeRenderer when a model is added to the scene */ override fun onChildAdded(child: Node) { + Log.d("ARview onChildAdded", "called") super.onChildAdded(child) try { if (this::modelNode.isInitialized && modelNode.isAttached) { @@ -163,9 +176,10 @@ open class ArViewerView @JvmOverloads constructor( * how the planeRenderer when there is no model shown on the scene */ override fun onChildRemoved(child: Node) { + Log.d("ARview onChildRemoved", "called") super.onChildRemoved(child) try { - if (this::modelNode.isInitialized && !modelNode.isAttached) { + if (child.name == "mainModel") { planeRenderer.isVisible = true } } catch (e: Exception) { @@ -190,6 +204,13 @@ open class ArViewerView @JvmOverloads constructor( addChild(modelNode) val anchor = hitResult.createAnchor() modelNode.anchor = anchor + val event = Arguments.createMap() + val reactContext = context as ReactContext + reactContext.getJSModule(RCTEventEmitter::class.java).receiveEvent( + id, + "onModelPlaced", + event + ) } /** diff --git a/android/src/main/java/com/reactnativearviewer/ArViewerViewManager.kt b/android/src/main/java/com/reactnativearviewer/ArViewerViewManager.kt index 4abf0e8..8e688c7 100644 --- a/android/src/main/java/com/reactnativearviewer/ArViewerViewManager.kt +++ b/android/src/main/java/com/reactnativearviewer/ArViewerViewManager.kt @@ -19,6 +19,7 @@ class ArViewerViewManager : SimpleViewManager() { companion object { const val COMMAND_SNAPSHOT = 1 const val COMMAND_RESET = 2 + const val COMMAND_ROTATE_MODEL = 3 } /** @@ -71,17 +72,18 @@ class ArViewerViewManager : SimpleViewManager() { override fun getCommandsMap(): Map? { return MapBuilder.of( "takeScreenshot", COMMAND_SNAPSHOT, - "reset", COMMAND_RESET + "reset", COMMAND_RESET, + "rotateModel", COMMAND_ROTATE_MODEL ) } /** * Map methods calls to view methods */ - override fun receiveCommand(view: ArViewerView, commandId: String?, @Nullable args: ReadableArray?) { + override fun receiveCommand(view: ArViewerView, commandId: Int, @Nullable args: ReadableArray?) { super.receiveCommand(view, commandId, args) Log.d("ARview receiveCommand", commandId.toString()); - when (commandId!!.toInt()) { + when (commandId) { COMMAND_SNAPSHOT -> { if (args != null) { val requestId = args.getInt(0); @@ -91,6 +93,14 @@ class ArViewerViewManager : SimpleViewManager() { COMMAND_RESET -> { view.resetModel() } + COMMAND_ROTATE_MODEL -> { + if (args != null) { + val pitch = args.getInt(0); + val yaw = args.getInt(1); + val roll = args.getInt(2); + view.rotateModel(pitch, yaw, roll) + } + } } } @@ -101,7 +111,10 @@ class ArViewerViewManager : SimpleViewManager() { return MapBuilder.of( "onDataReturned", MapBuilder.of("registrationName","onDataReturned"), "onError", MapBuilder.of("registrationName","onError"), - "onStarted", MapBuilder.of("registrationName","onStarted") + "onStarted", MapBuilder.of("registrationName","onStarted"), + "onEnded", MapBuilder.of("registrationName","onEnded"), + "onModelPlaced", MapBuilder.of("registrationName","onModelPlaced"), + "onModelRemoved", MapBuilder.of("registrationName","onModelRemoved") ) } diff --git a/ArViewer.podspec b/react-native-ar-viewer.podspec similarity index 93% rename from ArViewer.podspec rename to react-native-ar-viewer.podspec index 692048d..c09569c 100644 --- a/ArViewer.podspec +++ b/react-native-ar-viewer.podspec @@ -3,7 +3,7 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) Pod::Spec.new do |s| - s.name = "ArViewer" + s.name = "react-native-ar-viewer" s.version = package["version"] s.summary = package["description"] s.homepage = package["homepage"] diff --git a/src/index.tsx b/src/index.tsx index bbed138..b5e72d5 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -167,9 +167,7 @@ export class ArViewerView extends Component< this.nativeRef.current && UIManager.dispatchViewManagerCommand( findNodeHandle(this.nativeRef.current as unknown as number), - (UIManager as ArViewUIManager)[ - ComponentName - ].Commands.takeScreenshot, + (UIManager as ArViewUIManager)[ComponentName].Commands.takeScreenshot, [requestId] ); return promise; @@ -179,7 +177,7 @@ export class ArViewerView extends Component< * Reset the model positionning * @returns void */ - reset() { + reset() { this.nativeRef.current && UIManager.dispatchViewManagerCommand( findNodeHandle(this.nativeRef.current as unknown as number), @@ -188,7 +186,6 @@ export class ArViewerView extends Component< ); } - /** * Rotate the model * @returns void