From 23f24ba22859b5b947a77b2a579941db7fc00103 Mon Sep 17 00:00:00 2001 From: tyBrave Date: Wed, 19 Jun 2024 17:28:18 +0800 Subject: [PATCH] fix:solve question setSize method does not work causing a blank header Signed-off-by: tyBrave --- harmony/smart_refresh_layout/index.ets | 3 +- .../cpp/RNCAnyHeaderComponentInstance.cpp | 27 +++++++---- .../src/main/cpp/RNCSmartRefreshTurboModule.h | 37 +++++++++++++++ .../main/cpp/RNCSmartRefreshTurbomodule.cpp | 42 +++++++++++++++++ .../src/main/cpp/SmartRefreshLayoutPackage.h | 15 ++++++ .../src/main/ets/SmartRefreshPackage.ts | 46 +++++++++++++++++++ .../src/main/ets/SmartRefreshTurboModule.ts | 32 +++++++++++++ harmony/smart_refresh_layout/ts.ts | 26 +++++++++++ 8 files changed, 217 insertions(+), 11 deletions(-) create mode 100644 harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurboModule.h create mode 100644 harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurbomodule.cpp create mode 100644 harmony/smart_refresh_layout/src/main/ets/SmartRefreshPackage.ts create mode 100644 harmony/smart_refresh_layout/src/main/ets/SmartRefreshTurboModule.ts create mode 100644 harmony/smart_refresh_layout/ts.ts diff --git a/harmony/smart_refresh_layout/index.ets b/harmony/smart_refresh_layout/index.ets index 5bb98e6..bf47609 100644 --- a/harmony/smart_refresh_layout/index.ets +++ b/harmony/smart_refresh_layout/index.ets @@ -14,4 +14,5 @@ */ export * from "./src/main/ets/SmartRefreshControl" export * from "./src/main/ets/RNCAnyHeader" -export * from "./src/main/ets/RNCDefaultHeader" \ No newline at end of file +export * from "./src/main/ets/RNCDefaultHeader" +export * from './ts' \ No newline at end of file diff --git a/harmony/smart_refresh_layout/src/main/cpp/RNCAnyHeaderComponentInstance.cpp b/harmony/smart_refresh_layout/src/main/cpp/RNCAnyHeaderComponentInstance.cpp index 403a72b..3bc2212 100644 --- a/harmony/smart_refresh_layout/src/main/cpp/RNCAnyHeaderComponentInstance.cpp +++ b/harmony/smart_refresh_layout/src/main/cpp/RNCAnyHeaderComponentInstance.cpp @@ -25,16 +25,23 @@ namespace rnoh { void RNCAnyHeaderComponentInstance::finalizeUpdates() { std::vector child = getChildren(); - float childHeight = 0.0; - for (ComponentInstance::Shared c : child) { - if (c) { - auto height = c->getLayoutMetrics().frame.size.height; - if (height>childHeight) { - childHeight = height; - } - } - } - m_stackNode.setSize(facebook::react::Size({ getLayoutMetrics().frame.size.width, childHeight})); + float childHeight = 0.0; + for (ComponentInstance::Shared c : child) { + if (c) { + auto height = c->getLayoutMetrics().frame.size.height; + if (height>childHeight) { + childHeight = height; + } + } + } + auto rnInstancePtr = this->m_deps->rnInstance.lock(); + if (rnInstancePtr != nullptr) { + auto turboModule = rnInstancePtr->getTurboModule("RNCSmartRefreshContext"); + auto arkTsTurboModule = std::dynamic_pointer_cast(turboModule); + folly::dynamic result = arkTsTurboModule->callSync("cvp2px", {getLayoutMetrics().frame.size.width});; + folly::dynamic result1 = arkTsTurboModule->callSync("cvp2px", {childHeight}); + m_stackNode.setLayoutRect({0, 0}, {result["values"].asDouble(), result1["values"].asDouble()}, 1.0); + } } } // namespace rnoh \ No newline at end of file diff --git a/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurboModule.h b/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurboModule.h new file mode 100644 index 0000000..1363692 --- /dev/null +++ b/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurboModule.h @@ -0,0 +1,37 @@ +/** + * MIT License + * + * Copyright (C) 2023 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include +#include "RNOH/ArkTSTurboModule.h" + +namespace rnoh { + + class JSI_EXPORT RNCSmartRefreshTurboModule : public ArkTSTurboModule { + public: + RNCSmartRefreshTurboModule(const ArkTSTurboModule::Context ctx, const std::string name); + }; + +} // namespace rnoh \ No newline at end of file diff --git a/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurbomodule.cpp b/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurbomodule.cpp new file mode 100644 index 0000000..59504c3 --- /dev/null +++ b/harmony/smart_refresh_layout/src/main/cpp/RNCSmartRefreshTurbomodule.cpp @@ -0,0 +1,42 @@ +/** + * MIT License + * + * Copyright (C) 2023 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "RNCSmartRefreshTurbomodule.h" + +using namespace rnoh; +using namespace facebook; + +static jsi::Value __hostFunction_RNCSmartRefreshTurboModule_cvp2px(jsi::Runtime &rt, + react::TurboModule &turboModule, + const jsi::Value *args, size_t count) +{ + return static_cast(turboModule).call(rt, "cvp2px", args, count); +} + +RNCSmartRefreshTurboModule::RNCSmartRefreshTurboModule(const ArkTSTurboModule::Context ctx, + const std::string name) + : ArkTSTurboModule(ctx, name) +{ + methodMap_["cvp2px"] = MethodMetadata{0, __hostFunction_RNCSmartRefreshTurboModule_cvp2px}; +} \ No newline at end of file diff --git a/harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutPackage.h b/harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutPackage.h index 8dd59a1..01092f5 100644 --- a/harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutPackage.h +++ b/harmony/smart_refresh_layout/src/main/cpp/SmartRefreshLayoutPackage.h @@ -35,6 +35,7 @@ #include "RNCDefaultHeaderComponentInstance.h" #include "RNCClassicsHeaderComponentInstance.h" #include "RNCMaterialHeaderComponentInstance.h" +#include "RNCSmartRefreshTurboModule.h" namespace rnoh { @@ -62,6 +63,16 @@ namespace rnoh { } }; + class RNCSmartRefreshFactoryDelegate : public TurboModuleFactoryDelegate { + public: + SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override { + if (name == "RNCSmartRefreshContext") { + return std::make_shared(ctx, name); + } + return nullptr; + }; + }; + class SmartRefreshLayoutPackage : public Package { public: SmartRefreshLayoutPackage(Package::Context ctx) : Package(ctx) {} @@ -70,6 +81,10 @@ namespace rnoh { return std::make_shared(); } + std::unique_ptr createTurboModuleFactoryDelegate() override { + return std::make_unique(); + } + std::vector createComponentDescriptorProviders() override { return { facebook::react::concreteComponentDescriptorProvider< diff --git a/harmony/smart_refresh_layout/src/main/ets/SmartRefreshPackage.ts b/harmony/smart_refresh_layout/src/main/ets/SmartRefreshPackage.ts new file mode 100644 index 0000000..e979fd1 --- /dev/null +++ b/harmony/smart_refresh_layout/src/main/ets/SmartRefreshPackage.ts @@ -0,0 +1,46 @@ +/** + * MIT License + * + * Copyright (C) 2023 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import { RNPackage, TurboModulesFactory } from '@rnoh/react-native-openharmony/ts'; +import type { TurboModule, TurboModuleContext } from '@rnoh/react-native-openharmony/ts'; +import { SmartRefreshTurboModule } from './SmartRefreshTurboModule'; + +class SmartRefreshTurboModulesFactory extends TurboModulesFactory { + createTurboModule(name: string): TurboModule | null { + if (name === 'RNCSmartRefreshContext') { + return new SmartRefreshTurboModule(this.ctx); + } + return null; + } + + hasTurboModule(name: string): boolean { + return name === 'RNCSmartRefreshContext'; + } +} + +export class SmartRefreshPackage extends RNPackage { + createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory { + return new SmartRefreshTurboModulesFactory(ctx); + } +} \ No newline at end of file diff --git a/harmony/smart_refresh_layout/src/main/ets/SmartRefreshTurboModule.ts b/harmony/smart_refresh_layout/src/main/ets/SmartRefreshTurboModule.ts new file mode 100644 index 0000000..ee57cd3 --- /dev/null +++ b/harmony/smart_refresh_layout/src/main/ets/SmartRefreshTurboModule.ts @@ -0,0 +1,32 @@ +/** + * MIT License + * + * Copyright (C) 2024 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import { TurboModule } from "@rnoh/react-native-openharmony/ts"; + +export class SmartRefreshTurboModule extends TurboModule { + cvp2px(value: number):{} { + const v= vp2px(value); + return {values:v}; + } +} \ No newline at end of file diff --git a/harmony/smart_refresh_layout/ts.ts b/harmony/smart_refresh_layout/ts.ts new file mode 100644 index 0000000..739a6ba --- /dev/null +++ b/harmony/smart_refresh_layout/ts.ts @@ -0,0 +1,26 @@ +/** + * MIT License + * + * Copyright (C) 2023 Huawei Device Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +export * from "./src/main/ets/SmartRefreshTurboModule" +export * from "./src/main/ets/SmartRefreshPackage" \ No newline at end of file