diff --git a/common/src/main/ets/default/model/browser/SelectManager.ts b/common/src/main/ets/default/model/browser/SelectManager.ts index 66c44f17..7a84e85d 100644 --- a/common/src/main/ets/default/model/browser/SelectManager.ts +++ b/common/src/main/ets/default/model/browser/SelectManager.ts @@ -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); diff --git a/feature/thirdselect/src/main/ets/default/utils/ThirdSelectConstants.ts b/feature/thirdselect/src/main/ets/default/utils/ThirdSelectConstants.ts index 2d7892ec..89732c0f 100644 --- a/feature/thirdselect/src/main/ets/default/utils/ThirdSelectConstants.ts +++ b/feature/thirdselect/src/main/ets/default/utils/ThirdSelectConstants.ts @@ -42,6 +42,7 @@ export class SelectParams { cameraAble: boolean; /* whether picker can edit */ editAble: boolean; + preselectedUris?: string[]; uri: string; itemDisplayName: string; itemId: string; diff --git a/feature/thirdselect/src/main/ets/default/view/ThirdSelectPhotoGridBase.ets b/feature/thirdselect/src/main/ets/default/view/ThirdSelectPhotoGridBase.ets index e17ef748..212bb416 100644 --- a/feature/thirdselect/src/main/ets/default/view/ThirdSelectPhotoGridBase.ets +++ b/feature/thirdselect/src/main/ets/default/view/ThirdSelectPhotoGridBase.ets @@ -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() { diff --git a/product/phone/src/main/ets/MainAbility/MainAbility.ts b/product/phone/src/main/ets/MainAbility/MainAbility.ts index 4e0072b7..4b464b33 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -51,6 +51,7 @@ let isShowMenuFromThirdView: boolean; export default class MainAbility extends Ability { private formCurrentUri: string = ''; private formAlbumUri: string = ''; + private preselectedUris: Array = []; 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; 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);