• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 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 { FileInfo } from '../../../../../../common/src/main/ets/components/Data/FileInfo'
17import { getListFile } from '../../../../../../common/src/main/ets/components/Utils/FileManagerServiceUtils'
18import { logDebug, logInfo } from '../../../../../../common/src/main/ets/components/Utils/LogUtils'
19import { ChooseDialog } from '../../../../../../common/src/main/ets/components/View/ChooseDialog'
20
21let TAG: string = 'ThirdLevelHasPic'
22
23@Component
24export struct ThirdLevelHasPic {
25  private mColumnTemplate: string
26  private mCurrentMode: string
27  private mCurrentIndex: number
28  private mWorker
29  @Link mFileList: Array<FileInfo>
30  @Link mMenuLevel: number
31  @Link mType: string
32  @Link mCurrentPath: string
33  @StorageLink('choseFiles') tmpFiles: Array<FileInfo> = []
34
35  aboutToAppear() {
36    logInfo(TAG, 'aboutToAppear send message to worker')
37    if (globalThis.debugMode) {
38      this.mFileList = getListFile('local', this.mType, this.mCurrentPath)
39      logDebug(TAG, 'mFileList length = ' + this.mFileList.length)
40    } else {
41      this.mWorker.postMessage({
42        request_data: 'listFile',
43        device_name: 'local',
44        menu_level: this.mMenuLevel,
45        MediaType: this.mType,
46        path: this.mCurrentPath,
47        offset: 0,
48        count: 100,
49        context: globalThis.context,
50      })
51    }
52  }
53
54  aboutToDisappear() {
55    logInfo(TAG, 'aboutToDisappear')
56    AppStorage.Set('choseFiles', [])
57  }
58
59  build() {
60    Column() {
61      Flex({
62        direction: FlexDirection.Row,
63        wrap: FlexWrap.Wrap,
64        justifyContent: FlexAlign.Start,
65        alignItems: ItemAlign.Start
66      }) {
67        ForEach(this.mFileList, (item: FileInfo) => {
68          GridItemView({ gridItem: item })
69        }, item => item.name)
70      }
71      .margin({ top: 8 * 1.3 })
72    }
73    .alignItems(HorizontalAlign.Start)
74    .border({ radius: 24 })
75    .opacity(0.9)
76    .backgroundColor(Color.White)
77  }
78}
79
80@Component
81struct GridItemView {
82  private dialogController: CustomDialogController = new CustomDialogController({
83    builder: ChooseDialog(),
84    cancel: () => {
85    },
86    autoCancel: true
87  })
88  @State gridItem: FileInfo = new FileInfo('')
89  @StorageLink('choseFiles') @Watch('choseFilesChange') choseFiles: Array<FileInfo> = []
90
91  private choseFilesChange() {
92    this.choseFiles.forEach((item: FileInfo) => {
93      logInfo(TAG, 'choseFilesChange item.name =  ' + item.name + ' gridItem.name = ' + this.gridItem.name)
94      logInfo(TAG, 'choseFilesChange item.path =  ' + item.path + ' gridItem.path = ' + this.gridItem.path)
95      if (item.name == this.gridItem.name && item.path == this.gridItem.path) {
96        this.gridItem.opacity = 0.1
97        this.gridItem.fontColor = '#FFFFFF'
98        this.gridItem.listBackground = 'rgba(46,136,255,0.1)'
99      } else {
100        this.gridItem.opacity = 1
101        this.gridItem.fontColor = '#000000'
102        this.gridItem.listBackground = ''
103      }
104    })
105  }
106
107  build() {
108    Flex({
109      direction: FlexDirection.Column,
110      alignItems: ItemAlign.Center,
111      justifyContent: FlexAlign.Center
112    }) {
113      Image(`${this.gridItem.path}/thumbnail/160/160`)
114        .borderRadius(12)
115        .objectFit(ImageFit.Contain)
116        .width(80 * 1.3)
117        .height(80 * 1.3)
118        .margin({
119          left: 8 * 1.3,
120          right: 8 * 1.3,
121          top: 4 * 1.3
122        })
123      Row() {
124        Text(this.gridItem.name)
125          .width(82 * 1.3)
126          .fontSize(12 * 1.3)
127          .fontColor(this.gridItem.listBackground == 'rgba(46,136,255,0.1)' ? Color.White : Color.Black)
128          .fontWeight(FontWeight.Regular)
129          .opacity(0.9)
130          .textAlign(TextAlign.Center)
131          .maxLines(2)
132          .textOverflow({ overflow: TextOverflow.Ellipsis })
133          .margin({ top: 2 * 1.3 })
134          .padding({ right: 4 * 1.3, left: 4 * 1.3 })
135      }
136      .borderRadius(12)
137      .backgroundColor(this.gridItem.listBackground == 'rgba(46,136,255,0.1)' ? 0x2E8BFF : '')
138      .alignItems(VerticalAlign.Top)
139      .width(82 * 1.3)
140      .height(36 * 1.3)
141      .margin({ top: 4 * 1.3 })
142    }
143    .padding({ bottom: 8 * 1.3 })
144    .width(96 * 1.3)
145    .height(126 * 1.3)
146    .margin({ bottom: 12 * 1.3, left: 12 * 1.3 })
147    .border({ radius: 20 })
148    .backgroundColor(this.gridItem.listBackground)
149    .gesture(
150    LongPressGesture({ repeat: false })
151      .onAction((event: GestureEvent) => {
152        AppStorage.SetOrCreate('selectedFileInfo', this.gridItem)
153        this.dialogController.open()
154      })
155    )
156    .onClick(() => {
157      let choseFiles: Array<FileInfo> = AppStorage.Get('choseFiles')
158      let index: number = -1
159      choseFiles.forEach((item: FileInfo) => {
160        if (item.name == this.gridItem.name && item.path == this.gridItem.path) {
161          index = choseFiles.indexOf(item)
162        }
163      })
164      choseFiles.splice(0, choseFiles.length)
165      if (index == -1) {
166        this.gridItem.opacity = 0.1
167        this.gridItem.fontColor = '#FFFFFF'
168        this.gridItem.listBackground = 'rgba(46,136,255,0.1)'
169        choseFiles.push(this.gridItem)
170      } else {
171        this.gridItem.opacity = 1
172        this.gridItem.fontColor = '#000000'
173        this.gridItem.listBackground = ''
174      }
175    })
176
177  }
178}