picker支持预选择

Signed-off-by: f00610455 <fanjingyu2@huawei.com>
This commit is contained in:
f00610455 2024-01-17 17:03:11 +08:00
parent bc287a5870
commit e25e4bda36
4 changed files with 62 additions and 1 deletions

View File

@ -385,6 +385,49 @@ export class ThirdSelectManager extends SelectManager {
return super.clickedSet;
}
/**
*
*
* @param targetIdList Id
*/
public initPreSelectPhotos(targetIdList: string[]): void {
this.deSelectAll();
if (this.getMediaItemFunc) {
targetIdList.forEach((targetId: string): void => {
let containUri = this.selectedMap.has(targetId);
if (!containUri) {
this.getMediaItemFunc(targetId, (item) => {
this.toggleWithItem(targetId, true, item);
});
}
});
}
}
/**
* mediaItem预加载缓存
*
* @param targetId Id
* @param isSelected
* @param mediaItem mediaItem
* @returns
*/
public toggleWithItem(targetId: string, isSelected: boolean, mediaItem: MediaItem): boolean {
if (mediaItem) {
let containUri = this.selectedMap.has(targetId);
if (isSelected && !containUri) {
this.selectedMap.set(targetId, mediaItem);
this.indexMap.set(mediaItem, undefined);
}
if (!isSelected && containUri) {
this.selectedMap.delete(targetId);
this.indexMap.delete(mediaItem);
}
return super.toggle(targetId, isSelected, undefined);
}
return false;
}
public toggle(targetId: string, isSelected: boolean, targetIndex?: number): boolean {
if (this.getMediaItemFunc) {
let containsUri = this.selectedMap.has(targetId);

View File

@ -42,6 +42,7 @@ export class SelectParams {
cameraAble: boolean;
/* whether picker can edit */
editAble: boolean;
preselectedUris?: string[];
uri: string;
itemDisplayName: string;
itemId: string;

View File

@ -606,6 +606,20 @@ export struct ThirdSelectPhotoGridBase {
}
Log.info(TAG, `isEmpty: ${this.isEmpty}`)
this.dataSource.onDataReloaded();
this.modifyPhotoSelectStatus();
}
/**
* 图库预选择
*/
private modifyPhotoSelectStatus(): void {
if (this.selectParams?.preselectedUris?.length > 0) {
if (this.selectParams.preselectedUris.length > this.selectParams.maxSelectCount) {
this.selectParams.preselectedUris = this.selectParams.preselectedUris.slice(0, this.selectParams.maxSelectCount);
}
this.selectManager.initPreSelectPhotos(this.selectParams.preselectedUris);
this.selectParams.preselectedUris = [];
}
}
private initSelectManager() {

View File

@ -51,6 +51,7 @@ let isShowMenuFromThirdView: boolean;
export default class MainAbility extends Ability {
private formCurrentUri: string = '';
private formAlbumUri: string = '';
private preselectedUris: Array<string> = [];
private isOnDestroy: boolean = false;
private localStorage: LocalStorage = new LocalStorage();
@ -119,6 +120,7 @@ export default class MainAbility extends Ability {
mCallerBundleName = wantParam[Constants.KEY_WANT_PARAMETERS_CALLER_BUNDLE_NAME] as string;
mMaxSelectCount = wantParam?.maxSelectCount as number;
mFilterMediaType = wantParam?.filterMediaType as string;
this.preselectedUris = wantParam?.preselectedUris as Array<string>;
AppStorage.SetOrCreate('entryFromHap', Constants.ENTRY_FROM_MULTIPLE_SELECT);
SmartPickerUtils.initIfNeeded(this.context, want, this.localStorage);
} else if (wantParamUri === Constants.WANT_PARAM_URI_FORM) {
@ -254,7 +256,8 @@ export default class MainAbility extends Ability {
isMultiPick: true,
filterMediaType: mFilterMediaType,
isFirstEnter: true,
maxSelectCount: mMaxSelectCount
maxSelectCount: mMaxSelectCount,
preselectedUris: this.preselectedUris,
}
};
router.replaceUrl(options);