Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
Fix to avoid staleness in the watchkit app
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Nov 17, 2015
1 parent ef10790 commit 0f3c614
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions Gulps WatchKit Extension/WatchEntryHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class WatchEntryHelper {
- parameter portion: The portion key
*/
func addGulp(portion: String) {
let quantity = userDefaults.doubleForKey(Constants.WatchContext.Current.key())
let portion = userDefaults.doubleForKey(portion)
userDefaults.setDouble(quantity() + portion, forKey: Constants.WatchContext.Current.key())
userDefaults.setObject(NSDate(), forKey: Constants.WatchContext.Date.key())
userDefaults.setDouble(quantity + portion, forKey: Constants.WatchContext.Current.key())
userDefaults.synchronize()
}

Expand All @@ -42,25 +41,30 @@ class WatchEntryHelper {
}

/**
Returns the current percentage, if available
The data might not be there yet (app just installed)
Returns the current quantity
It also checks if the data is stale, resetting the quantity if needed
- Returns: Int? the current percentage
*/
func percentage() -> Int? {
func quantity() -> Double {
let quantity = userDefaults.doubleForKey(Constants.WatchContext.Current.key())

guard quantity != 0 else {
return nil
}

if let date = userDefaults.objectForKey(Constants.WatchContext.Date.key()) as? NSDate {
if let tomorrow = date.startOfTomorrow where NSDate().compare(tomorrow) != NSComparisonResult.OrderedAscending {
// Data is stale, reset the counter
return 0
}
}

return quantity
}

/**
Returns the current percentage, if available
The data might not be there yet (app just installed)
- Returns: Int? the current percentage
*/
func percentage() -> Int? {
let goal = userDefaults.doubleForKey(Constants.Gulp.Goal.key())
return Int(round(quantity / goal * 100.0))
return Int(round(quantity() / goal * 100.0))
}
}

0 comments on commit 0f3c614

Please sign in to comment.