@capawesome/capacitor-alarm 0.0.1

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.
Files changed (45) hide show
  1. package/CapawesomeCapacitorAlarm.podspec +17 -0
  2. package/LICENSE +21 -0
  3. package/Package.swift +28 -0
  4. package/README.md +449 -0
  5. package/android/build.gradle +58 -0
  6. package/android/src/main/AndroidManifest.xml +9 -0
  7. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/Alarm.java +85 -0
  8. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/AlarmPlugin.java +154 -0
  9. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/CustomException.java +20 -0
  10. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/CustomExceptions.java +16 -0
  11. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/options/CreateAlarmOptions.java +124 -0
  12. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/options/CreateTimerOptions.java +48 -0
  13. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/results/CreateAlarmResult.java +25 -0
  14. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/classes/results/IsAvailableResult.java +22 -0
  15. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/interfaces/Callback.java +5 -0
  16. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/interfaces/EmptyCallback.java +5 -0
  17. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/interfaces/NonEmptyResultCallback.java +7 -0
  18. package/android/src/main/java/io/capawesome/capacitorjs/plugins/alarm/interfaces/Result.java +7 -0
  19. package/android/src/main/res/.gitkeep +0 -0
  20. package/dist/docs.json +743 -0
  21. package/dist/esm/definitions.d.ts +349 -0
  22. package/dist/esm/definitions.js +55 -0
  23. package/dist/esm/definitions.js.map +1 -0
  24. package/dist/esm/index.d.ts +4 -0
  25. package/dist/esm/index.js +7 -0
  26. package/dist/esm/index.js.map +1 -0
  27. package/dist/esm/web.d.ts +12 -0
  28. package/dist/esm/web.js +28 -0
  29. package/dist/esm/web.js.map +1 -0
  30. package/dist/plugin.cjs.js +97 -0
  31. package/dist/plugin.cjs.js.map +1 -0
  32. package/dist/plugin.js +100 -0
  33. package/dist/plugin.js.map +1 -0
  34. package/ios/Plugin/Alarm.swift +148 -0
  35. package/ios/Plugin/AlarmPlugin.swift +147 -0
  36. package/ios/Plugin/Classes/Options/CancelAlarmOptions.swift +20 -0
  37. package/ios/Plugin/Classes/Options/CreateAlarmOptions.swift +70 -0
  38. package/ios/Plugin/Classes/Results/CreateAlarmResult.swift +16 -0
  39. package/ios/Plugin/Classes/Results/GetAlarmsResult.swift +49 -0
  40. package/ios/Plugin/Classes/Results/IsAvailableResult.swift +16 -0
  41. package/ios/Plugin/Classes/Results/PermissionStatusResult.swift +16 -0
  42. package/ios/Plugin/Enums/CustomError.swift +47 -0
  43. package/ios/Plugin/Info.plist +24 -0
  44. package/ios/Plugin/Protocols/Result.swift +6 -0
  45. package/package.json +95 -0
@@ -0,0 +1,154 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.Logger;
6
+ import com.getcapacitor.Plugin;
7
+ import com.getcapacitor.PluginCall;
8
+ import com.getcapacitor.PluginMethod;
9
+ import com.getcapacitor.annotation.CapacitorPlugin;
10
+ import com.getcapacitor.annotation.Permission;
11
+ import io.capawesome.capacitorjs.plugins.alarm.classes.CustomException;
12
+ import io.capawesome.capacitorjs.plugins.alarm.classes.options.CreateAlarmOptions;
13
+ import io.capawesome.capacitorjs.plugins.alarm.classes.options.CreateTimerOptions;
14
+ import io.capawesome.capacitorjs.plugins.alarm.classes.results.CreateAlarmResult;
15
+ import io.capawesome.capacitorjs.plugins.alarm.classes.results.IsAvailableResult;
16
+ import io.capawesome.capacitorjs.plugins.alarm.interfaces.EmptyCallback;
17
+ import io.capawesome.capacitorjs.plugins.alarm.interfaces.NonEmptyResultCallback;
18
+ import io.capawesome.capacitorjs.plugins.alarm.interfaces.Result;
19
+
20
+ @CapacitorPlugin(name = "Alarm", permissions = @Permission(strings = {}, alias = "alarms"))
21
+ public class AlarmPlugin extends Plugin {
22
+
23
+ public static final String ERROR_UNKNOWN_ERROR = "An unknown error has occurred.";
24
+ public static final String TAG = "AlarmPlugin";
25
+
26
+ private Alarm implementation;
27
+
28
+ @Override
29
+ public void load() {
30
+ super.load();
31
+ this.implementation = new Alarm(this);
32
+ }
33
+
34
+ @PluginMethod
35
+ public void cancelAlarm(PluginCall call) {
36
+ rejectCallAsUnimplemented(call);
37
+ }
38
+
39
+ @PluginMethod
40
+ public void createAlarm(PluginCall call) {
41
+ try {
42
+ CreateAlarmOptions options = new CreateAlarmOptions(call);
43
+ NonEmptyResultCallback<CreateAlarmResult> callback = new NonEmptyResultCallback<>() {
44
+ @Override
45
+ public void success(@NonNull CreateAlarmResult result) {
46
+ resolveCall(call, result);
47
+ }
48
+
49
+ @Override
50
+ public void error(Exception exception) {
51
+ rejectCall(call, exception);
52
+ }
53
+ };
54
+ implementation.createAlarm(options, callback);
55
+ } catch (Exception exception) {
56
+ rejectCall(call, exception);
57
+ }
58
+ }
59
+
60
+ @PluginMethod
61
+ public void createTimer(PluginCall call) {
62
+ try {
63
+ CreateTimerOptions options = new CreateTimerOptions(call);
64
+ EmptyCallback callback = new EmptyCallback() {
65
+ @Override
66
+ public void success() {
67
+ resolveCall(call);
68
+ }
69
+
70
+ @Override
71
+ public void error(Exception exception) {
72
+ rejectCall(call, exception);
73
+ }
74
+ };
75
+ implementation.createTimer(options, callback);
76
+ } catch (Exception exception) {
77
+ rejectCall(call, exception);
78
+ }
79
+ }
80
+
81
+ @PluginMethod
82
+ public void getAlarms(PluginCall call) {
83
+ rejectCallAsUnimplemented(call);
84
+ }
85
+
86
+ @PluginMethod
87
+ public void isAvailable(PluginCall call) {
88
+ try {
89
+ NonEmptyResultCallback<IsAvailableResult> callback = new NonEmptyResultCallback<>() {
90
+ @Override
91
+ public void success(@NonNull IsAvailableResult result) {
92
+ resolveCall(call, result);
93
+ }
94
+
95
+ @Override
96
+ public void error(Exception exception) {
97
+ rejectCall(call, exception);
98
+ }
99
+ };
100
+ implementation.isAvailable(callback);
101
+ } catch (Exception exception) {
102
+ rejectCall(call, exception);
103
+ }
104
+ }
105
+
106
+ @PluginMethod
107
+ public void openAlarms(PluginCall call) {
108
+ try {
109
+ EmptyCallback callback = new EmptyCallback() {
110
+ @Override
111
+ public void success() {
112
+ resolveCall(call);
113
+ }
114
+
115
+ @Override
116
+ public void error(Exception exception) {
117
+ rejectCall(call, exception);
118
+ }
119
+ };
120
+ implementation.openAlarms(callback);
121
+ } catch (Exception exception) {
122
+ rejectCall(call, exception);
123
+ }
124
+ }
125
+
126
+ private void rejectCall(@NonNull PluginCall call, @NonNull Exception exception) {
127
+ String message = exception.getMessage();
128
+ if (message == null) {
129
+ message = ERROR_UNKNOWN_ERROR;
130
+ }
131
+ String code = null;
132
+ if (exception instanceof CustomException) {
133
+ code = ((CustomException) exception).getCode();
134
+ }
135
+ Logger.error(TAG, message, exception);
136
+ call.reject(message, code);
137
+ }
138
+
139
+ private void rejectCallAsUnimplemented(@NonNull PluginCall call) {
140
+ call.unimplemented("This method is not available on this platform.");
141
+ }
142
+
143
+ private void resolveCall(@NonNull PluginCall call) {
144
+ call.resolve();
145
+ }
146
+
147
+ private void resolveCall(@NonNull PluginCall call, @Nullable Result result) {
148
+ if (result == null) {
149
+ call.resolve();
150
+ } else {
151
+ call.resolve(result.toJSObject());
152
+ }
153
+ }
154
+ }
@@ -0,0 +1,20 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+
6
+ public class CustomException extends Exception {
7
+
8
+ @Nullable
9
+ private final String code;
10
+
11
+ public CustomException(@Nullable String code, @NonNull String message) {
12
+ super(message);
13
+ this.code = code;
14
+ }
15
+
16
+ @Nullable
17
+ public String getCode() {
18
+ return code;
19
+ }
20
+ }
@@ -0,0 +1,16 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes;
2
+
3
+ public class CustomExceptions {
4
+
5
+ public static final CustomException DAYS_INVALID = new CustomException(null, "days must contain only valid weekdays.");
6
+ public static final CustomException DURATION_INVALID = new CustomException(null, "duration must be between 1 and 86400.");
7
+ public static final CustomException DURATION_MISSING = new CustomException(null, "duration must be provided.");
8
+ public static final CustomException HOUR_INVALID = new CustomException(null, "hour must be between 0 and 23.");
9
+ public static final CustomException HOUR_MISSING = new CustomException(null, "hour must be provided.");
10
+ public static final CustomException MINUTE_INVALID = new CustomException(null, "minute must be between 0 and 59.");
11
+ public static final CustomException MINUTE_MISSING = new CustomException(null, "minute must be provided.");
12
+ public static final CustomException NO_CLOCK_APP = new CustomException(
13
+ "NO_CLOCK_APP",
14
+ "No app that can handle alarms was found on the device."
15
+ );
16
+ }
@@ -0,0 +1,124 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes.options;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.JSArray;
6
+ import com.getcapacitor.JSObject;
7
+ import com.getcapacitor.PluginCall;
8
+ import io.capawesome.capacitorjs.plugins.alarm.classes.CustomExceptions;
9
+ import java.util.ArrayList;
10
+ import java.util.Calendar;
11
+
12
+ public class CreateAlarmOptions {
13
+
14
+ @Nullable
15
+ private final ArrayList<Integer> days;
16
+
17
+ private final int hour;
18
+
19
+ @Nullable
20
+ private final String label;
21
+
22
+ private final int minute;
23
+
24
+ private final boolean skipUi;
25
+
26
+ @Nullable
27
+ private final Boolean vibrate;
28
+
29
+ public CreateAlarmOptions(@NonNull PluginCall call) throws Exception {
30
+ JSObject androidOptions = call.getObject("android");
31
+ this.days = CreateAlarmOptions.getDaysFromCall(call);
32
+ this.hour = CreateAlarmOptions.getHourFromCall(call);
33
+ this.label = call.getString("label");
34
+ this.minute = CreateAlarmOptions.getMinuteFromCall(call);
35
+ this.skipUi = androidOptions != null && androidOptions.optBoolean("skipUi", false);
36
+ this.vibrate = androidOptions != null && androidOptions.has("vibrate") ? androidOptions.optBoolean("vibrate", false) : null;
37
+ }
38
+
39
+ @Nullable
40
+ public ArrayList<Integer> getDays() {
41
+ return days;
42
+ }
43
+
44
+ public int getHour() {
45
+ return hour;
46
+ }
47
+
48
+ @Nullable
49
+ public String getLabel() {
50
+ return label;
51
+ }
52
+
53
+ public int getMinute() {
54
+ return minute;
55
+ }
56
+
57
+ public boolean getSkipUi() {
58
+ return skipUi;
59
+ }
60
+
61
+ @Nullable
62
+ public Boolean getVibrate() {
63
+ return vibrate;
64
+ }
65
+
66
+ private static int getCalendarDayFromWeekday(@Nullable String weekday) throws Exception {
67
+ if (weekday == null) {
68
+ throw CustomExceptions.DAYS_INVALID;
69
+ }
70
+ switch (weekday) {
71
+ case "FRIDAY":
72
+ return Calendar.FRIDAY;
73
+ case "MONDAY":
74
+ return Calendar.MONDAY;
75
+ case "SATURDAY":
76
+ return Calendar.SATURDAY;
77
+ case "SUNDAY":
78
+ return Calendar.SUNDAY;
79
+ case "THURSDAY":
80
+ return Calendar.THURSDAY;
81
+ case "TUESDAY":
82
+ return Calendar.TUESDAY;
83
+ case "WEDNESDAY":
84
+ return Calendar.WEDNESDAY;
85
+ default:
86
+ throw CustomExceptions.DAYS_INVALID;
87
+ }
88
+ }
89
+
90
+ @Nullable
91
+ private static ArrayList<Integer> getDaysFromCall(@NonNull PluginCall call) throws Exception {
92
+ JSArray days = call.getArray("days");
93
+ if (days == null) {
94
+ return null;
95
+ }
96
+ ArrayList<Integer> result = new ArrayList<>();
97
+ for (int index = 0; index < days.length(); index++) {
98
+ result.add(CreateAlarmOptions.getCalendarDayFromWeekday(days.optString(index, null)));
99
+ }
100
+ return result;
101
+ }
102
+
103
+ private static int getHourFromCall(@NonNull PluginCall call) throws Exception {
104
+ Integer hour = call.getInt("hour");
105
+ if (hour == null) {
106
+ throw CustomExceptions.HOUR_MISSING;
107
+ }
108
+ if (hour < 0 || hour > 23) {
109
+ throw CustomExceptions.HOUR_INVALID;
110
+ }
111
+ return hour;
112
+ }
113
+
114
+ private static int getMinuteFromCall(@NonNull PluginCall call) throws Exception {
115
+ Integer minute = call.getInt("minute");
116
+ if (minute == null) {
117
+ throw CustomExceptions.MINUTE_MISSING;
118
+ }
119
+ if (minute < 0 || minute > 59) {
120
+ throw CustomExceptions.MINUTE_INVALID;
121
+ }
122
+ return minute;
123
+ }
124
+ }
@@ -0,0 +1,48 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes.options;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.JSObject;
6
+ import com.getcapacitor.PluginCall;
7
+ import io.capawesome.capacitorjs.plugins.alarm.classes.CustomExceptions;
8
+
9
+ public class CreateTimerOptions {
10
+
11
+ private final int duration;
12
+
13
+ @Nullable
14
+ private final String label;
15
+
16
+ private final boolean skipUi;
17
+
18
+ public CreateTimerOptions(@NonNull PluginCall call) throws Exception {
19
+ JSObject androidOptions = call.getObject("android");
20
+ this.duration = CreateTimerOptions.getDurationFromCall(call);
21
+ this.label = call.getString("label");
22
+ this.skipUi = androidOptions != null && androidOptions.optBoolean("skipUi", false);
23
+ }
24
+
25
+ public int getDuration() {
26
+ return duration;
27
+ }
28
+
29
+ @Nullable
30
+ public String getLabel() {
31
+ return label;
32
+ }
33
+
34
+ public boolean getSkipUi() {
35
+ return skipUi;
36
+ }
37
+
38
+ private static int getDurationFromCall(@NonNull PluginCall call) throws Exception {
39
+ Integer duration = call.getInt("duration");
40
+ if (duration == null) {
41
+ throw CustomExceptions.DURATION_MISSING;
42
+ }
43
+ if (duration < 1 || duration > 86400) {
44
+ throw CustomExceptions.DURATION_INVALID;
45
+ }
46
+ return duration;
47
+ }
48
+ }
@@ -0,0 +1,25 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.getcapacitor.JSObject;
6
+ import io.capawesome.capacitorjs.plugins.alarm.interfaces.Result;
7
+ import org.json.JSONObject;
8
+
9
+ public class CreateAlarmResult implements Result {
10
+
11
+ @Nullable
12
+ private final String id;
13
+
14
+ public CreateAlarmResult(@Nullable String id) {
15
+ this.id = id;
16
+ }
17
+
18
+ @Override
19
+ @NonNull
20
+ public JSObject toJSObject() {
21
+ JSObject result = new JSObject();
22
+ result.put("id", id == null ? JSONObject.NULL : id);
23
+ return result;
24
+ }
25
+ }
@@ -0,0 +1,22 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.classes.results;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import com.getcapacitor.JSObject;
5
+ import io.capawesome.capacitorjs.plugins.alarm.interfaces.Result;
6
+
7
+ public class IsAvailableResult implements Result {
8
+
9
+ private final boolean available;
10
+
11
+ public IsAvailableResult(boolean available) {
12
+ this.available = available;
13
+ }
14
+
15
+ @Override
16
+ @NonNull
17
+ public JSObject toJSObject() {
18
+ JSObject result = new JSObject();
19
+ result.put("available", available);
20
+ return result;
21
+ }
22
+ }
@@ -0,0 +1,5 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.interfaces;
2
+
3
+ public interface Callback {
4
+ void error(Exception exception);
5
+ }
@@ -0,0 +1,5 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.interfaces;
2
+
3
+ public interface EmptyCallback extends Callback {
4
+ void success();
5
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.interfaces;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ public interface NonEmptyResultCallback<T extends Result> extends Callback {
6
+ void success(@NonNull T result);
7
+ }
@@ -0,0 +1,7 @@
1
+ package io.capawesome.capacitorjs.plugins.alarm.interfaces;
2
+
3
+ import com.getcapacitor.JSObject;
4
+
5
+ public interface Result {
6
+ JSObject toJSObject();
7
+ }
File without changes