Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to edit or clear the due date, and priority. #87

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions Sources/RemindersLibrary/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@ private struct Edit: ParsableCommand {
@Argument(
help: "The index or id of the reminder to delete, see 'show' for indexes")
var index: String

@Option(
name: .shortAndLong,
help: "The new date the reminder is due")
var dueDate: DateComponents?

@Flag(
name: .shortAndLong,
help: "Clear the due date.")
var clearDueDate: Bool = false

@Option(
name: .shortAndLong,
help: "The new priority of the reminder")
var priority: Priority?

@Flag(
name: .shortAndLong,
help: "Clear the priority of the reminder.")
var clearPriority: Bool = false

@Option(
name: .shortAndLong,
Expand All @@ -241,8 +261,13 @@ private struct Edit: ParsableCommand {
var reminder: [String] = []

func validate() throws {
if self.reminder.isEmpty && self.notes == nil {
throw ValidationError("Must specify either new reminder content or new notes")

if self.dueDate != nil && self.clearDueDate {
throw ValidationError("Don't try to set & clear the due date at the same time.")
}

if self.reminder.isEmpty && self.notes == nil && self.dueDate == nil && !self.clearDueDate {
throw ValidationError("Must specify new reminder content, new notes, or a new due date.")
}
}

Expand All @@ -252,7 +277,11 @@ private struct Edit: ParsableCommand {
itemAtIndex: self.index,
onListNamed: self.listName,
newText: newText.isEmpty ? nil : newText,
newNotes: self.notes
newNotes: self.notes,
dueDateComponents: self.dueDate,
clearDueDate: self.clearDueDate,
priority: self.priority,
clearPriority: self.clearPriority
)
}
}
Expand Down
28 changes: 27 additions & 1 deletion Sources/RemindersLibrary/Reminders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public final class Reminders {
}
}

func edit(itemAtIndex index: String, onListNamed name: String, newText: String?, newNotes: String?) {
func edit(itemAtIndex index: String, onListNamed name: String, newText: String?, newNotes: String?, dueDateComponents: DateComponents? = nil, clearDueDate: Bool, priority: Priority?, clearPriority: Bool) {
let calendar = self.calendar(withName: name)
let semaphore = DispatchSemaphore(value: 0)

Expand All @@ -236,6 +236,32 @@ public final class Reminders {
do {
reminder.title = newText ?? reminder.title
reminder.notes = newNotes ?? reminder.notes


if clearPriority {
// https://developer.apple.com/documentation/eventkit/ekreminderpriority/none
reminder.priority = 0
}
else if priority != nil {
reminder.priority = Int(priority?.value.rawValue ?? UInt(reminder.priority))

}

if clearDueDate || (dueDateComponents != nil) {
// remove previous time-based alarms, leaving location alarms.
reminder.dueDateComponents = nil
for alarm in reminder.alarms ?? [] {
if alarm.structuredLocation != nil { continue } else { reminder.removeAlarm(alarm) }
}

}
if dueDateComponents != nil {
reminder.dueDateComponents = dueDateComponents
if let dueDate = dueDateComponents?.date, dueDateComponents?.hour != nil {
reminder.addAlarm(EKAlarm(absoluteDate: dueDate))
}
}

try Store.save(reminder, commit: true)
print("Updated reminder '\(reminder.title!)'")
} catch let error {
Expand Down
Loading