Commit 41a518db by Daniel Dahan

development: added Reminders API, initial approach

parent 928ba8cb
......@@ -10,6 +10,8 @@
961EFC581D738FF600E84652 /* SnackbarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961EFC571D738FF600E84652 /* SnackbarController.swift */; };
961F18E81CD93E3E008927C5 /* ErrorTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 961F18E71CD93E3E008927C5 /* ErrorTextField.swift */; };
96230AB81D6A520C00AF47DC /* Divider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96230AB71D6A520C00AF47DC /* Divider.swift */; };
96264BE71D833D2800576F37 /* Reminders.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96264BE61D833D2800576F37 /* Reminders.swift */; };
96264BE91D833D3400576F37 /* RemindersController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96264BE81D833D3400576F37 /* RemindersController.swift */; };
9628645A1D53FE3E00690B69 /* Material+UIWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 962864591D53FE3E00690B69 /* Material+UIWindow.swift */; };
9628645F1D540AF300690B69 /* DynamicFontType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9628645E1D540AF300690B69 /* DynamicFontType.swift */; };
962864621D54111D00690B69 /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 962864611D54111D00690B69 /* JSON.swift */; };
......@@ -166,7 +168,6 @@
96BCB8571CB4115200C806FE /* View.swift in Headers */ = {isa = PBXBuildFile; fileRef = 96BCB78C1CB40DC500C806FE /* View.swift */; settings = {ATTRIBUTES = (Public, ); }; };
96BCB8591CB4118E00C806FE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 96334EF51C8B84660083986B /* Assets.xcassets */; };
96C1C8811D42C62800E6608F /* Material+Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C1C8801D42C62800E6608F /* Material+Array.swift */; };
96C9CDBC1D7B1040006A4154 /* Reminder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96C9CDBB1D7B1040006A4154 /* Reminder.swift */; };
96D88C321C1328D800B91418 /* Material.h in Headers */ = {isa = PBXBuildFile; fileRef = 96D88C091C1328D800B91418 /* Material.h */; settings = {ATTRIBUTES = (Public, ); }; };
96E3C3941D397AE90086A024 /* Material+UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96E3C3931D397AE90086A024 /* Material+UIView.swift */; };
96E3C3951D3A1CC20086A024 /* IconButton.swift in Headers */ = {isa = PBXBuildFile; fileRef = 9658F2161CD6FA4700B902C1 /* IconButton.swift */; settings = {ATTRIBUTES = (Public, ); }; };
......@@ -202,6 +203,8 @@
961EFC571D738FF600E84652 /* SnackbarController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SnackbarController.swift; sourceTree = "<group>"; };
961F18E71CD93E3E008927C5 /* ErrorTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorTextField.swift; sourceTree = "<group>"; };
96230AB71D6A520C00AF47DC /* Divider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Divider.swift; sourceTree = "<group>"; };
96264BE61D833D2800576F37 /* Reminders.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reminders.swift; sourceTree = "<group>"; };
96264BE81D833D3400576F37 /* RemindersController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemindersController.swift; sourceTree = "<group>"; };
962864591D53FE3E00690B69 /* Material+UIWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Material+UIWindow.swift"; sourceTree = "<group>"; };
9628645E1D540AF300690B69 /* DynamicFontType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicFontType.swift; sourceTree = "<group>"; };
962864611D54111D00690B69 /* JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSON.swift; sourceTree = "<group>"; };
......@@ -289,7 +292,6 @@
96BCB7F11CB40DE900C806FE /* Roboto-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Roboto-Regular.ttf"; sourceTree = "<group>"; };
96BCB7F21CB40DE900C806FE /* Roboto-Thin.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Roboto-Thin.ttf"; sourceTree = "<group>"; };
96C1C8801D42C62800E6608F /* Material+Array.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Material+Array.swift"; sourceTree = "<group>"; };
96C9CDBB1D7B1040006A4154 /* Reminder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reminder.swift; sourceTree = "<group>"; };
96D88BFC1C1328D800B91418 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
96D88BFD1C1328D800B91418 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
96D88C091C1328D800B91418 /* Material.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Material.h; sourceTree = "<group>"; };
......@@ -350,6 +352,15 @@
name = BarView;
sourceTree = "<group>";
};
96264BE51D833CFF00576F37 /* Reminders */ = {
isa = PBXGroup;
children = (
96264BE61D833D2800576F37 /* Reminders.swift */,
96264BE81D833D3400576F37 /* RemindersController.swift */,
);
name = Reminders;
sourceTree = "<group>";
};
962864601D5410FD00690B69 /* JSON */ = {
isa = PBXGroup;
children = (
......@@ -538,7 +549,7 @@
96BCB8011CB40F1700C806FE /* Navigation */,
962DDD071D6FBBB7001C307C /* Page */,
96EA9A411D4E68E60052C74D /* PhotoLibrary */,
96C9CDBA1D7B101C006A4154 /* Reminder */,
96264BE51D833CFF00576F37 /* Reminders */,
963FBF031D6696EF008F8512 /* SearchBar */,
963FBEFB1D6694E8008F8512 /* Snackbar */,
967A48171D0F424B00B8CEB7 /* StatusBar */,
......@@ -744,14 +755,6 @@
name = Text;
sourceTree = "<group>";
};
96C9CDBA1D7B101C006A4154 /* Reminder */ = {
isa = PBXGroup;
children = (
96C9CDBB1D7B1040006A4154 /* Reminder.swift */,
);
name = Reminder;
sourceTree = "<group>";
};
96D88BF41C1328D800B91418 /* Sources */ = {
isa = PBXGroup;
children = (
......@@ -1059,7 +1062,6 @@
96BCB7CA1CB40DC500C806FE /* Layout.swift in Sources */,
96BCB7D11CB40DC500C806FE /* Switch.swift in Sources */,
96BCB7BA1CB40DC500C806FE /* CollectionView.swift in Sources */,
96C9CDBC1D7B1040006A4154 /* Reminder.swift in Sources */,
96BCB7A31CB40DC500C806FE /* CapturePreview.swift in Sources */,
96BCB7BC1CB40DC500C806FE /* CollectionViewDataSource.swift in Sources */,
96EA9A4B1D4E7A430052C74D /* PhotoLibraryController.swift in Sources */,
......@@ -1079,6 +1081,7 @@
96230AB81D6A520C00AF47DC /* Divider.swift in Sources */,
96BCB7C41CB40DC500C806FE /* Font.swift in Sources */,
96BCB7AA1CB40DC500C806FE /* Grid.swift in Sources */,
96264BE91D833D3400576F37 /* RemindersController.swift in Sources */,
96BCB7E71CB40DC500C806FE /* TextView.swift in Sources */,
96BCB7C31CB40DC500C806FE /* EdgeInsets.swift in Sources */,
96BCB7CE1CB40DC500C806FE /* CornerRadius.swift in Sources */,
......@@ -1088,6 +1091,7 @@
96BCB7D41CB40DC500C806FE /* TransitionAnimation.swift in Sources */,
967A48191D0F425A00B8CEB7 /* StatusBarController.swift in Sources */,
962864621D54111D00690B69 /* JSON.swift in Sources */,
96264BE71D833D2800576F37 /* Reminders.swift in Sources */,
96BCB7C21CB40DC500C806FE /* Device.swift in Sources */,
96BCB7A41CB40DC500C806FE /* CaptureSession.swift in Sources */,
963FBF081D669D14008F8512 /* PageTabBarController.swift in Sources */,
......
//
// Reminder.swift
// Material
//
// Created by Daniel Dahan on 2016-09-03.
// Copyright © 2016 CosmicMind, Inc. All rights reserved.
//
import Foundation
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import EventKit
@objc(RemindersAuthorizationStatus)
public enum RemindersAuthorizationStatus: Int {
case authorized
case denied
}
@objc(RemindersDelegate)
public protocol RemindersDelegate {
/**
A delegation method that is executed when the Reminder status is updated.
- Parameter reminders: A reference to the Reminder.
- Parameter status: A reference to the ReminderAuthorizationStatus.
*/
@objc
optional func reminders(reminders: Reminders, status: RemindersAuthorizationStatus)
/**
A delegation method that is executed when Reminders is authorized.
- Parameter reminders: A reference to the Reminders.
*/
@objc
optional func reminders(authorized reminders: Reminders)
/**
A delegation method that is executed when Reminders is denied.
- Parameter reminders: A reference to the Reminders.
*/
@objc
optional func reminders(denied reminders: Reminders)
/**
A delegation method that is executed when a new Reminders list is created
- Parameter reminders: A reference to the Reminders.
- Parameter list: A reference to the calendar created
- Parameter created: A boolean describing if the operation succeeded or not.
*/
@objc
optional func reminders(reminders: Reminders, list: EKCalendar, created: Bool)
/**
A delegation method that is executed when a new Reminders list is created
- Parameter reminders: A reference to the Reminder.
- Parameter list: A reference to the calendar created
- Parameter deleted: A boolean describing if the operation succeeded or not.
*/
@objc
optional func reminders(reminders: Reminders, list: EKCalendar, deleted: Bool)
/**
A delegation method that is executed when a new Reminders list is created
- Parameter reminders: A reference to the Reminder.
- Parameter created: A boolean describing if the operation succeeded or not.
*/
@objc
optional func reminders(reminders: Reminders, created: Bool)
/**
A delegation method that is executed when a new Reminders list is created
- Parameter reminders: A reference to the Reminder.
- Parameter deleted: A boolean describing if the operation succeeded or not.
*/
@objc
optional func reminders(reminders: Reminders, deleted: Bool)
}
@objc(Reminders)
open class Reminders: NSObject {
/// The current ReminderAuthorizationStatus.
public var authorizationStatus: RemindersAuthorizationStatus {
return .authorized == EKEventStore.authorizationStatus(for: .reminder) ? .authorized : .denied
}
/// A reference to the eventStore.
internal lazy var eventStore = EKEventStore()
/// A reference to a RemindersDelegate.
open weak var delegate: RemindersDelegate?
open func requestAuthorization(_ completion: (@escaping (RemindersAuthorizationStatus) -> Void)? = nil) {
eventStore.requestAccess(to: .reminder) { [weak self, completion = completion] (permission, _) in
DispatchQueue.main.async { [weak self, completion = completion] in
guard let s = self else {
return
}
if permission {
s.delegate?.reminders?(reminders: s, status: .authorized)
s.delegate?.reminders?(authorized: s)
completion?(.authorized)
} else {
s.delegate?.reminders?(reminders: s, status: .denied)
s.delegate?.reminders?(denied: s)
completion?(.denied)
}
}
}
}
}
// List CRUD operations
extension Reminders {
/**
A method for creating new Reminder lists
- Parameter list title: the name of the list
- Parameter completion: optional completion call back
*/
public func create(list title: String, completion: (@escaping (Bool, Error?) -> Void)? = nil) {
DispatchQueue.global(qos: .default).async { [weak self, completion = completion] in
guard let s = self else {
return
}
let list = EKCalendar(for: .reminder, eventStore: s.eventStore)
list.title = title
for source in s.eventStore.sources {
if .local == source.sourceType {
list.source = source
var created = false
var error: Error?
do {
try s.eventStore.saveCalendar(list, commit: true)
created = true
} catch let e {
error = e
}
DispatchQueue.main.async { [weak self, completion = completion] in
guard let s = self else {
return
}
s.delegate?.reminders?(reminders: s, list: list, created: created)
completion?(created, error)
}
}
}
}
}
/**
A method for deleting existing Reminder lists
- Parameter list identifier: the name of the list
- Parameter completion: optional completion call back
*/
public func delete(list identifier: String, completion: (@escaping (Bool, Error?) -> Void)? = nil) {
DispatchQueue.global(qos: .default).async { [weak self, completion = completion] in
guard let s = self else {
return
}
guard let list = s.eventStore.calendar(withIdentifier: identifier) else {
return
}
var deleted = false
var error: Error?
do {
try s.eventStore.removeCalendar(list, commit: true)
deleted = true
} catch let e {
error = e
}
DispatchQueue.main.async { [weak self, completion = completion] in
guard let s = self else {
return
}
s.delegate?.reminders?(reminders: s, list: list, deleted: deleted)
completion?(deleted, error)
}
}
}
/**
A method for retrieving reminder lists
- Parameter completion: completion call back
*/
public func fetchLists(completion: ([EKCalendar]) -> Void) {
completion(eventStore.calendars(for: .reminder))
}
}
// Reminder list CRUD operations
extension Reminders {
/**
A method for retrieving reminders from an optionally existing list.
if the list does not exist the reminders will be retrieved from the default reminders list
- Parameter list: An optional EKCalendar.
- Parameter completion: completion call back
*/
public func fetchReminders(list: EKCalendar, completion: @escaping ([EKReminder]) -> Void) {
var lists = [EKCalendar]()
lists.append(list)
eventStore.fetchReminders(matching: eventStore.predicateForReminders(in: lists)) { [completion = completion] (reminders) in
DispatchQueue.main.async { [completion = completion] in
completion(reminders ?? [])
}
}
}
// FIX ME: Should we use the calendar identifier here instead of the title for finding the right cal?
/**
A method for adding a new reminder to an optionally existing list.
if the list does not exist it will be added to the default reminders list.
- Parameter completion: optional completion call back
*/
public func create(title: String, dateComponents: DateComponents, in list: EKCalendar? = nil, completion: (@escaping (Error?) -> Void)? = nil) {
var reminderCal = [EKCalendar]()
if list != nil {
fetchLists(completion: { (calendars) in
for calendar in calendars {
if calendar.title == list!.title {
reminderCal.append(calendar)
}
}
})
}
let reminder = EKReminder(eventStore: eventStore)
reminder.title = title
reminder.dueDateComponents = dateComponents
reminder.calendar = reminderCal.last!
var created: Bool = false
var error: Error?
do {
try eventStore.save(reminder, commit: true)
created = true
} catch let e {
error = e
}
DispatchQueue.main.async { [weak self] in
guard let s = self else {
return
}
s.delegate?.reminders?(reminders: s, created: created)
if let c = completion {
c(error)
}
}
}
// FIX ME: Should we use the calendar identifier here instead of the title for finding the right cal?
/**
A method for adding a new reminder to an optionally existing list.
if the list does not exist it will be added to the default reminders list.
- Parameter completion: optional completion call back
*/
public func delete(reminder: EKReminder, completion: (@escaping (Error?) -> Void)? = nil) {
var deleted: Bool = false
var error: Error?
do {
try eventStore.remove(reminder, commit: true)
deleted = true
} catch let e {
error = e
}
DispatchQueue.main.async { [weak self] in
guard let s = self else {
return
}
s.delegate?.reminders?(reminders: s, deleted: deleted)
if let c = completion {
c(error)
}
}
}
}
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of CosmicMind nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
open class RemindersController: UIViewController {
/// A reference to a Reminder.
public private(set) lazy var reminders: Reminders = Reminders()
open override func viewDidLoad() {
super.viewDidLoad()
prepareView()
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
open func prepareView() {
view.clipsToBounds = true
view.backgroundColor = Color.white
view.contentScaleFactor = Device.scale
prepareReminder()
}
/// Prepares the reminder.
private func prepareReminder() {
reminders.delegate = self
}
}
extension RemindersController: RemindersDelegate {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment