Add events handlers

This commit is contained in:
2026-04-19 21:05:55 +02:00
parent 835510115e
commit f2332b4844
26 changed files with 1184 additions and 48 deletions
+76
View File
@@ -0,0 +1,76 @@
///
/// BoolProperty.hpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///
#pragma once
#if __has_include(<NitroModules/NitroHash.hpp>)
#include <NitroModules/NitroHash.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
#if __has_include(<NitroModules/JSIConverter.hpp>)
#include <NitroModules/JSIConverter.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
#if __has_include(<NitroModules/NitroDefines.hpp>)
#include <NitroModules/NitroDefines.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
namespace margelo::nitro::omni {
/**
* An enum which can be represented as a JavaScript union (BoolProperty).
*/
enum class BoolProperty {
ISPLAYING SWIFT_NAME(isplaying) = 0,
MUTED SWIFT_NAME(muted) = 1,
} CLOSED_ENUM;
} // namespace margelo::nitro::omni
namespace margelo::nitro {
// C++ BoolProperty <> JS BoolProperty (union)
template <>
struct JSIConverter<margelo::nitro::omni::BoolProperty> final {
static inline margelo::nitro::omni::BoolProperty fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("isPlaying"): return margelo::nitro::omni::BoolProperty::ISPLAYING;
case hashString("muted"): return margelo::nitro::omni::BoolProperty::MUTED;
default: [[unlikely]]
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum BoolProperty - invalid value!");
}
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::omni::BoolProperty arg) {
switch (arg) {
case margelo::nitro::omni::BoolProperty::ISPLAYING: return JSIConverter<std::string>::toJSI(runtime, "isPlaying");
case margelo::nitro::omni::BoolProperty::MUTED: return JSIConverter<std::string>::toJSI(runtime, "muted");
default: [[unlikely]]
throw std::invalid_argument("Cannot convert BoolProperty to JS - invalid value: "
+ std::to_string(static_cast<int>(arg)) + "!");
}
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (!value.isString()) {
return false;
}
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("isPlaying"):
case hashString("muted"):
return true;
default:
return false;
}
}
};
} // namespace margelo::nitro
@@ -14,6 +14,12 @@ namespace margelo::nitro::omni {
HybridObject::loadHybridMethods();
// load custom methods/properties
registerHybrids(this, [](Prototype& prototype) {
prototype.registerHybridMethod("addStateListener", &HybridOmniEventMapSpec::addStateListener);
prototype.registerHybridMethod("removeStateListener", &HybridOmniEventMapSpec::removeStateListener);
prototype.registerHybridMethod("addStateBoolListener", &HybridOmniEventMapSpec::addStateBoolListener);
prototype.registerHybridMethod("removeStateBoolListener", &HybridOmniEventMapSpec::removeStateBoolListener);
prototype.registerHybridMethod("addPlayerStatusListener", &HybridOmniEventMapSpec::addPlayerStatusListener);
prototype.registerHybridMethod("removePlayerStatusListener", &HybridOmniEventMapSpec::removePlayerStatusListener);
prototype.registerHybridMethod("addOnEndListener", &HybridOmniEventMapSpec::addOnEndListener);
prototype.registerHybridMethod("removeOnEndListener", &HybridOmniEventMapSpec::removeOnEndListener);
prototype.registerHybridMethod("addOnPrevListener", &HybridOmniEventMapSpec::addOnPrevListener);
+15
View File
@@ -13,12 +13,21 @@
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
// Forward declaration of `NumberProperty` to properly resolve imports.
namespace margelo::nitro::omni { enum class NumberProperty; }
// Forward declaration of `BoolProperty` to properly resolve imports.
namespace margelo::nitro::omni { enum class BoolProperty; }
// Forward declaration of `PlayerStatus` to properly resolve imports.
namespace margelo::nitro::omni { enum class PlayerStatus; }
// Forward declaration of `Track` to properly resolve imports.
namespace margelo::nitro::omni { struct Track; }
// Forward declaration of `Rendition` to properly resolve imports.
namespace margelo::nitro::omni { struct Rendition; }
#include "NumberProperty.hpp"
#include <functional>
#include "BoolProperty.hpp"
#include "PlayerStatus.hpp"
#include <string>
#include "Track.hpp"
#include <optional>
@@ -55,6 +64,12 @@ namespace margelo::nitro::omni {
public:
// Methods
virtual void addStateListener(NumberProperty key, const std::function<void(double /* value */)>& cb) = 0;
virtual void removeStateListener(NumberProperty key, const std::function<void(double /* value */)>& cb) = 0;
virtual void addStateBoolListener(BoolProperty key, const std::function<void(bool /* value */)>& cb) = 0;
virtual void removeStateBoolListener(BoolProperty key, const std::function<void(bool /* value */)>& cb) = 0;
virtual void addPlayerStatusListener(const std::function<void(PlayerStatus /* value */)>& cb) = 0;
virtual void removePlayerStatusListener(const std::function<void(PlayerStatus /* value */)>& cb) = 0;
virtual void addOnEndListener(const std::function<void()>& cb) = 0;
virtual void removeOnEndListener(const std::function<void()>& cb) = 0;
virtual void addOnPrevListener(const std::function<void()>& cb) = 0;
+88
View File
@@ -0,0 +1,88 @@
///
/// NumberProperty.hpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///
#pragma once
#if __has_include(<NitroModules/NitroHash.hpp>)
#include <NitroModules/NitroHash.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
#if __has_include(<NitroModules/JSIConverter.hpp>)
#include <NitroModules/JSIConverter.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
#if __has_include(<NitroModules/NitroDefines.hpp>)
#include <NitroModules/NitroDefines.hpp>
#else
#error NitroModules cannot be found! Are you sure you installed NitroModules properly?
#endif
namespace margelo::nitro::omni {
/**
* An enum which can be represented as a JavaScript union (NumberProperty).
*/
enum class NumberProperty {
CURRENTTIME SWIFT_NAME(currenttime) = 0,
BUFFERED SWIFT_NAME(buffered) = 1,
DURATION SWIFT_NAME(duration) = 2,
PLAYBACKRATE SWIFT_NAME(playbackrate) = 3,
VOLUME SWIFT_NAME(volume) = 4,
} CLOSED_ENUM;
} // namespace margelo::nitro::omni
namespace margelo::nitro {
// C++ NumberProperty <> JS NumberProperty (union)
template <>
struct JSIConverter<margelo::nitro::omni::NumberProperty> final {
static inline margelo::nitro::omni::NumberProperty fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("currentTime"): return margelo::nitro::omni::NumberProperty::CURRENTTIME;
case hashString("buffered"): return margelo::nitro::omni::NumberProperty::BUFFERED;
case hashString("duration"): return margelo::nitro::omni::NumberProperty::DURATION;
case hashString("playbackRate"): return margelo::nitro::omni::NumberProperty::PLAYBACKRATE;
case hashString("volume"): return margelo::nitro::omni::NumberProperty::VOLUME;
default: [[unlikely]]
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum NumberProperty - invalid value!");
}
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::omni::NumberProperty arg) {
switch (arg) {
case margelo::nitro::omni::NumberProperty::CURRENTTIME: return JSIConverter<std::string>::toJSI(runtime, "currentTime");
case margelo::nitro::omni::NumberProperty::BUFFERED: return JSIConverter<std::string>::toJSI(runtime, "buffered");
case margelo::nitro::omni::NumberProperty::DURATION: return JSIConverter<std::string>::toJSI(runtime, "duration");
case margelo::nitro::omni::NumberProperty::PLAYBACKRATE: return JSIConverter<std::string>::toJSI(runtime, "playbackRate");
case margelo::nitro::omni::NumberProperty::VOLUME: return JSIConverter<std::string>::toJSI(runtime, "volume");
default: [[unlikely]]
throw std::invalid_argument("Cannot convert NumberProperty to JS - invalid value: "
+ std::to_string(static_cast<int>(arg)) + "!");
}
}
static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
if (!value.isString()) {
return false;
}
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("currentTime"):
case hashString("buffered"):
case hashString("duration"):
case hashString("playbackRate"):
case hashString("volume"):
return true;
default:
return false;
}
}
};
} // namespace margelo::nitro
+7 -7
View File
@@ -29,10 +29,10 @@ namespace margelo::nitro::omni {
* An enum which can be represented as a JavaScript union (PlayerStatus).
*/
enum class PlayerStatus {
ERROR SWIFT_NAME(error) = 0,
IDLE SWIFT_NAME(idle) = 1,
LOADING SWIFT_NAME(loading) = 2,
READYTOPLAY SWIFT_NAME(readytoplay) = 3,
IDLE SWIFT_NAME(idle) = 0,
LOADING SWIFT_NAME(loading) = 1,
READYTOPLAY SWIFT_NAME(readytoplay) = 2,
ERROR SWIFT_NAME(error) = 3,
} CLOSED_ENUM;
} // namespace margelo::nitro::omni
@@ -45,20 +45,20 @@ namespace margelo::nitro {
static inline margelo::nitro::omni::PlayerStatus fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, arg);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("error"): return margelo::nitro::omni::PlayerStatus::ERROR;
case hashString("idle"): return margelo::nitro::omni::PlayerStatus::IDLE;
case hashString("loading"): return margelo::nitro::omni::PlayerStatus::LOADING;
case hashString("readyToPlay"): return margelo::nitro::omni::PlayerStatus::READYTOPLAY;
case hashString("error"): return margelo::nitro::omni::PlayerStatus::ERROR;
default: [[unlikely]]
throw std::invalid_argument("Cannot convert \"" + unionValue + "\" to enum PlayerStatus - invalid value!");
}
}
static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::omni::PlayerStatus arg) {
switch (arg) {
case margelo::nitro::omni::PlayerStatus::ERROR: return JSIConverter<std::string>::toJSI(runtime, "error");
case margelo::nitro::omni::PlayerStatus::IDLE: return JSIConverter<std::string>::toJSI(runtime, "idle");
case margelo::nitro::omni::PlayerStatus::LOADING: return JSIConverter<std::string>::toJSI(runtime, "loading");
case margelo::nitro::omni::PlayerStatus::READYTOPLAY: return JSIConverter<std::string>::toJSI(runtime, "readyToPlay");
case margelo::nitro::omni::PlayerStatus::ERROR: return JSIConverter<std::string>::toJSI(runtime, "error");
default: [[unlikely]]
throw std::invalid_argument("Cannot convert PlayerStatus to JS - invalid value: "
+ std::to_string(static_cast<int>(arg)) + "!");
@@ -70,10 +70,10 @@ namespace margelo::nitro {
}
std::string unionValue = JSIConverter<std::string>::fromJSI(runtime, value);
switch (hashString(unionValue.c_str(), unionValue.size())) {
case hashString("error"):
case hashString("idle"):
case hashString("loading"):
case hashString("readyToPlay"):
case hashString("error"):
return true;
default:
return false;