From 84f1c58a35c6a2619278d8babf7448d7082c6af9 Mon Sep 17 00:00:00 2001 From: xieyalong Date: Mon, 10 Oct 2022 14:21:45 +0800 Subject: [PATCH] add latelylist --- entry/src/main/ets/Components/LatelyList.ets | 288 +++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 entry/src/main/ets/Components/LatelyList.ets diff --git a/entry/src/main/ets/Components/LatelyList.ets b/entry/src/main/ets/Components/LatelyList.ets new file mode 100644 index 0000000..99f8ca7 --- /dev/null +++ b/entry/src/main/ets/Components/LatelyList.ets @@ -0,0 +1,288 @@ +import worker from '@ohos.worker'; +import statfs from '@ohos.statfs' +import router from '@system.router' +import { FileInfo} from '../Model/FileInfo' +import { FilesItem } from '../Item/FilesItem' +import { ImageItem } from '../Item/ImageItem' +import { VideoItem } from '../Item/VideoItem' +import { VoiceItem } from '../Item/VoiceItem' +import classIfy from './ClassifyTitle' + +@Preview +@Component +export struct LatelyList { + @State fileList: any = [] + @State FileListData: any = [] + @State imageList: any = [] + @State audioList: any = [] + @State videoList: any = [] + private date : Date + private mWorker + @State listData: Array = [] + @State columnsIndex: number = 4 + @State rowIndex: number = 1 + @State rowsTemplate: string = '' + @State columnsTemplate: string = '' + @State freeBytes: number = 0 + @State totalBytes: number = 0 + @State imageDate : string = '' + @State videoDate : string = '' + @State audioDate : string = '' + @State fileDate : string = '' + + aboutToAppear() { + console.log('aaa-->send message to worker') + this.initWork() + this.mWorker.postMessage({ + request_data: 'getRoot', + device_name: 'local', + media_type: '', + path: '' + }) + this.getStorageData() + } + + async getStorageData() { + + const bytesTotal = await statfs.getTotalBytes(globalThis.context.filesDir) + this.totalBytes = bytesTotal + + const bytesFree = await statfs.getFreeBytes(globalThis.context.filesDir) + this.freeBytes = bytesFree + + } + + getPercentage(num: number, total: number) { + if (num == 0 || total == 0) { + return 0 + } + return Number(Number((num / total) * 100).toFixed(2)) + } + + getSizeNum(num: number) { + if (num == 0) { + return 0 + } + return Number((num / 1024 / 1024 / 1024).toFixed(2)) + } + + private initWork() { + console.log('aaa-->initWorkerr') + if (this.mWorker == undefined) { + this.mWorker = new worker.Worker('workers/FilesWorker.js', { type: 'classic', name: 'LatelyList' }) + this.mWorker.onerror = this.onError.bind(this) + this.mWorker.onmessageerror = this.onMessageError.bind(this) + this.mWorker.onexit = this.onExit.bind(this) + this.mWorker.onmessage = this.onMessage.bind(this) + } + } + + private onError(e): void{ + console.log('aaa-->onError = ' + JSON.stringify(e)) + } + + private onMessageError(e): void{ + console.log('aaa-->onMessageError = ' + JSON.stringify(e)) + } + + private onExit(): void{ + console.log('aaa-->onExit') + this.mWorker = undefined + } + + //加了四个类型的请求 + private onMessage(result) { + var params = result.data.params + console.log('imageList'+params.request_data) + + var file = JSON.parse(String.fromCharCode.apply(null, new Uint16Array(result.data.data))) + if (Array.isArray(file)) { + if (params.request_data == 'getRoot') { + for (var index = 0;index < file.length; index++) { + var info = file[index] + var mediaType = '' + var name = '' + if (info.name == 'image_album') { + name = '图片' + mediaType = 'image' + } else if (info.name == 'video_album') { + name = '视频' + mediaType = 'video' + } else if (info.name == 'audio_album') { + name = '音乐' + mediaType = 'audio' + } else if (info.name == 'file_folder') { + name = '文档' + mediaType = 'file' + } + let item = new FileInfo(info.name, info.path, info.type, info.size, info.added_time, info.modified_time, mediaType, null, 0) + this.listData.push(item) + } + for (var index = 0; index < this.listData.length; index++) { + var item = this.listData[index] + console.log('imageList-' + item.name) + this.mWorker.postMessage({ + request_data: item.name, + device_name: 'local', + media_type: item.mediaType, + path: item.path + }) + } + } else if (params.media_type == 'image') { + this.filesList(file,'image') + } + else if (params.media_type == 'video') { + this.filesList(file,'video') + } + } else if (params.media_type == 'audio') { + this.filesList(file,'audio') + } + else if (params.media_type == 'file') { + this.filesList(file,'file') + } + } + + build() { + Column() { + Flex({ justifyContent: FlexAlign.Start, alignItems:ItemAlign.Center }) { + Image($r('app.media.search_note')).width(20).height(20).margin({left:10}) + Text( $r('app.string.searchNote') ) + .backgroundColor('#f7f8f9') + .fontSize(16) + .fontColor(Color.Gray) + .margin({left:6}) + } + .backgroundColor('#f7f8f9') + .height(60) + .opacity(1) + .margin(20) + .borderRadius(10) + .border({ width: 2, color: '#e4e4e4' }) + .onClick(() => { + //LogInfo(this.TAG ,'onClick pages/SearchPage2') + router.push({ + uri: 'pages/FileLevelList', + }) + }) + + + if(this.imageList.length > 0){ + classIfy({title:'图片',rightText :this.imageDate}) + Grid() { + //循环创建item横列显示最多三个 + ForEach(this.imageList, (item) => { + GridItem() { + Column({space : 3}){ + ImageItem({image : item.path}) + } + } + .width('100%') + .height('100%') + .onClick(() => { + router.push({ + uri:"pages/ImagePreview", + params: { + title:item.name, + image:item.path + } + }) + }) + + }) + } + .columnsTemplate("1fr 1fr 1fr") + .rowsTemplate('1fr') + .rowsGap(10) + .width('100%') + .height(200) + .margin({top:10,right:10}) + } + if(this.fileList.length > 0){ + classIfy({title:'文件',rightText : this.fileDate}) + Grid() { + //循环创建item横列显示最多三个 + ForEach(this.fileList, (item) => { + GridItem() { + Column({space : 3}){ + FilesItem({filesImage: item.path,fileNumber : item.size,fileTime : item.modified_time, filesText : item.name}) + } + } + }) + } + .columnsTemplate("1fr 1fr 1fr") + .rowsTemplate('1fr') + .rowsGap(10) + .width('100%') + .height(200) + .margin({top:10,right:10}) + } + if(this.audioList.length > 0){ + classIfy({title:'音频',rightText :this.audioDate}) + Grid() { + //循环创建item横列显示最多三个 + ForEach(this.audioList, (item) => { + GridItem() { + Column({space : 1}){ + VoiceItem({voiceImage : item.path,voiceNumber : item.size , voiceTime : item.modified_time,voiceText: item.name}) + } + } + }) + } + .columnsTemplate("1fr") + .rowsTemplate('1fr') + .rowsGap(10) + .width('100%') + .height(100) + .margin({top:10,right:10}) + } + if(this.videoList.length > 0){ + classIfy({title:'视频',rightText :this.videoDate}) + Grid() { + //循环创建item横列显示最多三个 + ForEach(this.videoList, (item) => { + GridItem() { + Column({space : 3}){ + VideoItem({src : item.path}) + } + } + }) + } + .columnsTemplate("1fr 1fr 1fr") + .rowsTemplate('1fr') + .rowsGap(10) + .width('100%') + .height(200) + .margin({top:10,right:10}) + } + + } + } + + filesList(file,type){ + for (var i = 0; i < file.length; i++) { + var fileItem = file[i] + this.date = new Date(fileItem.modified_time) + var Y = this.date.getFullYear() + '-'; + var M = (this.date.getMonth()+1 < 10 ? '0'+(this.date.getMonth()+1) : this.date.getMonth()+1) + '-'; + var D = this.date.getDate() + ' '; + if(type == 'image'){ + this.imageDate = Y+M+D + let imageData = new FileInfo(fileItem.name, fileItem.path, fileItem.type, fileItem.size, fileItem.added_time,this.imageDate, fileItem.media_type, null, 0) + this.imageList.push(imageData) + }else if (type == 'video') { + this.videoList = Y+M+D + let videoData = new FileInfo(fileItem.name, fileItem.path, fileItem.type, fileItem.size, fileItem.added_time,this.imageDate, fileItem.media_type, null, 0) + this.videoList.push(videoData) + }else if(type == 'audio'){ + this.audioDate = Y+M+D + let audioData = new FileInfo(fileItem.name, fileItem.path, fileItem.type, fileItem.size, fileItem.added_time,this.imageDate, fileItem.media_type, null, 0) + this.audioList.push(audioData) + }else if(type == 'file'){ + this.fileDate = Y+M+D + let fileData = new FileInfo(fileItem.name, fileItem.path, fileItem.type, fileItem.size, fileItem.added_time,this.imageDate, fileItem.media_type, null, 0) + this.fileList.push(fileData) + } + + } + } +} \ No newline at end of file -- Gitee