Beispiel #1
0
        public void NoAnchor()
        {
            TestRuntime.AssertXcodeVersion(6, 0);

            using (var t = HKObjectType.GetCategoryType(HKCategoryTypeIdentifierKey.SleepAnalysis))
                using (var aoq = new HKAnchoredObjectQuery(t, null, HKAnchoredObjectQuery.NoAnchor, 0, delegate(HKAnchoredObjectQuery query, HKSample[] results, nuint newAnchor, NSError error) {
                })) {
                    Assert.That(aoq.Handle, Is.Not.EqualTo(IntPtr.Zero), "handle");
                }
        }
Beispiel #2
0
        public void NoAnchor()
        {
            if (!TestRuntime.CheckSystemAndSDKVersion(8, 0))
            {
                Assert.Inconclusive("Requires iOS8+");
            }

            using (var t = HKObjectType.GetCategoryType(HKCategoryTypeIdentifierKey.SleepAnalysis))
                using (var aoq = new HKAnchoredObjectQuery(t, null, HKAnchoredObjectQuery.NoAnchor, 0, delegate(HKAnchoredObjectQuery query, HKSample[] results, nuint newAnchor, NSError error) {
                })) {
                    Assert.That(aoq.Handle, Is.Not.EqualTo(IntPtr.Zero), "handle");
                }
        }
        private void StartQuery(HKQuantityTypeIdentifier quantityTypeIdentifier, NSDate startDate, HKAnchoredObjectUpdateHandler handler)
        {
            var datePredicate   = HKQuery.GetPredicateForSamples(startDate, null, HKQueryOptions.StrictStartDate);
            var devicePredicate = HKQuery.GetPredicateForObjectsFromDevices(new NSSet <HKDevice>(HKDevice.LocalDevice));
            var queryPredicate  = NSCompoundPredicate.CreateAndPredicate(new NSPredicate[] { datePredicate, devicePredicate });

            var quantityType = HKQuantityType.Create(quantityTypeIdentifier);
            var query        = new HKAnchoredObjectQuery(quantityType, queryPredicate, null, HKSampleQuery.NoLimit, handler);

            query.UpdateHandler = handler;
            this.healthStore.ExecuteQuery(query);

            this.activeDataQueries.Add(query);
        }
Beispiel #4
0
        public void NoAnchor()
        {
            TestRuntime.AssertXcodeVersion(6, 0);

            using (var t = HKCategoryType.Create(HKCategoryTypeIdentifier.SleepAnalysis))
#if __WATCHOS__
                using (var aoq = new HKAnchoredObjectQuery(t, null, HKQueryAnchor.Create(HKAnchoredObjectQuery.NoAnchor), 0, delegate(HKAnchoredObjectQuery query, HKSample [] addedObjects, HKDeletedObject [] deletedObjects, HKQueryAnchor newAnchor, NSError error) {
#else
                using (var aoq = new HKAnchoredObjectQuery(t, null, HKAnchoredObjectQuery.NoAnchor, 0, delegate(HKAnchoredObjectQuery query, HKSample[] results, nuint newAnchor, NSError error) {
#endif
                })) {
                    Assert.That(aoq.Handle, Is.Not.EqualTo(IntPtr.Zero), "handle");
                }
        }
    }
		public void BeginWorkout (DateTime beginDate)
		{
			// Obtain the `HKObjectType` for active energy burned and the `HKUnit` for kilocalories.
			var activeEnergyType = HKQuantityType.Create (HKQuantityTypeIdentifier.ActiveEnergyBurned);
			if (activeEnergyType == null)
				return;

			var energyUnit = HKUnit.Kilocalorie;

			// Update properties.
			WorkoutBeginDate = beginDate;
			workoutButton.SetTitle ("End Workout");

			// Set up a predicate to obtain only samples from the local device starting from `beginDate`.
		
			var datePredicate = HKQuery.GetPredicateForSamples ((NSDate)beginDate, null, HKQueryOptions.None);

			var devices = new NSSet<HKDevice> (new HKDevice[] { HKDevice.LocalDevice });
			var devicePredicate = HKQuery.GetPredicateForObjectsFromDevices(devices);
			var predicate = NSCompoundPredicate.CreateAndPredicate (new NSPredicate[] { datePredicate, devicePredicate });

			//Create a results handler to recreate the samples generated by a query of active energy samples so that they can be associated with this app in the move graph.It should be noted that if your app has different heuristics for active energy burned you can generate your own quantities rather than rely on those from the watch.The sum of your sample's quantity values should equal the energy burned value provided for the workout
			Action <List<HKSample>> sampleHandler;
			sampleHandler = (List<HKSample> samples) => {
				DispatchQueue.MainQueue.DispatchAsync (delegate {
					var accumulatedSamples = new List<HKQuantitySample> ();

					var initialActivityEnergy = CurrentActiveEnergyQuantity.GetDoubleValue (energyUnit);
					double accumulatedValue = initialActivityEnergy;
					foreach (HKQuantitySample sample in samples) {
						accumulatedValue = accumulatedValue + sample.Quantity.GetDoubleValue (energyUnit);
						var ourSample = HKQuantitySample.FromType (activeEnergyType, sample.Quantity, sample.StartDate, sample.EndDate);
						accumulatedSamples.Add (ourSample);
					}

					// Update the UI.
					CurrentActiveEnergyQuantity = HKQuantity.FromQuantity (energyUnit, accumulatedValue);
					activeEnergyBurnedLabel.SetText ($"{accumulatedValue}");

					// Update our samples.
					ActiveEnergySamples.AddRange (accumulatedSamples);
				});
			};

			// Create a query to report new Active Energy Burned samples to our app.
			var activeEnergyQuery = new HKAnchoredObjectQuery (activeEnergyType, predicate, null,HKSampleQuery.NoLimit, (query, addedObjects, deletedObjects, newAnchor, error) => {
				if (error == null) {
					// NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout
					ActiveEnergySamples = new List<HKSample>(addedObjects);
					sampleHandler(ActiveEnergySamples);

				} else {
					Console.WriteLine ($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
				}
			});

			// Assign the same handler to process future samples generated while the query is still active.
			activeEnergyQuery.UpdateHandler = (query, addedObjects, deletedObjects, newAnchor, error) => {
				if (error == null) {
					ActiveEnergySamples = new List<HKSample> (addedObjects);
					sampleHandler(ActiveEnergySamples);
				} else {
					Console.WriteLine ($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
				}
			};

			// Start Query
			CurrentQuery = activeEnergyQuery;
			HealthStore.ExecuteQuery (activeEnergyQuery);
		}
        public void BeginWorkout(DateTime beginDate)
        {
            // Obtain the `HKObjectType` for active energy burned and the `HKUnit` for kilocalories.
            var activeEnergyType = HKQuantityType.Create(HKQuantityTypeIdentifier.ActiveEnergyBurned);

            if (activeEnergyType == null)
            {
                return;
            }

            var energyUnit = HKUnit.Kilocalorie;

            // Update properties.
            WorkoutBeginDate = beginDate;
            workoutButton.SetTitle("End Workout");

            // Set up a predicate to obtain only samples from the local device starting from `beginDate`.

            var datePredicate = HKQuery.GetPredicateForSamples((NSDate)beginDate, null, HKQueryOptions.None);

            var devices         = new NSSet <HKDevice> (new HKDevice[] { HKDevice.LocalDevice });
            var devicePredicate = HKQuery.GetPredicateForObjectsFromDevices(devices);
            var predicate       = NSCompoundPredicate.CreateAndPredicate(new NSPredicate[] { datePredicate, devicePredicate });

            //Create a results handler to recreate the samples generated by a query of active energy samples so that they can be associated with this app in the move graph.It should be noted that if your app has different heuristics for active energy burned you can generate your own quantities rather than rely on those from the watch.The sum of your sample's quantity values should equal the energy burned value provided for the workout
            Action <List <HKSample> > sampleHandler;

            sampleHandler = (List <HKSample> samples) => {
                DispatchQueue.MainQueue.DispatchAsync(delegate {
                    var accumulatedSamples = new List <HKQuantitySample> ();

                    var initialActivityEnergy = CurrentActiveEnergyQuantity.GetDoubleValue(energyUnit);
                    double accumulatedValue   = initialActivityEnergy;
                    foreach (HKQuantitySample sample in samples)
                    {
                        accumulatedValue = accumulatedValue + sample.Quantity.GetDoubleValue(energyUnit);
                        var ourSample    = HKQuantitySample.FromType(activeEnergyType, sample.Quantity, sample.StartDate, sample.EndDate);
                        accumulatedSamples.Add(ourSample);
                    }

                    // Update the UI.
                    CurrentActiveEnergyQuantity = HKQuantity.FromQuantity(energyUnit, accumulatedValue);
                    activeEnergyBurnedLabel.SetText($"{accumulatedValue}");

                    // Update our samples.
                    ActiveEnergySamples.AddRange(accumulatedSamples);
                });
            };

            // Create a query to report new Active Energy Burned samples to our app.
            var activeEnergyQuery = new HKAnchoredObjectQuery(activeEnergyType, predicate, null, HKSampleQuery.NoLimit, (query, addedObjects, deletedObjects, newAnchor, error) => {
                if (error == null)
                {
                    // NOTE: `deletedObjects` are not considered in the handler as there is no way to delete samples from the watch during a workout
                    ActiveEnergySamples = new List <HKSample>(addedObjects);
                    sampleHandler(ActiveEnergySamples);
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            });

            // Assign the same handler to process future samples generated while the query is still active.
            activeEnergyQuery.UpdateHandler = (query, addedObjects, deletedObjects, newAnchor, error) => {
                if (error == null)
                {
                    ActiveEnergySamples = new List <HKSample> (addedObjects);
                    sampleHandler(ActiveEnergySamples);
                }
                else
                {
                    Console.WriteLine($"An error occured executing the query. In your app, try to handle this gracefully. The error was: {error}.");
                }
            };

            // Start Query
            CurrentQuery = activeEnergyQuery;
            HealthStore.ExecuteQuery(activeEnergyQuery);
        }