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