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