1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 { Log } from '../utils/Log'; 17import { AlbumDataImpl } from './AlbumDataImpl'; 18import { LazyItem, ItemDataSource } from './ItemDataSource'; 19import { AlbumDataItem } from './AlbumDataItem'; 20import { MediaConstants } from '../constants/MediaConstants' 21 22const TAG = 'AlbumsDataSource' 23 24export class AlbumsDataSource extends ItemDataSource { 25 private albumDataItems: AlbumDataItem[] = []; 26 private albumDataImpl: AlbumDataImpl = new AlbumDataImpl(); 27 28 setBlackList(blackList: string[]): void { 29 this.albumDataImpl.setBlackList(blackList); 30 } 31 32 setSelectType(selectType: number): void { 33 this.albumDataImpl.setSelectType(selectType); 34 } 35 36 setDeviceId(deviceId: string): void { 37 this.albumDataImpl.setDeviceId(deviceId); 38 } 39 40 totalCount(): number { 41 return this.albumDataItems.length; 42 } 43 44 getData(index: number): LazyItem<AlbumDataItem> { 45 return new LazyItem<AlbumDataItem>(this.getDataByIndex(index), index, (index: number): void => this.onDataUpdateBindImpl(index)); 46 } 47 48 getDataByIndex(index: number): AlbumDataItem { 49 if (index < 0 || index >= this.albumDataItems.length) { 50 Log.warn(TAG, index + '/' + this.albumDataItems.length); 51 return undefined; 52 } 53 this.albumDataItems[index].index = index; 54 return this.albumDataItems[index]; 55 } 56 57 isSelect(): boolean { 58 let isSelect = true; 59 for (let i = 0;i < this.albumDataItems.length; i++) { 60 if (!this.albumDataItems[i].isSelect) { 61 isSelect = false; 62 break; 63 } 64 } 65 return isSelect; 66 } 67 68 resetLoadState(): void { 69 for (let i = 0;i < this.albumDataItems.length; i++) { 70 if (this.albumDataItems[i].status === MediaConstants.LOADED) { 71 this.albumDataItems[i].status = MediaConstants.UNDEFINED; 72 } 73 } 74 } 75 76 getAlbumDataItemById(id: string): AlbumDataItem | null { 77 let albumDataItem: AlbumDataItem = null; 78 for (let i = 0;i < this.albumDataItems.length; i++) { 79 if (this.albumDataItems[i].id === id) { 80 albumDataItem = this.albumDataItems[i]; 81 } 82 } 83 return albumDataItem; 84 } 85 86 getSelectedUris(): string[] { 87 let uris: string[] = []; 88 this.albumDataItems.forEach((item: AlbumDataItem): void => { 89 if (item.isSelect) { 90 uris.push(item.uri); 91 } 92 }) 93 return uris; 94 } 95 96 isDisableRename(): boolean { 97 let isDisableRename = false; 98 for (let i = 0;i < this.albumDataItems.length; i++) { 99 if (this.albumDataItems[i].isSelect && this.albumDataItems[i].isDisableRename) { 100 isDisableRename = true; 101 break; 102 } 103 } 104 return isDisableRename; 105 } 106 107 isDisableDelete(): boolean { 108 let isDisableDelete = false; 109 for (let i = 0;i < this.albumDataItems.length; i++) { 110 if (this.albumDataItems[i].isSelect && this.albumDataItems[i].isDisableDelete) { 111 isDisableDelete = true; 112 break; 113 } 114 } 115 return isDisableDelete; 116 } 117 118 setSelect(isSelect: boolean): void { 119 this.albumDataItems.forEach((item: AlbumDataItem): void => { 120 item.setSelect(isSelect); 121 }) 122 } 123 124 getSelectedCount(): number { 125 let count = 0; 126 for (let i = 0;i < this.albumDataItems.length; i++) { 127 if (this.albumDataItems[i].isSelect) { 128 count++; 129 } 130 } 131 return count; 132 } 133 134 getSelectedItems(): Object[] { 135 let items: AlbumDataItem[] = []; 136 this.albumDataItems.forEach((item: AlbumDataItem): void => { 137 if (item.isSelect) { 138 items.push(item); 139 } 140 }) 141 return items; 142 } 143 144 onDataUpdate(index: number): void { 145 this.onDataUpdateBindImpl(index); 146 } 147 148 private onDataUpdateBindImpl(index: number): void { 149 Log.info(TAG, 'onDataUpdate ' + index); 150 this.notifyDataChange(index); 151 } 152 153 dataReload(): void { 154 this.reloadAlbumItemData().then<void, void>((isEmpty: boolean): void => { 155 this.notifyDataReload(); 156 }) 157 } 158 159 dataRemove(): void { 160 for (let i = this.albumDataItems.length - 1;i >= 0; i--) { 161 if (this.albumDataItems[i].isDeleted()) { 162 this.albumDataItems.splice(i, 1); 163 super.notifyDataDelete(i); 164 } 165 } 166 } 167 168 async reloadAlbumItemData(): Promise<boolean> { 169 this.albumDataItems = await this.albumDataImpl.reloadAlbumItemData(); 170 return this.albumDataItems.length === 0; 171 } 172 173 async reloadAlbumListItemData(): Promise<boolean> { 174 Log.info(TAG, 'reloadAlbumListItemData'); 175 this.albumDataItems = await this.albumDataImpl.reloadAlbumItemData(); 176 this.notifyDataReload(); 177 return this.albumDataItems.length === 0; 178 } 179} 180