1/* 2 * Copyright (c) 2022 Huawei Device 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 type { MenuContext, MenuOperation } from '@ohos/common'; 17import { BrowserOperationFactory, BrowserConstants as Constants, Log } from '@ohos/common'; 18 19const TAG: string = 'browser_FavoriteMenuOperation'; 20 21export class FavoriteMenuOperation implements MenuOperation { 22 private menuContext: MenuContext; 23 24 constructor(menuContext: MenuContext) { 25 this.menuContext = menuContext; 26 } 27 28 doAction(): void { 29 if (this.menuContext == null) { 30 Log.error(TAG, 'menuContext is null, return'); 31 return; 32 } 33 let mediaItem = this.menuContext.mediaItem; 34 if (mediaItem == null) { 35 Log.error(TAG, 'mediaItem is null, return'); 36 return; 37 } 38 let requestState = mediaItem.isFavor; 39 this.changeFavor(mediaItem.uri, requestState); 40 Log.info(TAG, `progressFavorite: ${(requestState ? 'FAVORITE' : 'NOT_FAVORITE')}`); 41 } 42 43 private async changeFavor(uri, requestState) { 44 let operationImpl = BrowserOperationFactory.getFeature(BrowserOperationFactory.TYPE_PHOTO); 45 let result = await operationImpl.favor(uri, requestState); 46 this.menuContext.broadCast.emit(Constants.SAVE_SCALE, []); 47 this.menuContext.broadCast.emit(Constants.SET_FAVOR, [result]); 48 } 49}