void UpdateBiologicalSex()
 {
     //https://www.npmjs.com/package/cordova-plugin-health
     //-- Helpful mapping of HealthKit attributes to Google Fit constants.
     // Retrieval of gender is not supported, but can be added as a custom attribute for just this app.
     HealthStateMutator.MutateBiologicalSex(
         StateDispatcher <HealthState> .State, () => "Unknown");
 }
        private void RefreshCharacteristicValue(NSString characteristicTypeKey, HKCharacteristicType characteristicType)
        {
            if (characteristicTypeKey == HKCharacteristicTypeIdentifierKey.BiologicalSex)
            {
                NSError error         = null;
                var     biologicalSex = HealthStore.GetBiologicalSex(out error);
                if (error == null)
                {
                    DispatchQueue.MainQueue.DispatchAsync(() => {
                        var dataStore = StateDispatcher <HealthState> .State;

                        HealthStateMutator.MutateBiologicalSex(
                            dataStore, () => GetDisplayableBiologicalSex(biologicalSex.BiologicalSex));

                        StateDispatcher <HealthState> .Refresh();
                    });
                }
            }
        }
        async Task UpdateHeight()
        {
            //http://stackoverflow.com/questions/28482176/read-the-height-in-googlefit-in-android

            DateTime endTime = DateTime.Now;

            //TimeSpan has to be huge, otherwise we may never get the user's height.
            //May also have to query back for YEARS or multiple query in loop until you get an answer.
            //Don't like the Google Fit API...

            DateTime startTime        = endTime.Subtract(TimeSpan.FromDays(1024));
            long     endTimeElapsed   = GetMsSinceEpochAsLong(endTime);
            long     startTimeElapsed = GetMsSinceEpochAsLong(startTime);


            var readRequest = new DataReadRequest.Builder()
                              .Read(Android.Gms.Fitness.Data.DataType.TypeHeight)
                              .SetTimeRange(startTimeElapsed, endTimeElapsed, TimeUnit.Milliseconds)
                              .SetLimit(1)
                              .Build();



            var readResult = await FitnessClass.HistoryApi.ReadDataAsync(mClient, readRequest);

            if (!readResult.Status.IsSuccess)
            {
                if (readResult.Status.HasResolution)
                {
                    readResult.Status.StartResolutionForResult(_activity, REQUEST_GET_REQUEST_PERMISSION);
                    return;
                }
            }

            var value = GetLastValueInDataSet(readResult.DataSets.LastOrDefault());

            if (value != null)
            {
                HealthStateMutator.MutateHeight(
                    StateDispatcher <HealthState> .State, () => value.AsFloat());
            }
        }
        void RefreshQuantityValue(NSString quantityTypeKey, HKQuantityType quantityType)
        {
            NSSortDescriptor timeSortDescriptor = new NSSortDescriptor(HKSample.SortIdentifierEndDate, false);

            // Since we are interested in retrieving the user's latest sample, we sort the samples in descending order, and set the limit to 1. We are not filtering the data, and so the predicate is set to nil.
            HKSampleQuery query = new HKSampleQuery(quantityType, null, 100, new NSSortDescriptor[] { timeSortDescriptor },
                                                    new HKSampleQueryResultsHandler(new Action <HKSampleQuery, HKSample[], NSError>((query2, results, error) =>
            {
                if (results != null && results.Length > 0)
                {
                    if (quantityTypeKey == HKQuantityTypeIdentifierKey.Height)
                    {
                        //We have height, process the last entry into inches.
                        var quantitySample = results.LastOrDefault() as HKQuantitySample;

                        var quantity = quantitySample.Quantity;

                        var heightUnit = HKUnit.Inch;

                        DispatchQueue.MainQueue.DispatchAsync(() => {
                            var dataStore = StateDispatcher <HealthState> .State;

                            HealthStateMutator.MutateHeight(dataStore,
                                                            () => quantity.GetDoubleValue(heightUnit));

                            StateDispatcher <HealthState> .Refresh();
                        });
                    }
                    else if (quantityTypeKey == HKQuantityTypeIdentifierKey.StepCount)
                    {
                        DispatchQueue.MainQueue.DispatchAsync(() => {
                            //Now we need to deliver all the blood glucose entries in a list to any listeners.
                            var entries = new List <StepCountEntry>();

                            //Now also deliver all blood glucose readings up to the UI via the 'diff engine' for easy UITableViewController based updating.
                            foreach (var entry in results)
                            {
                                var sample = entry as HKQuantitySample;
                                if (sample != null)
                                {
                                    entries.Add(new HealthKitStepCountEntry(sample));
                                }
                            }

                            HealthStateDispatchers.StepCountListStateDispatcher.Refresh(
                                entries.Cast <StepCountEntry>().ToList());
                        });
                    }
                    else if (quantityTypeKey == HKQuantityTypeIdentifierKey.BloodGlucose)
                    {
                        DispatchQueue.MainQueue.DispatchAsync(() => {
                            //Refresh the views with the last known blood glucose quantity via HealthState and BloodGlucoseRecommendationState.
                            var lastBloodGlucoseQuantity = (results.LastOrDefault() as HKQuantitySample).Quantity;

                            var healthState = StateDispatcher <HealthState> .State;

                            var mgPerDL = HKUnit.FromString("mg/dL");
                            HealthStateMutator.MutateBloodGlucose(healthState,
                                                                  () => lastBloodGlucoseQuantity.GetDoubleValue(mgPerDL));


                            //At this point all UI subscribers to the HealthState object will update.
                            StateDispatcher <HealthState> .Refresh();



                            var recommendationStore = StateDispatcher <BloodGlucoseRecommendationState> .State;

                            BloodGlucoseRecommendationMutator.MutateBloodGlucose(
                                recommendationStore, () => healthState.BloodGlucose);

                            //At this point all UI subscribers to the BloodGlucoseRecommendationState will update.
                            StateDispatcher <BloodGlucoseRecommendationState> .Refresh();



                            //Now we need to deliver all the blood glucose entries in a list to any listeners.
                            var newBloodGlucoseEntries = new List <HealthKitBloodGlucoseEntry>();

                            //Now also deliver all blood glucose readings up to the UI via the 'diff engine' for easy UITableViewController based updating.
                            foreach (var bloodGlucoseEntry in results)
                            {
                                var bloodGlucoseSample = bloodGlucoseEntry as HKQuantitySample;
                                if (bloodGlucoseSample != null)
                                {
                                    newBloodGlucoseEntries.Add(new HealthKitBloodGlucoseEntry(bloodGlucoseSample));
                                }
                            }

                            HealthStateDispatchers.BloodGlucoseListStateDispatcher.Refresh(
                                newBloodGlucoseEntries.Cast <BloodGlucoseEntry>().ToList());
                        });
                    }
                }
            })));

            HealthStore.ExecuteQuery(query);
        }