-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sorting reminders when showing (#57)
- Loading branch information
Showing
3 changed files
with
57 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import ArgumentParser | ||
import EventKit | ||
|
||
public enum Sort: String, Decodable, ExpressibleByArgument, CaseIterable { | ||
case none | ||
case creationDate = "creation-date" | ||
case dueDate = "due-date" | ||
|
||
public static let commaSeparatedCases = Self.allCases.map { $0.rawValue }.joined(separator: ", ") | ||
|
||
func sortFunction(order: CustomSortOrder) -> (EKReminder, EKReminder) -> Bool { | ||
let comparison: (Date, Date) -> Bool = order == .ascending ? (<) : (>) | ||
switch self { | ||
case .none: return { _, _ in fatalError() } | ||
case .creationDate: return { comparison($0.creationDate!, $1.creationDate!) } | ||
case .dueDate: return { | ||
switch ($0.dueDateComponents, $1.dueDateComponents) { | ||
case (.none, .none): return false | ||
case (.none, .some): return false | ||
case (.some, .none): return true | ||
case (.some, .some): return comparison($0.dueDateComponents!.date!, $1.dueDateComponents!.date!) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// TODO: Replace with SortOrder when we drop < macOS 12.0 | ||
public enum CustomSortOrder: String, Decodable, ExpressibleByArgument, CaseIterable { | ||
case ascending | ||
case descending | ||
|
||
public static let commaSeparatedCases = Self.allCases.map { $0.rawValue }.joined(separator: ", ") | ||
} |