• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device 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 */
15
16// [Start distribute_child_thread]
17import { Calculator } from '../sdk/Calculator';
18import { TimerSdk } from '../sdk/TimerSdk';
19import { taskpool } from '@kit.ArkTS';
20
21@Concurrent
22function initCalculator(): Calculator {
23  return Calculator.init();
24}
25
26@Concurrent
27function initTimerSdk(): TimerSdk {
28  return TimerSdk.init();
29}
30
31@Entry
32@Component
33struct Index {
34  @State calculateAdd: string = 'calculate add';
35  @State showHistory: string = 'show history';
36  @State countdown: string = 'countdown';
37  calc?: Calculator;
38  timer?: TimerSdk;
39
40  aboutToAppear(): void {
41    taskpool.execute(initCalculator).then((ret) => {
42      this.calc = ret as Calculator;
43    })
44    taskpool.execute(initTimerSdk).then((ret) => {
45      this.timer = ret as TimerSdk;
46    })
47  }
48
49  build() {
50    Row() {
51      Column() {
52        Text(this.calculateAdd)
53          .id('add')
54          .fontSize(50)
55          .fontWeight(FontWeight.Bold)
56          .alignRules({
57            center: { anchor: '__container__', align: VerticalAlign.Center },
58            middle: { anchor: '__container__', align: HorizontalAlign.Center }
59          })
60          .onClick(async () => {
61            let result = this.calc?.add(1, 2)
62            console.info(`Result is ${result}`)
63            this.calculateAdd = 'success';
64          })
65        Text(this.showHistory)
66          .id('show')
67          .fontSize(50)
68          .fontWeight(FontWeight.Bold)
69          .alignRules({
70            center: { anchor: '__container__', align: VerticalAlign.Center },
71            middle: { anchor: '__container__', align: HorizontalAlign.Center }
72          })
73          .onClick(async () => {
74            this.calc?.showHistory();
75            this.showHistory = 'success';
76          })
77        Text(this.countdown)
78          .id('get')
79          .fontSize(50)
80          .fontWeight(FontWeight.Bold)
81          .alignRules({
82            center: { anchor: '__container__', align: VerticalAlign.Center },
83            middle: { anchor: '__container__', align: HorizontalAlign.Center }
84          })
85          .onClick(async () => {
86            console.info(`Timer start`);
87            await this.timer?.Countdown(1000);
88            console.info(`Timer end`);
89            this.countdown = 'success';
90          })
91      }
92      .width('100%')
93    }
94    .height('100%')
95  }
96}
97// [End distribute_child_thread]