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 { HistogramManager, HistogramStatus } from '../model/common/HistogramManager';
17import { Log } from '../utils/Log';
18import { Constants } from '../model/common/Constants';
19import { WorkerThreadPool } from '../model/common/WorkerThreadPool';
20import { TraceControllerUtils } from '../utils/TraceControllerUtils';
21
22const TAG = 'Histogram'
23
24@Component
25export struct Histogram {
26  @StorageLink('HistogramReadyStatus') @Watch('statusReady') status: number = HistogramStatus.PREPARING;
27  @Consume @Watch('adjustLayout') listCardWidth: number;
28  private setting: RenderingContextSettings = new RenderingContextSettings(true);
29  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.setting);
30
31  aboutToAppear() {
32    Log.info(TAG, 'histogram aboutToAppear');
33    TraceControllerUtils.startTrace('HistogramAboutToAppear');
34  }
35
36  aboutToDisappear() {
37    Log.info(TAG, 'histogram aboutToDisappear, ready status reset');
38    AppStorage.setOrCreate<number>(Constants.HISTOGRAM_READY_STATUS_KEY, HistogramStatus.PREPARING);
39    HistogramManager.getInstance().clear();
40    WorkerThreadPool.getInstance().stop();
41  }
42
43  /**
44   * 适配pc屏幕大小变化,以及phone横竖屏
45   */
46  adjustLayout() {
47    Log.info(TAG, 'histogram adjustLayout');
48    HistogramManager.getInstance()
49      .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2)
50      .startDraw();
51  }
52
53  /**
54   * worker统计完毕后,触发页面更新,开始绘制
55   */
56  statusReady() {
57    if (this.status !== HistogramStatus.READY) {
58      Log.info(TAG, 'status changed, but not ready');
59      return;
60    }
61    Log.info(TAG, 'statusReady, start draw histogram');
62    HistogramManager.getInstance()
63      .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2)
64      .setHeight(Constants.HISTOGRAM_HEIGHT)
65      .setContext(this.context)
66      .startDraw();
67    this.status = HistogramStatus.LOADING_FINISHED;
68    TraceControllerUtils.finishTrace('HistogramAboutToAppear');
69  }
70
71  build() {
72    Canvas(this.context)
73      .width(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2)
74      .height(Constants.HISTOGRAM_HEIGHT)
75      .backgroundColor($r('app.color.histogram_background_light'))
76      .onReady(() => {
77        // 手机横竖屏时偶现直方图不显示,重新绘制放在onReady中可以解决
78        if (this.status === HistogramStatus.LOADING_FINISHED) {
79          Log.info(TAG, 'histogram adjustLayout');
80          HistogramManager.getInstance()
81            .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2)
82            .startDraw();
83        }
84      })
85  }
86}