Skip to content

Commit

Permalink
Swift 3.0 conversion (#5)
Browse files Browse the repository at this point in the history
* Migrate to Swift 3

* Add Makefile and travis config
  • Loading branch information
Keith Smiley authored Sep 19, 2016
1 parent 30372a1 commit 80179d5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.build
Packages
reminders
reminders.tar.gz
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-02-08-a
3.0
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: objective-c
osx_image: xcode8

script:
- make build
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
RELEASE_BUILD=./.build/release
EXECUTABLE=reminders
PREFIX?=/usr/local/bin
ARCHIVE=$(EXECUTABLE).tar.gz

.PHONY: clean build release package install uninstall
SRC=$(wildcard Sources/*.swift)

clean:
rm -f $(EXECUTABLE) $(ARCHIVE)
swift build --clean

build: $(SRC)
swift build

release: clean
swift build \
--configuration release \
-Xswiftc -static-stdlib

package: release
tar -pvczf $(ARCHIVE) -C $(RELEASE_BUILD) $(EXECUTABLE)
tar -zxvf $(ARCHIVE)
@shasum -a 256 $(ARCHIVE)
@shasum -a 256 $(EXECUTABLE)

install: release
install $(RELEASE_BUILD)/$(EXECUTABLE) $(PREFIX)

uninstall:
rm "$(PREFIX)/$(EXECUTABLE)"
8 changes: 4 additions & 4 deletions Sources/CollectionType+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extension CollectionType {
func find(@noescape predicate: (Generator.Element) throws -> Bool) rethrows -> Generator.Element? {
return try self.indexOf(predicate).flatMap { self[$0] }
extension Collection {
func find(where predicate: (Generator.Element) throws -> Bool) rethrows -> Generator.Element? {
return try self.index(where: predicate).flatMap { self[$0] }
}
}

extension CollectionType where Index == Int {
extension Collection where Index == Int, IndexDistance == Int {
subscript(safe index: Int) -> Generator.Element? {
return index < self.count && index >= 0 ? self[index] : nil
}
Expand Down
5 changes: 0 additions & 5 deletions Sources/GCD.swift

This file was deleted.

11 changes: 0 additions & 11 deletions Sources/NSDate+Extension.swift

This file was deleted.

49 changes: 24 additions & 25 deletions Sources/Reminders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import EventKit
private let Store = EKEventStore()

final class Reminders {
func requestAccess(completion: (granted: Bool) -> Void) {
Store.requestAccessToEntityType(.Reminder) { granted, _ in
executeOnMainQueue {
completion(granted: granted)
func requestAccess(completion: @escaping (_ granted: Bool) -> Void) {
Store.requestAccess(to: .reminder) { granted, _ in
DispatchQueue.main.async {
completion(granted)
}
}
}
Expand All @@ -20,22 +20,22 @@ final class Reminders {

func showListItems(withName name: String) {
let calendar = self.calendar(withName: name)
let semaphore = dispatch_semaphore_create(0)
let semaphore = DispatchSemaphore(value: 0)

self.reminders(onCalendar: calendar) { reminders in
for (i, reminder) in reminders.enumerate() {
for (i, reminder) in reminders.enumerated() {
print(i, reminder.title)
}

dispatch_semaphore_signal(semaphore)
semaphore.signal()
}

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
semaphore.wait()
}

func complete(itemAtIndex index: Int, onListNamed name: String) {
let calendar = self.calendar(withName: name)
let semaphore = dispatch_semaphore_create(0)
let semaphore = DispatchSemaphore(value: 0)

self.reminders(onCalendar: calendar) { reminders in
guard let reminder = reminders[safe: index] else {
Expand All @@ -44,28 +44,28 @@ final class Reminders {
}

do {
reminder.completed = true
try Store.saveReminder(reminder, commit: true)
reminder.isCompleted = true
try Store.save(reminder, commit: true)
print("Completed '\(reminder.title)'")
} catch let error {
print("Failed to save reminder with error: \(error)")
exit(1)
}

dispatch_semaphore_signal(semaphore)
semaphore.signal()
}

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
semaphore.wait()
}

func addReminder(string string: String, toListNamed name: String) {
func addReminder(string: String, toListNamed name: String) {
let calendar = self.calendar(withName: name)
let reminder = EKReminder(eventStore: Store)
reminder.calendar = calendar
reminder.title = string

do {
try Store.saveReminder(reminder, commit: true)
try Store.save(reminder, commit: true)
print("Added '\(reminder.title)' to '\(calendar.title)'")
} catch let error {
print("Failed to save reminder with error: \(error)")
Expand All @@ -76,20 +76,19 @@ final class Reminders {
// MARK: - Private functions

private func reminders(onCalendar calendar: EKCalendar,
completion: (reminders: [EKReminder]) -> Void)
completion: @escaping (_ reminders: [EKReminder]) -> Void)
{
let predicate = Store.predicateForRemindersInCalendars([calendar])
Store.fetchRemindersMatchingPredicate(predicate) { reminders in
let reminders = reminders?.filter { !$0.completed }
.sort { $0.creationDate < $1.creationDate }
completion(reminders: reminders ?? [])
let predicate = Store.predicateForReminders(in: [calendar])
Store.fetchReminders(matching: predicate) { reminders in
let reminders = reminders?
.filter { !$0.isCompleted }
.sorted { ($0.creationDate ?? Date.distantPast) < ($1.creationDate ?? Date.distantPast) }
completion(reminders ?? [])
}
}

private func calendar(withName name: String) -> EKCalendar {
if let calendar = self.getCalendars()
.find({ $0.title.lowercaseString == name.lowercaseString })
{
if let calendar = self.getCalendars().find(where: { $0.title.lowercased() == name.lowercased() }) {
return calendar
} else {
print("No reminders list matching \(name)")
Expand All @@ -98,7 +97,7 @@ final class Reminders {
}

private func getCalendars() -> [EKCalendar] {
return Store.calendarsForEntityType(.Reminder)
return Store.calendars(for: .reminder)
.filter { $0.allowsContentModifications }
}
}
4 changes: 2 additions & 2 deletions Sources/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private func createCLI() -> Group {
reminders.complete(itemAtIndex: index, onListNamed: listName)
}
$0.command("add") { (listName: String, parser: ArgumentParser) in
let string = parser.remainder.joinWithSeparator(" ")
let string = parser.remainder.joined(separator: " ")
reminders.addReminder(string: string, toListNamed: listName)
}
}
Expand All @@ -29,4 +29,4 @@ reminders.requestAccess { granted in
}
}

NSApplication.sharedApplication().run()
NSApplication.shared().run()

0 comments on commit 80179d5

Please sign in to comment.