100aff185Sopenharmony_ci/* 200aff185Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400aff185Sopenharmony_ci * you may not use this file except in compliance with the License. 500aff185Sopenharmony_ci * You may obtain a copy of the License at 600aff185Sopenharmony_ci * 700aff185Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800aff185Sopenharmony_ci * 900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and 1300aff185Sopenharmony_ci * limitations under the License. 1400aff185Sopenharmony_ci */ 1500aff185Sopenharmony_ci 1600aff185Sopenharmony_ciimport { HistogramManager, HistogramStatus } from '../model/common/HistogramManager'; 1700aff185Sopenharmony_ciimport { Log } from '../utils/Log'; 1800aff185Sopenharmony_ciimport { Constants } from '../model/common/Constants'; 1900aff185Sopenharmony_ciimport { WorkerThreadPool } from '../model/common/WorkerThreadPool'; 2000aff185Sopenharmony_ciimport { TraceControllerUtils } from '../utils/TraceControllerUtils'; 2100aff185Sopenharmony_ci 2200aff185Sopenharmony_ciconst TAG = 'Histogram' 2300aff185Sopenharmony_ci 2400aff185Sopenharmony_ci@Component 2500aff185Sopenharmony_ciexport struct Histogram { 2600aff185Sopenharmony_ci @StorageLink('HistogramReadyStatus') @Watch('statusReady') status: number = HistogramStatus.PREPARING; 2700aff185Sopenharmony_ci @Consume @Watch('adjustLayout') listCardWidth: number; 2800aff185Sopenharmony_ci private setting: RenderingContextSettings = new RenderingContextSettings(true); 2900aff185Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.setting); 3000aff185Sopenharmony_ci 3100aff185Sopenharmony_ci aboutToAppear() { 3200aff185Sopenharmony_ci Log.info(TAG, 'histogram aboutToAppear'); 3300aff185Sopenharmony_ci TraceControllerUtils.startTrace('HistogramAboutToAppear'); 3400aff185Sopenharmony_ci } 3500aff185Sopenharmony_ci 3600aff185Sopenharmony_ci aboutToDisappear() { 3700aff185Sopenharmony_ci Log.info(TAG, 'histogram aboutToDisappear, ready status reset'); 3800aff185Sopenharmony_ci AppStorage.setOrCreate<number>(Constants.HISTOGRAM_READY_STATUS_KEY, HistogramStatus.PREPARING); 3900aff185Sopenharmony_ci HistogramManager.getInstance().clear(); 4000aff185Sopenharmony_ci WorkerThreadPool.getInstance().stop(); 4100aff185Sopenharmony_ci } 4200aff185Sopenharmony_ci 4300aff185Sopenharmony_ci /** 4400aff185Sopenharmony_ci * 适配pc屏幕大小变化,以及phone横竖屏 4500aff185Sopenharmony_ci */ 4600aff185Sopenharmony_ci adjustLayout() { 4700aff185Sopenharmony_ci Log.info(TAG, 'histogram adjustLayout'); 4800aff185Sopenharmony_ci HistogramManager.getInstance() 4900aff185Sopenharmony_ci .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2) 5000aff185Sopenharmony_ci .startDraw(); 5100aff185Sopenharmony_ci } 5200aff185Sopenharmony_ci 5300aff185Sopenharmony_ci /** 5400aff185Sopenharmony_ci * worker统计完毕后,触发页面更新,开始绘制 5500aff185Sopenharmony_ci */ 5600aff185Sopenharmony_ci statusReady() { 5700aff185Sopenharmony_ci if (this.status !== HistogramStatus.READY) { 5800aff185Sopenharmony_ci Log.info(TAG, 'status changed, but not ready'); 5900aff185Sopenharmony_ci return; 6000aff185Sopenharmony_ci } 6100aff185Sopenharmony_ci Log.info(TAG, 'statusReady, start draw histogram'); 6200aff185Sopenharmony_ci HistogramManager.getInstance() 6300aff185Sopenharmony_ci .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2) 6400aff185Sopenharmony_ci .setHeight(Constants.HISTOGRAM_HEIGHT) 6500aff185Sopenharmony_ci .setContext(this.context) 6600aff185Sopenharmony_ci .startDraw(); 6700aff185Sopenharmony_ci this.status = HistogramStatus.LOADING_FINISHED; 6800aff185Sopenharmony_ci TraceControllerUtils.finishTrace('HistogramAboutToAppear'); 6900aff185Sopenharmony_ci } 7000aff185Sopenharmony_ci 7100aff185Sopenharmony_ci build() { 7200aff185Sopenharmony_ci Canvas(this.context) 7300aff185Sopenharmony_ci .width(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2) 7400aff185Sopenharmony_ci .height(Constants.HISTOGRAM_HEIGHT) 7500aff185Sopenharmony_ci .backgroundColor($r('app.color.histogram_background_light')) 7600aff185Sopenharmony_ci .onReady(() => { 7700aff185Sopenharmony_ci // 手机横竖屏时偶现直方图不显示,重新绘制放在onReady中可以解决 7800aff185Sopenharmony_ci if (this.status === HistogramStatus.LOADING_FINISHED) { 7900aff185Sopenharmony_ci Log.info(TAG, 'histogram adjustLayout'); 8000aff185Sopenharmony_ci HistogramManager.getInstance() 8100aff185Sopenharmony_ci .setWidth(this.listCardWidth - Constants.COLUMN_PADDING * Constants.NUMBER_2) 8200aff185Sopenharmony_ci .startDraw(); 8300aff185Sopenharmony_ci } 8400aff185Sopenharmony_ci }) 8500aff185Sopenharmony_ci } 8600aff185Sopenharmony_ci}