• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2023 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
16import { FilesData, MkDirItem } from '../../../databases/model/FileData'
17import FileAccessExec from '../../../base/utils/FileAccessExec'
18import { toast, isValidFileName } from '../../../base/utils/Common'
19import { getResourceString } from '../../../base/utils/Tools'
20import Logger from '../../../base/log/Logger'
21import { DialogTitle, DialogButton, DialogButtonDivider } from '../common/DialogComponent'
22import { BusinessError } from '@ohos.base';
23
24const TAG = 'FileMkdirDialog'
25
26@Component
27@CustomDialog
28export struct FileMkdirDialog {
29  controller: CustomDialogController
30  cancel: Function = () => {};
31  confirm: Function = (data: MkDirItem) => {};
32  @State folderName: string = ''
33  @State errorText: Resource = $r('app.string.illegal');
34  fileItems: Array<FilesData> = [];
35  getCurrentDir: string = ''
36
37  aboutToAppear() {
38    this.folderName = this.getNewFolderName()
39  }
40
41  getNewFolderName(): string {
42    const newFolderText = getResourceString($r('app.string.addFolder'));
43    const regExp = new RegExp(`^${newFolderText}([ ]{1}[0-9]+)*$`);
44    const tempFolderList = this.fileItems.filter(item => item.isFolder && regExp.test(item.fileName));
45    let tempFolderName = newFolderText;
46    let index = 0;
47    while (tempFolderList.some(item => item.fileName === tempFolderName)) {
48      index += 1;
49      tempFolderName = newFolderText + ' ' + index.toString();
50    };
51    return tempFolderName;
52  }
53
54  isSameName() {
55    let nameArr: string[] = [];
56    this.fileItems.forEach(item => {
57      nameArr.push(item.fileName)
58    })
59    return nameArr.includes(this.folderName)
60  }
61
62  build() {
63    Column() {
64      DialogTitle({
65        title: $r('app.string.addFolder')
66      })
67      TextInput({ text: this.folderName })
68        .margin({
69          left: $r('app.float.text_input_margin_minus10'),
70          right: $r('app.float.text_input_margin_minus20')
71        })
72        .fontSize($r('app.float.common_font_size16'))
73        .backgroundColor($r('app.color.text_input_bg_color'))
74        .onSubmit(() => {
75        })
76        .onChange((value: string) => {
77          this.folderName = value
78          this.errorText =  $r('app.string.illegal')
79        })
80      Divider().vertical(false).strokeWidth(1).color(Color.Gray)
81        .margin({
82          bottom: $r('app.float.common_margin10')
83        })
84      Text(this.errorText)
85        .width('100%')
86        .padding({
87          top: $r('app.float.common_padding5'),
88          bottom: $r('app.float.common_padding10')
89        })
90        .fontSize($r('app.float.common_font_size14'))
91        .fontColor($r('app.color.error_message_color'))
92      Row() {
93        DialogButton({
94          text: $r('app.string.cancel'),
95          isDisabled: false,
96          click: () => {
97            this.controller.close()
98          }
99        })
100        DialogButtonDivider()
101        DialogButton({
102          text: $r('app.string.confirm'),
103          isDisabled: !this.folderName.trim(),
104          click: () => {
105            if (!isValidFileName(this.folderName)) {
106              this.errorText = $r('app.string.illegal')
107            } else if (this.isSameName()) {
108              this.errorText = $r('app.string.sameName')
109            } else {
110              FileAccessExec.createFolder(this.getCurrentDir, this.folderName).then((folderUri: string) => {
111                let data: MkDirItem = {
112                  mkdirName: this.folderName,
113                  path: folderUri
114                }
115                this.confirm(data)
116              }).catch((err: BusinessError) => {
117                Logger.e(TAG, 'create Folder err: ' + JSON.stringify(err))
118                toast($r('app.string.addFolder_fail'))
119              })
120              this.controller.close()
121            }
122          }
123        })
124      }.width('100%')
125      .margin({ bottom: $r('app.float.common_margin10') })
126    }.padding({
127      left: $r('app.float.common_margin24'),
128      right: $r('app.float.common_margin24')
129    })
130  }
131}
132
133