• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = (index: number, operate: number) => {
62    let tmpMissions = this.missionInfos
63    this.missionInfos = []
64    let mission = tmpMissions[index]
65    if (operate === MISSION_OPERATE.CHANGE_LOCK) {
66      if (mission.lockedState) {
67        MissionModel.unLockMission(mission.missionId)
68      } else {
69        MissionModel.lockMission(mission.missionId)
70      }
71      mission.lockedState = !mission.lockedState
72      tmpMissions[index] = mission
73      this.missionInfos = tmpMissions
74      this.missionsData['dataArray'] = this.missionInfos
75      this.missionsData.notifyDataReload()
76    } else {
77      MissionModel.clearMission(mission.missionId)
78      this.missionInfos = tmpMissions
79      animateTo({ duration: 700, curve: Curve.Ease }, () => {
80        this.missionInfos.splice(index, 1)
81        if (this.missionInfos.length === 0) {
82          this.showEmptyMessage = true
83        }
84        this.missionsData['dataArray'] = this.missionInfos
85        this.missionsData.notifyDataReload()
86      })
87    }
88  }
89
90  async clearAllMissions() {
91    await MissionModel.clearAllMission()
92    CommonEvent.publish(EventConstants.EVENT_CLEAR_RECENTS, () => {
93      Logger.info(TAG, `clearAllMission,then exit recents`)
94    })
95  }
96
97  build() {
98    Column() {
99      if (this.showEmptyMessage) {
100        Text($r('app.string.message_no_mission'))
101          .fontSize(20)
102          .fontColor(Color.White)
103          .alignSelf(ItemAlign.Center)
104      } else {
105        List({ space: 16 }) {
106          LazyForEach(this.missionsData, (item, index) => {
107            ListItem() {
108              MissionItem({ missionInfo: item, index: index, operateMission: this.handleOperateMission })
109            }
110          }, item => item.appName)
111        }
112        .listDirection(Axis.Horizontal)
113        .height('60%')
114        .width('100%')
115
116        Image($r("app.media.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}