• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 { Log } from '../../utils/Log';
17import { Dispatch, getStore, OhCombinedState } from '../../redux/store';
18import { Action } from '../../redux/actions/Action';
19import { EventBus } from '../../worker/eventbus/EventBus';
20import { EventBusManager } from '../../worker/eventbus/EventBusManager';
21import ReportUtil from '../../utils/ReportUtil';
22import { CameraService } from '../../camera/CameraService';
23import { GlobalContext } from '../../utils/GlobalContext';
24import Want from '@ohos.app.ability.Want';
25
26class StateStruct {
27  thumbnail: Resource = $r('app.media.ic_camera_thumbnail_default_white');
28}
29
30class UpdateThumbnailStruct {
31  thumbnail: PixelMap | undefined = undefined;
32  resourceUri: string = '';
33}
34
35class ThumbnailStruct {
36  thumbnail: PixelMap | undefined = undefined;
37}
38
39class StartAbilityParameterStruct {
40  uri: string = '';
41}
42
43class ThumbnailViewDispatcher {
44  public setDispatch(dispatch: Dispatch) {
45    this.mDispatch = dispatch;
46  }
47
48  private mDispatch: Dispatch = (data) => data;
49}
50
51@Component
52export struct ThumbnailView {
53  @State thumbnailBorder: BorderOptions = {};
54  @State state: StateStruct = new StateStruct()
55  @State thumbnail: Resource | PixelMap = $r('app.media.ic_camera_thumbnail_default_white')
56  @State hasThumbnail: boolean = false
57  @State scaleValue: number = 1
58  @State tempOpacity: number = 1
59  private TAG: string = '[ThumbnailView]:'
60  private appEventBus: EventBus = EventBusManager.getInstance().getEventBus()
61  private cameraService = CameraService.getInstance()
62  private mAction: ThumbnailViewDispatcher = new ThumbnailViewDispatcher();
63
64  aboutToAppear() {
65    Log.info(`${this.TAG} aboutToAppear E`)
66    getStore().subscribe((state: OhCombinedState) => {
67      this.state = {
68        thumbnail: state.cameraInitReducer.thumbnail
69      };
70    }, (dispatch: Dispatch) => {
71      this.mAction.setDispatch(dispatch);
72    });
73    this.appEventBus.on(Action.ACTION_UPDATE_THUMBNAIL, (data: UpdateThumbnailStruct) => this.onThumbnailUpdate(data));
74    this.appEventBus.on(Action.ACTION_LOAD_THUMBNAIL, (data: ThumbnailStruct) => this.onThumbnailLoad(data));
75    Log.info(`${this.TAG} aboutToAppear X`)
76  }
77
78  aboutToDisappear(): void {
79    Log.info(`${this.TAG} aboutToDisappear E`)
80    this.appEventBus.off(Action.ACTION_UPDATE_THUMBNAIL, (data: UpdateThumbnailStruct) => this.onThumbnailUpdate(data))
81    this.appEventBus.off(Action.ACTION_LOAD_THUMBNAIL, (data: ThumbnailStruct) => this.onThumbnailLoad(data));
82    Log.info(`${this.TAG} aboutToDisappear X`)
83  }
84
85  build() {
86    Column() {
87      Stack() {
88        Image(this.thumbnail)
89          .width('100%').aspectRatio(1).borderRadius(22).objectFit(ImageFit.Fill)
90      }
91      .width('100%').height('100%')
92      .enabled(this.hasThumbnail)
93      .onClick(async () => {
94        Log.info(`${this.TAG} launch bundle com.ohos.photos`)
95        ReportUtil.write(ReportUtil.CLICK_THUMBNAIL)
96        GlobalContext.get().setObject('keepCameraZoomRatio', true);
97        const recentUri: string = this.cameraService.getRecentFileUri();
98        Log.info(`${this.TAG} uri === ` + recentUri)
99        const abilityParameter: Record<string, string> = { 'uri': recentUri };
100        await GlobalContext.get().getCameraAbilityContext().startAbility(this.buildCameraAbilityWant(abilityParameter))
101      })
102    }
103    .width(44)
104    .aspectRatio(1)
105    .borderRadius(22)
106    .border(this.thumbnailBorder)
107    .opacity(this.tempOpacity)
108    .scale({ x: this.scaleValue, y: this.scaleValue })
109  }
110
111  private async onThumbnailUpdate(data: UpdateThumbnailStruct): Promise<void> {
112    Log.info(`${this.TAG} onThumbnailUpdate data: ${JSON.stringify(data)} E`)
113    this.thumbnail = (data.thumbnail == null ? $r('app.media.ic_camera_thumbnail_default_white') : data.thumbnail)
114    this.hasThumbnail = data.thumbnail != undefined
115    if (this.hasThumbnail) {
116      this.thumbnailBorder = { width: 1, color: Color.White, style: BorderStyle.Solid }
117    } else {
118      this.thumbnailBorder = { width: 0 }
119    }
120    this.scaleValue = 1.5
121    this.tempOpacity = 0.0
122    animateTo({ duration: 100, curve: Curve.Sharp }, () => {
123      this.tempOpacity = 1
124    })
125    animateTo({ duration: 300, curve: Curve.Sharp }, () => {
126      this.scaleValue = 1
127    })
128    Log.info(`${this.TAG} onThumbnailUpdate this.state.thumbnail: ${JSON.stringify(this.thumbnail)} X`)
129  }
130
131  private async onThumbnailLoad(data: ThumbnailStruct): Promise<void> {
132    Log.info(`${this.TAG} onThumbnailLoad data: ${JSON.stringify(data)} E`)
133    this.thumbnail = (data.thumbnail == null ? $r('app.media.ic_camera_thumbnail_default_white') : data.thumbnail)
134    this.hasThumbnail = data.thumbnail != undefined
135    if (this.hasThumbnail) {
136      this.thumbnailBorder = { width: 1, color: Color.White, style: BorderStyle.Solid }
137    } else {
138      this.thumbnailBorder = { width: 0 }
139    }
140    this.scaleValue = 1
141    this.tempOpacity = 1
142    Log.info(`${this.TAG} onThumbnailLoad this.state.thumbnail: ${JSON.stringify(this.thumbnail)} X`)
143  }
144
145  private buildCameraAbilityWant(parameter: Record<string, string>): Want {
146    let res: Want = {
147      parameters: parameter,
148      action: 'ohos.want.action.viewData',
149      bundleName: 'com.ohos.photos',
150      abilityName: 'com.ohos.photos.MainAbility'
151    };
152    return res;
153  }
154}