1 2/* 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16import { 17 MenuContext , 18 ThirdDeleteOperation , 19 MenuOperationFactory, 20 Log , 21 BroadCast, 22 SelectManager , 23 MediaObserver 24} from '@ohos/common'; 25import { CustomDialogView } from '@ohos/common/CommonComponents'; 26import { MenuOperation } from '@ohos/common'; 27import dialogRequest from '@ohos.app.ability.dialogRequest'; 28import uri from '@ohos.uri'; 29import common from '@ohos.app.ability.common'; 30 31const TAG: string = 'ResourceDeletePage'; 32 33@Entry 34@Component 35struct ResourceDeletePage { 36 @Provide broadCast: BroadCast = new BroadCast(); 37 38 isShow: boolean = false; 39 selectManager: SelectManager = new SelectManager(); 40 41 onPageShow() { 42 if (this.isShow) { 43 return; 44 } 45 this.isShow = true; 46 Log.info(TAG, 'onPageShow'); 47 let menuOperation: MenuOperation; 48 let menuContext: MenuContext; 49 menuContext = new MenuContext(); 50 menuContext 51 .withSelectManager(this.selectManager) 52 .withOperationEndCallback((): void => this.onOperationEnd()) 53 .withOperationCancelCallback((): void => this.onOperationCancel()) 54 .withBroadCast(this.broadCast); 55 menuOperation = MenuOperationFactory.getInstance() 56 .createMenuOperation(ThirdDeleteOperation, menuContext); 57 menuOperation.doAction(); 58 } 59 60 aboutToAppear() { 61 this.selectManager = new SelectManager(); 62 let uris: string[] | undefined = AppStorage.get<string[]>('uris'); 63 Log.info(TAG, 'aboutToAppear uris : ' + JSON.stringify(uris)); 64 if (!uris?.length) { 65 return; 66 } 67 uris.forEach((uri: string): void => { 68 if (this.selectManager !== null && !this.selectManager.clickedSet.has(uri)) { 69 this.selectManager.clickedSet.add(uri); 70 } 71 }) 72 } 73 74 aboutToDisappear() { 75 Log.info(TAG, 'aboutToDisappear'); 76 MediaObserver.getInstance().unregisterForAllPhotos(); 77 MediaObserver.getInstance().unregisterForAllAlbums(); 78 } 79 80 build() { 81 Column() { 82 83 CustomDialogView({ broadCast: $broadCast}) 84 .width('100%') 85 .height('100%'); 86 } 87 .backgroundColor("#00000000") 88 .height('100%'); 89 } 90 91 onOperationEnd () { 92 this.setDeleteResult(dialogRequest.ResultCode.RESULT_OK); 93 } 94 95 onOperationCancel () { 96 this.setDeleteResult(dialogRequest.ResultCode.RESULT_CANCEL); 97 } 98 99 private setDeleteResult(result: dialogRequest.ResultCode): void { 100 Log.info(TAG, 'start to setDeleteResult : ' + result); 101 try { 102 (AppStorage.get<dialogRequest.RequestCallback>('requestCallback') as dialogRequest.RequestCallback).setRequestResult({ result }); 103 (AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext).terminateSelf(); 104 } catch (err) { 105 Log.info(TAG, `getRequestInfo err= ${JSON.stringify(err)}`); 106 } 107 } 108}