• 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 access_global_config]
17import { config } from './Config';
18import { taskpool } from '@kit.ArkTS';
19
20@Concurrent
21async function download() {
22  if (!await config.isWifiOn()) {
23    console.info('wifi is off');
24    return false;
25  }
26  if (!await config.getIsLogin()) {
27    console.info('not login');
28    return false;
29  }
30  console.info(`User[${await config.getUser()}] start download ...`);
31  return true;
32}
33
34@Entry
35@Component
36struct Index {
37  @State message: string = 'not login';
38  @State wifiState: string = 'wifi off';
39  @State downloadResult: string = '';
40  input: string = '';
41
42  build() {
43    Row() {
44      Column() {
45        Text(this.message)
46          .fontSize(50)
47          .fontWeight(FontWeight.Bold)
48          .alignRules({
49            center: { anchor: '__container__', align: VerticalAlign.Center },
50            middle: { anchor: '__container__', align: HorizontalAlign.Center }
51          })
52        TextInput({ placeholder: '请输入用户名' })
53          .id('textInput')
54          .fontSize(20)
55          .fontWeight(FontWeight.Bold)
56          .alignRules({
57            center: { anchor: '__container__', align: VerticalAlign.Center },
58            middle: { anchor: '__container__', align: HorizontalAlign.Center }
59          })
60          .onChange((value) => {
61            this.input = value;
62          })
63        Text('login')
64          .fontSize(50)
65          .fontWeight(FontWeight.Bold)
66          .alignRules({
67            center: { anchor: '__container__', align: VerticalAlign.Center },
68            middle: { anchor: '__container__', align: HorizontalAlign.Center }
69          })
70          .onClick(async () => {
71            if (!await config.getIsLogin() && this.input) {
72              this.message = 'login: ' + this.input;
73              config.login(this.input);
74            }
75          })
76          .backgroundColor(0xcccccc)
77        Text('logout')
78          .fontSize(50)
79          .fontWeight(FontWeight.Bold)
80          .alignRules({
81            center: { anchor: '__container__', align: VerticalAlign.Center },
82            middle: { anchor: '__container__', align: HorizontalAlign.Center }
83          })
84          .onClick(async () => {
85            if (await config.getIsLogin()) {
86              this.message = 'not login';
87              config.logout();
88            }
89          })
90          .backgroundColor(0xcccccc)
91        Text(this.wifiState)
92          .fontSize(50)
93          .fontWeight(FontWeight.Bold)
94          .alignRules({
95            center: { anchor: '__container__', align: VerticalAlign.Center },
96            middle: { anchor: '__container__', align: HorizontalAlign.Center }
97          })
98        Toggle({ type: ToggleType.Switch })
99          .onChange(async (isOn: boolean) => {
100            await config.setWifiState(isOn)
101            this.wifiState = isOn ? 'wifi on' : 'wifi off';
102          })
103        Text('download')
104          .fontSize(50)
105          .fontWeight(FontWeight.Bold)
106          .alignRules({
107            center: { anchor: '__container__', align: VerticalAlign.Center },
108            middle: { anchor: '__container__', align: HorizontalAlign.Center }
109          })
110          .onClick(async () => {
111            let ret = await taskpool.execute(download);
112            this.downloadResult = ret ? 'download success' : 'download fail';
113          })
114        Text(this.downloadResult)
115          .fontSize(20)
116          .fontWeight(FontWeight.Bold)
117          .alignRules({
118            center: { anchor: '__container__', align: VerticalAlign.Center },
119            middle: { anchor: '__container__', align: HorizontalAlign.Center }
120          })
121      }
122      .width('100%')
123    }
124    .height('100%')
125  }
126}
127// [End access_global_config]