From 532dd90dea09f6f45e352f4a8bd09fa668d88a55 Mon Sep 17 00:00:00 2001 From: Andrea Mazzini Date: Wed, 27 May 2015 11:36:33 +0200 Subject: [PATCH] Improve code quality --- Gulps/AppDelegate.swift | 13 ++++++----- Gulps/Model/Entry.swift | 23 ++++++++++--------- Gulps/Support/AnimatedShareButton.swift | 15 ++++++------ Gulps/Support/Constants.swift | 2 +- Gulps/Support/Globals.swift | 8 +++---- Gulps/Support/Settings.swift | 4 ++-- .../CalendarViewController.swift | 10 +++++--- .../Onboarding/GoalViewController.swift | 2 +- .../Onboarding/GulpsViewController.swift | 2 +- .../NotificationViewController.swift | 5 ++-- .../Onboarding/OnboardingViewController.swift | 2 +- .../SettingsViewController.swift | 20 ++++++++-------- 12 files changed, 57 insertions(+), 49 deletions(-) diff --git a/Gulps/AppDelegate.swift b/Gulps/AppDelegate.swift index cdbb3ea..3004fcd 100644 --- a/Gulps/AppDelegate.swift +++ b/Gulps/AppDelegate.swift @@ -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) @@ -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) { @@ -64,4 +66,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } } - diff --git a/Gulps/Model/Entry.swift b/Gulps/Model/Entry.swift index fe41e14..911edf7 100644 --- a/Gulps/Model/Entry.swift +++ b/Gulps/Model/Entry.swift @@ -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 } @@ -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 diff --git a/Gulps/Support/AnimatedShareButton.swift b/Gulps/Support/AnimatedShareButton.swift index 22bd6d9..2a52ba9 100644 --- a/Gulps/Support/AnimatedShareButton.swift +++ b/Gulps/Support/AnimatedShareButton.swift @@ -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) } } diff --git a/Gulps/Support/Constants.swift b/Gulps/Support/Constants.swift index a28904d..daabdf5 100644 --- a/Gulps/Support/Constants.swift +++ b/Gulps/Support/Constants.swift @@ -4,4 +4,4 @@ class Constants { class func bundle() -> String { return "it.fancypixel.BigGulp" } -} \ No newline at end of file +} diff --git a/Gulps/Support/Globals.swift b/Gulps/Support/Globals.swift index 236bbaf..6d2fab1 100644 --- a/Gulps/Support/Globals.swift +++ b/Gulps/Support/Globals.swift @@ -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) @@ -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() @@ -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) { diff --git a/Gulps/Support/Settings.swift b/Gulps/Support/Settings.swift index d26774a..85a633a 100644 --- a/Gulps/Support/Settings.swift +++ b/Gulps/Support/Settings.swift @@ -83,7 +83,7 @@ public class Settings { } userDefaults.synchronize() } - + public class func registerDefaultsForLiter() { let userDefaults = NSUserDefaults.groupUserDefaults() userDefaults.setDouble(0.5, forKey: Gulp.Big.key()) @@ -91,7 +91,7 @@ public class Settings { userDefaults.setDouble(2, forKey: Gulp.Goal.key()) userDefaults.synchronize() } - + public class func registerDefaultsForOunces() { let userDefaults = NSUserDefaults.groupUserDefaults() userDefaults.setDouble(16, forKey: Gulp.Big.key()) diff --git a/Gulps/ViewControllers/CalendarViewController.swift b/Gulps/ViewControllers/CalendarViewController.swift index 445376f..0a9fb68 100644 --- a/Gulps/ViewControllers/CalendarViewController.swift +++ b/Gulps/ViewControllers/CalendarViewController.swift @@ -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) @@ -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) } @@ -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" } diff --git a/Gulps/ViewControllers/Onboarding/GoalViewController.swift b/Gulps/ViewControllers/Onboarding/GoalViewController.swift index f99b59b..611ec21 100644 --- a/Gulps/ViewControllers/Onboarding/GoalViewController.swift +++ b/Gulps/ViewControllers/Onboarding/GoalViewController.swift @@ -18,7 +18,7 @@ class GoalViewController: OnboardingViewController, UITextFieldDelegate { func dismissAndSave() { let numberFormatter = NSNumberFormatter() numberFormatter.numberStyle = .DecimalStyle - + self.goalTextField.resignFirstResponder() var goal = 0.0 diff --git a/Gulps/ViewControllers/Onboarding/GulpsViewController.swift b/Gulps/ViewControllers/Onboarding/GulpsViewController.swift index deba914..d56e0d2 100644 --- a/Gulps/ViewControllers/Onboarding/GulpsViewController.swift +++ b/Gulps/ViewControllers/Onboarding/GulpsViewController.swift @@ -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() { diff --git a/Gulps/ViewControllers/Onboarding/NotificationViewController.swift b/Gulps/ViewControllers/Onboarding/NotificationViewController.swift index 159673e..a10ab08 100644 --- a/Gulps/ViewControllers/Onboarding/NotificationViewController.swift +++ b/Gulps/ViewControllers/Onboarding/NotificationViewController.swift @@ -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() + } } } diff --git a/Gulps/ViewControllers/Onboarding/OnboardingViewController.swift b/Gulps/ViewControllers/Onboarding/OnboardingViewController.swift index fe10f1d..816b860 100644 --- a/Gulps/ViewControllers/Onboarding/OnboardingViewController.swift +++ b/Gulps/ViewControllers/Onboarding/OnboardingViewController.swift @@ -10,7 +10,7 @@ class OnboardingViewController: AMWaveViewController { self.navigationController?.view.backgroundColor = UIColor.mainColor() self.view.backgroundColor = UIColor.clearColor() - + updateUI() } diff --git a/Gulps/ViewControllers/SettingsViewController.swift b/Gulps/ViewControllers/SettingsViewController.swift index 3d37e18..0f40857 100644 --- a/Gulps/ViewControllers/SettingsViewController.swift +++ b/Gulps/ViewControllers/SettingsViewController.swift @@ -19,7 +19,7 @@ class SettingsViewController: UITableViewController, UIAlertViewDelegate, UIText let formatter = NSNumberFormatter() formatter.numberStyle = .DecimalStyle return formatter - }() + }() override func viewDidLoad() { super.viewDidLoad() @@ -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 @@ -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() }