private void GetDataFromQuery(DateTime start, DateTime end, HKQuantityType quentityType, HKUnit unit, Action <IEnumerable <double> > act) { NSCalendar calendar = NSCalendar.CurrentCalendar; NSDateComponents interval = new NSDateComponents { Day = 1 }; NSDate startDate = start.ToNsDate(); NSDate anchorDate = end.ToNsDate(); HKStatisticsCollectionQuery query = new HKStatisticsCollectionQuery( quentityType, null, HKStatisticsOptions.CumulativeSum, anchorDate, interval ) { InitialResultsHandler = (localQuery, result, error) => { if (error != null) { OnError?.Invoke(error.Description); return; } int daysCount = (end - start).Days + 1; double[] st = new double[daysCount]; result.EnumerateStatistics(startDate, anchorDate, (statistics, stop) => { HKQuantity quantity = statistics?.SumQuantity(); int index = (statistics.StartDate.ToDateTime() - start).Days; if (index < 0 || index > st.Length) { return; } double value = quantity?.GetDoubleValue(unit) ?? 0; st[index] = value; }); act(st.AsEnumerable()); } }; try { _healthStore.ExecuteQuery(query); } catch (Exception e) { OnError?.Invoke(e.Message); } }
public static string Format(HKQuantity totalDistance = null, HKQuantity totalEnergyBurned = null) { var result = string.Empty; if (totalDistance != null) { result = Format(totalDistance: totalDistance.GetDoubleValue(HKUnit.Meter)); } else if (totalEnergyBurned != null) { result = Format(totalEnergyBurned: totalEnergyBurned.GetDoubleValue(HKUnit.Kilocalorie)); } return(result); }
public Task <ExerciseData> AddExercise(ExerciseData data) { var completionSource = new TaskCompletionSource <ExerciseData>(); var metadata = new HKMetadata() { GroupFitness = true, IndoorWorkout = true, CoachedWorkout = true, }; if (data.Time.TotalMinutes == 0) { DateTime start = ((DateTime)data.DateInit).ToLocalTime(); DateTime end = ((DateTime)data.DateEnd).ToLocalTime(); data.Time = end.Subtract(start); } HKQuantity calories = getCaloriesFromData(data); HKWorkout workOut = HKWorkout.Create(HKWorkoutActivityType.TraditionalStrengthTraining, data.DateInit, data.DateEnd, null, calories, null, metadata); HealthStore.SaveObject(workOut, (succes, error) => { if (succes) { data.Kilocalories = calories.GetDoubleValue(HKUnit.Kilocalorie); completionSource.SetResult(data); } else { completionSource.SetResult(null); } }); return(completionSource.Task); }
public async Task <double> QueryTotalHeight() { var heightType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.Height); double usersHeight = 0.0; var timeSortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierEndDate, false); var query = new HKSampleQuery(heightType, new NSPredicate(IntPtr.Zero), 1, new NSSortDescriptor[] { timeSortDescriptor }, (HKSampleQuery resultQuery, HKSample[] results, NSError error) => { HKQuantity quantity = null; if (results.Length != 0) { var quantitySample = (HKQuantitySample)results [results.Length - 1]; quantity = quantitySample.Quantity; usersHeight = quantity.GetDoubleValue(HKUnit.Meter); HealthKitDataContext.ActiveHealthKitData.Height = usersHeight; Console.WriteLine(string.Format("height of Fetched: {0}", usersHeight)); } }); await Task.Factory.StartNew(() => HealthKitStore.ExecuteQuery(query)); return(usersHeight); }
public async Task <int> QueryLastRegistratedSteps() { var stepType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.StepCount); int lastRegistratedSteps = 0; var timeSortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierEndDate, false); var query = new HKSampleQuery(stepType, new NSPredicate(IntPtr.Zero), 1, new NSSortDescriptor[] { timeSortDescriptor }, (HKSampleQuery resultQuery, HKSample[] results, NSError error) => { HKQuantity quantity = null; if (results.Length != 0) { var quantitySample = (HKQuantitySample)results [results.Length - 1]; quantity = quantitySample.Quantity; var source = quantitySample.Source.Name; lastRegistratedSteps = (int)quantity.GetDoubleValue(HKUnit.Count); HealthKitDataContext.ActiveHealthKitData.DistanceReadings.TotalStepsOfLastRecording = lastRegistratedSteps; HealthKitDataContext.ActiveHealthKitData.DistanceReadings.Source = source; } }); m_healthKitStore.ExecuteQuery(query); return(lastRegistratedSteps); }
public async Task <double> QueryLastRegistratedWalkingDistance() { var distanceType = HKQuantityType.GetQuantityType(HKQuantityTypeIdentifierKey.DistanceWalkingRunning); double lastRegistratedWalkingDistance = 0.0; var timeSortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierEndDate, false); var query = new HKSampleQuery(distanceType, new NSPredicate(IntPtr.Zero), 1, new NSSortDescriptor[] { timeSortDescriptor }, (HKSampleQuery resultQuery, HKSample[] results, NSError error) => { HKQuantity quantity = null; string resultString = string.Empty; if (results.Length != 0) { var quantitySample = (HKQuantitySample)results [results.Length - 1]; quantity = quantitySample.Quantity; lastRegistratedWalkingDistance = quantity.GetDoubleValue(HKUnit.Meter); HealthKitDataContext.ActiveHealthKitData.DistanceReadings.TotalDistanceOfLastRecording = lastRegistratedWalkingDistance; Console.WriteLine(string.Format("value of QueryLastRegistratedWalkingDistance: {0}", lastRegistratedWalkingDistance)); } }); m_healthKitStore.ExecuteQuery(query); return(lastRegistratedWalkingDistance); }
//Attempts to store in the Health Kit database a quantity, which must be of a type compatible with beats-per-minute public void StoreHeartRate(HKQuantity quantity) { var bpm = HKUnit.Count.UnitDividedBy(HKUnit.Minute); //Confirm that the value passed in is of a valid type (can be converted to beats-per-minute) if (!quantity.IsCompatible(bpm)) { InvokeOnMainThread(() => ErrorMessageChanged(this, new GenericEventArgs <string> ("Units must be compatible with BPM"))); } var heartRateId = HKQuantityTypeIdentifierKey.HeartRate; var heartRateQuantityType = HKQuantityType.GetQuantityType(heartRateId); var heartRateSample = HKQuantitySample.FromType(heartRateQuantityType, quantity, new NSDate(), new NSDate(), new HKMetadata()); using (var healthKitStore = new HKHealthStore()) { healthKitStore.SaveObject(heartRateSample, (success, error) => { InvokeOnMainThread(() => { if (success) { HeartRateStored(this, new GenericEventArgs <Double> (quantity.GetDoubleValue(bpm))); } else { ErrorMessageChanged(this, new GenericEventArgs <string> ("Save failed")); } if (error != null) { //If there's some kind of error, disable Enabled = false; ErrorMessageChanged(this, new GenericEventArgs <string> (error.ToString())); } }); }); } }