private void Bind()
        {
            StateDispatcher <HealthState> .Bind(this);

            StateDispatcher <BloodGlucoseRecommendationState> .Bind(this);

            HealthStateDispatchers.StepCountListStateDispatcher.Bind(this);
        }
Example #2
0
        private async void RefreshHealthStateData()
        {
            await UpdateHeight();
            await UpdateFood();

            UpdateBiologicalSex();


            var stepEntries = await UpdateStepEntries();

            _activity.RunOnUiThread(() => {
                StateDispatcher <HealthState> .Refresh();
                if (stepEntries != null)
                {
                    HealthStateDispatchers.StepCountListStateDispatcher.Refresh(stepEntries);
                }
            });
        }
        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();
                    });
                }
            }
        }
        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);
        }
Example #5
0
        private void Bind()
        {
            StateDispatcher <HealthState> .Bind(this);

            HealthStateDispatchers.StepCountListStateDispatcher.Bind(this);
        }
        private async void RefreshHealthStateData()
        {
            // Setting a start and end date using a range of 1 week before this moment.

            /*Calendar cal = Calendar.getInstance();
             * Date now = new Date();
             * cal.setTime(now);
             * long endTime = cal.getTimeInMillis();
             * cal.add(Calendar.WEEK_OF_YEAR, -1);
             * long startTime = cal.getTimeInMillis();
             *
             * java.text.DateFormat dateFormat = getDateInstance();
             * Log.i(TAG, "Range Start: " + dateFormat.format(startTime));
             * Log.i(TAG, "Range End: " + dateFormat.format(endTime));
             *
             * DataReadRequest readRequest = new DataReadRequest.Builder()
             *      // The data request can specify multiple data types to return, effectively
             *      // combining multiple data queries into one call.
             *      // In this example, it's very unlikely that the request is for several hundred
             *      // datapoints each consisting of a few steps and a timestamp.  The more likely
             *      // scenario is wanting to see how many steps were walked per day, for 7 days.
             *      .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
             *      // Analogous to a "Group By" in SQL, defines how data should be aggregated.
             *      // bucketByTime allows for a time span, whereas bucketBySession would allow
             *      // bucketing by "sessions", which would need to be defined in code.
             *      .bucketByTime(1, TimeUnit.DAYS)
             *      .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
             *      .build();*/

            //https://github.com/xamarin/monodroid-samples/blob/master/google-services/Fitness/BasicHistoryApi/BasicHistoryApi/MainActivity.cs

            /*
             *      // Setting a start and end date using a range of 1 week before this moment.
             *      DateTime endTime = DateTime.Now;
             *      DateTime startTime = endTime.Subtract (TimeSpan.FromDays (7));
             *      long endTimeElapsed = GetMsSinceEpochAsLong (endTime);
             *      long startTimeElapsed = GetMsSinceEpochAsLong (startTime);
             *
             *      Log.Info (TAG, "Range Start: " + startTime.ToString (DATE_FORMAT));
             *      Log.Info (TAG, "Range End: " + endTime.ToString (DATE_FORMAT));
             *
             *      var readRequest = new DataReadRequest.Builder ()
             *              .Aggregate (DataType.TypeStepCountDelta, DataType.AggregateStepCountDelta)
             *              .BucketByTime (1, TimeUnit.Days)
             *              .SetTimeRange (startTimeElapsed, endTimeElapsed, TimeUnit.Milliseconds)
             *              .Build ();
             */


            await UpdateHeight();

            UpdateBiologicalSex();

            /*if (readResult.Buckets.Count > 0) {
             *
             *      Value value = null;
             *      int index = 0;
             *      while (value == null && index < readResult.Buckets.Count) {
             *              value = GetLastValueInBucket (readResult.Buckets [index]);
             *
             *              if (value != null) {
             *                      StateDispatcher<HealthState>.State.Height = value.AsFloat ();
             *                      break;
             *              }
             *              index++;
             *      }
             * } else {
             *
             * }*/

            var stepEntries = await UpdateStepEntries();

            _activity.RunOnUiThread(() => {
                StateDispatcher <HealthState> .Refresh();
                if (stepEntries != null)
                {
                    HealthStateDispatchers.StepCountListStateDispatcher.Refresh(stepEntries);
                }
            });
        }