• 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 missionManager from '@ohos.application.missionManager'
17import Logger from '../modle/Logger'
18import Mission from '../modle/Mission'
19import { MissionInfoComponent } from '../common/MissionInfoComponent'
20import { TitleBar } from '../common/TitleBar'
21
22const TAG: string = 'Index'
23const NUMMAX: number = 10
24
25class BasicDataSource implements IDataSource {
26  private listeners: DataChangeListener[] = []
27
28  public totalCount(): number {
29    return 0
30  }
31
32  public getData(index: number): any {
33    return undefined
34  }
35
36  registerDataChangeListener(listener: DataChangeListener): void {
37    if (this.listeners.indexOf(listener) < 0) {
38      this.listeners.push(listener)
39    }
40  }
41
42  unregisterDataChangeListener(listener: DataChangeListener): void {
43    const pos = this.listeners.indexOf(listener);
44    if (pos >= 0) {
45      this.listeners.splice(pos, 1)
46    }
47  }
48
49  notifyDataReload(): void {
50    this.listeners.forEach(listener => {
51      listener.onDataReloaded()
52    })
53  }
54
55  notifyDataAdd(index: number): void {
56    this.listeners.forEach(listener => {
57      listener.onDataAdded(index)
58    })
59  }
60
61  notifyDataChange(index: number): void {
62    this.listeners.forEach(listener => {
63      listener.onDataChanged(index)
64    })
65  }
66}
67
68class MyDataSource extends BasicDataSource {
69  public dataArray: Mission[] = []
70
71  constructor(ele) {
72    super()
73    for (var index = 0;index < ele.length; index++) {
74      this.dataArray.push(ele[index])
75    }
76  }
77
78  public totalCount(): number {
79    return this.dataArray.length
80  }
81
82  public getData(index: number): any {
83    return this.dataArray[index]
84  }
85
86  public addData(index: number, data: Mission): void {
87    this.dataArray.splice(index, 0)
88    this.notifyDataAdd(index)
89  }
90}
91
92@Entry
93@Component
94struct Index {
95  @State missionInfos: MyDataSource = new MyDataSource([])
96  @State listenerId: number = 0
97  @State isRefreshing: boolean = false
98  @State refreshMission: Resource = $r('app.string.get_mission_manager')
99  @State isShow: boolean = true
100
101  onMissionCreatedCallback() {
102    Logger.info(TAG, `onMissionCreatedCallback Created`)
103  }
104
105  onMissionDestroyedCallback() {
106    Logger.info(TAG, `onMissionDestroyedCallback destroyed`)
107  }
108
109  onMissionSnapshotChangedCallback() {
110    Logger.info(TAG, `onMissionSnapshotChangedCallback  snapshot changed`)
111  }
112
113  onMissionMovedToFrontCallback() {
114    Logger.info(TAG, `onMissionMovedToFrontCallback moved to fornt`)
115  }
116
117  onMissionIconUpdatedCallback() {
118    Logger.info(TAG, `onMissionIconUpdatedCallback icon has changed`)
119  }
120
121  handlerRefresh = () => {
122    Logger.info(TAG, `clickCallback start`)
123    this.isShow = false
124    missionManager.getMissionInfos('', NUMMAX, (error, data) => {
125      var missionInfos = []
126      setTimeout(() => {
127        missionInfos = data.map((item) => {
128          return item
129        })
130        this.missionInfos['dataArray'] = missionInfos
131        this.missionInfos.notifyDataReload()
132        Logger.info(TAG, `getMissionInfos missionInfos= ${JSON.stringify(this.missionInfos)}`)
133      }, 1000)
134    })
135  }
136
137  aboutToAppear() {
138    let listener = {
139      onMissionCreated: this.onMissionCreatedCallback,
140      onMissionDestroyed: this.onMissionDestroyedCallback,
141      onMissionSnapshotChanged: this.onMissionSnapshotChangedCallback,
142      onMissionMovedToFront: this.onMissionMovedToFrontCallback,
143      onMissionIconUpdated: this.onMissionIconUpdatedCallback
144    }
145
146    this.listenerId = missionManager.registerMissionListener(listener)
147    Logger.info(TAG, `aboutToAppear ${this.listenerId}`)
148  }
149
150  aboutToDisappear() {
151    missionManager.unregisterMissionListener(this.listenerId)
152  }
153
154  build() {
155    Column() {
156      TitleBar()
157      Text(this.isShow ? this.refreshMission : '')
158        .fontSize(30)
159        .margin(10)
160      Refresh({ refreshing: this.isRefreshing, offset: 120, friction: 100 }) {
161        List({ space: 20, initialIndex: 0 }) {
162          LazyForEach(this.missionInfos, (item) => {
163            ListItem() {
164              MissionInfoComponent({ mission: item, handlerRefresh: this.handlerRefresh })
165            }
166          }, item => item.timestamp)
167        }
168        .width('100%')
169        .height('90%')
170      }
171      .width('100%')
172      .onRefreshing(this.handlerRefresh)
173    }
174    .width('100%')
175    .height('100%')
176  }
177}