1/* 2 * Copyright (c) 2022 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 prompt from '@ohos.prompt' 17import CommonEvent from '@ohos.commonEvent' 18import MissionItem from '../components/MissionItem' 19import MissionInfo from '../bean/MisssionInfo' 20import MissionModel from '../model/MissionModel' 21import { EventConstants, Logger, MyDataSource } from '@ohos/base' 22 23 24const TAG: string = 'RecentsLayout' 25const SUBSCRIBER_INFO = { 26 events: [ 27 EventConstants.EVENT_ENTER_RECENTS 28 ] 29} 30const MISSION_OPERATE = { 31 CHANGE_LOCK: 0, 32 CLEAR: 1 33} 34 35@Component 36export struct RecentsLayout { 37 @State missionInfos: Array<MissionInfo> = new Array<MissionInfo>() 38 @State showEmptyMessage: boolean = false 39 private subscriber = undefined 40 private missionsData: MyDataSource = new MyDataSource([]) 41 42 async aboutToAppear() { 43 await this.refreshMissionInfos() 44 this.subscriber = await CommonEvent.createSubscriber(SUBSCRIBER_INFO) 45 CommonEvent.subscribe(this.subscriber, this.refreshMissionInfos.bind(this)) 46 } 47 48 async refreshMissionInfos() { 49 let context = getContext(this) 50 this.missionInfos = await MissionModel.getMissionInfos(context) 51 Logger.info(TAG, `refreshRecentsData end,result = ${this.missionInfos.length}`) 52 this.missionsData['dataArray'] = this.missionInfos 53 this.missionsData.notifyDataReload() 54 if (this.missionInfos.length > 0) { 55 this.showEmptyMessage = false 56 } else { 57 this.showEmptyMessage = true 58 } 59 } 60 61 handleOperateMission = async (index: number, operate: number) => { 62 Logger.info(TAG, `handleOperateMission, index=${index}, operate=${operate}`); 63 this.missionInfos = this.missionsData['dataArray']; 64 this.missionsData['dataArray'] = []; 65 this.missionsData.notifyDataReload(); 66 if (this.missionInfos[index]) { 67 if (operate === MISSION_OPERATE.CHANGE_LOCK) { 68 if (this.missionInfos[index].lockedState) { 69 await MissionModel.unLockMission(this.missionInfos[index].missionId); 70 } else { 71 await MissionModel.lockMission(this.missionInfos[index].missionId); 72 } 73 } else { 74 await MissionModel.clearMission(this.missionInfos[index].missionId); 75 } 76 } 77 setTimeout(() => { 78 CommonEvent.publish(EventConstants.EVENT_ENTER_RECENTS, () => { 79 Logger.info(TAG, `enter recents`); 80 }) 81 }, 20) 82 } 83 84 async clearAllMissions() { 85 await MissionModel.clearAllMission() 86 CommonEvent.publish(EventConstants.EVENT_CLEAR_RECENTS, () => { 87 Logger.info(TAG, `clearAllMission,then exit recents`) 88 }) 89 } 90 91 build() { 92 Column() { 93 if (this.showEmptyMessage) { 94 Text($r('app.string.message_no_mission')) 95 .fontSize(20) 96 .fontColor(Color.White) 97 .alignSelf(ItemAlign.Center) 98 } else { 99 List({ space: 16 }) { 100 LazyForEach(this.missionsData, (item, index) => { 101 ListItem() { 102 MissionItem({ missionInfo: item, index: index, operateMission: this.handleOperateMission }) 103 } 104 }, item => item.appName) 105 } 106 .listDirection(Axis.Horizontal) 107 .height('60%') 108 .width('100%') 109 110 Image($r("app.media.ic_delete")) 111 .width(45) 112 .height(45) 113 .objectFit(ImageFit.Contain) 114 .margin({ top: 20 }) 115 .padding(5) 116 .borderRadius(25) 117 .backgroundColor('#77F5F5F5') 118 .onClick(this.clearAllMissions) 119 } 120 } 121 .width('100%') 122 .height('100%') 123 .justifyContent(FlexAlign.Center) 124 .onClick(() => { 125 CommonEvent.publish(EventConstants.EVENT_ENTER_HOME, () => { 126 Logger.info(TAG, `exit recents`) 127 }) 128 }) 129 } 130}