objc2-health-kit 0.3.2

Bindings to the HealthKit framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! This file has been automatically generated by `objc2`'s `header-translator`.
//! DO NOT EDIT
use core::ffi::*;
use core::ptr::NonNull;
use objc2::__framework_prelude::*;
use objc2_foundation::*;

use crate::*;

/// This enumerated type is used to represent the state of a workout session.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessionstate?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct HKWorkoutSessionState(pub NSInteger);
impl HKWorkoutSessionState {
    #[doc(alias = "HKWorkoutSessionStateNotStarted")]
    pub const NotStarted: Self = Self(1);
    #[doc(alias = "HKWorkoutSessionStateRunning")]
    pub const Running: Self = Self(2);
    #[doc(alias = "HKWorkoutSessionStateEnded")]
    pub const Ended: Self = Self(3);
    #[doc(alias = "HKWorkoutSessionStatePaused")]
    pub const Paused: Self = Self(4);
    #[doc(alias = "HKWorkoutSessionStatePrepared")]
    pub const Prepared: Self = Self(5);
    #[doc(alias = "HKWorkoutSessionStateStopped")]
    pub const Stopped: Self = Self(6);
}

unsafe impl Encode for HKWorkoutSessionState {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for HKWorkoutSessionState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// This enumerated type is used to represent the type of a workout session.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessiontype?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct HKWorkoutSessionType(pub NSInteger);
impl HKWorkoutSessionType {
    #[doc(alias = "HKWorkoutSessionTypePrimary")]
    pub const Primary: Self = Self(0);
    #[doc(alias = "HKWorkoutSessionTypeMirrored")]
    pub const Mirrored: Self = Self(1);
}

unsafe impl Encode for HKWorkoutSessionType {
    const ENCODING: Encoding = NSInteger::ENCODING;
}

unsafe impl RefEncode for HKWorkoutSessionType {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

extern_class!(
    /// An HKWorkoutSession is an object describing the properties of a workout activity session.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsession?language=objc)
    #[unsafe(super(NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct HKWorkoutSession;
);

unsafe impl Send for HKWorkoutSession {}

unsafe impl Sync for HKWorkoutSession {}

extern_conformance!(
    unsafe impl NSCoding for HKWorkoutSession {}
);

extern_conformance!(
    unsafe impl NSObjectProtocol for HKWorkoutSession {}
);

extern_conformance!(
    unsafe impl NSSecureCoding for HKWorkoutSession {}
);

impl HKWorkoutSession {
    extern_methods!(
        #[cfg(feature = "HKWorkout")]
        /// Indicates the type of workout that will be performed during the session.
        #[deprecated]
        #[unsafe(method(activityType))]
        #[unsafe(method_family = none)]
        pub unsafe fn activityType(&self) -> HKWorkoutActivityType;

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// Indicates the type of location (indoors vs. outdoors) where the workout will take place.
        ///
        /// Knowing the location type allows for more accurate measurements and better performance.
        #[deprecated]
        #[unsafe(method(locationType))]
        #[unsafe(method_family = none)]
        pub unsafe fn locationType(&self) -> HKWorkoutSessionLocationType;

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// The configuration object describing the workout.
        ///
        /// This returns a copy of the configuration passed when creating the HKWorkoutSession. Changes made to
        /// the returned object have no impact on the HKWorkoutSession.
        #[unsafe(method(workoutConfiguration))]
        #[unsafe(method_family = none)]
        pub unsafe fn workoutConfiguration(&self) -> Retained<HKWorkoutConfiguration>;

        /// The session delegate, which receives
        ///
        /// The session delegate object is the one implementing the methods that get called when the session
        /// state changes or a failure occurs in the session.
        #[unsafe(method(delegate))]
        #[unsafe(method_family = none)]
        pub unsafe fn delegate(
            &self,
        ) -> Option<Retained<ProtocolObject<dyn HKWorkoutSessionDelegate>>>;

        /// Setter for [`delegate`][Self::delegate].
        ///
        /// This is a [weak property][objc2::topics::weak_property].
        #[unsafe(method(setDelegate:))]
        #[unsafe(method_family = none)]
        pub unsafe fn setDelegate(
            &self,
            delegate: Option<&ProtocolObject<dyn HKWorkoutSessionDelegate>>,
        );

        /// Indicates the current state of the workout session.
        ///
        /// Each time this value is updated, the delegate method workoutSession:didChangeToState:fromState:date:
        /// will be called.
        #[unsafe(method(state))]
        #[unsafe(method_family = none)]
        pub unsafe fn state(&self) -> HKWorkoutSessionState;

        /// Indicates the type of the workout session.
        ///
        /// A workout session created using an initializer will be primary, while a session retrieved with
        /// the `HKHealthStore` `workoutSessionMirroringStartHandler` property will be mirrored.
        #[unsafe(method(type))]
        #[unsafe(method_family = none)]
        pub unsafe fn r#type(&self) -> HKWorkoutSessionType;

        /// Indicates the date when the workout session started running.
        ///
        /// This value is nil when a workout session is initialized. It is set when the workout session state
        /// changes to HKWorkoutSessionStateRunning.
        #[unsafe(method(startDate))]
        #[unsafe(method_family = none)]
        pub unsafe fn startDate(&self) -> Option<Retained<NSDate>>;

        /// Indicates the date when the workout session stopped.
        ///
        /// This value is nil when a workout session is initialized. It is set when the workout session state
        /// changes to HKWorkoutSessionStateStopped.
        #[unsafe(method(endDate))]
        #[unsafe(method_family = none)]
        pub unsafe fn endDate(&self) -> Option<Retained<NSDate>>;

        #[cfg(feature = "HKWorkoutActivity")]
        /// The current workout activity.
        ///
        /// This returns a copy of the session's current workout activity. It will return
        /// a copy of the main workout activity if no new activity has begun. Changes made
        /// to the returned object have no impact on the HKWorkoutSession.
        #[unsafe(method(currentActivity))]
        #[unsafe(method_family = none)]
        pub unsafe fn currentActivity(&self) -> Retained<HKWorkoutActivity>;

        #[cfg(all(feature = "HKWorkout", feature = "HKWorkoutConfiguration"))]
        /// Parameter `activityType`: The activity type of the workout session.
        ///
        /// Parameter `locationType`: The type of location where the workout will be performed.
        #[deprecated]
        #[unsafe(method(initWithActivityType:locationType:))]
        #[unsafe(method_family = init)]
        pub unsafe fn initWithActivityType_locationType(
            this: Allocated<Self>,
            activity_type: HKWorkoutActivityType,
            location_type: HKWorkoutSessionLocationType,
        ) -> Retained<Self>;

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// Parameter `workoutConfiguration`: Configuration object describing the various properties of a workout.
        ///
        /// Parameter `error`: If the configuration does not specify valid configuration properties, an
        /// an NSError describing the error is set and nil is returned.
        #[deprecated]
        #[unsafe(method(initWithConfiguration:error:_))]
        #[unsafe(method_family = init)]
        pub unsafe fn initWithConfiguration_error(
            this: Allocated<Self>,
            workout_configuration: &HKWorkoutConfiguration,
        ) -> Result<Retained<Self>, Retained<NSError>>;

        #[cfg(all(feature = "HKHealthStore", feature = "HKWorkoutConfiguration"))]
        /// Parameter `healthStore`: Specifies the HKHealthStore object to use.
        ///
        /// Parameter `workoutConfiguration`: Configuration object describing the various properties of a workout.
        ///
        /// Parameter `error`: If the configuration does not specify valid configuration properties, an
        /// an NSError describing the error is set and nil is returned.
        #[unsafe(method(initWithHealthStore:configuration:error:_))]
        #[unsafe(method_family = init)]
        pub unsafe fn initWithHealthStore_configuration_error(
            this: Allocated<Self>,
            health_store: &HKHealthStore,
            workout_configuration: &HKWorkoutConfiguration,
        ) -> Result<Retained<Self>, Retained<NSError>>;

        #[unsafe(method(init))]
        #[unsafe(method_family = init)]
        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;

        /// Prepares the workout session for starting.
        ///
        /// This method will asynchronously prepare the workout session for starting. The state of the workout session
        /// will transition to HKWorkoutSessionStatePrepared. A prepared session will put the system in session mode,
        /// but will not start the session activity.
        /// You might call this methods, for example, prior to displaying a countdown on your application while waiting
        /// for the activity to start.
        #[unsafe(method(prepare))]
        #[unsafe(method_family = none)]
        pub unsafe fn prepare(&self);

        /// Parameter `date`: Start date for the workout session activity
        ///
        /// Starts the workout session activity.
        ///
        /// This method will asynchronously begin the workout session activity. The state of the workout session will
        /// transition to HKWorkoutSessionStateRunning. Once a session activity is started the system will be in session
        /// mode and sensor algorithms will be applied to generate data for the workout activity.
        #[unsafe(method(startActivityWithDate:))]
        #[unsafe(method_family = none)]
        pub unsafe fn startActivityWithDate(&self, date: Option<&NSDate>);

        /// Parameter `date`: Stop date for the workout session activity
        ///
        /// Stops the workout session activity.
        ///
        /// This method will asynchronously stop the session activity if it is currently running. The state of the workout
        /// session will transition to HKWorkoutSessionStateStopped. Once a workout session is stopped, it cannot be reused to
        /// start a new workout session. Sensor algorithms will be stopped and no new data will be generated for this session.
        /// However, the system will remain in session mode.
        #[unsafe(method(stopActivityWithDate:))]
        #[unsafe(method_family = none)]
        pub unsafe fn stopActivityWithDate(&self, date: Option<&NSDate>);

        /// Ends the workout session.
        ///
        /// This method will end the session if it is currently running or stopped. The state of the workout session will
        /// transition to HKWorkoutSessionStateEnded. Once a workout session is ended, it cannot be reused to start a new
        /// workout session. Sensor algorithms will be stopped, no new data will be generated for this session, and the
        /// system will exit session mode.
        #[unsafe(method(end))]
        #[unsafe(method_family = none)]
        pub unsafe fn end(&self);

        /// Pauses the workout session.
        ///
        /// This method will pause the session if it is currently running. The state of the workout session
        /// will transition to HKWorkoutSessionStatePaused. An HKWorkoutEventTypePause will be generated and
        /// delivered to the workout session's delegate.
        #[unsafe(method(pause))]
        #[unsafe(method_family = none)]
        pub unsafe fn pause(&self);

        /// Resumes the workout session.
        ///
        /// This method will resume the session if it is currently paused. The state of the workout session
        /// will transition to HKWorkoutSessionStateRunning. An HKWorkoutEventTypeResume will be generated and
        /// delivered to the workout session's delegate.
        #[unsafe(method(resume))]
        #[unsafe(method_family = none)]
        pub unsafe fn resume(&self);

        #[cfg(all(feature = "HKLiveWorkoutBuilder", feature = "HKWorkoutBuilder"))]
        /// Retrieves (and creates if necessary) an HKLiveWorkoutBuilder associated with this session.
        ///
        /// A session may have associated with it an HKLiveWorkoutBuilder that will be used to record the workout
        /// for this session. This method will return the session's associated builder, creating it if needed.
        /// Calling this method more than once will return the previously-created builder. If this session was not
        /// initialized with initWithHealthStore:configuration:error:, an exception will be thrown.
        #[unsafe(method(associatedWorkoutBuilder))]
        #[unsafe(method_family = none)]
        pub unsafe fn associatedWorkoutBuilder(&self) -> Retained<HKLiveWorkoutBuilder>;

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// Begins a new workout activity for this session.
        ///
        /// This method will asynchronously begin the workout activity. The delegate for this session would be
        /// informed once the activity effectively begins.  Sensor algorithms to generate data would be updated
        /// to match the new activity.
        ///
        /// # Safety
        ///
        /// `metadata` generic should be of the correct type.
        #[unsafe(method(beginNewActivityWithConfiguration:date:metadata:))]
        #[unsafe(method_family = none)]
        pub unsafe fn beginNewActivityWithConfiguration_date_metadata(
            &self,
            workout_configuration: &HKWorkoutConfiguration,
            date: &NSDate,
            metadata: Option<&NSDictionary<NSString, AnyObject>>,
        );

        /// Ends the current workout activity.
        ///
        /// This method will end the current activity, reverting to the main session activity. The delegate for this session
        /// would be informed once the activity effectively ends. Sensor algorithms to generate data would be updated to
        /// match the main session activity.
        #[unsafe(method(endCurrentActivityOnDate:))]
        #[unsafe(method_family = none)]
        pub unsafe fn endCurrentActivityOnDate(&self, date: &NSDate);

        #[cfg(feature = "block2")]
        /// Starts mirroring the session to the companion device.
        ///
        /// Calling this method will result in your app on the companion device being launched in the background.
        /// When your app is launched set the `HKHealthStore` `workoutSessionMirroringStartHandler` property to retrieve
        /// the mirrored session.
        /// This method will fail if called for a session that is ended.
        /// The completion handler will be executed on an arbitrary background queue.
        ///
        /// # Safety
        ///
        /// `completion` block must be sendable.
        #[unsafe(method(startMirroringToCompanionDeviceWithCompletion:))]
        #[unsafe(method_family = none)]
        pub unsafe fn startMirroringToCompanionDeviceWithCompletion(
            &self,
            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
        );

        #[cfg(feature = "block2")]
        /// Stops mirroring the session to the companion device.
        ///
        /// Calling this method will stop sending data to the companion device. The mirrored session's delegate method
        /// `didDisconnectFromRemoteDeviceWithError:` will be called to indicate that.
        /// When a workout session is ended, mirroring is automatically stopped.
        /// The completion handler will be executed on an arbitrary background queue.
        ///
        /// # Safety
        ///
        /// `completion` block must be sendable.
        #[unsafe(method(stopMirroringToCompanionDeviceWithCompletion:))]
        #[unsafe(method_family = none)]
        pub unsafe fn stopMirroringToCompanionDeviceWithCompletion(
            &self,
            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
        );

        #[cfg(feature = "block2")]
        /// Sends an NSData object to the connected remote workout session.
        ///
        /// This method can be called to send data from a primary session to its mirrored counterpart and vice-versa.
        /// It's only intended to be used for data that describes the current state of the workout, such as accumulated metrics, and any
        /// data needed to keep your app on both devices in sync.
        /// The maximum amount of data that can be sent is 100 KB in any given 10-second time window.
        /// If this limit is exceeded, an error will be returned in the completion handler.
        /// An error will also be returned if the session is not mirroring.
        /// The completion handler will be executed on an arbitrary background queue.
        ///
        /// # Safety
        ///
        /// `completion` block must be sendable.
        #[unsafe(method(sendDataToRemoteWorkoutSession:completion:))]
        #[unsafe(method_family = none)]
        pub unsafe fn sendDataToRemoteWorkoutSession_completion(
            &self,
            data: &NSData,
            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
        );
    );
}

/// Methods declared on superclass `NSObject`.
impl HKWorkoutSession {
    extern_methods!(
        #[unsafe(method(new))]
        #[unsafe(method_family = new)]
        pub unsafe fn new() -> Retained<Self>;
    );
}

extern_protocol!(
    /// This protocol should be implemented to be notified when a workout session's state changes.
    ///
    /// The methods on this protocol are called on an anonymous serial background queue.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessiondelegate?language=objc)
    pub unsafe trait HKWorkoutSessionDelegate: NSObjectProtocol {
        /// This method is called when a workout session transitions to a new state.
        ///
        /// The date is provided to indicate when the state change actually happened.  If your application is
        /// suspended then the delegate will receive this call once the application resumes, which may be much later
        /// than when the original state change ocurred.
        #[unsafe(method(workoutSession:didChangeToState:fromState:date:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didChangeToState_fromState_date(
            &self,
            workout_session: &HKWorkoutSession,
            to_state: HKWorkoutSessionState,
            from_state: HKWorkoutSessionState,
            date: &NSDate,
        );

        /// This method is called when an error occurs that stops a workout session.
        ///
        /// When the state of the workout session changes due to an error occurring, this method is always called
        /// before workoutSession:didChangeToState:fromState:date:.
        #[unsafe(method(workoutSession:didFailWithError:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didFailWithError(
            &self,
            workout_session: &HKWorkoutSession,
            error: &NSError,
        );

        #[cfg(feature = "HKWorkout")]
        /// This method is called whenever the system generates a workout event.
        ///
        /// Whenever a workout event is generated, such as pause or resume detection, the event will be passed
        /// to the session delegate via this method. Clients may save the generated events to use when creating an
        /// HKWorkout object.
        #[optional]
        #[unsafe(method(workoutSession:didGenerateEvent:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didGenerateEvent(
            &self,
            workout_session: &HKWorkoutSession,
            event: &HKWorkoutEvent,
        );

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// This method is called whenever a new activity begins.
        ///
        /// Whenever a new workout activity begins, the new configuration would be passed to the session delegate
        /// via this method. Clients may use this data to create a workout activity to save when building an HKWorkout
        /// object.
        #[optional]
        #[unsafe(method(workoutSession:didBeginActivityWithConfiguration:date:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didBeginActivityWithConfiguration_date(
            &self,
            workout_session: &HKWorkoutSession,
            workout_configuration: &HKWorkoutConfiguration,
            date: &NSDate,
        );

        #[cfg(feature = "HKWorkoutConfiguration")]
        /// This method is called whenever the current workout activity ends.
        ///
        /// Whenever a new workout activity ends, the new configuration would be passed to the session delegate
        /// via this method. Clients may use this data to create a workout activity to save when building an HKWorkout
        /// object.
        #[optional]
        #[unsafe(method(workoutSession:didEndActivityWithConfiguration:date:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didEndActivityWithConfiguration_date(
            &self,
            workout_session: &HKWorkoutSession,
            workout_configuration: &HKWorkoutConfiguration,
            date: &NSDate,
        );

        /// This method is called when the session receives data from its remote counterpart.
        #[optional]
        #[unsafe(method(workoutSession:didReceiveDataFromRemoteWorkoutSession:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didReceiveDataFromRemoteWorkoutSession(
            &self,
            workout_session: &HKWorkoutSession,
            data: &NSArray<NSData>,
        );

        /// This method is called when the session is disconnected from its remote counterpart.
        ///
        /// After this method is called for a mirrored workout session, it is no longer considered valid.
        #[optional]
        #[unsafe(method(workoutSession:didDisconnectFromRemoteDeviceWithError:))]
        #[unsafe(method_family = none)]
        unsafe fn workoutSession_didDisconnectFromRemoteDeviceWithError(
            &self,
            workout_session: &HKWorkoutSession,
            error: Option<&NSError>,
        );
    }
);