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 { GroupDataImpl } from './GroupDataImpl'; 18import { LazyItem, ItemDataSource } from './ItemDataSource'; 19import { UserFileDataItem } from '../base/UserFileDataItem'; 20 21const TAG = 'GroupItemDataSource'; 22 23export class GroupItemDataSource extends ItemDataSource { 24 groupDataItem: UserFileDataItem[] = []; 25 private groupDataImpl: GroupDataImpl = new GroupDataImpl(); 26 27 constructor() { 28 super(); 29 } 30 31 setSelectType(selectType: number): void { 32 this.groupDataImpl.setSelectType(selectType); 33 } 34 35 setAlbumId(id: string): void { 36 Log.info(TAG, 'setAlbumId: ' + id); 37 this.groupDataImpl.setAlbumId(id); 38 } 39 40 setAlbumType(id: number): void { 41 Log.info(TAG, 'setAlbumType: ' + id); 42 this.groupDataImpl.setAlbumType(id); 43 } 44 45 setAlbumSubtype(id: number): void { 46 Log.info(TAG, 'setAlbumSubtype: ' + id); 47 this.groupDataImpl.setAlbumSubtype(id); 48 } 49 50 setAlbumName(name: string): void { 51 Log.info(TAG, 'setAlbumName: ' + name); 52 this.groupDataImpl.setAlbumName(name); 53 } 54 55 setDeviceId(id: string): void { 56 Log.info(TAG, 'setDeviceId: ' + id); 57 this.groupDataImpl.setDeviceId(id); 58 } 59 60 totalCount(): number { 61 return this.groupDataItem.length; 62 } 63 64 getIndexByItem(item: UserFileDataItem): number { 65 let index = -1; 66 let length = this.groupDataItem.length; 67 for (let i = 0;i < length; i++) { 68 if (this.groupDataItem[i].uri === item.uri) { 69 index = i; 70 break; 71 } 72 } 73 return index; 74 } 75 76 getData(index: number): LazyItem<UserFileDataItem> { 77 if (index < 0 || index >= this.groupDataItem.length) { 78 Log.warn(TAG, index + '/' + this.groupDataItem.length); 79 return undefined; 80 } 81 if (this.groupDataItem[index] != null && this.groupDataItem[index] != undefined) { 82 this.groupDataItem[index].index = index; 83 } 84 return new LazyItem<UserFileDataItem>(this.groupDataItem[index], index, (index: number): void => this.onDataUpdateBindImpl(index)); 85 } 86 87 getDataByIndex(index: number): UserFileDataItem { 88 if (index < 0 || index >= this.groupDataItem.length) { 89 Log.warn(TAG, index + '/' + this.groupDataItem.length); 90 return undefined; 91 } 92 if (this.groupDataItem[index] != null && this.groupDataItem[index] != undefined) { 93 this.groupDataItem[index].index = index; 94 } 95 return this.groupDataItem[index]; 96 } 97 98 isSelect(): boolean { 99 let isSelect = true; 100 for (let i = 0;i < this.groupDataItem.length; i++) { 101 if (!this.groupDataItem[i].isSelect) { 102 isSelect = false; 103 break; 104 } 105 } 106 return isSelect; 107 } 108 109 getSelectedCount(): number { 110 let count = 0; 111 this.groupDataItem.forEach((item: UserFileDataItem): void => { 112 if (item.isSelect) { 113 count++; 114 } 115 }) 116 return count; 117 } 118 119 getItems(): UserFileDataItem[] { 120 let items: UserFileDataItem[] = []; 121 this.groupDataItem.forEach((item: UserFileDataItem): void => { 122 items.push(item); 123 }) 124 return items; 125 } 126 127 getSelectedItems(): Object[] { 128 let items: UserFileDataItem[] = []; 129 this.groupDataItem.forEach((item: UserFileDataItem): void => { 130 if (item.isSelect) { 131 items.push(item); 132 } 133 }) 134 return items; 135 } 136 137 getSelectedUris(): string[] { 138 let uris: string[] = []; 139 this.groupDataItem.forEach((item: UserFileDataItem): void => { 140 if (item.isSelect) { 141 uris.push(item.uri); 142 } 143 }) 144 return uris; 145 } 146 147 setSelect(isSelect: boolean): void { 148 this.groupDataItem.forEach((item: UserFileDataItem): void => { 149 item.setSelect(isSelect); 150 }) 151 this.notifyDataReload(); 152 } 153 154 async reloadGroupItemData(isGrid: boolean): Promise<boolean> { 155 this.groupDataItem = await this.groupDataImpl.reloadGroupItemData(isGrid); 156 return this.groupDataItem.length === 0; 157 } 158 159 onDataUpdate(index: number): void { 160 this.onDataUpdateBindImpl(index); 161 } 162 163 private onDataUpdateBindImpl(index: number): void { 164 Log.debug(TAG, 'onDataUpdate ' + index); 165 if (index !== -1) { 166 this.notifyDataChange(index); 167 } 168 } 169 170 dataReload(isGrid: boolean): void { 171 this.reloadGroupItemData(isGrid).then<void, void>((isEmpty: boolean): void => { 172 this.notifyDataReload(); 173 }) 174 } 175 176 dataRemove(): void { 177 for (let i = this.groupDataItem.length - 1;i >= 0; i--) { 178 if (this.groupDataItem[i] != undefined && this.groupDataItem[i].isDeleted()) { 179 this.groupDataItem.splice(i, 1); 180 super.notifyDataDelete(i); 181 } 182 } 183 } 184 185 dataDelete(uri: string): void { 186 const mediaDataItemIndex = this.groupDataItem.findIndex((item: UserFileDataItem): boolean => { 187 return item.uri === uri; 188 }) 189 if (mediaDataItemIndex !== -1 && this.groupDataItem[mediaDataItemIndex].isDeleted()) { 190 this.groupDataItem.splice(mediaDataItemIndex, 1); 191 super.notifyDataDelete(mediaDataItemIndex); 192 } 193 } 194} 195