Skip to content

Commit

Permalink
Fixed podspec for release + forces commands IDs in Int on android
Browse files Browse the repository at this point in the history
  • Loading branch information
riderodd committed May 12, 2022
1 parent cba4442 commit 1d17b31
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
33 changes: 27 additions & 6 deletions android/src/main/java/com/reactnativearviewer/ArViewerView.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -51,8 +50,6 @@ open class ArViewerView @JvmOverloads constructor(
*/
private var allowTransform = mutableSetOf<EditableTransform>()



/**
* Start the loading of a GLB model URI
*/
Expand All @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ArViewerViewManager : SimpleViewManager<ArViewerView>() {
companion object {
const val COMMAND_SNAPSHOT = 1
const val COMMAND_RESET = 2
const val COMMAND_ROTATE_MODEL = 3
}

/**
Expand Down Expand Up @@ -71,17 +72,18 @@ class ArViewerViewManager : SimpleViewManager<ArViewerView>() {
override fun getCommandsMap(): Map<String, Int>? {
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);
Expand All @@ -91,6 +93,14 @@ class ArViewerViewManager : SimpleViewManager<ArViewerView>() {
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)
}
}
}
}

Expand All @@ -101,7 +111,10 @@ class ArViewerViewManager : SimpleViewManager<ArViewerView>() {
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")
)
}

Expand Down
2 changes: 1 addition & 1 deletion ArViewer.podspec → react-native-ar-viewer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 2 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand All @@ -188,7 +186,6 @@ export class ArViewerView extends Component<
);
}


/**
* Rotate the model
* @returns void
Expand Down

0 comments on commit 1d17b31

Please sign in to comment.