|
| 1 | +#include "AnimatedSensorModule.h" |
| 2 | +#include "MutableValue.h" |
| 3 | +#include "ValueWrapper.h" |
| 4 | + |
| 5 | +namespace reanimated { |
| 6 | + |
| 7 | +AnimatedSensorModule::AnimatedSensorModule( |
| 8 | + const PlatformDepMethodsHolder &platformDepMethodsHolder, |
| 9 | + RuntimeManager *runtimeManager) |
| 10 | + : platformRegisterSensorFunction_(platformDepMethodsHolder.registerSensor), |
| 11 | + platformUnregisterSensorFunction_( |
| 12 | + platformDepMethodsHolder.unregisterSensor), |
| 13 | + runtimeManager_(runtimeManager) {} |
| 14 | + |
| 15 | +AnimatedSensorModule::~AnimatedSensorModule() { |
| 16 | + // It is called during app reload because app reload doesn't call hooks |
| 17 | + // unmounting |
| 18 | + for (auto sensorId : sensorsIds_) { |
| 19 | + platformUnregisterSensorFunction_(sensorId); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +jsi::Value AnimatedSensorModule::registerSensor( |
| 24 | + jsi::Runtime &rt, |
| 25 | + const jsi::Value &sensorType, |
| 26 | + const jsi::Value &interval, |
| 27 | + const jsi::Value &sensorDataContainer) { |
| 28 | + std::shared_ptr<ShareableValue> sensorsData = ShareableValue::adapt( |
| 29 | + rt, sensorDataContainer.getObject(rt), runtimeManager_); |
| 30 | + auto &mutableObject = |
| 31 | + ValueWrapper::asMutableValue(sensorsData->valueContainer); |
| 32 | + |
| 33 | + std::function<void(double[])> setter; |
| 34 | + if (sensorType.asNumber() == SensorType::ROTATION_VECTOR) { |
| 35 | + setter = [&, mutableObject](double newValues[]) { |
| 36 | + jsi::Runtime &runtime = *runtimeManager_->runtime.get(); |
| 37 | + jsi::Object value(runtime); |
| 38 | + value.setProperty(runtime, "qw", newValues[0]); |
| 39 | + value.setProperty(runtime, "qx", newValues[1]); |
| 40 | + value.setProperty(runtime, "qy", newValues[2]); |
| 41 | + value.setProperty(runtime, "qz", newValues[3]); |
| 42 | + value.setProperty(runtime, "yaw", newValues[4]); |
| 43 | + value.setProperty(runtime, "pitch", newValues[5]); |
| 44 | + value.setProperty(runtime, "roll", newValues[6]); |
| 45 | + mutableObject->setValue(runtime, std::move(value)); |
| 46 | + }; |
| 47 | + } else { |
| 48 | + setter = [&, mutableObject](double newValues[]) { |
| 49 | + jsi::Runtime &runtime = *runtimeManager_->runtime.get(); |
| 50 | + jsi::Object value(runtime); |
| 51 | + value.setProperty(runtime, "x", newValues[0]); |
| 52 | + value.setProperty(runtime, "y", newValues[1]); |
| 53 | + value.setProperty(runtime, "z", newValues[2]); |
| 54 | + mutableObject->setValue(runtime, std::move(value)); |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + int sensorId = platformRegisterSensorFunction_( |
| 59 | + sensorType.asNumber(), interval.asNumber(), setter); |
| 60 | + if (sensorId != -1) { |
| 61 | + sensorsIds_.insert(sensorId); |
| 62 | + } |
| 63 | + return jsi::Value(sensorId); |
| 64 | +} |
| 65 | + |
| 66 | +void AnimatedSensorModule::unregisterSensor(const jsi::Value &sensorId) { |
| 67 | + // It is called during sensor hook unmounting |
| 68 | + sensorsIds_.erase(sensorId.getNumber()); |
| 69 | + platformUnregisterSensorFunction_(sensorId.asNumber()); |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace reanimated |
0 commit comments