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

Commit

Permalink
Update pods. Embed pods in the repo. Update Swift syntax to 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Aug 15, 2016
1 parent 979be54 commit c84546f
Show file tree
Hide file tree
Showing 637 changed files with 129,103 additions and 1,609 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ xcuserdata
DerivedData
*.xcuserstate

### Cocoapods ###
Pods
Podfile.lock
*.xcworkspace
# OS X Finder
.DS_Store
# Xcode per-user config
Expand All @@ -33,7 +29,6 @@ Podfile.lock
*~
*.dat
*.dep
# Cocoapods
# AppCode specific files
.idea/
*.iml
Expand Down
4 changes: 2 additions & 2 deletions Gulps WatchKit Extension/ComplicationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class ComplicationController: NSObject, CLKComplicationDataSource {

func requestedUpdateDidBegin() {
let server = CLKComplicationServer.sharedInstance()

server.activeComplications.forEach { server.reloadTimelineForComplication($0) }
guard let activeComplications = server.activeComplications else { return }
activeComplications.forEach { server.reloadTimelineForComplication($0) }
}

func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
Expand Down
152 changes: 76 additions & 76 deletions Gulps.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Gulps.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 57 additions & 57 deletions Gulps/Model/Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@ import Foundation
import RealmSwift

/**
An Entry represents an entire day worth of input
*/
An Entry represents an entire day worth of input
*/
public class Entry: Object {
dynamic public var date = Entry.defaultDate()
dynamic public var quantity = 0.0
dynamic public var percentage = 0.0
dynamic public var goal = 0.0
public let gulps = List<Gulp>()
dynamic public var date = Entry.defaultDate()
dynamic public var quantity = 0.0
dynamic public var percentage = 0.0
dynamic public var goal = 0.0
public let gulps = List<Gulp>()

/**
The date is the primary key. `defaultDate` provides the current day in string format.
The string format is required by Realm for primary keys
*/
class func defaultDate() -> String {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
return dateFormat.stringFromDate(NSDate())
}
/**
The date is the primary key. `defaultDate` provides the current day in string format.
The string format is required by Realm for primary keys
*/
class func defaultDate() -> String {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
return dateFormat.stringFromDate(NSDate())
}

override public class func primaryKey() -> String {
return "date"
}
override public class func primaryKey() -> String {
return "date"
}

/**
Adds a portion of water to the current day
- parameter quantity: The portion size
- parameter goal: The daily goal
- parameter date: The date of the portion
*/
func addGulp(quantity: Double, goal: Double, date: NSDate?) {
let gulp = Gulp(quantity: quantity)
self.gulps.append(gulp)
self.quantity += quantity
self.goal = goal
if let date = date {
gulp.date = date
}
if goal > 0 {
self.percentage = (self.quantity / self.goal) * 100.0
}
/**
Adds a portion of water to the current day
- parameter quantity: The portion size
- parameter goal: The daily goal
- parameter date: The date of the portion
*/
func addGulp(quantity: Double, goal: Double, date: NSDate?) {
let gulp = Gulp(quantity: quantity)
self.gulps.append(gulp)
self.quantity += quantity
self.goal = goal
if let date = date {
gulp.date = date
}

/**
Removes the last portion
*/
func removeLastGulp() {
if let gulp = self.gulps.last {
self.quantity -= gulp.quantity
if goal > 0 {
self.percentage = (self.quantity / self.goal) * 100.0
}
if (self.percentage < 0) {
self.percentage = 0
}
self.gulps.removeLast()
}
if goal > 0 {
self.percentage = (self.quantity / self.goal) * 100.0
}
}

/**
Returns the formatted percentage value
- returns: String
*/
func formattedPercentage() -> String {
return percentage.formattedPercentage()
/**
Removes the last portion
*/
func removeLastGulp() {
if let gulp = self.gulps.last {
self.quantity -= gulp.quantity
if goal > 0 {
self.percentage = (self.quantity / self.goal) * 100.0
}
if (self.percentage < 0) {
self.percentage = 0
}
self.gulps.removeLast()
}
}

/**
Returns the formatted percentage value
- returns: String
*/
func formattedPercentage() -> String {
return percentage.formattedPercentage()
}
}
202 changes: 101 additions & 101 deletions Gulps/Model/EntryHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,122 @@ import Foundation
import RealmSwift

/**
Helper singleton to perform operations on the Realm database
*/
Helper singleton to perform operations on the Realm database
*/
public class EntryHandler: NSObject {

public static let sharedHandler = EntryHandler()
public lazy var userDefaults = NSUserDefaults.groupUserDefaults()
public static let sharedHandler = EntryHandler()
public lazy var userDefaults = NSUserDefaults.groupUserDefaults()

/**
Realm is initialized lazily, using the group bundle identifier.
*/
public lazy var realm: Realm = {
guard let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.\(Constants.bundle())") else {
fatalError("Unable to setup Realm. Make sure to setup your app group in the developer portal")
}
/**
Realm is initialized lazily, using the group bundle identifier.
*/
public lazy var realm: Realm = {
guard let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.\(Constants.bundle())") else {
fatalError("Unable to setup Realm. Make sure to setup your app group in the developer portal")
}

let realmPath = directory.URLByAppendingPathComponent("db.realm")
var config = Realm.Configuration()
config.path = realmPath.path
Realm.Configuration.defaultConfiguration = config
let realmPath = directory.URLByAppendingPathComponent("db.realm")
var config = Realm.Configuration()
config.fileURL = realmPath
Realm.Configuration.defaultConfiguration = config

return try! Realm()
}()
return try! Realm()
}()

/**
Returns the current entry
- returns: Entry?
*/
public func entryForToday() -> Entry? {
return entryForDate(NSDate())
}
/**
Returns the current entry
- returns: Entry?
*/
public func entryForToday() -> Entry? {
return entryForDate(NSDate())
}

/**
Returns an entry for the given date
- parameter date: The desired date
- returns: Entry?
*/
public func entryForDate(date: NSDate) -> Entry? {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
let p: NSPredicate = NSPredicate(format: "date = %@", argumentArray: [ dateFormat.stringFromDate(date) ])
let objects = realm.objects(Entry).filter(p)
return objects.first
}
/**
Returns an entry for the given date
- parameter date: The desired date
- returns: Entry?
*/
public func entryForDate(date: NSDate) -> Entry? {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
let p: NSPredicate = NSPredicate(format: "date = %@", argumentArray: [ dateFormat.stringFromDate(date) ])
let objects = realm.objects(Entry).filter(p)
return objects.first
}

/**
Returns the current entry if available, or creates a new one instead
- returns: Entry
*/
public func currentEntry() -> Entry {
if let entry = entryForToday() {
return entry
} else {
let newEntry = Entry()
try! realm.write {
self.realm.add(newEntry, update: true)
}
return newEntry
}
/**
Returns the current entry if available, or creates a new one instead
- returns: Entry
*/
public func currentEntry() -> Entry {
if let entry = entryForToday() {
return entry
} else {
let newEntry = Entry()
try! realm.write {
self.realm.add(newEntry, update: true)
}
return newEntry
}
}

/**
Gets the current percentage
- returns: Double
*/
public func currentPercentage() -> Double {
return currentEntry().percentage
}
/**
Gets the current percentage
- returns: Double
*/
public func currentPercentage() -> Double {
return currentEntry().percentage
}

/**
Adds a portion to the current entry. If available, the sample is saved in HealthKit as well
- parameter quantity: The sample value
*/
public func addGulp(quantity: Double) {
addGulp(quantity, date: nil)
}
/**
Adds a portion to the current entry. If available, the sample is saved in HealthKit as well
- parameter quantity: The sample value
*/
public func addGulp(quantity: Double) {
addGulp(quantity, date: nil)
}

/**
Adds a portion to the current entry for a given date. If available, the sample is saved in HealthKit as well
- parameter quantity: The sample value
- parameter date: The sample date
*/
public func addGulp(quantity: Double, date: NSDate?) {
HealthKitHelper.sharedHelper.saveSample(quantity)
let entry = currentEntry()
try! realm.write {
entry.addGulp(quantity, goal: self.userDefaults.doubleForKey(Constants.Gulp.Goal.key()), date: date)
}
/**
Adds a portion to the current entry for a given date. If available, the sample is saved in HealthKit as well
- parameter quantity: The sample value
- parameter date: The sample date
*/
public func addGulp(quantity: Double, date: NSDate?) {
HealthKitHelper.sharedHelper.saveSample(quantity)
let entry = currentEntry()
try! realm.write {
entry.addGulp(quantity, goal: self.userDefaults.doubleForKey(Constants.Gulp.Goal.key()), date: date)
}
}

/**
Removes the last portion to the current entry. If available, the sample is removed in HealthKit as well
*/
public func removeLastGulp() {
HealthKitHelper.sharedHelper.removeLastSample()
let entry = currentEntry()
if let gulp = entry.gulps.last {
try! realm.write {
entry.removeLastGulp()
self.realm.delete(gulp)
}
}
/**
Removes the last portion to the current entry. If available, the sample is removed in HealthKit as well
*/
public func removeLastGulp() {
HealthKitHelper.sharedHelper.removeLastSample()
let entry = currentEntry()
if let gulp = entry.gulps.last {
try! realm.write {
entry.removeLastGulp()
self.realm.delete(gulp)
}
}
}

/**
Returns the value of all the portions recorded
- returns: Double
*/
public func overallQuantity() -> Double {
return realm.objects(Entry).sum("quantity") as Double
}
/**
Returns the value of all the portions recorded
- returns: Double
*/
public func overallQuantity() -> Double {
return realm.objects(Entry).sum("quantity") as Double
}

/**
Returns the value number of days tracked
- returns: Int
*/
public func daysTracked() -> Int {
return realm.objects(Entry).count
}
/**
Returns the value number of days tracked
- returns: Int
*/
public func daysTracked() -> Int {
return realm.objects(Entry).count
}
}
Loading

0 comments on commit c84546f

Please sign in to comment.