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

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed May 27, 2015
1 parent 17ba5e4 commit 532dd90
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 49 deletions.
13 changes: 7 additions & 6 deletions Gulps/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
DPMeterView.appearance().progressTintColor = .mainColor()

Globals.actionSheetAppearance()

UITabBar.appearance().tintColor = .mainColor()

let font = UIFont(name: "KaushanScript-Regular", size: 22)
Expand All @@ -40,15 +40,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func loadOnboardingInterface() {
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Slide)
let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as! UIViewController
self.window?.rootViewController = controller
if let controller = storyboard.instantiateInitialViewController() as? UIViewController {
self.window?.rootViewController = controller
}
}

func loadMainInterface() {
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: .Slide)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController() as! UIViewController
self.window?.rootViewController = controller
if let controller = storyboard.instantiateInitialViewController() as? UIViewController {
self.window?.rootViewController = controller
}
}

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
Expand All @@ -64,4 +66,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
}
}

23 changes: 12 additions & 11 deletions Gulps/Model/Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Entry: RLMObject {
class func entryForDate(date: NSDate) -> Entry? {
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
let p : NSPredicate = NSPredicate(format: "date = %@", argumentArray: [ dateFormat.stringFromDate(date) ])
let p: NSPredicate = NSPredicate(format: "date = %@", argumentArray: [ dateFormat.stringFromDate(date) ])
let objects = Entry.objectsWithPredicate(p)
return objects.firstObject() as? Entry
}
Expand All @@ -43,18 +43,19 @@ public class Entry: RLMObject {
}

func removeLastGulp() {
let gulp = self.gulps.lastObject() as! Gulp
self.quantity -= gulp.quantity
self.percentage = self.quantity / self.goal * 100.0
if (self.percentage < 0) {
self.percentage = 0
if let gulp = self.gulps.lastObject() as? Gulp {
self.quantity -= gulp.quantity
self.percentage = self.quantity / self.goal * 100.0
if (self.percentage < 0) {
self.percentage = 0
}
if (self.percentage > 100) {
self.percentage = 100
}
self.gulps.removeLastObject()
}
if (self.percentage > 100) {
self.percentage = 100
}
self.gulps.removeLastObject()
}

func formattedPercentage() -> String {
let percentageFormatter = NSNumberFormatter()
percentageFormatter.numberStyle = .PercentStyle
Expand Down
15 changes: 8 additions & 7 deletions Gulps/Support/AnimatedShareButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ class AnimatedShareButton: UIButton {
extension CALayer {
// Thanks to https://github.com/robb/hamburger-button
func ocb_applyAnimation(animation: CABasicAnimation) {
let copy = animation.copy() as! CABasicAnimation

if copy.fromValue == nil {
copy.fromValue = self.presentationLayer().valueForKeyPath(copy.keyPath)
if let copy = animation.copy() as? CABasicAnimation {

if copy.fromValue == nil {
copy.fromValue = self.presentationLayer().valueForKeyPath(copy.keyPath)
}

self.addAnimation(copy, forKey: copy.keyPath)
self.setValue(copy.toValue, forKeyPath:copy.keyPath)
}

self.addAnimation(copy, forKey: copy.keyPath)
self.setValue(copy.toValue, forKeyPath:copy.keyPath)
}
}
2 changes: 1 addition & 1 deletion Gulps/Support/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ class Constants {
class func bundle() -> String {
return "it.fancypixel.BigGulp"
}
}
}
8 changes: 4 additions & 4 deletions Gulps/Support/Globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Globals {
class func numericToolbar(target: AnyObject, selector: Selector, barColor: UIColor = UIColor.whiteColor(), textColor: UIColor = UIColor.mainColor()) -> UIToolbar {
let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 50))
numberToolbar.barStyle = .BlackTranslucent

let button = UIBarButtonItem(title: "Ok", style: .Done, target: target, action: selector)
if let font = UIFont(name: "Avenir-Book", size: 16) {
button.setTitleTextAttributes([NSFontAttributeName: font, NSForegroundColorAttributeName: textColor], forState: UIControlState.Normal)
Expand All @@ -16,10 +16,10 @@ class Globals {
numberToolbar.translucent = true
numberToolbar.tintColor = textColor
numberToolbar.barTintColor = barColor

return numberToolbar
}

class func actionSheetAppearance() {
AHKActionSheet.appearance().separatorColor = UIColor(white: 1, alpha: 0.3)
AHKActionSheet.appearance().blurTintColor = UIColor.mainColor()
Expand All @@ -30,7 +30,7 @@ class Globals {
AHKActionSheet.appearance().cancelButtonTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: font]
}
}

class func numericTextField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newValue = textField.text.stringByReplacingCharactersInRange(range.toRange(textField.text), withString: string)
if (count(newValue) == 0) {
Expand Down
4 changes: 2 additions & 2 deletions Gulps/Support/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public class Settings {
}
userDefaults.synchronize()
}

public class func registerDefaultsForLiter() {
let userDefaults = NSUserDefaults.groupUserDefaults()
userDefaults.setDouble(0.5, forKey: Gulp.Big.key())
userDefaults.setDouble(0.2, forKey: Gulp.Small.key())
userDefaults.setDouble(2, forKey: Gulp.Goal.key())
userDefaults.synchronize()
}

public class func registerDefaultsForOunces() {
let userDefaults = NSUserDefaults.groupUserDefaults()
userDefaults.setDouble(16, forKey: Gulp.Big.key())
Expand Down
10 changes: 7 additions & 3 deletions Gulps/ViewControllers/CalendarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CalendarViewController: UIViewController, JTCalendarDataSource {
[daysCountLabel, quantityLabel].map { $0.format = "%d" }
[quantityLabel, daysLabel, daysCountLabel, measureLabel].map({ $0.textColor = .mainColor() })
shareButton.backgroundColor = .mainColor()

self.navigationItem.rightBarButtonItem = {
let animatedButton = AnimatedShareButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
animatedButton.addTarget(self, action: Selector("presentStats:"), forControlEvents: .TouchUpInside)
Expand Down Expand Up @@ -77,7 +77,11 @@ class CalendarViewController: UIViewController, JTCalendarDataSource {
let text = "Keeping healthy! I drank \(quantitiy) liters of water over \(days) days. https://goo.gl/reTyQU"
let items = [text]
let activityController = UIActivityViewController(activityItems: items, applicationActivities: nil)
activityController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList, UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo]
let exclusion = [
UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList,
UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo
]
activityController.excludedActivityTypes = exclusion
presentViewController(activityController, animated: true, completion: nil)
}

Expand Down Expand Up @@ -116,7 +120,7 @@ private extension CalendarViewController {
func updateStats() {
daysCountLabel.countFromZeroTo(Float(EntryHandler.overallQuantity()))
quantityLabel.countFromZeroTo(Float(EntryHandler.daysTracked()))
if let unit = UnitsOfMeasure(rawValue: NSUserDefaults.groupUserDefaults().integerForKey(Settings.General.UnitOfMeasure.key())) {
if let unit = UnitsOfMeasure(rawValue: userDefaults.integerForKey(Settings.General.UnitOfMeasure.key())) {
let unitName = unit.nameForUnitOfMeasure()
measureLabel.text = "\(unitName) drank over"
}
Expand Down
2 changes: 1 addition & 1 deletion Gulps/ViewControllers/Onboarding/GoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GoalViewController: OnboardingViewController, UITextFieldDelegate {
func dismissAndSave() {
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .DecimalStyle

self.goalTextField.resignFirstResponder()

var goal = 0.0
Expand Down
2 changes: 1 addition & 1 deletion Gulps/ViewControllers/Onboarding/GulpsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GulpsViewController: OnboardingViewController, UITextFieldDelegate {
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var smallBackgroundView: UIView!
@IBOutlet weak var bigBackgroundView: UIView!

let userDefaults = NSUserDefaults.groupUserDefaults()

override func viewDidLoad() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class NotificationViewController: OnboardingViewController, UIActionSheetDelegat
NotificationHelper.unscheduleNotifications()
NotificationHelper.askPermission()

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.loadMainInterface()
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.loadMainInterface()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OnboardingViewController: AMWaveViewController {

self.navigationController?.view.backgroundColor = UIColor.mainColor()
self.view.backgroundColor = UIColor.clearColor()

updateUI()
}

Expand Down
20 changes: 10 additions & 10 deletions Gulps/ViewControllers/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SettingsViewController: UITableViewController, UIAlertViewDelegate, UIText
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
return formatter
}()
}()

override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -67,7 +67,7 @@ class SettingsViewController: UITableViewController, UIAlertViewDelegate, UIText
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

if (indexPath.section == 0 && indexPath.row == 0) {
let toActionSheet = AHKActionSheet(title: "Unit Of Measure:")
toActionSheet!.addButtonWithTitle(UnitsOfMeasure.Liters.nameForUnitOfMeasure(), type: .Default, handler: { (actionSheet) -> Void in
Expand Down Expand Up @@ -131,14 +131,14 @@ class SettingsViewController: UITableViewController, UIAlertViewDelegate, UIText
userDefaults.synchronize()
self.tableView.reloadData()
// This crashes if you touch the switch and drag...
// let indexes = [NSIndexPath(forRow: 1, inSection: 2), NSIndexPath(forRow: 2, inSection: 2), NSIndexPath(forRow: 3, inSection: 2)]
// self.tableView.beginUpdates()
// if (sender.on) {
// self.tableView.insertRowsAtIndexPaths(indexes, withRowAnimation: .Automatic)
// } else {
// self.tableView.deleteRowsAtIndexPaths(indexes, withRowAnimation: .Automatic)
// }
// self.tableView.endUpdates()
// let indexes = [NSIndexPath(forRow: 1, inSection: 2), NSIndexPath(forRow: 2, inSection: 2), NSIndexPath(forRow: 3, inSection: 2)]
// self.tableView.beginUpdates()
// if (sender.on) {
// self.tableView.insertRowsAtIndexPaths(indexes, withRowAnimation: .Automatic)
// } else {
// self.tableView.deleteRowsAtIndexPaths(indexes, withRowAnimation: .Automatic)
// }
// self.tableView.endUpdates()
updateNotificationPreferences()
}

Expand Down

0 comments on commit 532dd90

Please sign in to comment.