Skip to content

Instantly share code, notes, and snippets.

@rzaitov
Last active August 29, 2015 14:03
Show Gist options
  • Save rzaitov/cd3f1e733ec5bba32558 to your computer and use it in GitHub Desktop.
Save rzaitov/cd3f1e733ec5bba32558 to your computer and use it in GitHub Desktop.
1. [JournalViewController.cs]
NSNotificationCenter.DefaultCenter.AddObserver (this, new Selector ("updateJournal"), UIApplication.DidBecomeActiveNotification, null);
//Should replace with:
UIApplication.Notifications.ObserveDidBecomeActive (handler);
2. [JournalViewController.cs]
var components = new NSDateComponents ();
components.Day = calendar.GetComponentFromDate (NSCalendarUnit.Day, now);
components.Month = calendar.GetComponentFromDate (NSCalendarUnit.Month, now);
components.Year = calendar.GetComponentFromDate (NSCalendarUnit.Year, now);
var startDate = calendar.DateFromComponents (components);
var endDate = calendar.DateByAddingUnit (NSCalendarUnit.Day, 1, startDate, NSCalendarOptions.None);
var predicate = HKQuery.GetPredicateForSamples (startDate, endDate, HKQueryOptions.None);
// This arithmetic should be replace with .Net equivalent:
var startDate = DateTime.Now.Date;
var endDate = startDate.AddDays(1);
var predicate = HKQuery.GetPredicateForSamples ((NSDate)startDate, (NSDate)endDate, HKQueryOptions.None);
3. [EnergyViewController.cs]
RefreshControl.AddTarget (this, new Selector ("refreshStatistics"), UIControlEvent.ValueChanged);
// Should be replaced with event
RefreshControl.ValueChanged += RefreshStatistics;
4. [EnergyViewController.cs]
NSNotificationCenter.DefaultCenter.AddObserver (this, new Selector ("refreshStatistics"), UIApplication.DidBecomeActiveNotification, null);
// Should be replaced with Strongly type equivalent:
UIApplication.Notifications.ObserveDidBecomeActive (handler);
5. [EnergyViewController.cs]
// Calendar arithmetic should be replace with .Net equivalent:
6. [AppDelegate.cs]
// Why it is public? As I can see it used only inside AppDelegate.cs and then injects to other controllers
// maybe it should be replaced with private field?
public static HKHealthStore HealthStore { get; private set; }
7. [AppDelegate.cs]
// error message is broken – %@ must be replaced with {0} and error must be provided as second argument
Console.WriteLine ("You didn't allow HealthKit to access these read/write data types. " +
"In your app, try to handle this error gracefully when a user decides not to provide access. " +
"The error was: %@. If you're using a simulator, try it on a device.");
8. [AppDelegate.cs]
foreach (UINavigationController navigationController in tabBarController.ViewControllers) {
var viewController = navigationController.TopViewController;
if (viewController is ProfileViewController)
(viewController as ProfileViewController).HealthStore = HealthStore;
else if (viewController is JournalViewController)
(viewController as JournalViewController).HealthStore = HealthStore;
else if (viewController is EnergyViewController)
(viewController as EnergyViewController).HealthStore = HealthStore;
}
// Declare IHealthStore interface which contains just one property HealthStore.
// ProfileViewController must implement this interface. After that replace code above with:
foreach (UINavigationController navigationController in tabBarController.ViewControllers) {
IHealthStore controller = navigationController.TopViewController as IHealthStore;
if(controller != null)
controller.HealthStore = HealthStore;
}
9. [AppDelegate.cs] // this topic just my pref
HealthStore.RequestAuthorizationToShare (writeDataTypes, readDataTypes, (success, error) => {
if (!success) {
Console.WriteLine ("super important message");
} else {
SetupHealthStoreForTabBarControllers ();
}
}
// I think it more beautifully because it is branchless (easily to understand)
HealthStore.RequestAuthorizationToShare (writeDataTypes, readDataTypes, (success, error) => {
if (!success) {
Console.WriteLine ("super important message");
return;
}
SetupHealthStoreForTabBarControllers ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment