• 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 AbilityManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager';
17import Log from '../../../../../../../../common/src/main/ets/default/Log';
18import ResourceUtil from '../../../../../../../../common/src/main/ets/default/ResourceUtil';
19import NoDisturbingModel from '../model/noDisturbingModel';
20import ConfigData, {DoNotDisturbType} from '../common/constants';
21
22const TAG = 'ManagementComponent-NoDisturbComponentViewModel';
23
24export default class NoDisturbComponentViewModel {
25  defaultStartTime: Date= new Date();
26  defaultEndTime: Date= new Date();
27  startTime = '';
28  endTime = '';
29  repeatMode = 0;
30  repeatName: Resource;
31  prompt = '';
32  isEffective = true;
33  nextDayLabel = '';
34
35  viewModelInit(): void{
36    Log.showInfo(TAG, 'ViewModelInit');
37    void this.getNextDayLabel();
38    this.getNoDisturbingDate.bind(this)();
39  }
40
41  getNoDisturbingDate(): void {
42    Log.showInfo(TAG, 'getNoDisturbingDate');
43    NoDisturbingModel.getNoDisturbingDate((data) => {
44      Log.showDebug(TAG, 'getNoDisturbingDate data:' + JSON.stringify(data));
45      this.repeatMode = data.type??0;
46      this.startTime = data.begin??'';
47      this.endTime = data.end??'';
48      Log.showDebug(TAG, `getNoDisturbingDate repeatMode : ${this.repeatMode} startTime : ${this.startTime} endTime : ${this.endTime}`);
49      this.repeatName = this.refreshRepeatName(this.repeatMode);
50      this.setClues.bind(this)();
51    });
52  }
53
54  setNoDisturbingDate(): void {
55    if (!this.isEffective) {
56      this.repeatMode = DoNotDisturbType.TYPE_NONE;
57    }
58    Log.showDebug(TAG, `this.repeatMode is : ${this.repeatMode}`);
59    let noDisturbingTime = {
60      type: this.repeatMode, begin: this.defaultStartTime, end: this.defaultEndTime
61    };
62    NoDisturbingModel.setNoDisturbingDate(noDisturbingTime, () => {
63      Log.showInfo(TAG, 'setNoDisturbingDate is success');
64    });
65  }
66
67  setClues(): void {
68    Log.showInfo(TAG, 'setClues');
69    if (this.repeatMode == DoNotDisturbType.TYPE_DAILY ||
70    this.repeatMode == DoNotDisturbType.TYPE_ONCE ||
71    this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
72      this.isEffective = true;
73    } else {
74      this.isEffective = false;
75    }
76
77    this.setCluesWithoutSetEffect();
78    Log.showDebug(TAG, `this.prompt : ${this.prompt}`);
79  }
80  setCluesWithoutSetEffect(): void {
81    Log.showInfo(TAG, 'setCluesWithoutSetEffect start');
82    if (this.repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
83      this.prompt = this.getDateTimeLabel(this.startTime) + ' - ' + this.getDateTimeLabel(this.endTime);
84      this.defaultStartTime = this.getDateByDateTime(this.startTime);
85      this.defaultEndTime = this.getDateByDateTime(this.endTime);
86    } else if (this.repeatMode == DoNotDisturbType.TYPE_DAILY || this.repeatMode == DoNotDisturbType.TYPE_ONCE) {
87      if (this.startTime >= this.endTime) {
88        this.prompt = this.startTime + ' - ' + this.nextDayLabel + this.endTime;
89      } else {
90        this.prompt = this.startTime + ' - ' + this.endTime;
91      }
92      this.defaultStartTime = this.getDateByHHMI(this.startTime);
93      this.defaultEndTime = this.getDateByHHMI(this.endTime);
94    } else {
95      this.prompt = '';
96      this.defaultStartTime = this.getDateByHHMI(this.startTime);
97      this.defaultEndTime = this.getDateByHHMI(this.endTime);
98    }
99    Log.showInfo(TAG, 'setCluesWithoutSetEffect end');
100  }
101  refreshDate(repeatMode: number, inputStartTime: Date, inputEndTime: Date): void {
102    this.defaultStartTime = inputStartTime;
103    this.defaultEndTime = inputEndTime;
104    if (repeatMode == DoNotDisturbType.TYPE_CLEARLY) {
105      this.startTime = NoDisturbingModel.formatDateTime(this.defaultStartTime) ;
106      this.endTime = NoDisturbingModel.formatDateTime(this.defaultEndTime) ;
107    } else {
108      this.startTime = NoDisturbingModel.formatTime(this.defaultStartTime) ;
109      this.endTime = NoDisturbingModel.formatTime(this.defaultEndTime) ;
110    }
111  }
112
113  refreshRepeatName(inputRepeatMode: number):  Resource {
114    let result: Resource ;
115    if (inputRepeatMode == DoNotDisturbType.TYPE_ONCE) {
116      result = $r('app.string.noDisturb_once');
117    } else if (inputRepeatMode == DoNotDisturbType.TYPE_DAILY) {
118      result = $r('app.string.noDisturb_daily');
119    } else if (inputRepeatMode == DoNotDisturbType.TYPE_CLEARLY) {
120      result = $r('app.string.noDisturb_clearly');
121    } else {
122      result = $r('app.string.noDisturb_none');
123    }
124    return result;
125  }
126
127  getDateByHHMI(selectDate: string): Date{
128    Log.showInfo(TAG, 'getDateByHHMI selectDate:' + selectDate);
129    let tempDate: Date = new Date();
130    let hhmiArr = selectDate.split(':');
131    let hour = parseInt(hhmiArr[0]);
132    let minute = parseInt(hhmiArr[1]);
133    tempDate = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate(),
134      hour, minute);
135    return tempDate;
136  }
137  getDateByDateTime(selectDate: string): Date{
138    Log.showInfo(TAG, 'getDateByDateTime selectDate:' + selectDate);
139    let arrayValue = this.getValuesFromDate(selectDate);
140    let tempDate: Date = new Date();
141    tempDate = new Date(arrayValue[ConfigData.DATE_YEAR_IDX], arrayValue[ConfigData.DATE_MONTH_IDX] - 1,
142      arrayValue[ConfigData.DATE_DAY_IDX], arrayValue[ConfigData.DATE_HOUR_IDX],
143      arrayValue[ConfigData.DATE_MINUTE_IDX]);
144    return tempDate;
145  }
146  getValuesFromDate(selectDate: string): number[] {
147    let arrayValue = [];
148    let idxStart = 0;
149    let chrFlg = false;
150    if (selectDate) {
151      for (let idx = 0;idx < selectDate.length + 1;idx++) {
152        if (isNaN(parseInt(selectDate.substring(idx, idx + 1)))) {
153          arrayValue.push(parseInt(selectDate.substring(idxStart, idx)));
154          chrFlg = true;
155        } else if (chrFlg) {
156          idxStart = idx;
157          chrFlg = false;
158        }
159      }
160    }
161    return arrayValue;
162  }
163  getDateLabel(selectDate: string): string{
164    let result = '';
165    if (selectDate) {
166      let arrayValue = this.getValuesFromDate(selectDate);
167      let cutDict = ['年' , '月', '日'];
168      for (let idx2 = 0;idx2 < cutDict.length;idx2++) {
169        result += arrayValue[idx2];
170        result += cutDict[idx2];
171      }
172    }
173    return result;
174  }
175  getDateTimeLabel(selectDate: string): string{
176    let result = '';
177    if (selectDate) {
178      result += this.getDateLabel(selectDate);
179      result += ' ' + NoDisturbingModel.formatTime(this.getDateByDateTime(selectDate));
180    }
181    return result;
182  }
183  async getNextDayLabel(): Promise<void> {
184    await ResourceUtil.initResourceManager(AbilityManager.ABILITY_NAME_NOTIFICATION_MANAGEMENT);
185    this.nextDayLabel = await ResourceUtil.getString($r('app.string.noDisturb_nextDay'));
186  }
187}
188
189