Commit 48e80fb4 authored by dong's avatar dong

恢复nativeApi.ArkTsToKotlin()

parent 0cbeb711
...@@ -27,7 +27,7 @@ EXTERN_C_START ...@@ -27,7 +27,7 @@ EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) { static napi_value Init(napi_env env, napi_value exports) {
androidx_compose_ui_arkui_init(env, exports); androidx_compose_ui_arkui_init(env, exports);
napi_property_descriptor desc[] = { napi_property_descriptor desc[] = {
{"MainArkUIVisewController", nullptr, NapiMainArkUIViewController, nullptr, nullptr, nullptr, napi_default, nullptr}, {"MainArkUIViewController", nullptr, NapiMainArkUIViewController, nullptr, nullptr, nullptr, napi_default, nullptr},
{"testArkTsToKotin", nullptr, nativeTestArkTsToKotin, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testArkTsToKotin", nullptr, nativeTestArkTsToKotin, nullptr, nullptr, nullptr, napi_default, nullptr},
}; };
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
......
import { ArkUIViewController,testArkTsToKotin} from "@cpf-kmp-cmp/compose/src/main/cpp/types/libcompose_arkui_utils"; import { ArkUIViewController } from "@cpf-kmp-cmp/compose/src/main/cpp/types/libcompose_arkui_utils";
export const MainArkUIViewController: () => ArkUIViewController export const MainArkUIViewController: () => ArkUIViewController
export const testArkTsToKotin:()=>void export const testArkTsToKotin: () => void
declare const nativeApi: {
MainArkUIViewController: typeof MainArkUIViewController
testArkTsToKotin: typeof testArkTsToKotin
}
export default nativeApi
import { Compose } from '@cpf-kmp-cmp/compose'; import { ArkUIViewController, Compose } from '@cpf-kmp-cmp/compose';
import { hilog } from '@kit.PerformanceAnalysisKit'; import { hilog } from '@kit.PerformanceAnalysisKit';
import nativeApi from 'libentry.so'; import nativeApi from 'libentry.so';
import { MainArkUIViewController, testArkTsToKotin } from 'libentry.so';
import { testArkTsToArkTs } from './TestJumpClass'; import { testArkTsToArkTs } from './TestJumpClass';
import { MyFunction } from './MyFunction'; import { MyFunction } from './MyFunction';
import { promptAction } from '@kit.ArkUI'; // 未使用的依赖 import { promptAction } from '@kit.ArkUI'; // 未使用的依赖
...@@ -13,58 +12,84 @@ const DOMAIN = 0x0000; ...@@ -13,58 +12,84 @@ const DOMAIN = 0x0000;
@Entry @Entry
@Component @Component
struct Index { struct Index {
private controller = MainArkUIViewController(); private controller: ArkUIViewController | undefined = undefined;
@State errorMessage: string = 'Native module not ready';
aboutToAppear() { aboutToAppear() {
hilog.info(DOMAIN, 'Compose', 'aboutToAppear'); hilog.info(DOMAIN, 'Compose', 'aboutToAppear');
//ArkTs生命周期断点处
if (nativeApi === undefined) {
hilog.error(DOMAIN, 'Compose', 'nativeApi is undefined, cannot create controller');
this.errorMessage = 'nativeApi is undefined';
return;
}
let unusedInt: number = 0;//未使用的变量 let unusedInt: number = 0;//未使用的变量
//ArkTs到Kotlin的单向跳转 //ArkTs到Kotlin的单向跳转
nativeApi.testArkTsToKotin(); nativeApi.testArkTsToKotin();
//原生内部跳转 //原生内部跳转
testArkTsToArkTs(); testArkTsToArkTs();
try {
this.controller = nativeApi.MainArkUIViewController();
const state = this.controller ? 'created' : 'not_created';
hilog.info(DOMAIN, 'Compose', 'controller init state: %{public}s', state);
if (!this.controller) {
this.errorMessage = 'Controller creation failed (returned null)';
}
} catch (e) {
hilog.error(DOMAIN, 'Compose', 'Exception creating controller: %{public}s', JSON.stringify(e));
this.errorMessage = 'Exception creating controller: ' + JSON.stringify(e);
}
} }
// 模拟耗时操作 // 模拟耗时操作
private loadData() {// 未使用的方法 private loadData() {// 未使用的方法
for (let i = 0; i < 10000; i++) { for (let i = 0; i < 10000; i++) {
// 模拟数据处理 // 模拟数据处理
let temp = i * i; let temp = i * i;
}
} }
}
build() { build() {
Column() { Column() {
Compose({ if (this.controller) {
controller: this.controller, Compose({
libraryName: 'entry', controller: this.controller,
onBackPressed: () => false libraryName: 'entry',
// onBackPressed: () => this.controller.onBackPress() 不能使用这个 会出现无限循环 onBackPressed: () => false
}) // onBackPressed: () => this.controller!.onBackPress() 不能使用这个 会出现无限循环
.width('100%') })
.height('100%') } else {
Text(this.errorMessage)
}
} }
.width('100%') .width('100%')
.height('100%') .height('100%')
} }
onPageShow() { onPageShow() {
this.controller.onPageShow(); if (this.controller) {
this.controller.onPageShow();
}
} }
onPageHide() { onPageHide() {
this.controller.onPageHide(); if (this.controller) {
this.controller.onPageHide();
}
} }
onBackPress(): boolean { onBackPress(): boolean {
return this.controller.onBackPress(); return this.controller ? this.controller.onBackPress() : false;
} }
//测试-自定义鸿蒙原生函数 //测试-自定义鸿蒙原生函数
} }
...@@ -115,5 +140,3 @@ export function testFindUsages(){ ...@@ -115,5 +140,3 @@ export function testFindUsages(){
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment