public override void RowSelected (UITableView tableView, NSIndexPath indexPath) { NSIndexPath indexPathForSelectedRow = TableView.IndexPathForSelectedRow; SelectedFoodItem = FoodItems.GetItem<FoodItem> (indexPathForSelectedRow.Row); ((JournalViewController)NavigationController.ViewControllers [NavigationController.ViewControllers.Length - 2]). AddFoodItem (SelectedFoodItem); NavigationController.PopViewController (true); }
public void AddFoodItem (FoodItem item) { var quantityType = HKQuantityType.GetQuantityType (HKQuantityTypeIdentifierKey.DietaryEnergyConsumed); var quantity = HKQuantity.FromQuantity (HKUnit.Joule, item.Joules); var now = NSDate.Now; var metadata = NSDictionary.FromObjectAndKey (new NSString (item.Name), HKMetadataKey.FoodType); var caloriesSample = HKQuantitySample.FromType (quantityType, quantity, now, now, metadata); HealthStore.SaveObject (caloriesSample, (success, error) => { if (success) { FoodItems.Insert (item, 0); var indexPathForInsertedFoodItem = NSIndexPath.FromRowSection (0, 0); InvokeOnMainThread (() => { TableView.InsertRows (new NSIndexPath[] { indexPathForInsertedFoodItem }, UITableViewRowAnimation.Automatic); }); } else { Console.WriteLine ("An error occured saving the food {0}. In your app, try to handle this gracefully. " + "The error was: {1}.", item.Name, error); } }); }