1/* 2 * Copyright (c) 2021-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 Log from '../../../../../../../common/src/main/ets/default/Log' 17import { TintContentInfo } from '../../../../../../../common/src/main/ets/default/TintStateManager' 18import StyleConfiguration, { ClockComponentStyle } from '../common/StyleConfiguration' 19import StyleConfigurationCommon, { CommonStyle 20} from '../../../../../../../common/src/main/ets/default/StyleConfiguration' 21import ViewModel from '../viewmodel/ClockVM' 22import TimeManager, { 23 TimeEventArgs, 24 TIME_CHANGE_EVENT, 25} from '../../../../../../../common/src/main/ets/default/TimeManager' 26import EventManager, { unsubscribe } from '../../../../../../../common/src/main/ets/default/event/EventManager' 27 28const TAG = 'ClockComponent-clockIcon'; 29 30@Component 31export default struct ClockIcon { 32 @State mTime: string = '16:01'; 33 @State mTintContentInfo: TintContentInfo = ViewModel.getTintContentInfo(); 34 @State style: ClockComponentStyle = StyleConfiguration.getClockComponentStyle(); 35 @State styleCommon: CommonStyle = StyleConfigurationCommon.getCommonStyle(); 36 unSubscriber?: unsubscribe; 37 38 aboutToAppear() { 39 Log.showInfo(TAG, 'aboutToAppear'); 40 this.unSubscriber = EventManager.subscribe(TIME_CHANGE_EVENT, (args: TimeEventArgs) => { 41 this.mTime = TimeManager.formatTime(args.date); 42 }); 43 this.mTime = TimeManager.formatTime(new Date()); 44 } 45 46 aboutToDisappear() { 47 Log.showInfo(TAG, 'aboutToDisappear'); 48 this.unSubscriber && this.unSubscriber(); 49 this.unSubscriber = undefined; 50 } 51 52 build() { 53 Row() { 54 Row().width(this.styleCommon.statusBarMarginLeftRight).height('100%') 55 Text(this.mTime) 56 .fontColor(this.mTintContentInfo.contentColor) 57 .fontSize($r("sys.float.ohos_id_text_size_body2")) 58 .width(this.style.statusBarClockMaxWidth) 59 .textAlign(TextAlign.Center) 60 .margin({ left: $r("app.float.mTime_text_margin_left") }) 61 Row().width(this.styleCommon.statusBarMarginLeftRight).height('100%') 62 }.opacity($r("app.float.icon_component_opacity")) 63 } 64}