Пример #1
0
        partial void ToggleWorkout()
        {
            if (IsWorkoutRunning && CurrentWorkoutSession != null)
            {
                HealthStore.EndWorkoutSession(CurrentWorkoutSession);
                IsWorkoutRunning = false;
            }
            else
            {
                // Begin workout.
                IsWorkoutRunning = true;

                // Clear the local Active Energy Burned quantity when beginning a workout session.
                CurrentActiveEnergyQuantity = HKQuantity.FromQuantity(HKUnit.Kilocalorie, 0.0);

                CurrentQuery        = null;
                ActiveEnergySamples = new List <HKSample> ();

                // An indoor walk workout session. There are other activity and location types available to you.

                // Create a workout configuration
                var configuration = new HKWorkoutConfiguration {
                    ActivityType = HKWorkoutActivityType.Walking,
                    LocationType = HKWorkoutSessionLocationType.Indoor
                };

                NSError error = null;
                CurrentWorkoutSession = new HKWorkoutSession(configuration, out error)
                {
                    Delegate = this
                };

                HealthStore.StartWorkoutSession(CurrentWorkoutSession);
            }
        }
Пример #2
0
        public override void Awake(NSObject context)
        {
            base.Awake(context);

            // Configure interface objects here.
            Console.WriteLine("{0} awake with context", this);

            var workoutConfiguration = context as HKWorkoutConfiguration;

            if (workoutConfiguration == null)
            {
                throw new ArgumentException("The 'context' is not a HKWorkoutConfiguration", nameof(context));
            }

            // create a workout session with the workout configuration
            NSError error;

            this.workoutSession = new HKWorkoutSession(workoutConfiguration, out error);
            if (error == null)
            {
                // start a workout session
                this.workoutSession.Delegate = this;
                this.healthStoreManager.Start(this.workoutSession);
            }
            else
            {
                throw new Exception(error.Description ?? "Unknown exception");
            }
        }
		partial void ToggleWorkout ()
		{
			if (IsWorkoutRunning && CurrentWorkoutSession != null) {
				HealthStore.EndWorkoutSession (CurrentWorkoutSession);
				IsWorkoutRunning = false;
			} else {
				// Begin workout.
				IsWorkoutRunning = true;

				// Clear the local Active Energy Burned quantity when beginning a workout session.
				CurrentActiveEnergyQuantity = HKQuantity.FromQuantity (HKUnit.Kilocalorie, 0.0);

				CurrentQuery = null;
				ActiveEnergySamples = new List<HKSample> ();

				// An indoor walk workout session. There are other activity and location types available to you.

				// Create a workout configuration
				var configuration = new HKWorkoutConfiguration {
					ActivityType = HKWorkoutActivityType.Walking,
					LocationType = HKWorkoutSessionLocationType.Indoor
				};

				NSError error = null;
				CurrentWorkoutSession = new HKWorkoutSession (configuration, out error) {
					Delegate = this
				};

				HealthStore.StartWorkoutSession(CurrentWorkoutSession);
			}
		}
Пример #4
0
        public void SaveWorkout(HKWorkoutSession workoutSession, NSDate startDate, NSDate endDate)
        {
            // Create and save a workout sample
            var configuration = workoutSession.WorkoutConfiguration;
            var metadata      = new HKMetadata
            {
                IndoorWorkout = configuration.LocationType == HKWorkoutSessionLocationType.Indoor,
            };

            var workout = HKWorkout.Create(configuration.ActivityType,
                                           startDate,
                                           endDate,
                                           this.workoutEvents.ToArray(),
                                           this.TotalBurningEnergyQuantity(),
                                           this.TotalDistanceQuantity(),
                                           metadata);

            this.healthStore.SaveObject(workout, (isSuccess, error) =>
            {
                if (isSuccess)
                {
                    this.AddSamples(workout, startDate, endDate);
                }
            });
        }
Пример #5
0
 public void DidChangeToState(HKWorkoutSession workoutSession, HKWorkoutSessionState toState, HKWorkoutSessionState fromState, NSDate date)
 {
     DispatchQueue.MainQueue.DispatchAsync(() =>
     {
         this.HandleWorkoutSessionState(toState, fromState);
     });
 }
Пример #6
0
 public void WorkoutSession(HKWorkoutSession workoutSession, HKWorkoutEvent @event)
 {
     DispatchQueue.MainQueue.DispatchAsync(() =>
     {
         this.healthStoreManager.WorkoutEvents.Add(@event);
     });
 }
Пример #7
0
        public WorkoutDelegate(HKHealthStore healthStore, HKWorkoutSession workoutSession)
        {
            // Initialize
            this.HealthStore    = healthStore;
            this.WorkoutSession = workoutSession;

            // Attach this delegate to the session
            workoutSession.Delegate = this;
        }
Пример #8
0
        private void StartOutdoorRun()
        {
            // Create a workout configuration
            var configuration = new HKWorkoutConfiguration()
            {
                ActivityType = HKWorkoutActivityType.Running,
                LocationType = HKWorkoutSessionLocationType.Outdoor
            };

            // Create workout session
            // Start workout session
            NSError error          = null;
            var     workoutSession = new HKWorkoutSession(configuration, out error);

            // Successful?
            if (error != null)
            {
                // Report error to user and return
                return;
            }

            // Create workout session delegate and wire-up events
            RunDelegate = new WorkoutDelegate(HealthStore, workoutSession);

            RunDelegate.Failed += () => {
                System.Diagnostics.Debug.WriteLine("Failed");
            };

            RunDelegate.Paused += () => {
                System.Diagnostics.Debug.WriteLine("Paused");
            };

            RunDelegate.Running += () => {
                System.Diagnostics.Debug.WriteLine("Running");
            };

            RunDelegate.Ended += () => {
                System.Diagnostics.Debug.WriteLine("Ended");
            };

            // Start session
            HealthStore.StartWorkoutSession(workoutSession);
        }
Пример #9
0
        public void DidChangeToState(HKWorkoutSession workoutSession, HKWorkoutSessionState toState, HKWorkoutSessionState fromState, NSDate date)
        {
            DispatchQueue.MainQueue.DispatchAsync(delegate {
                // Take action based on the change in state
                switch (toState)
                {
                case HKWorkoutSessionState.Running:
                    BeginWorkout((DateTime)date);
                    break;

                case HKWorkoutSessionState.Ended:
                    EndWorkout((DateTime)date);
                    break;

                default:
                    Console.WriteLine($"Unexpected workout session: {toState}.");
                    break;
                }
            });
        }
Пример #10
0
        partial void OnToggleWorkout()
        {
            if (!IsWorkoutRunning && CurrentWorkoutSession == null)
            {
                // Begin workoutt
                IsWorkoutRunning = true;
                ToggleWorkoutButton.SetTitle("Rest little Baby");;

                // Clear the local Active Energy Burned quantity when beginning a workout session
                CurrentActiveEnergyQuantity = HKQuantity.FromQuantity(HKUnit.Kilocalorie, 0.0);
                CurrentHeartRate            = HKQuantity.FromQuantity(HKUnit.FromString("count/min"), 0.0);

                CurrentQuery        = null;
                HeartRateQuery      = null;
                ActiveEnergySamples = new List <HKSample>();
                HeartRateSamples    = new List <HKSample>();

                // An indoor walk workout session. There are other activity and location types available to you.

                // Create a workout configuratio
                var configuration = new HKWorkoutConfiguration
                {
                    ActivityType = HKWorkoutActivityType.Walking,                     // Why not crawling? :
                    LocationType = HKWorkoutSessionLocationType.Indoor
                };

                NSError error = null;
                CurrentWorkoutSession = new HKWorkoutSession(configuration, out error)
                {
                    Delegate = this
                };

                HealthStore.StartWorkoutSession(CurrentWorkoutSession);
            }
            else
            {
                HealthStore.EndWorkoutSession(CurrentWorkoutSession);
                IsWorkoutRunning = false;
                ResetUI();
            }
        }
Пример #11
0
        public override void DidChangeToState(HKWorkoutSession workoutSession, HKWorkoutSessionState toState, HKWorkoutSessionState fromState, NSDate date)
        {
            // Take action based on the change in state
            switch (toState)
            {
            case HKWorkoutSessionState.NotStarted:
                break;

            case HKWorkoutSessionState.Paused:
                RaisePaused();
                break;

            case HKWorkoutSessionState.Running:
                RaiseRunning();
                break;

            case HKWorkoutSessionState.Ended:
                RaiseEnded();
                break;
            }
        }
Пример #12
0
        public override void HandleWorkoutConfiguration(HKWorkoutConfiguration workoutConfiguration)
        {
            // Create workout session
            // Start workout session
            NSError error = null;

            WorkoutSession = new HKWorkoutSession(workoutConfiguration, out error);

            // Successful?
            if (error != null)
            {
                // Report error to user and return
                return;
            }

            // Create workout session delegate and wire-up events
            RunDelegate = new WorkoutDelegate(HealthStore, WorkoutSession);

            RunDelegate.Failed += () => {
                System.Diagnostics.Debug.WriteLine("Failed");
            };

            RunDelegate.Paused += () => {
                System.Diagnostics.Debug.WriteLine("Paused");
            };

            RunDelegate.Running += () => {
                System.Diagnostics.Debug.WriteLine("Running");
            };

            RunDelegate.Ended += () => {
                System.Diagnostics.Debug.WriteLine("Ended");
            };

            // Start session
            HealthStore.StartWorkoutSession(WorkoutSession);
        }
Пример #13
0
 public void Resume(HKWorkoutSession workoutSession)
 {
     this.healthStore.ResumeWorkoutSession(workoutSession);
 }
Пример #14
0
 public void Pause(HKWorkoutSession workoutSession)
 {
     this.healthStore.PauseWorkoutSession(workoutSession);
 }
Пример #15
0
 public void End(HKWorkoutSession workoutSession)
 {
     this.healthStore.EndWorkoutSession(workoutSession);
 }
Пример #16
0
 public void Start(HKWorkoutSession workoutSession)
 {
     this.healthStore.StartWorkoutSession(workoutSession);
 }
Пример #17
0
 public void DidFail(HKWorkoutSession workoutSession, NSError error)
 {
     Console.WriteLine($"An error occured with the workout session. In your app, try to handle this gracefully. The error was: {error}.");
 }
		public void DidFail (HKWorkoutSession workoutSession, NSError error) 
		{ 
			Console.WriteLine ($"An error occured with the workout session. In your app, try to handle this gracefully. The error was: {error}.");
		}
		public void DidChangeToState (HKWorkoutSession workoutSession, HKWorkoutSessionState toState, HKWorkoutSessionState fromState, NSDate date)
		{
			DispatchQueue.MainQueue.DispatchAsync (delegate {
				// Take action based on the change in state
				switch (toState) {
					case HKWorkoutSessionState.Running:
						BeginWorkout ((DateTime)date);
						break;
					case HKWorkoutSessionState.Ended:
						EndWorkout ((DateTime)date);
						break;
					default:
						Console.WriteLine ($"Unexpected workout session: {toState}.");
						break;
				}
			});
		}
Пример #20
0
 public override void DidFail(HKWorkoutSession workoutSession, NSError error)
 {
     // Handle workout session failing
     RaiseFailed();
 }
Пример #21
0
 public void DidFail(HKWorkoutSession workoutSession, NSError error)
 {
     this.StopTimer();
     this.workoutSession.Dispose();
     Console.WriteLine($"workout session failed with an error: ({error})");
 }
Пример #22
0
 public override void DidGenerateEvent(HKWorkoutSession workoutSession, HKWorkoutEvent @event)
 {
     base.DidGenerateEvent(workoutSession, @event);
 }