Skip to content

Commit

Permalink
feat(local-notifications): Add weekday scheduling support for Android…
Browse files Browse the repository at this point in the history
… and iOS (#756)



Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
nkalupahana and jcesarmobile authored Jan 18, 2022
1 parent a960489 commit 430b485
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
34 changes: 26 additions & 8 deletions local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ If the device has entered [Doze](https://developer.android.com/training/monitori
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
* [Type Aliases](#type-aliases)
* [Enums](#enums)

</docgen-index>

Expand Down Expand Up @@ -434,14 +435,15 @@ Enables basic storage and retrieval of dates and times.

#### ScheduleOn

| Prop | Type |
| ------------ | ------------------- |
| **`year`** | <code>number</code> |
| **`month`** | <code>number</code> |
| **`day`** | <code>number</code> |
| **`hour`** | <code>number</code> |
| **`minute`** | <code>number</code> |
| **`second`** | <code>number</code> |
| Prop | Type |
| ------------- | ------------------------------------------- |
| **`year`** | <code>number</code> |
| **`month`** | <code>number</code> |
| **`day`** | <code>number</code> |
| **`weekday`** | <code><a href="#weekday">Weekday</a></code> |
| **`hour`** | <code>number</code> |
| **`minute`** | <code>number</code> |
| **`second`** | <code>number</code> |


#### Attachment
Expand Down Expand Up @@ -611,4 +613,20 @@ The notification visibility. For more details, see the [Android Developer Docs](

<code>'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'</code>


### Enums


#### Weekday

| Members | Value |
| --------------- | -------------- |
| **`Sunday`** | <code>1</code> |
| **`Monday`** | <code>2</code> |
| **`Tuesday`** | <code>3</code> |
| **`Wednesday`** | <code>4</code> |
| **`Thursday`** | <code>5</code> |
| **`Friday`** | <code>6</code> |
| **`Saturday`** | <code>7</code> |

</docgen-api>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DateMatch {
private Integer year;
private Integer month;
private Integer day;
private Integer weekday;
private Integer hour;
private Integer minute;
private Integer second;
Expand Down Expand Up @@ -48,6 +49,14 @@ public void setDay(Integer day) {
this.day = day;
}

public Integer getWeekday() {
return weekday;
}

public void setWeekday(Integer weekday) {
this.weekday = weekday;
}

public Integer getHour() {
return hour;
}
Expand Down Expand Up @@ -106,6 +115,8 @@ private long postponeTriggerIfNeeded(Calendar current, Calendar next) {
incrementUnit = Calendar.YEAR;
} else if (unit == Calendar.DAY_OF_MONTH) {
incrementUnit = Calendar.MONTH;
} else if (unit == Calendar.DAY_OF_WEEK) {
incrementUnit = Calendar.WEEK_OF_MONTH;
} else if (unit == Calendar.HOUR_OF_DAY) {
incrementUnit = Calendar.DAY_OF_MONTH;
} else if (unit == Calendar.MINUTE) {
Expand Down Expand Up @@ -135,6 +146,10 @@ private Calendar buildNextTriggerTime(Date date) {
next.set(Calendar.DAY_OF_MONTH, day);
if (unit == -1) unit = Calendar.DAY_OF_MONTH;
}
if (weekday != null) {
next.set(Calendar.DAY_OF_WEEK, weekday);
if (unit == -1) unit = Calendar.DAY_OF_WEEK;
}
if (hour != null) {
next.set(Calendar.HOUR_OF_DAY, hour);
if (unit == -1) unit = Calendar.HOUR_OF_DAY;
Expand All @@ -160,6 +175,8 @@ public String toString() {
month +
", day=" +
day +
", weekday=" +
weekday +
", hour=" +
hour +
", minute=" +
Expand All @@ -180,6 +197,7 @@ public boolean equals(Object o) {
if (year != null ? !year.equals(dateMatch.year) : dateMatch.year != null) return false;
if (month != null ? !month.equals(dateMatch.month) : dateMatch.month != null) return false;
if (day != null ? !day.equals(dateMatch.day) : dateMatch.day != null) return false;
if (weekday != null ? !weekday.equals(dateMatch.weekday) : dateMatch.weekday != null) return false;
if (hour != null ? !hour.equals(dateMatch.hour) : dateMatch.hour != null) return false;
if (minute != null ? !minute.equals(dateMatch.minute) : dateMatch.minute != null) return false;
return second != null ? second.equals(dateMatch.second) : dateMatch.second == null;
Expand All @@ -190,6 +208,7 @@ public int hashCode() {
int result = year != null ? year.hashCode() : 0;
result = 31 * result + (month != null ? month.hashCode() : 0);
result = 31 * result + (day != null ? day.hashCode() : 0);
result = 31 * result + (weekday != null ? weekday.hashCode() : 0);
result = 31 * result + (hour != null ? hour.hashCode() : 0);
result = 31 * result + (minute != null ? minute.hashCode() : 0);
result = 31 + result + (second != null ? second.hashCode() : 0);
Expand All @@ -203,7 +222,21 @@ public int hashCode() {
*/
public String toMatchString() {
String matchString =
year + separator + month + separator + day + separator + hour + separator + minute + separator + second + separator + unit;
year +
separator +
month +
separator +
day +
separator +
weekday +
separator +
hour +
separator +
minute +
separator +
second +
separator +
unit;
return matchString.replace("null", "*");
}

Expand All @@ -216,23 +249,25 @@ public String toMatchString() {
public static DateMatch fromMatchString(String matchString) {
DateMatch date = new DateMatch();
String[] split = matchString.split(separator);
if (split != null && split.length == 6) {
if (split != null && split.length == 7) {
date.setYear(getValueFromCronElement(split[0]));
date.setMonth(getValueFromCronElement(split[1]));
date.setDay(getValueFromCronElement(split[2]));
date.setHour(getValueFromCronElement(split[3]));
date.setMinute(getValueFromCronElement(split[4]));
date.setUnit(getValueFromCronElement(split[5]));
date.setWeekday(getValueFromCronElement(split[3]));
date.setHour(getValueFromCronElement(split[4]));
date.setMinute(getValueFromCronElement(split[5]));
date.setUnit(getValueFromCronElement(split[6]));
}

if (split != null && split.length == 7) {
if (split != null && split.length == 8) {
date.setYear(getValueFromCronElement(split[0]));
date.setMonth(getValueFromCronElement(split[1]));
date.setDay(getValueFromCronElement(split[2]));
date.setHour(getValueFromCronElement(split[3]));
date.setMinute(getValueFromCronElement(split[4]));
date.setSecond(getValueFromCronElement(split[5]));
date.setUnit(getValueFromCronElement(split[6]));
date.setWeekday(getValueFromCronElement(split[3]));
date.setHour(getValueFromCronElement(split[4]));
date.setMinute(getValueFromCronElement(split[5]));
date.setSecond(getValueFromCronElement(split[6]));
date.setUnit(getValueFromCronElement(split[7]));
}

return date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private void buildOnElement(JSObject schedule) {
on.setYear(onJson.getInteger("year"));
on.setMonth(onJson.getInteger("month"));
on.setDay(onJson.getInteger("day"));
on.setWeekday(onJson.getInteger("weekday"));
on.setHour(onJson.getInteger("hour"));
on.setMinute(onJson.getInteger("minute"));
on.setSecond(onJson.getInteger("second"));
Expand Down
3 changes: 3 additions & 0 deletions local-notifications/ios/Plugin/LocalNotificationsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ public class LocalNotificationsPlugin: CAPPlugin {
if let second = at["second"] as? Int {
dateInfo.second = second
}
if let weekday = at["weekday"] as? Int {
dateInfo.weekday = weekday
}
return dateInfo
}

Expand Down
14 changes: 14 additions & 0 deletions local-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ export interface ScheduleOn {
year?: number;
month?: number;
day?: number;
weekday?: Weekday;
hour?: number;
minute?: number;
second?: number;
Expand Down Expand Up @@ -938,6 +939,19 @@ export interface Channel {
vibration?: boolean;
}

/**
* Day of the week. Used for scheduling notifications on a particular weekday.
*/
export enum Weekday {
Sunday = 1,
Monday = 2,
Tuesday = 3,
Wednesday = 4,
Thursday = 5,
Friday = 6,
Saturday = 7,
}

/**
* The importance level. For more details, see the [Android Developer Docs](https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_DEFAULT)
* @since 1.0.0
Expand Down

0 comments on commit 430b485

Please sign in to comment.