1/*
2* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15import napitest from 'libnapitest.so';
16import testEntry from 'libentry.so';
17
18let ns: napitest.NodeISayHello =  new napitest.NodeISayHello();
19class NodeISayHelloListenerImpl {
20  onSayHelloStart(info: object) {
21    AppStorage.SetOrCreate("textInfoStart", JSON.stringify(info))
22    console.log('napiTestDemo ----onSayHelloStart', JSON.stringify(info));
23  }
24  onSayHelloEnd(info: object) {
25    console.info("napiTestDemo onSayHelloEnd info type is : " + typeof (info));
26    AppStorage.SetOrCreate("textInfoEnd", JSON.stringify(info))
27    console.log('napiTestDemo ----onSayHelloEnd.', JSON.stringify(info));
28  }
29}
30
31let listener: NodeISayHelloListenerImpl = new NodeISayHelloListenerImpl()
32
33// register注册的回调
34function onCallbackfunnm(wid: number) {
35  AppStorage.SetOrCreate("callBackNum", JSON.stringify(wid))
36  console.info("napiTestDemo ----onCallbackfunnm wid = " + wid)
37  return "ocCallbackfuncnm";
38}
39
40@Entry
41@Component
42struct Index {
43  @State addResult: string = '';
44  @State promiseRes: string = ''
45  @State returnVal: string = ''
46  @StorageLink("textInfoStart")textInfoStart: string = ""
47  @StorageLink("textInfoEnd")textInfoEnd: string = ""
48  @StorageLink("callBackNum")callBackNum: string = ""
49
50  build() {
51    Row() {
52      Column() {
53        Button() {
54          Text('注册object回调后SayHello调用回调')
55            .fontSize(20)
56            .fontWeight(FontWeight.Bold)
57        }
58        .type(ButtonType.Capsule)
59        .margin({
60          top: 10
61        })
62        .backgroundColor('#0D9FFB')
63        .width('90%')
64        .height('5%')
65        .onClick(() => {
66          AppStorage.SetOrCreate("textInfoStart", "")
67          AppStorage.SetOrCreate("textInfoEnd", "")
68          // 注册回调
69          ns.addSayHelloListener(listener);
70          // 调用回调
71          ns.sayHello("js1", "native1", napitest.SayType.kInitiative);
72        })
73
74        Button() {
75          Text('注销object回调后SayHello调用回调')
76            .fontSize(20)
77            .fontWeight(FontWeight.Bold)
78        }
79        .type(ButtonType.Capsule)
80        .margin({
81          top: 10
82        })
83        .backgroundColor('#0D9FFB')
84        .width('90%')
85        .height('5%')
86        .onClick(() => {
87          AppStorage.SetOrCreate("textInfoStart", "")
88          AppStorage.SetOrCreate("textInfoEnd", "")
89          // 注销回调 removeSayHelloListener
90          ns.removeSayHelloListener(listener);
91          // 调用回调
92          ns.sayHello("js2", "native2", napitest.SayType.kInitiative);
93        })
94
95        // promise回调
96        Button() {
97          Text('Promise 回调')
98            .fontSize(20)
99            .fontWeight(FontWeight.Bold)
100        }
101        .type(ButtonType.Capsule)
102        .margin({
103          top: 10
104        })
105        .backgroundColor('#0D9FFB')
106        .width('90%')
107        .height('5%')
108        .onClick(async () => {
109          await ns.sayHelloWithResponse("response from", "response to", napitest.SayType.kResponse).then((ret: object) => {
110            this.promiseRes = JSON.stringify(ret);
111            console.info("napiTestDemo ----sayHelloWithResponse ret = " + JSON.stringify(ret));
112          });
113        })
114
115        Button() {
116          Text('register回调后sayHi调用回调')
117            .fontSize(20)
118            .fontWeight(FontWeight.Bold)
119        }
120        .type(ButtonType.Capsule)
121        .margin({
122          top: 10
123        })
124        .backgroundColor('#0D9FFB')
125        .width('90%')
126        .height('5%')
127        .onClick( () => {
128          AppStorage.SetOrCreate("callBackNum", "")
129          // 注册回调
130          ns.registerCallbackfunc(onCallbackfunnm);
131          // 调用回调
132          ns.sayHi("js3", "native3", napitest.SayType.kResponse);
133        })
134
135        Button() {
136          Text('unRegister回调后sayHi调用回调')
137            .fontSize(20)
138            .fontWeight(FontWeight.Bold)
139        }
140        .type(ButtonType.Capsule)
141        .margin({
142          top: 10
143        })
144        .backgroundColor('#0D9FFB')
145        .width('90%')
146        .height('5%')
147        .onClick( () => {
148          AppStorage.SetOrCreate("callBackNum", "")
149          // 注销回调
150          ns.unRegisterCallbackfunc(onCallbackfunnm);
151          // 调用回调
152          ns.sayHi("js4", "native4", napitest.SayType.kResponse);
153        })
154
155        // 调用普通函数
156        Button() {
157          Text('调用funcTest方法')
158            .fontSize(20)
159            .fontWeight(FontWeight.Bold)
160        }
161        .type(ButtonType.Capsule)
162        .margin({
163          top: 10
164        })
165        .backgroundColor('#0D9FFB')
166        .width('90%')
167        .height('5%')
168        .onClick( () => {
169          this.returnVal = napitest.funcTest(false);
170          console.info("napiTestDemo ----funcTest returnVal = " + this.returnVal)
171        })
172
173        // 调用其他模块的方法
174        Button() {
175          Text('调用entry模块的方法')
176            .fontSize(20)
177            .fontWeight(FontWeight.Bold)
178        }
179        .type(ButtonType.Capsule)
180        .margin({
181          top: 10
182        })
183        .backgroundColor('#0D9FFB')
184        .width('90%')
185        .height('5%')
186        .onClick( () => {
187          this.addResult = testEntry.add(2, 3).toString();
188          console.info("napiTestDemo ----add addResult = " + this.addResult)
189        })
190
191        Text('promise回调: promiseResult = ' + this.promiseRes).margin({ top: 10 })
192        Text('sayHelloStart回调: info = ' + this.textInfoStart).margin({ top: 10 })
193        Text('sayHelloEnd回调: info = ' + this.textInfoEnd).margin({ top: 10 })
194        Text('register注册的回调: wid = ' + this.callBackNum).margin({ top: 10 })
195        Text('普通方法funcTest返回值: returnVal = ' + this.returnVal).margin({ top: 10 })
196        Text('libentry模块: 2 + 3 = ' + this.addResult).margin({ top: 10 })
197      }
198      .width('100%')
199    }
200    .height('100%')
201  }
202}
203