Skip to content

Commit

Permalink
fix: Wrap in state
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 15, 2025
1 parent 172032f commit 207e254
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
22 changes: 19 additions & 3 deletions packages/nitrogen/src/views/ViewComponentShadowNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ${createFileMetadataString(`${component}.hpp`)}
#include "NitroDefines.hpp"
#include "NitroHash.hpp"
#include <optional>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
Expand All @@ -92,7 +93,7 @@ namespace ${namespace} {
/**
* Props for the "${spec.name}" View.
*/
class ${propsClassName}: public react::ViewProps {
class ${propsClassName} final: public react::ViewProps {
public:
explicit ${propsClassName}() = default;
${propsClassName}(const react::PropsParserContext& context,
Expand All @@ -109,9 +110,16 @@ namespace ${namespace} {
/**
* State for the "${spec.name}" View.
*/
class ${stateClassName} {
class ${stateClassName} final {
public:
explicit ${stateClassName}() = default;
public:
void setProps(const ${propsClassName}& props) { _props = props; }
const std::optional<${propsClassName}>& getProps() const { return _props; }
private:
std::optional<${propsClassName}> _props;
};
/**
Expand All @@ -125,7 +133,7 @@ namespace ${namespace} {
/**
* The Component Descriptor for the "${spec.name}" View.
*/
class ${descriptorClassName}: public react::ConcreteComponentDescriptor<${shadowNodeClassName}> {
class ${descriptorClassName} final: public react::ConcreteComponentDescriptor<${shadowNodeClassName}> {
public:
${descriptorClassName}(const react::ComponentDescriptorParameters& parameters);
Expand Down Expand Up @@ -189,8 +197,16 @@ namespace ${namespace} {
void ${descriptorClassName}::adopt(react::ShadowNode& shadowNode) const {
// This is called immediately after \`ShadowNode\` is created, cloned or in progress.
#ifdef ANDROID
// On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++.
auto& concreteShadowNode = static_cast<${shadowNodeClassName}&>(shadowNode);
const ${propsClassName}& props = concreteShadowNode.getConcreteProps();
${stateClassName} state;
state.setProps(props);
concreteShadowNode.setStateData(std::move(state));
#else
// On iOS, prop updating happens through the updateProps: Obj-C selector.
#endif
}
} // namespace ${namespace}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext
class ${manager} extends SimpleViewManager<View> {
class ${manager}: SimpleViewManager<View>() {
override fun getName(): String {
return "${spec.name}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.StateWrapper
import com.facebook.react.uimanager.ThemedReactContext

class TestViewManager extends SimpleViewManager<View> {
class TestViewManager: SimpleViewManager<View>() {
override fun getName(): String {
return "TestView"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,16 @@ namespace margelo::nitro::image::views {

void HybridTestViewComponentDescriptor::adopt(react::ShadowNode& shadowNode) const {
// This is called immediately after `ShadowNode` is created, cloned or in progress.
#ifdef ANDROID
// On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++.
auto& concreteShadowNode = static_cast<HybridTestViewShadowNode&>(shadowNode);
const HybridTestViewProps& props = concreteShadowNode.getConcreteProps();
HybridTestViewState state;
state.setProps(props);
concreteShadowNode.setStateData(std::move(state));
#else
// On iOS, prop updating happens through the updateProps: Obj-C selector.
#endif
}

} // namespace margelo::nitro::image::views
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "NitroDefines.hpp"
#include "NitroHash.hpp"
#include <optional>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
Expand All @@ -30,7 +31,7 @@ namespace margelo::nitro::image::views {
/**
* Props for the "TestView" View.
*/
class HybridTestViewProps: public react::ViewProps {
class HybridTestViewProps final: public react::ViewProps {
public:
explicit HybridTestViewProps() = default;
HybridTestViewProps(const react::PropsParserContext& context,
Expand All @@ -48,9 +49,16 @@ namespace margelo::nitro::image::views {
/**
* State for the "TestView" View.
*/
class HybridTestViewState {
class HybridTestViewState final {
public:
explicit HybridTestViewState() = default;

public:
void setProps(const HybridTestViewProps& props) { _props = props; }
const std::optional<HybridTestViewProps>& getProps() const { return _props; }

private:
std::optional<HybridTestViewProps> _props;
};

/**
Expand All @@ -64,7 +72,7 @@ namespace margelo::nitro::image::views {
/**
* The Component Descriptor for the "TestView" View.
*/
class HybridTestViewComponentDescriptor: public react::ConcreteComponentDescriptor<HybridTestViewShadowNode> {
class HybridTestViewComponentDescriptor final: public react::ConcreteComponentDescriptor<HybridTestViewShadowNode> {
public:
HybridTestViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters);

Expand Down

0 comments on commit 207e254

Please sign in to comment.