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