1/*
2 * Copyright (c) 2024 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 */
15const calendarManager = requireInternal('calendarManager');
16
17class JsCalendarManager {
18  nativeCalendarManager;
19
20  constructor(calendarManager) {
21    this.nativeCalendarManager = calendarManager;
22  }
23
24  async createCalendar(calendarAccount, callback) {
25    if (callback) {
26      this.nativeCalendarManager.createCalendar(calendarAccount, callback);
27    } else {
28      return await this.nativeCalendarManager.createCalendar(calendarAccount);
29    }
30  }
31
32  async deleteCalendar(calendar, callback) {
33    if (callback) {
34      this.nativeCalendarManager.deleteCalendar(calendar, callback);
35    } else {
36      return await this.nativeCalendarManager.deleteCalendar(calendar);
37    }
38  }
39
40  async getCalendar(calendarAccount, callback) {
41    let calendar;
42    if (calendarAccount) {
43      if (callback) {
44        this.nativeCalendarManager.getCalendar(calendarAccount, callback);
45      } else {
46        return await this.nativeCalendarManager.getCalendar(calendarAccount);
47      }
48    } else {
49      if (callback) {
50        this.nativeCalendarManager.getCalendar(callback);
51      } else {
52        return await this.nativeCalendarManager.getCalendar();
53      }
54    }
55  }
56
57  async getAllCalendars(callback) {
58    if (callback) {
59      this.nativeCalendarManager.getAllCalendars(callback);
60    } else {
61      return await this.nativeCalendarManager.getAllCalendars();
62    }
63  }
64
65  async editEvent(event) {
66    console.log('JsCalendarManager editEvent called');
67    let context = getContext(this);
68    let eventStr = JSON.stringify(event);
69    let callerPkg = context.applicationInfo?.name;
70    return await this.nativeCalendarManager.editEvent(context, eventStr, callerPkg);
71  }
72}
73
74let mJsCalendarManager;
75
76function getCalendarManager(context) {
77  let nativeCalendarManager = calendarManager.getCalendarManager(context);
78  if (mJsCalendarManager === null || mJsCalendarManager === undefined ||
79    nativeCalendarManager !== mJsCalendarManager.nativeCalendarManager) {
80    // 如果native层拿到的manager不一样了,就也重新创建一个JSCalendarManager包装对象
81    mJsCalendarManager = new JsCalendarManager(nativeCalendarManager);
82  }
83  return mJsCalendarManager;
84}
85
86export default {
87  getCalendarManager: getCalendarManager,
88  CalendarManager: JsCalendarManager,
89  Calendar: calendarManager.Calendar,
90  CalendarAccount: calendarManager.CalendarAccount,
91  CalendarConfig: calendarManager.CalendarConfig,
92  Event: calendarManager.Event,
93  CalendarType: calendarManager.CalendarType,
94  Location: calendarManager.Location,
95  EventFilter: calendarManager.EventFilter,
96  EventType: calendarManager.EventType,
97  RecurrenceRule: calendarManager.RecurrenceRule,
98  RecurrenceFrequency: calendarManager.RecurrenceFrequency,
99  Attendee: calendarManager.Attendee,
100  EventService: calendarManager.EventService,
101  ServiceType: calendarManager.ServiceType,
102  AttendeeRole: calendarManager.AttendeeRole,
103};