• 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 mediaLibrary from '@ohos.multimedia.mediaLibrary'
17
18class BasicDataSource implements IDataSource {
19  private listeners: DataChangeListener[] = []
20
21  public totalCount(): number {
22    return 0
23  }
24
25  public getData(index: number): any {
26    return undefined
27  }
28
29  registerDataChangeListener(listener: DataChangeListener): void {
30    if (this.listeners.indexOf(listener) < 0) {
31      console.info('add listener')
32      this.listeners.push(listener)
33    }
34  }
35
36  unregisterDataChangeListener(listener: DataChangeListener): void {
37    const pos = this.listeners.indexOf(listener);
38    if (pos >= 0) {
39      console.info('remove listener')
40      this.listeners.splice(pos, 1)
41    }
42  }
43
44  notifyDataReload(): void {
45    this.listeners.forEach(listener => {
46      listener.onDataReloaded()
47    })
48  }
49
50  notifyDataAdd(index: number): void {
51    this.listeners.forEach(listener => {
52      listener.onDataAdd(index)
53    })
54  }
55
56  notifyDataChange(index: number): void {
57    this.listeners.forEach(listener => {
58      listener.onDataChange(index)
59    })
60  }
61
62  notifyDataDelete(index: number): void {
63    this.listeners.forEach(listener => {
64      listener.onDataDelete(index)
65    })
66  }
67
68  notifyDataMove(from: number, to: number): void {
69    this.listeners.forEach(listener => {
70      listener.onDataMove(from, to)
71    })
72  }
73}
74
75export default class MediaDataSource extends BasicDataSource {
76  private dataArray: mediaLibrary.FileAsset[] = []
77
78  public totalCount(): number {
79    return this.dataArray.length
80  }
81
82  public getData(index: number): mediaLibrary.FileAsset {
83    return this.dataArray[index]
84  }
85
86  public addData(index: number, data: mediaLibrary.FileAsset): void {
87    this.dataArray.splice(index, 0, data)
88    this.notifyDataAdd(index)
89  }
90
91  public pushData(data: mediaLibrary.FileAsset): void {
92    this.dataArray.push(data)
93    this.notifyDataAdd(this.dataArray.length - 1)
94  }
95}