@ebarooni/capacitor-calendar 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ehsan Barooni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -29,10 +29,10 @@ npx cap sync
29
29
 
30
30
  ## Support Range
31
31
 
32
- | Platform | range |
33
- |:----------:|:------------------------------------:|
34
- | iOS | ≥ 13 |
35
- | Android | ≥ 4 (SDK 14 – Ice Cream Sandwich) |
32
+ | Platform | range |
33
+ |:--------:|:-------------------------:|
34
+ | iOS | ≥ 13 |
35
+ | Android | ≥ 7 (SDK 24 or Nougat) |
36
36
 
37
37
  ## [Demo (click for details)](./example/README.md)
38
38
 
@@ -74,6 +74,7 @@ permissions can be found below:
74
74
  * [`selectCalendarsWithPrompt(...)`](#selectcalendarswithprompt)
75
75
  * [`listCalendars()`](#listcalendars)
76
76
  * [`getDefaultCalendar()`](#getdefaultcalendar)
77
+ * [`createEvent(...)`](#createevent)
77
78
  * [Interfaces](#interfaces)
78
79
  * [Type Aliases](#type-aliases)
79
80
  * [Enums](#enums)
@@ -203,6 +204,23 @@ Retrieves the default calendar set on the device.
203
204
  --------------------
204
205
 
205
206
 
207
+ ### createEvent(...)
208
+
209
+ ```typescript
210
+ createEvent(options: { title: string; calendarId?: string; location?: string; startDate?: Date; endDate?: Date; isAllDay?: boolean; }) => Promise<{ eventCreated: boolean; }>
211
+ ```
212
+
213
+ Creates an event with the provided options.
214
+
215
+ | Param | Type | Description |
216
+ | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
217
+ | **`options`** | <code>{ title: string; calendarId?: string; location?: string; startDate?: <a href="#date">Date</a>; endDate?: <a href="#date">Date</a>; isAllDay?: boolean; }</code> | - Options for creating the event. |
218
+
219
+ **Returns:** <code>Promise&lt;{ eventCreated: boolean; }&gt;</code>
220
+
221
+ --------------------
222
+
223
+
206
224
  ### Interfaces
207
225
 
208
226
 
@@ -226,6 +244,57 @@ Represents a calendar object with an ID and title.
226
244
  | **`title`** | <code>string</code> |
227
245
 
228
246
 
247
+ #### Date
248
+
249
+ Enables basic storage and retrieval of dates and times.
250
+
251
+ | Method | Signature | Description |
252
+ | ---------------------- | ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------- |
253
+ | **toString** | () =&gt; string | Returns a string representation of a date. The format of the string depends on the locale. |
254
+ | **toDateString** | () =&gt; string | Returns a date as a string value. |
255
+ | **toTimeString** | () =&gt; string | Returns a time as a string value. |
256
+ | **toLocaleString** | () =&gt; string | Returns a value as a string value appropriate to the host environment's current locale. |
257
+ | **toLocaleDateString** | () =&gt; string | Returns a date as a string value appropriate to the host environment's current locale. |
258
+ | **toLocaleTimeString** | () =&gt; string | Returns a time as a string value appropriate to the host environment's current locale. |
259
+ | **valueOf** | () =&gt; number | Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. |
260
+ | **getTime** | () =&gt; number | Gets the time value in milliseconds. |
261
+ | **getFullYear** | () =&gt; number | Gets the year, using local time. |
262
+ | **getUTCFullYear** | () =&gt; number | Gets the year using Universal Coordinated Time (UTC). |
263
+ | **getMonth** | () =&gt; number | Gets the month, using local time. |
264
+ | **getUTCMonth** | () =&gt; number | Gets the month of a <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
265
+ | **getDate** | () =&gt; number | Gets the day-of-the-month, using local time. |
266
+ | **getUTCDate** | () =&gt; number | Gets the day-of-the-month, using Universal Coordinated Time (UTC). |
267
+ | **getDay** | () =&gt; number | Gets the day of the week, using local time. |
268
+ | **getUTCDay** | () =&gt; number | Gets the day of the week using Universal Coordinated Time (UTC). |
269
+ | **getHours** | () =&gt; number | Gets the hours in a date, using local time. |
270
+ | **getUTCHours** | () =&gt; number | Gets the hours value in a <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
271
+ | **getMinutes** | () =&gt; number | Gets the minutes of a <a href="#date">Date</a> object, using local time. |
272
+ | **getUTCMinutes** | () =&gt; number | Gets the minutes of a <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
273
+ | **getSeconds** | () =&gt; number | Gets the seconds of a <a href="#date">Date</a> object, using local time. |
274
+ | **getUTCSeconds** | () =&gt; number | Gets the seconds of a <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
275
+ | **getMilliseconds** | () =&gt; number | Gets the milliseconds of a <a href="#date">Date</a>, using local time. |
276
+ | **getUTCMilliseconds** | () =&gt; number | Gets the milliseconds of a <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
277
+ | **getTimezoneOffset** | () =&gt; number | Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). |
278
+ | **setTime** | (time: number) =&gt; number | Sets the date and time value in the <a href="#date">Date</a> object. |
279
+ | **setMilliseconds** | (ms: number) =&gt; number | Sets the milliseconds value in the <a href="#date">Date</a> object using local time. |
280
+ | **setUTCMilliseconds** | (ms: number) =&gt; number | Sets the milliseconds value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
281
+ | **setSeconds** | (sec: number, ms?: number \| undefined) =&gt; number | Sets the seconds value in the <a href="#date">Date</a> object using local time. |
282
+ | **setUTCSeconds** | (sec: number, ms?: number \| undefined) =&gt; number | Sets the seconds value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
283
+ | **setMinutes** | (min: number, sec?: number \| undefined, ms?: number \| undefined) =&gt; number | Sets the minutes value in the <a href="#date">Date</a> object using local time. |
284
+ | **setUTCMinutes** | (min: number, sec?: number \| undefined, ms?: number \| undefined) =&gt; number | Sets the minutes value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
285
+ | **setHours** | (hours: number, min?: number \| undefined, sec?: number \| undefined, ms?: number \| undefined) =&gt; number | Sets the hour value in the <a href="#date">Date</a> object using local time. |
286
+ | **setUTCHours** | (hours: number, min?: number \| undefined, sec?: number \| undefined, ms?: number \| undefined) =&gt; number | Sets the hours value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
287
+ | **setDate** | (date: number) =&gt; number | Sets the numeric day-of-the-month value of the <a href="#date">Date</a> object using local time. |
288
+ | **setUTCDate** | (date: number) =&gt; number | Sets the numeric day of the month in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
289
+ | **setMonth** | (month: number, date?: number \| undefined) =&gt; number | Sets the month value in the <a href="#date">Date</a> object using local time. |
290
+ | **setUTCMonth** | (month: number, date?: number \| undefined) =&gt; number | Sets the month value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
291
+ | **setFullYear** | (year: number, month?: number \| undefined, date?: number \| undefined) =&gt; number | Sets the year of the <a href="#date">Date</a> object using local time. |
292
+ | **setUTCFullYear** | (year: number, month?: number \| undefined, date?: number \| undefined) =&gt; number | Sets the year value in the <a href="#date">Date</a> object using Universal Coordinated Time (UTC). |
293
+ | **toUTCString** | () =&gt; string | Returns a date converted to a string using Universal Coordinated Time (UTC). |
294
+ | **toISOString** | () =&gt; string | Returns a date as a string value in ISO format. |
295
+ | **toJSON** | (key?: any) =&gt; string | Used by the JSON.stringify method to enable the transformation of an object's data for JavaScript Object Notation (JSON) serialization. |
296
+
297
+
229
298
  ### Type Aliases
230
299
 
231
300
 
@@ -26,7 +26,7 @@ android {
26
26
  namespace "dev.barooni.capacitor.calendar"
27
27
  compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34
28
28
  defaultConfig {
29
- minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
29
+ minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
30
30
  targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34
31
31
  versionCode 1
32
32
  versionName "1.0"
@@ -1,10 +1,16 @@
1
1
  package dev.barooni.capacitor.calendar
2
2
 
3
3
  import android.content.ContentResolver
4
+ import android.content.ContentValues
4
5
  import android.content.Context
6
+ import android.net.Uri
5
7
  import android.provider.CalendarContract
6
8
  import com.getcapacitor.JSArray
7
9
  import com.getcapacitor.JSObject
10
+ import java.text.SimpleDateFormat
11
+ import java.util.Calendar
12
+ import java.util.Locale
13
+ import java.util.TimeZone
8
14
 
9
15
  class CapacitorCalendar() {
10
16
  var eventsCount: Int = 0
@@ -88,4 +94,33 @@ class CapacitorCalendar() {
88
94
  }
89
95
  throw Exception("No primary calendar found")
90
96
  }
97
+
98
+ @Throws(Exception::class)
99
+ fun createEvent(
100
+ context: Context,
101
+ title: String,
102
+ calendarId: String?,
103
+ location: String?,
104
+ startDate: String?,
105
+ endDate: String?,
106
+ isAllDay: Boolean?
107
+ ): Uri? {
108
+ val isoFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault()).apply {
109
+ timeZone = TimeZone.getTimeZone("UTC")
110
+ }
111
+ val startMillis = startDate?.let { isoFormatter.parse(it)?.time } ?: Calendar.getInstance().timeInMillis
112
+ val endMillis = endDate?.let { isoFormatter.parse(it)?.time } ?: (startMillis + 3600 * 1000)
113
+
114
+ val values = ContentValues().apply {
115
+ put(CalendarContract.Events.DTSTART, startMillis)
116
+ put(CalendarContract.Events.DTEND, endMillis)
117
+ put(CalendarContract.Events.TITLE, title)
118
+ location?.let { put(CalendarContract.Events.EVENT_LOCATION, it) }
119
+ put(CalendarContract.Events.CALENDAR_ID, calendarId ?: getDefaultCalendar(context).getString("id"))
120
+ put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
121
+ isAllDay?.let { put(CalendarContract.Events.ALL_DAY, if (it) 1 else 0) }
122
+ }
123
+
124
+ return context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)
125
+ }
91
126
  }
@@ -3,6 +3,7 @@ package dev.barooni.capacitor.calendar
3
3
  import android.Manifest
4
4
  import android.content.Intent
5
5
  import android.provider.CalendarContract
6
+ import android.util.Log
6
7
  import androidx.activity.result.ActivityResult
7
8
  import com.getcapacitor.JSObject
8
9
  import com.getcapacitor.Plugin
@@ -161,4 +162,25 @@ class CapacitorCalendarPlugin: Plugin() {
161
162
  call.reject("", "[CapacitorCalendar.${::getDefaultCalendar.name}] No default calendar found")
162
163
  }
163
164
  }
165
+
166
+ @PluginMethod
167
+ fun createEvent(call: PluginCall) {
168
+ try {
169
+ val title = call.getString("title")
170
+ ?: throw Exception("[CapacitorCalendar.${::createEvent.name}] A title for the event was not provided")
171
+ val calendarId = call.getString("calendarId")
172
+ val location = call.getString("location")
173
+ val startDate = call.getString("startDate")
174
+ val endDate = call.getString("endDate")
175
+ val isAllDay = call.getBoolean("isAllDay", false)
176
+
177
+ val eventUri = implementation.createEvent(context, title, calendarId, location, startDate, endDate, isAllDay)
178
+ val ret = JSObject()
179
+ ret.put("eventCreated", eventUri != null)
180
+ call.resolve(ret)
181
+ } catch (error: Exception) {
182
+ call.reject("", "[CapacitorCalendar.${::createEvent.name}] Unable to create event")
183
+ return
184
+ }
185
+ }
164
186
  }