• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 MyWorker from '../fileFs/MyWorker';
16import common from '@ohos.app.ability.common';
17import fs from '@ohos.file.fs';
18import router from '@ohos.router';
19import { Logger } from '../common/Common';
20
21const TAG: string = '[ConcurrentModule].[CopyFile]';
22@Entry
23@Component
24struct Watcher {
25  @State message: Resource = $r('app.string.copyFileText');
26  @State myContext: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
27  @State filePathSize: Array<number> = [];
28  @State showFilePath: Array<string> = [];
29  @State eventFilePath: Array<string> = [];
30  @State fileListNames: Array<string> = [];
31  @StorageLink('fileNumber') fileNum: string = '0';
32  @StorageLink('fileListName1') fileListName1: string = ' ';
33  @StorageLink('fileListName2') fileListName2: string = '';
34  @StorageLink('copyFileLog1') copyFileLog1: string = '';
35  @StorageLink('copyFileLog2') copyFileLog2: string = '';
36  @StorageLink('copyFileLog3') copyFileLog3: string = '';
37  @StorageLink('copyFileLog4') copyFileLog4: string = '';
38  @StorageLink('copyFileShowLog') copyFileShowLog: string = '';
39  myWorker: MyWorker = new MyWorker();
40  scroller: Scroller = new Scroller();
41  public dirPath: string = '';
42  public baseDir: string | undefined = AppStorage.Get('sanBoxFileDir');
43
44  onPageShow() {
45    this.myWorker.readyFilesToWorker();
46    let filePathDir = this.baseDir + '/workerDir';
47    let filenames = fs.listFileSync(filePathDir);
48    Logger.info(TAG, 'listFile succeed');
49    for (let i = 0; i < filenames.length; i++) {
50      Logger.info(TAG, 'filename: %s', filenames[i]);
51      this.showFilePath[i] = filenames[i];
52      let filePath = filePathDir + '/' + filenames[i]
53      this.filePathSize[i] = fs.statSync(filePath).size;
54    }
55    this.dirPath = this.baseDir + '/workerCopy';
56    if (!fs.accessSync(this.dirPath)) {
57      fs.mkdirSync(this.dirPath);
58    }
59  }
60
61  onPageHide() {
62    this.dirPath = this.baseDir + '/workerCopy';
63    let isDirectory = fs.statSync(this.dirPath).isDirectory();
64    if (isDirectory) {
65      fs.rmdirSync(this.dirPath);
66      AppStorage.SetOrCreate('fileNumber', '0');
67      AppStorage.SetOrCreate('fileListName1', '');
68      AppStorage.SetOrCreate('fileListName2', '');
69      AppStorage.SetOrCreate('copyFileShowLog', '');
70    }
71    let filePathDir = this.baseDir + '/workerDir';
72    let isDir = fs.statSync(filePathDir).isDirectory();
73    if (isDir) {
74      fs.rmdirSync(filePathDir);
75    }
76  }
77
78  build() {
79    Scroll(this.scroller) {
80      Row() {
81        Column() {
82          Row() {
83            Image($r('app.media.ic_back'))
84              .id('backIndex2')
85              .width(20)
86              .height(18)
87              .align(Alignment.Start)
88              .margin({ top: 13, bottom: 15, left: 26, right: 18 })
89              .onClick(() => {
90                router.back();
91              })
92            Text(this.message)
93              .fontSize(20)
94              .fontColor('#182431')
95              .textAlign(TextAlign.Start)
96              .lineHeight(28)
97              .fontWeight(700)
98              .margin({ top: 13, bottom: 15 })
99          }
100          .width('100%')
101          .height(56)
102          .backgroundColor('#f1f3f5')
103          .align(Alignment.Start)
104
105          Flex({ direction: FlexDirection.Column }) {
106
107            Column() {
108              Row() {
109                Row() {
110                  Text($r('app.string.workerTitleText'))
111                    .fontSize(14)
112                    .margin({ top: 19.5, bottom: 9.5 })
113                    .lineHeight(19)
114                    .width(176)
115                    .fontColor('#182431')
116                    .fontWeight(500)
117                    .textAlign(TextAlign.Start)
118                }
119                .width('100%')
120                .backgroundColor('#f1f3f5')
121              }
122              .margin({ left: 24, right: 52 })
123              .height(48)
124              .backgroundColor('#f1f3f5')
125
126              Column() {
127                List({ space: 12, initialIndex: 0 }) {
128                  ForEach(this.showFilePath, (item: string, index: number) => {
129                    ListItem() {
130                      Row() {
131                        Text(item)
132                          .fontSize(16)
133                          .fontColor('#182431')
134                          .width('86%')
135                          .lineHeight(22)
136                          .textAlign(TextAlign.Start)
137                          .fontWeight(500)
138                          .margin({ left: 12 })
139                          .borderRadius(10)
140                          .backgroundColor(0xFFFFFF)
141                        Row() {
142                          Checkbox()
143                            .select(false)
144                            .id('checkbox' + index)
145                            .width(20)
146                            .height(20)
147                            .margin({ right: 12 })
148                            .selectedColor('#007DFF')
149                            .borderRadius(4)
150                            .onChange((value: boolean) => {
151                              Logger.info(TAG, 'Checkbox1 change is' + value);
152                              if (value) {
153                                Logger.info(TAG, 'Workerets Checkbox1 index ' + index + ' is true');
154                                this.eventFilePath.push(this.showFilePath[index]);
155                              } else {
156                                for (let i: number = 0; i < this.eventFilePath.length; i++) {
157                                  if (this.eventFilePath[i] === this.showFilePath[index]) {
158                                    this.eventFilePath[i] = 'deletedTag';
159                                  }
160                                }
161                              }
162                            })
163                        }
164                        .id('row' + index)
165                        .width('9%')
166                      }
167                      .borderRadius(20)
168                      .margin({ left: 12, right: 12 })
169                      .height(56)
170                      .backgroundColor(0xFFFFFF)
171                    }
172                    .id('listItem')
173                  }, (item: string) => item)
174                }
175                .id('listWorkerFile')
176                .alignListItem(ListItemAlign.Center)
177                .scrollBar(BarState.Auto)
178              }
179              .backgroundColor('#f1f3f5')
180              .height(436)
181              .align(Alignment.Center)
182
183              Column() {
184                Row() {
185                  Text($r('app.string.logTitle'))
186                    .fontSize(14)
187                    .fontColor('#182431')
188                    .textAlign(TextAlign.Start)
189                    .lineHeight(19)
190                    .fontWeight(500)
191                    .margin({ top: 19.5, left: 24 })
192                    .width(176)
193                }
194                .width('100%')
195                .align(Alignment.Start)
196
197                Column() {
198                  Column() {
199                    Text(this.copyFileLog1 + this.fileNum + this.copyFileLog2)
200                      .fontSize(16)
201                      .fontColor('#182431')
202                      .fontWeight(400)
203                      .width('100%')
204                    Text(this.copyFileShowLog)
205                      .fontSize(16)
206                      .id("copyFileLog")
207                      .fontColor('#182431')
208                      .fontWeight(400)
209                      .width('100%')
210                  }
211                  .margin({ top: 8, left: 12, right: 12, bottom: 8 })
212                  .height(64)
213                }
214                .borderRadius(20)
215                .height(80)
216                .margin({ top: 9.5, left: 16, right: 16 })
217                .backgroundColor('#ffffff')
218
219                Row({ space: 1 }) {
220                  Column() {
221                    Button($r('app.string.copyFileText'))
222                      .fontSize(16)
223                      .width(312)
224                      .height(40)
225                      .fontColor('#FFFFFF')
226                      .fontWeight(500)
227                      .id('copyFile')
228                      .onClick(async () => {
229                        this.myWorker.deleteCopyFile(this.dirPath);
230                        await this.myWorker.workToCopyFiles(this.eventFilePath, this.dirPath);
231                      })
232                  }
233                  .width('100%')
234                  .align(Alignment.End)
235                }
236                .align(Alignment.Center)
237                .width('100%')
238                .margin({ top: 24, left: 24, right: 24, bottom: 24 })
239              }
240              .backgroundColor('#f1f3f5')
241              .width('100%')
242              .height(216)
243            }
244            .backgroundColor('#f1f3f5')
245            .width('100%')
246          }
247          .width('100%')
248        }
249        .height('100%')
250      }
251    }
252  }
253}