From f85150f484836a1f67ef9b757c6b5b0b386ca30b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Wed, 7 May 2025 17:12:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/src/main/ets/utils/AvPlayerUtil.ets | 4 +++- .../base/src/main/ets/utils/DisplayUtil.ets | 4 +++- features/home/src/main/ets/view/BannerView.ets | 2 +- features/home/src/main/ets/view/DailyVideo.ets | 2 +- features/home/src/main/ets/view/IconView.ets | 4 ++-- .../home/src/main/ets/view/NewVideoRelease.ets | 2 +- .../src/main/ets/view/RecommendedVideo.ets | 18 +++++++++--------- .../home/src/main/ets/view/VideoDialog.ets | 2 +- .../src/main/ets/view/VideoDetail.ets | 6 +++--- .../src/main/ets/entryability/EntryAbility.ets | 1 + 10 files changed, 25 insertions(+), 20 deletions(-) diff --git a/commons/base/src/main/ets/utils/AvPlayerUtil.ets b/commons/base/src/main/ets/utils/AvPlayerUtil.ets index bbbe6f1..dd4d00b 100644 --- a/commons/base/src/main/ets/utils/AvPlayerUtil.ets +++ b/commons/base/src/main/ets/utils/AvPlayerUtil.ets @@ -20,9 +20,11 @@ import { BusinessError } from '@kit.BasicServicesKit'; import Logger from './Logger'; import { CommonConstants } from '../constants/CommonConstants'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + export class AvPlayerUtil { private avPlayer?: media.AVPlayer; - private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; + private context: common.UIAbilityContext = uiContext?.getHostContext() as common.UIAbilityContext; private url: resourceManager.RawFileDescriptor | null = null; private surfaceId: string = ''; private sliderBegin: number = 0; diff --git a/commons/base/src/main/ets/utils/DisplayUtil.ets b/commons/base/src/main/ets/utils/DisplayUtil.ets index b0fd762..f7085d1 100644 --- a/commons/base/src/main/ets/utils/DisplayUtil.ets +++ b/commons/base/src/main/ets/utils/DisplayUtil.ets @@ -15,6 +15,8 @@ import { display } from '@kit.ArkUI'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + export class DisplayUtil { static getFoldCreaseRegion(): void { if(canIUse('SystemCapability.Window.SessionManager')) { @@ -22,7 +24,7 @@ export class DisplayUtil { let foldRegion: display.FoldCreaseRegion = display.getCurrentFoldCreaseRegion(); let rect: display.Rect = foldRegion.creaseRects[0]; // Height of the avoidance area in the upper half screen and height of the avoidance area. - let creaseRegion: number[] = [px2vp(rect.top), px2vp(rect.height)]; + let creaseRegion: number[] = [uiContext!.px2vp(rect.top), uiContext!.px2vp(rect.height)]; AppStorage.setOrCreate('creaseRegion', creaseRegion); } } diff --git a/features/home/src/main/ets/view/BannerView.ets b/features/home/src/main/ets/view/BannerView.ets index 9a27b63..e3c407c 100644 --- a/features/home/src/main/ets/view/BannerView.ets +++ b/features/home/src/main/ets/view/BannerView.ets @@ -285,7 +285,7 @@ export struct BannerView { getBannerNewHeight(windowWidth: number): string { // Obtain the window width and subtract the blank parts on both sides. - let result: number = px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], + let result: number = this.getUIContext().px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], HomeConstants.VIDEO_GRID_MARGIN[1], HomeConstants.VIDEO_GRID_MARGIN[2]).getValue(this.currentWidthBreakpoint) - HomeConstants.LG_SIDEBAR_WIDTH; // Calculate the width of a single image based on the percent. diff --git a/features/home/src/main/ets/view/DailyVideo.ets b/features/home/src/main/ets/view/DailyVideo.ets index 781f5fa..1cc3275 100644 --- a/features/home/src/main/ets/view/DailyVideo.ets +++ b/features/home/src/main/ets/view/DailyVideo.ets @@ -245,7 +245,7 @@ export struct DailyVideo { getDailyVideoHeight(currentWidthBreakpoint: string, windowWidth: number, isMain: boolean): string { // Obtain the window width and subtract the blank parts on both sides. - let result: number = px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], + let result: number = this.getUIContext().px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], HomeConstants.VIDEO_GRID_MARGIN[1], HomeConstants.VIDEO_GRID_MARGIN[2]).getValue(this.currentWidthBreakpoint) - HomeConstants.VIDEO_GRID_ITEM_SPACE; // Calculate the width of a single image based on the number of grid columns. diff --git a/features/home/src/main/ets/view/IconView.ets b/features/home/src/main/ets/view/IconView.ets index 5a529e7..c65aac5 100644 --- a/features/home/src/main/ets/view/IconView.ets +++ b/features/home/src/main/ets/view/IconView.ets @@ -78,9 +78,9 @@ export struct IconView { getSpace(windowWidth: number, currentWidthBreakpoint: string): number { // Obtain the window width and subtract the blank parts on both sides. - let result: number = px2vp(windowWidth) - (new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], + let result: number = this.getUIContext().px2vp(windowWidth) - (new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], HomeConstants.VIDEO_GRID_MARGIN[1], HomeConstants.VIDEO_GRID_MARGIN[2]).getValue(this.currentWidthBreakpoint) / - HomeConstants.TWO) - getContext(this).resourceManager.getNumber($r('app.float.icon_img_size').id) / 2; + HomeConstants.TWO) - this.getUIContext().getHostContext()!.resourceManager.getNumber($r('app.float.icon_img_size').id) / 2; if (currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_SM) { result = (result - HomeConstants.ICONVIEW_ITEM_WIDTH[0] * CommonConstants.FOUR) / CommonConstants.FOUR; } else if (currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_MD) { diff --git a/features/home/src/main/ets/view/NewVideoRelease.ets b/features/home/src/main/ets/view/NewVideoRelease.ets index 031e227..a7b19ff 100644 --- a/features/home/src/main/ets/view/NewVideoRelease.ets +++ b/features/home/src/main/ets/view/NewVideoRelease.ets @@ -75,7 +75,7 @@ export struct NewVideoRelease { } getNewVideoHeight(currentWidthBreakpoint: string, windowWidth: number): string { - let result: number = px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], + let result: number = this.getUIContext().px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], HomeConstants.VIDEO_GRID_MARGIN[1], HomeConstants.VIDEO_GRID_MARGIN[2]).getValue(this.currentWidthBreakpoint); if (currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_LG) { result = result - HomeConstants.LG_SIDEBAR_WIDTH; diff --git a/features/home/src/main/ets/view/RecommendedVideo.ets b/features/home/src/main/ets/view/RecommendedVideo.ets index 5c6b3f1..5672bc9 100644 --- a/features/home/src/main/ets/view/RecommendedVideo.ets +++ b/features/home/src/main/ets/view/RecommendedVideo.ets @@ -72,14 +72,14 @@ export struct RecommendedVideo { } // Obtains all attributes of a component. let modePosition: componentUtils.ComponentInfo = - componentUtils.getRectangleById(JSON.stringify(item)); + this.getUIContext().getComponentUtils().getRectangleById(JSON.stringify(item)); let windowOffset = modePosition.windowOffset; let size = modePosition.size; // Obtains the height of the component from the top. - let rectTop: number = px2vp(windowOffset.y); - let rectTop2: number = px2vp(windowOffset.y + Math.floor(size.height)); + let rectTop: number = this.getUIContext().px2vp(windowOffset.y); + let rectTop2: number = this.getUIContext().px2vp(windowOffset.y + Math.floor(size.height)); // Obtains the width of the component from the left. - let rectLeft: number = px2vp(windowOffset.x); + let rectLeft: number = this.getUIContext().px2vp(windowOffset.x); let topHeightNeeded: number = new BreakpointType(HomeConstants.VIDEO_DIALOG_HEIGHTS[0], HomeConstants.VIDEO_DIALOG_HEIGHTS[1], HomeConstants.VIDEO_DIALOG_HEIGHTS[2]) .getValue(this.currentWidthBreakpoint) + rectTop - rectTop2; @@ -153,13 +153,13 @@ export struct RecommendedVideo { .gesture(PinchGesture({ fingers: 2 }).onActionUpdate((event: GestureEvent) => { if (event.scale > 1 && this.currentWidthBreakpoint !== BreakpointConstants.BREAKPOINT_SM) { if (this.currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_MD) { - animateTo({ + this.getUIContext().animateTo({ duration: HomeConstants.ANIMATION_DURATION }, () => { this.videoGridColumn = CommonConstants.VIDEO_GRID_COLUMNS[1]; }) } else { - animateTo({ + this.getUIContext().animateTo({ duration: HomeConstants.ANIMATION_DURATION }, () => { this.videoGridColumn = CommonConstants.VIDEO_GRID_COLUMNS[2]; @@ -167,13 +167,13 @@ export struct RecommendedVideo { } } else if (event.scale < 1 && this.currentWidthBreakpoint !== BreakpointConstants.BREAKPOINT_SM) { if (this.currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_MD) { - animateTo({ + this.getUIContext().animateTo({ duration: HomeConstants.ANIMATION_DURATION }, () => { this.videoGridColumn = CommonConstants.VIDEO_GRID_COLUMNS[2]; }) } else { - animateTo({ + this.getUIContext().animateTo({ duration: HomeConstants.ANIMATION_DURATION }, () => { this.videoGridColumn = CommonConstants.VIDEO_GRID_COLUMNS[3]; @@ -187,7 +187,7 @@ export struct RecommendedVideo { getGridHeight(videoGridColumn: string, currentWidthBreakpoint: string, windowWidth: number): string { // Obtain the window width and subtract the blank parts on both sides. - let result: number = px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], + let result: number = this.getUIContext().px2vp(windowWidth) - new BreakpointType(HomeConstants.VIDEO_GRID_MARGIN[0], HomeConstants.VIDEO_GRID_MARGIN[1], HomeConstants.VIDEO_GRID_MARGIN[2]).getValue(this.currentWidthBreakpoint); if (currentWidthBreakpoint === BreakpointConstants.BREAKPOINT_LG) { result = result - HomeConstants.LG_SIDEBAR_WIDTH; diff --git a/features/home/src/main/ets/view/VideoDialog.ets b/features/home/src/main/ets/view/VideoDialog.ets index 1d5a19d..d15f853 100644 --- a/features/home/src/main/ets/view/VideoDialog.ets +++ b/features/home/src/main/ets/view/VideoDialog.ets @@ -57,7 +57,7 @@ export struct VideoDialog { private xComponentController: XComponentController = new XComponentController(); private surfaceId: string = ''; private avPlayer?: media.AVPlayer; - private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; + private context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; private url?: resourceManager.RawFileDescriptor; private playState: boolean = true; private controller?: CustomDialogController; diff --git a/features/videoDetail/src/main/ets/view/VideoDetail.ets b/features/videoDetail/src/main/ets/view/VideoDetail.ets index 4ad557a..926a4bd 100644 --- a/features/videoDetail/src/main/ets/view/VideoDetail.ets +++ b/features/videoDetail/src/main/ets/view/VideoDetail.ets @@ -172,12 +172,12 @@ export struct VideoDetail { if (newValue.width !== 0) { let height: number = DetailConstants.COMMENT_IMAGE_MIN_HEIGHT_NUMBER + (Number(newValue.width) - DetailConstants.SIDE_BAR_MIN_WIDTH_NUMBER) / - (px2vp(this.windowWidth) * DetailConstants.COMMENTS_AREA_PERCENT - + (this.getUIContext().px2vp(this.windowWidth) * DetailConstants.COMMENTS_AREA_PERCENT - DetailConstants.SIDE_BAR_MIN_WIDTH_NUMBER) * (DetailConstants.COMMENT_IMAGE_MAX_HEIGHT_NUMBER - DetailConstants.COMMENT_IMAGE_MIN_HEIGHT_NUMBER); let width: number = DetailConstants.COMMENT_IMAGE_MIN_WIDTH_NUMBER + (Number(newValue.width) - DetailConstants.SIDE_BAR_MIN_WIDTH_NUMBER) / - (px2vp(this.windowWidth) * DetailConstants.COMMENTS_AREA_PERCENT - + (this.getUIContext().px2vp(this.windowWidth) * DetailConstants.COMMENTS_AREA_PERCENT - DetailConstants.SIDE_BAR_MIN_WIDTH_NUMBER) * (DetailConstants.COMMENT_IMAGE_MAX_WIDTH_NUMBER - DetailConstants.COMMENT_IMAGE_MIN_WIDTH_NUMBER); this.commentImgHeight = JSON.stringify(height); @@ -206,7 +206,7 @@ export struct VideoDetail { .sideBarPosition(SideBarPosition.End) .sideBarWidth($r('app.float.side_bar_min_width')) .minSideBarWidth($r('app.float.side_bar_min_width')) - .maxSideBarWidth(px2vp(this.windowWidth * DetailConstants.COMMENTS_AREA_PERCENT)) + .maxSideBarWidth(this.getUIContext().px2vp(this.windowWidth * DetailConstants.COMMENTS_AREA_PERCENT)) } .height(CommonConstants.FULL_PERCENT) } diff --git a/products/phone/src/main/ets/entryability/EntryAbility.ets b/products/phone/src/main/ets/entryability/EntryAbility.ets index 53ac660..7b554df 100644 --- a/products/phone/src/main/ets/entryability/EntryAbility.ets +++ b/products/phone/src/main/ets/entryability/EntryAbility.ets @@ -49,6 +49,7 @@ export default class EntryAbility extends UIAbility { }) windowStage.loadContent('pages/Index', (err, data) => { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; -- Gitee