• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 { fileUtils } from '../utils/FileUtils';
17
18@Preview
19@Component
20export struct FileBrowse {
21  @State folders: Array<string> = ['folder'];
22  @State files: Array<string> = [];
23  @State currentFolder: string = '';
24  @Link downloadDir: string;
25
26  aboutToAppear() {
27    fileUtils.listFolders(this.downloadDir).then((folders: Array<string>) => {
28      this.folders = folders;
29    })
30  }
31
32  build() {
33    Navigation() {
34      List({ space: 12 }) {
35        ForEach(this.folders, (item: string) => {
36          ListItem() {
37            NavRouter() {
38              Row() {
39                Image($r('app.media.ic_files_folder'))
40                  .size({ width: 32, height: 26 })
41                  .objectFit(ImageFit.Contain)
42                Text(item)
43                  .fontSize(16)
44                  .width('100%')
45                  .margin({ left: 12 })
46              }
47              .height(56)
48              .padding({ left: 16 })
49              .backgroundColor(Color.White)
50              .borderRadius(24)
51
52              NavDestination() {
53                this.FilesView()
54              }
55              .title(this.CustomTitle(item))
56              .backgroundColor($r('app.color.light_gray'))
57            }
58            .onStateChange(async (isActivated: boolean) => {
59              if (isActivated) {
60                this.currentFolder = item;
61                this.files = await fileUtils.listFiles(this.downloadDir,item);
62              }
63            })
64          }
65        })
66      }
67      .padding({ left: 12, right: 12 })
68    }
69    .hideBackButton(false)
70    .titleMode(NavigationTitleMode.Mini)
71    .title($r('app.string.download_files_title'))
72    .mode(NavigationMode.Stack)
73    .backgroundColor($r('app.color.light_gray'))
74  }
75
76  @Builder
77  CustomTitle(title: string) {
78    Row() {
79      Text(title)
80        .fontSize(20)
81        .fontColor($r('app.color.text_normal'))
82        .fontWeight(700)
83        .margin({ left: 8 })
84    }
85    .width('100%')
86  }
87
88  @Builder
89  FilesView() {
90    Column() {
91      List({ space: 12 }) {
92        if (this.files.length === 0) {
93          ListItem() {
94            Text($r('app.string.folder_empty'))
95              .fontSize(16)
96              .width('100%')
97              .margin({ top: 50 })
98              .textAlign(TextAlign.Center)
99          }
100        }
101        ForEach(this.files, (item: string) => {
102          ListItem() {
103            Text(decodeURIComponent(item))
104              .fontSize(16)
105              .width('100%')
106          }
107          .padding(12)
108          .height(48)
109          .backgroundColor(Color.White)
110          .borderRadius(24)
111        })
112      }
113      .padding({ left: 12, right: 12 })
114      .layoutWeight(1)
115
116      Column() {
117        Button() {
118          Image($r('app.media.ic_public_delete'))
119            .objectFit(ImageFit.Cover)
120            .size({ width: 24, height: 24 })
121        }
122        .type(ButtonType.Circle)
123        .width(40)
124        .height(40)
125        .backgroundColor('#FF0000')
126        .margin({ left: 5 })
127
128        Text($r('app.string.clear_folder'))
129          .fontSize(14)
130          .fontColor($r('app.color.text_normal'))
131          .opacity(0.6)
132          .margin({ top: 8 })
133      }
134      .margin({ bottom: 24, top: 6 })
135      .onClick(() => {
136        fileUtils.clearFolder(this.downloadDir,this.currentFolder);
137        this.files = [];
138      })
139    }
140    .height('100%')
141    .backgroundColor($r('app.color.light_gray'))
142  }
143}