Skip to content

Instantly share code, notes, and snippets.

@zackshapiro
Created March 16, 2015 22:30
Show Gist options
  • Save zackshapiro/e708f9a62d63edbf65a2 to your computer and use it in GitHub Desktop.
Save zackshapiro/e708f9a62d63edbf65a2 to your computer and use it in GitHub Desktop.
func showStreakIndicator(#today: NSDate, yesterday: NSDate?, tomorrow: NSDate?) -> (top: Bool, bottom: Bool) {
var tuple = (top: false, bottom: false)
let calendar = NSCalendar.currentCalendar()
if yesterday == nil && tomorrow == nil {
tuple.top = true
return tuple
}
if let yest = yesterday {
let components = calendar.components(.DayCalendarUnit, fromDate: yest, toDate: today, options: nil)
if components.day == 0 || components.day == 1 {
tuple.bottom = true
}
} else {
// this is explicit and I can remove it later
tuple.bottom = false
}
if let tom = tomorrow {
let components = calendar.components(.DayCalendarUnit, fromDate: today, toDate: tom, options: nil)
if components.day == 0 || components.day == 1 {
tuple.top = true
}
} else {
// today here is always the most recent meditation session
// use NSDate() for today's date to compare last session today's date
let components = calendar.components(.DayCalendarUnit, fromDate: today, toDate: NSDate(), options: nil)
if components.day > 1 {
// if your last day was more than 1 day ago, don't show the top streak indicator
tuple.top = false
} else {
// so the top cell always has an encouraging future streak if you sat < 1 days ago
tuple.top = true
}
}
return tuple
}
func prepareEntries() {
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "Meditation")
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
request.sortDescriptors = [sortDescriptor]
var error: NSError?
var showStreak = (top: false, bottom: false)
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Meditation] {
for (index, object) in enumerate(objects) {
// note: item 0 is most recent, item 30 is a month away
if objects.count == 1 {
showStreak = showStreakIndicator(today: object.date, yesterday: nil, tomorrow: nil)
} else {
// handles the first element of the array, tomorrow is nil
if ((index - 1) == -1 ) {
showStreak = showStreakIndicator(today: object.date, yesterday: objects[index + 1].date, tomorrow: nil)
} else {
if (index != objects.count - 1) {
showStreak = showStreakIndicator(today: object.date, yesterday: objects[index + 1].date, tomorrow: objects[index - 1].date)
} else {
// handles the last element of the array, yesterday is nil
showStreak = showStreakIndicator(today: object.date, yesterday: nil, tomorrow: objects[index - 1].date)
}
}
}
var timeLabel = lengthMinutesLabel(minutesString: lengthMinutes(object.length))
var entry = Entry(sessionLength: lengthMinutes(object.length), sessionTimeUnit: timeLabel, sessionStartTime: "\(object.date.format())", sessionJournalEntry: object.journalEntry, sessionStreakTop: showStreak.top, sessionStreakBottom: showStreak.bottom)
arrayOfEntriesNew.append(entry)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment