Created
May 7, 2016 19:42
-
-
Save jeffreybergier/9efd991a8a15dfafcf76b0ff17bc5710 to your computer and use it in GitHub Desktop.
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
// | |
// Remindable.swift | |
// WaterMe | |
// | |
// Created by Jeffrey Bergier on 5/7/16. | |
// | |
// Copyright (c) 2015 Jeffrey Bergier | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF COwNTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
// THE SOFTWARE. | |
// | |
import Foundation | |
public protocol Remindable { | |
var creationDate: NSDate { get set } | |
var reminderDelayDays: Int { get set } | |
var reminderDate: NSDate { get } | |
} | |
public extension Remindable { | |
var reminderDate: NSDate { | |
return self.creationDate.dateByAddingTimeInterval((24 * 60 * 60) * Double(self.reminderDelayDays)) | |
} | |
} | |
public extension Remindable { | |
static func fireDateDictionaryOfGroupedRemindableItems<T: protocol<Remindable, Hashable>>(inputItems: [T], withFireHour fireHour: Int) -> [NSDate : [T]] { | |
// grab the system calendar | |
let calendar = NSCalendar.currentCalendar() | |
// get the date components that can be adjusted as we iterate through plants | |
let comparisonDateComponents = calendar.components([.Calendar, .TimeZone, .Year, .Month, .Day, .Hour], fromDate: NSDate(timeIntervalSinceNow: 0)) | |
comparisonDateComponents.hour = fireHour | |
comparisonDateComponents.day += 0 // can be incremented to start searching tomorrow | |
// convert to sets because they can subtract | |
let inputSet = Set(inputItems) | |
// plants get removed from here as we iterate over them | |
var remainingItemsSet = inputSet | |
// store the fireDate as the key and the array of plants for each notification | |
var outputDictionary = [NSDate : [T]]() | |
while remainingItemsSet.count > 0 { | |
// get a date we can compare against from the adjusted date components | |
let comparisonDate = comparisonDateComponents.date! | |
// filter plants so only plants with a water date BEFORE the comparison date get kept | |
let filteredItemsArray = inputItems.filter() { item -> Bool in | |
let timeInterval = item.reminderDate.timeIntervalSinceDate(comparisonDate) | |
let isInToday = calendar.isDate(comparisonDate, inSameDayAsDate: item.reminderDate) | |
if isInToday == true || timeInterval <= 0 { return true } else { return false } | |
} | |
// now we have the matching plants, add them to the dictionary | |
if filteredItemsArray.isEmpty == false { | |
outputDictionary[comparisonDate] = filteredItemsArray | |
} | |
// subtract them from the original set so the while loop can end | |
remainingItemsSet.subtractInPlace(filteredItemsArray) | |
// increment the day in the date components, so the next iteration checks the next day | |
comparisonDateComponents.day += 1 | |
} | |
// return the dictionary | |
return outputDictionary | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have a reminders app (or in my case, a plant watering app) and you want to send a daily digest notification that tells them how many reminders they have that day, this generic type and algorithm can accomplish that.
The way it works is if you have 2 reminders on day 1 and 2 reminders on day 2, then on day 1 you'll get a notification for 2 items and on day 2 you'll get a notification for 4 items.
So this way you can use local notifications (as opposed to push notifications) and then you can just reset and recalculate all notifications on next launch. So if the user opens the app and marks 1 of the 2 reminds for day 1 done, then the next day they will get a notification for 3 reminders.
I used the following simple for loop to create the notifications for my app: