1 | #include "RNScreensTurboModule.h"
|
2 |
|
3 | using namespace facebook;
|
4 |
|
5 | namespace RNScreens {
|
6 |
|
7 | const char RNScreensTurboModule::MODULE_NAME[] = "RNScreensTurboModule";
|
8 |
|
9 | std::function<std::array<int, 2>(int)> RNScreensTurboModule::startTransition_;
|
10 | std::function<void(int, double)> RNScreensTurboModule::updateTransition_;
|
11 | std::function<void(int, bool)> RNScreensTurboModule::finishTransition_;
|
12 | std::function<void(int)> RNScreensTurboModule::disableSwipeBackForTopScreen_;
|
13 |
|
14 | RNScreensTurboModule::RNScreensTurboModule(
|
15 | std::function<std::array<int, 2>(int)> startTransition,
|
16 | std::function<void(int, double)> updateTransition,
|
17 | std::function<void(int, bool)> finishTransition,
|
18 | std::function<void(int)> disableSwipeBackForTopScreen) {
|
19 | startTransition_ = startTransition;
|
20 | updateTransition_ = updateTransition;
|
21 | finishTransition_ = finishTransition;
|
22 | disableSwipeBackForTopScreen_ = disableSwipeBackForTopScreen;
|
23 | }
|
24 |
|
25 | RNScreensTurboModule::~RNScreensTurboModule(){};
|
26 |
|
27 | jsi::Value RNScreensTurboModule::get(
|
28 | jsi::Runtime &rt,
|
29 | const jsi::PropNameID &name) {
|
30 | if (name.utf8(rt) == "startTransition") {
|
31 | return jsi::Function::createFromHostFunction(rt, name, 1, startTransition);
|
32 | } else if (name.utf8(rt) == "updateTransition") {
|
33 | return jsi::Function::createFromHostFunction(rt, name, 2, updateTransition);
|
34 | } else if (name.utf8(rt) == "finishTransition") {
|
35 | return jsi::Function::createFromHostFunction(rt, name, 2, finishTransition);
|
36 | } else if (name.utf8(rt) == "disableSwipeBackForTopScreen") {
|
37 | return jsi::Function::createFromHostFunction(
|
38 | rt, name, 1, disableSwipeBackForTopScreen);
|
39 | }
|
40 | return jsi::Value::undefined();
|
41 | }
|
42 |
|
43 | void RNScreensTurboModule::set(
|
44 | jsi::Runtime &,
|
45 | const jsi::PropNameID &,
|
46 | const jsi::Value &){};
|
47 |
|
48 | std::vector<jsi::PropNameID> RNScreensTurboModule::getPropertyNames(
|
49 | jsi::Runtime &rt) {
|
50 | std::vector<jsi::PropNameID> properties;
|
51 | properties.push_back(jsi::PropNameID::forUtf8(rt, "startTransition"));
|
52 | properties.push_back(jsi::PropNameID::forUtf8(rt, "updateTransition"));
|
53 | properties.push_back(jsi::PropNameID::forUtf8(rt, "finishTransition"));
|
54 | properties.push_back(
|
55 | jsi::PropNameID::forUtf8(rt, "disableSwipeBackForTopScreen"));
|
56 | return properties;
|
57 | }
|
58 |
|
59 | JSI_HOST_FUNCTION(RNScreensTurboModule::startTransition) {
|
60 | if (count < 1) {
|
61 | throw jsi::JSError(
|
62 | rt, "[RNScreens] `startTransition` method requires 1 argument.");
|
63 | }
|
64 | int stackTag = arguments[0].asNumber();
|
65 | auto screenTags = startTransition_(stackTag);
|
66 | jsi::Object screenTagsObject(rt);
|
67 | jsi::Value topScreenTag, belowTopScreenTag, canStartTransition;
|
68 | if (screenTags[0] > -1) {
|
69 | topScreenTag = jsi::Value(screenTags[0]);
|
70 | belowTopScreenTag = jsi::Value(screenTags[1]);
|
71 | canStartTransition = jsi::Value(true);
|
72 | } else {
|
73 | topScreenTag = jsi::Value(-1);
|
74 | belowTopScreenTag = jsi::Value(-1);
|
75 | canStartTransition = jsi::Value(false);
|
76 | }
|
77 | screenTagsObject.setProperty(rt, "topScreenTag", topScreenTag);
|
78 | screenTagsObject.setProperty(rt, "belowTopScreenTag", belowTopScreenTag);
|
79 | screenTagsObject.setProperty(rt, "canStartTransition", canStartTransition);
|
80 | return screenTagsObject;
|
81 | }
|
82 |
|
83 | JSI_HOST_FUNCTION(RNScreensTurboModule::updateTransition) {
|
84 | if (count < 2) {
|
85 | throw jsi::JSError(
|
86 | rt, "[RNScreens] `updateTransition` requires 2 arguments.");
|
87 | }
|
88 | int stackTag = arguments[0].asNumber();
|
89 | double progress = arguments[1].asNumber();
|
90 | updateTransition_(stackTag, progress);
|
91 | return jsi::Value::undefined();
|
92 | }
|
93 |
|
94 | JSI_HOST_FUNCTION(RNScreensTurboModule::finishTransition) {
|
95 | if (count < 2) {
|
96 | throw jsi::JSError(
|
97 | rt, "[RNScreens] `finishTransition` requires 2 arguments.");
|
98 | }
|
99 | int stackTag = arguments[0].asNumber();
|
100 | bool canceled = arguments[1].getBool();
|
101 | finishTransition_(stackTag, canceled);
|
102 | return jsi::Value::undefined();
|
103 | }
|
104 |
|
105 | JSI_HOST_FUNCTION(RNScreensTurboModule::disableSwipeBackForTopScreen) {
|
106 | if (count < 1) {
|
107 | throw jsi::JSError(
|
108 | rt, "[RNScreens] `startTransition` method requires 1 argument.");
|
109 | }
|
110 | int stackTag = arguments[0].asNumber();
|
111 | disableSwipeBackForTopScreen_(stackTag);
|
112 | return jsi::Value::undefined();
|
113 | }
|
114 |
|
115 | }
|