Skip to content

Commit

Permalink
feat(local-notifications): Support setting seconds in Schedule "on" (#…
Browse files Browse the repository at this point in the history
…253)

* [android] add support for setting seconds for schedule `on`

* [android] do not override calendar seconds set from date

Co-authored-by: Joseph Pender <joseph@ionic.io>
  • Loading branch information
theproducer and Joseph Pender committed Feb 17, 2021
1 parent aa897ab commit 4ec8d06
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions local-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ Enables basic storage and retrieval of dates and times.
| **`day`** | <code>number</code> |
| **`hour`** | <code>number</code> |
| **`minute`** | <code>number</code> |
| **`second`** | <code>number</code> |


#### Attachment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DateMatch {
private Integer day;
private Integer hour;
private Integer minute;
private Integer second;

// Unit used to save the last used unit for a trigger.
// One of the Calendar constants values
Expand Down Expand Up @@ -63,6 +64,14 @@ public void setMinute(Integer minute) {
this.minute = minute;
}

public Integer getSecond() {
return second;
}

public void setSecond(Integer second) {
this.second = second;
}

/**
* Gets a calendar instance pointing to the specified date.
*
Expand All @@ -72,7 +81,6 @@ private Calendar buildCalendar(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
return cal;
}

Expand Down Expand Up @@ -102,6 +110,8 @@ private long postponeTriggerIfNeeded(Calendar current, Calendar next) {
incrementUnit = Calendar.DAY_OF_MONTH;
} else if (unit == Calendar.MINUTE) {
incrementUnit = Calendar.HOUR_OF_DAY;
} else if (unit == Calendar.SECOND) {
incrementUnit = Calendar.MINUTE;
}

if (incrementUnit != -1) {
Expand Down Expand Up @@ -133,12 +143,31 @@ private Calendar buildNextTriggerTime(Date date) {
next.set(Calendar.MINUTE, minute);
if (unit == -1) unit = Calendar.MINUTE;
}
if (second != null) {
next.set(Calendar.SECOND, second);
if (unit == -1) unit = Calendar.SECOND;
}
return next;
}

@Override
public String toString() {
return "DateMatch{" + "year=" + year + ", month=" + month + ", day=" + day + ", hour=" + hour + ", minute=" + minute + '}';
return (
"DateMatch{" +
"year=" +
year +
", month=" +
month +
", day=" +
day +
", hour=" +
hour +
", minute=" +
minute +
", second=" +
second +
'}'
);
}

@Override
Expand All @@ -152,7 +181,8 @@ public boolean equals(Object o) {
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 (hour != null ? !hour.equals(dateMatch.hour) : dateMatch.hour != null) return false;
return minute != null ? minute.equals(dateMatch.minute) : dateMatch.minute == null;
if (minute != null ? !minute.equals(dateMatch.minute) : dateMatch.minute != null) return false;
return second != null ? second.equals(dateMatch.second) : dateMatch.second == null;
}

@Override
Expand All @@ -162,6 +192,7 @@ public int hashCode() {
result = 31 * result + (day != null ? day.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);
return result;
}

Expand All @@ -171,7 +202,8 @@ public int hashCode() {
* @return
*/
public String toMatchString() {
String matchString = year + separator + month + separator + day + separator + hour + separator + minute + separator + unit;
String matchString =
year + separator + month + separator + day + separator + hour + separator + minute + separator + second + separator + unit;
return matchString.replace("null", "*");
}

Expand All @@ -192,6 +224,17 @@ public static DateMatch fromMatchString(String matchString) {
date.setMinute(getValueFromCronElement(split[4]));
date.setUnit(getValueFromCronElement(split[5]));
}

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.setSecond(getValueFromCronElement(split[5]));
date.setUnit(getValueFromCronElement(split[6]));
}

return date;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private void buildOnElement(JSObject schedule) {
on.setDay(onJson.getInteger("day"));
on.setHour(onJson.getInteger("hour"));
on.setMinute(onJson.getInteger("minute"));
on.setSecond(onJson.getInteger("second"));
}
}

Expand Down
1 change: 1 addition & 0 deletions local-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ export interface ScheduleOn {
day?: number;
hour?: number;
minute?: number;
second?: number;
}

export type ScheduleEvery =
Expand Down

0 comments on commit 4ec8d06

Please sign in to comment.