• 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 ConfigData from '../common/constants';
17import Log from '../../../../../../../common/src/main/ets/default/Log';
18import NoDisturbingModel from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/model/noDisturbingModel';
19import NoDisturbComponentViewModel from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/vm/noDisturbComponentViewModel';
20import FeaturesConfigData, {DoNotDisturbType} from '../../../../../../../features/managementcomponent/src/main/ets/com/ohos/common/constants';
21
22const TAG = 'NotificationManagement-NoDisturbViewModel';
23const CONST_DAY_LENGTH = 24*3600*1000;
24
25@Observed
26export default class NoDisturbViewModel extends NoDisturbComponentViewModel {
27
28  startDateClue = '';
29  startTimeClue = '';
30  endDateClue = '';
31  endTimeClue = '';
32
33  viewModelInit(): void{
34    Log.showDebug(TAG, 'ViewModelInit');
35    void this.getNextDayLabel();
36    this.getNoDisturbingDate.bind(this)();
37  }
38  getNoDisturbingDate(): void {
39    Log.showDebug(TAG, 'getNoDisturbingDate');
40    NoDisturbingModel.getNoDisturbingDate((data) => {
41      Log.showInfo(TAG, 'getNoDisturbingDate data:' + JSON.stringify(data));
42      this.repeatMode = data.type??0;
43      this.startTime = data.begin??'';
44      this.endTime = data.end??'';
45      Log.showInfo(TAG, `getNoDisturbingDate this.repeatMode : ${this.repeatMode}, this.startTime : ${this.startTime}, this.endTime : ${this.endTime}`)
46      this.setClues.bind(this)();
47    });
48  }
49
50  setClues(): void {
51    Log.showDebug(TAG, 'setClues');
52    if (this.repeatMode == DoNotDisturbType.TYPE_DAILY ||
53    this.repeatMode == DoNotDisturbType.TYPE_ONCE ||
54    this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
55      this.isEffective = true;
56    } else {
57      this.isEffective = false;
58    }
59    let tmpStartDateTime = null;
60    let tmpEndDateTime = null;
61    if (this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
62      tmpStartDateTime = this.getDateByDateTime(this.startTime);
63      tmpEndDateTime = this.getDateByDateTime(this.endTime);
64      this.startDateClue = this.getDateLabel(this.startTime);
65      this.startTimeClue = NoDisturbingModel.formatTime(tmpStartDateTime);
66      this.endDateClue = this.getDateLabel(this.endTime);
67      this.endTimeClue = NoDisturbingModel.formatTime(tmpEndDateTime);
68    } else {
69      tmpStartDateTime = this.getDateByHHMI(this.startTime);
70      tmpEndDateTime = this.getDateByHHMI(this.endTime);
71      this.startDateClue = '';
72      this.startTimeClue = this.startTime;
73      this.endDateClue = '';
74      if (tmpStartDateTime.getTime() >= tmpEndDateTime.getTime()) {
75        this.endTimeClue = this.nextDayLabel + this.endTime;
76      } else {
77        this.endTimeClue = this.endTime;
78      }
79    }
80    this.defaultStartTime = tmpStartDateTime;
81    this.defaultEndTime = tmpEndDateTime;
82    this.repeatName = this.refreshRepeatName(this.repeatMode);
83  }
84
85  onStartTimeAccept(data: string): void {
86    Log.showDebug(TAG, 'onStartTimeAccept');
87    this.startTime = data;
88    if (this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
89      let tmpDateTime = this.getDateByDateTime(this.startTime);
90      if (this.defaultEndTime.getTime() < tmpDateTime.getTime()) {
91        this.startTime = this.endTime;
92      }
93    }
94    this.setClues();
95    this.setNoDisturbingDate();
96
97  }
98
99  onEndTimeAccept(data: string): void {
100    Log.showDebug(TAG, 'onEndTimeAccept');
101    this.endTime = data;
102    if (this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
103      let tmpDateTime = this.getDateByDateTime(this.endTime);
104      if (tmpDateTime.getTime() < this.defaultStartTime.getTime()) {
105        this.endTime = this.startTime;
106      }
107    }
108    this.setClues();
109    this.setNoDisturbingDate();
110  }
111
112  onRepeatModeAccect(data: number): void {
113    Log.showDebug(TAG, 'onRepeatModeAccect');
114    this.repeatMode = data;
115    if (this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
116      let dateSource = new Date();
117      this.startTime = NoDisturbingModel.formatDate(dateSource) + ' ' + FeaturesConfigData.TIME_EMPTY;
118      this.endTime = NoDisturbingModel.formatDate(new Date(dateSource.getTime() + CONST_DAY_LENGTH)) + ' ' + FeaturesConfigData.TIME_EMPTY;
119    } else {
120      this.startTime = FeaturesConfigData.TIME_EMPTY;
121      this.endTime = FeaturesConfigData.TIME_EMPTY;
122    }
123    this.setClues();
124    this.setNoDisturbingDate();
125  }
126
127  setDateIntoDateTime(dateSource: Date, inputData: string): string {
128    let result = inputData + ' ' + NoDisturbingModel.formatTime(dateSource);
129    return result;
130  }
131
132  setTimeIntoDateTime(dateSource: Date, inputData: string): string {
133    let result = NoDisturbingModel.formatDate(dateSource) + ' ' + inputData;
134    return result;
135  }
136
137  onCancel(): void {
138    Log.showDebug(TAG, 'onCancel');
139  }
140}