• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 featureAbility from '@ohos.ability.featureAbility'
17import commonEvent from '@ohos.commonEvent';
18import Log from '../../../../../../../../common/src/main/ets/default/Log'
19import DateTimeCommon from '../../../../../../../../common/src/main/ets/default/DateTimeCommon'
20import sTimeManager, {TimeEventArgs, TIME_CHANGE_EVENT,
21}  from '../../../../../../../../common/src/main/ets/default/TimeManager';
22import EventManager, {unsubscribe} from '../../../../../../../../common/src/main/ets/default/event/EventManager'
23
24const TAG = 'ScreenLock-DateTimeViewModel'
25
26let mCommonEventSubscribeInfo = {
27    events: [
28        commonEvent.Support.COMMON_EVENT_TIME_CHANGED,
29        commonEvent.Support.COMMON_EVENT_TIMEZONE_CHANGED,
30        commonEvent.Support.COMMON_EVENT_TIME_TICK
31    ]
32};
33
34let mEventSubscriber
35
36/**
37 * DateTimeViewModel class
38 */
39export default class DateTimeViewModel {
40    timeVal: string = ''
41    dateVal: any = {}
42    weekVal: any = {}
43    calendarVal: any = {}
44    unSubscriber?: unsubscribe;
45
46    ViewModelInit(): void{
47        Log.showDebug(TAG, 'ViewModelInit');
48
49        this.getAndSetDateTime.bind(this)()
50        commonEvent.createSubscriber(mCommonEventSubscribeInfo, this.createSubscriberCallBack.bind(this));
51        this.unSubscriber = EventManager.subscribe(TIME_CHANGE_EVENT, (args: TimeEventArgs) => {
52            this.setDateTime(args.date)
53        });
54        Log.showDebug(TAG, 'ViewModelInit end');
55    }
56
57    private getAndSetDateTime() {
58        Log.showDebug(TAG, `getAndSetDateTime`)
59        this.setDateTime(new Date())
60    }
61
62    private setDateTime(date: Date) {
63        Log.showDebug(TAG, `setDateTime`)
64        this.timeVal = sTimeManager.formatTime(date)
65        this.dateVal = DateTimeCommon.getSystemDate()
66        this.weekVal = DateTimeCommon.getSystemWeek()
67        this.calendarVal = DateTimeCommon.getCalendarDate()
68    }
69
70    private createSubscriberCallBack(err, data) {
71        Log.showDebug(TAG, "start createSubscriberCallBack " + JSON.stringify(data))
72        mEventSubscriber = data
73        commonEvent.subscribe(data, this.getAndSetDateTime.bind(this));
74        Log.showDebug(TAG, "start createSubscriberCallBack finish")
75    }
76
77    stopPolling() {
78        Log.showDebug(TAG, `stopPolling start`)
79        commonEvent.unsubscribe(mEventSubscriber);
80        this.unSubscriber && this.unSubscriber();
81        this.unSubscriber = undefined;
82        Log.showDebug(TAG, `stopPolling end`)
83    }
84}
85