objc2_health_kit/generated/
HKWorkoutSession.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10/// This enumerated type is used to represent the state of a workout session.
11///
12/// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessionstate?language=objc)
13// NS_ENUM
14#[repr(transparent)]
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
16pub struct HKWorkoutSessionState(pub NSInteger);
17impl HKWorkoutSessionState {
18    #[doc(alias = "HKWorkoutSessionStateNotStarted")]
19    pub const NotStarted: Self = Self(1);
20    #[doc(alias = "HKWorkoutSessionStateRunning")]
21    pub const Running: Self = Self(2);
22    #[doc(alias = "HKWorkoutSessionStateEnded")]
23    pub const Ended: Self = Self(3);
24    #[doc(alias = "HKWorkoutSessionStatePaused")]
25    pub const Paused: Self = Self(4);
26    #[doc(alias = "HKWorkoutSessionStatePrepared")]
27    pub const Prepared: Self = Self(5);
28    #[doc(alias = "HKWorkoutSessionStateStopped")]
29    pub const Stopped: Self = Self(6);
30}
31
32unsafe impl Encode for HKWorkoutSessionState {
33    const ENCODING: Encoding = NSInteger::ENCODING;
34}
35
36unsafe impl RefEncode for HKWorkoutSessionState {
37    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
38}
39
40/// This enumerated type is used to represent the type of a workout session.
41///
42/// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessiontype?language=objc)
43// NS_ENUM
44#[repr(transparent)]
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
46pub struct HKWorkoutSessionType(pub NSInteger);
47impl HKWorkoutSessionType {
48    #[doc(alias = "HKWorkoutSessionTypePrimary")]
49    pub const Primary: Self = Self(0);
50    #[doc(alias = "HKWorkoutSessionTypeMirrored")]
51    pub const Mirrored: Self = Self(1);
52}
53
54unsafe impl Encode for HKWorkoutSessionType {
55    const ENCODING: Encoding = NSInteger::ENCODING;
56}
57
58unsafe impl RefEncode for HKWorkoutSessionType {
59    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
60}
61
62extern_class!(
63    /// An HKWorkoutSession is an object describing the properties of a workout activity session.
64    ///
65    /// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsession?language=objc)
66    #[unsafe(super(NSObject))]
67    #[derive(Debug, PartialEq, Eq, Hash)]
68    pub struct HKWorkoutSession;
69);
70
71unsafe impl Send for HKWorkoutSession {}
72
73unsafe impl Sync for HKWorkoutSession {}
74
75extern_conformance!(
76    unsafe impl NSCoding for HKWorkoutSession {}
77);
78
79extern_conformance!(
80    unsafe impl NSObjectProtocol for HKWorkoutSession {}
81);
82
83extern_conformance!(
84    unsafe impl NSSecureCoding for HKWorkoutSession {}
85);
86
87impl HKWorkoutSession {
88    extern_methods!(
89        #[cfg(feature = "HKWorkout")]
90        /// Indicates the type of workout that will be performed during the session.
91        #[deprecated]
92        #[unsafe(method(activityType))]
93        #[unsafe(method_family = none)]
94        pub unsafe fn activityType(&self) -> HKWorkoutActivityType;
95
96        #[cfg(feature = "HKWorkoutConfiguration")]
97        /// Indicates the type of location (indoors vs. outdoors) where the workout will take place.
98        ///
99        /// Knowing the location type allows for more accurate measurements and better performance.
100        #[deprecated]
101        #[unsafe(method(locationType))]
102        #[unsafe(method_family = none)]
103        pub unsafe fn locationType(&self) -> HKWorkoutSessionLocationType;
104
105        #[cfg(feature = "HKWorkoutConfiguration")]
106        /// The configuration object describing the workout.
107        ///
108        /// This returns a copy of the configuration passed when creating the HKWorkoutSession. Changes made to
109        /// the returned object have no impact on the HKWorkoutSession.
110        #[unsafe(method(workoutConfiguration))]
111        #[unsafe(method_family = none)]
112        pub unsafe fn workoutConfiguration(&self) -> Retained<HKWorkoutConfiguration>;
113
114        /// The session delegate, which receives
115        ///
116        /// The session delegate object is the one implementing the methods that get called when the session
117        /// state changes or a failure occurs in the session.
118        #[unsafe(method(delegate))]
119        #[unsafe(method_family = none)]
120        pub unsafe fn delegate(
121            &self,
122        ) -> Option<Retained<ProtocolObject<dyn HKWorkoutSessionDelegate>>>;
123
124        /// Setter for [`delegate`][Self::delegate].
125        ///
126        /// This is a [weak property][objc2::topics::weak_property].
127        #[unsafe(method(setDelegate:))]
128        #[unsafe(method_family = none)]
129        pub unsafe fn setDelegate(
130            &self,
131            delegate: Option<&ProtocolObject<dyn HKWorkoutSessionDelegate>>,
132        );
133
134        /// Indicates the current state of the workout session.
135        ///
136        /// Each time this value is updated, the delegate method workoutSession:didChangeToState:fromState:date:
137        /// will be called.
138        #[unsafe(method(state))]
139        #[unsafe(method_family = none)]
140        pub unsafe fn state(&self) -> HKWorkoutSessionState;
141
142        /// Indicates the type of the workout session.
143        ///
144        /// A workout session created using an initializer will be primary, while a session retrieved with
145        /// the `HKHealthStore` `workoutSessionMirroringStartHandler` property will be mirrored.
146        #[unsafe(method(type))]
147        #[unsafe(method_family = none)]
148        pub unsafe fn r#type(&self) -> HKWorkoutSessionType;
149
150        /// Indicates the date when the workout session started running.
151        ///
152        /// This value is nil when a workout session is initialized. It is set when the workout session state
153        /// changes to HKWorkoutSessionStateRunning.
154        #[unsafe(method(startDate))]
155        #[unsafe(method_family = none)]
156        pub unsafe fn startDate(&self) -> Option<Retained<NSDate>>;
157
158        /// Indicates the date when the workout session stopped.
159        ///
160        /// This value is nil when a workout session is initialized. It is set when the workout session state
161        /// changes to HKWorkoutSessionStateStopped.
162        #[unsafe(method(endDate))]
163        #[unsafe(method_family = none)]
164        pub unsafe fn endDate(&self) -> Option<Retained<NSDate>>;
165
166        #[cfg(feature = "HKWorkoutActivity")]
167        /// The current workout activity.
168        ///
169        /// This returns a copy of the session's current workout activity. It will return
170        /// a copy of the main workout activity if no new activity has begun. Changes made
171        /// to the returned object have no impact on the HKWorkoutSession.
172        #[unsafe(method(currentActivity))]
173        #[unsafe(method_family = none)]
174        pub unsafe fn currentActivity(&self) -> Retained<HKWorkoutActivity>;
175
176        #[cfg(all(feature = "HKWorkout", feature = "HKWorkoutConfiguration"))]
177        /// Parameter `activityType`: The activity type of the workout session.
178        ///
179        /// Parameter `locationType`: The type of location where the workout will be performed.
180        #[deprecated]
181        #[unsafe(method(initWithActivityType:locationType:))]
182        #[unsafe(method_family = init)]
183        pub unsafe fn initWithActivityType_locationType(
184            this: Allocated<Self>,
185            activity_type: HKWorkoutActivityType,
186            location_type: HKWorkoutSessionLocationType,
187        ) -> Retained<Self>;
188
189        #[cfg(feature = "HKWorkoutConfiguration")]
190        /// Parameter `workoutConfiguration`: Configuration object describing the various properties of a workout.
191        ///
192        /// Parameter `error`: If the configuration does not specify valid configuration properties, an
193        /// an NSError describing the error is set and nil is returned.
194        #[deprecated]
195        #[unsafe(method(initWithConfiguration:error:_))]
196        #[unsafe(method_family = init)]
197        pub unsafe fn initWithConfiguration_error(
198            this: Allocated<Self>,
199            workout_configuration: &HKWorkoutConfiguration,
200        ) -> Result<Retained<Self>, Retained<NSError>>;
201
202        #[cfg(all(feature = "HKHealthStore", feature = "HKWorkoutConfiguration"))]
203        /// Parameter `healthStore`: Specifies the HKHealthStore object to use.
204        ///
205        /// Parameter `workoutConfiguration`: Configuration object describing the various properties of a workout.
206        ///
207        /// Parameter `error`: If the configuration does not specify valid configuration properties, an
208        /// an NSError describing the error is set and nil is returned.
209        #[unsafe(method(initWithHealthStore:configuration:error:_))]
210        #[unsafe(method_family = init)]
211        pub unsafe fn initWithHealthStore_configuration_error(
212            this: Allocated<Self>,
213            health_store: &HKHealthStore,
214            workout_configuration: &HKWorkoutConfiguration,
215        ) -> Result<Retained<Self>, Retained<NSError>>;
216
217        #[unsafe(method(init))]
218        #[unsafe(method_family = init)]
219        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
220
221        /// Prepares the workout session for starting.
222        ///
223        /// This method will asynchronously prepare the workout session for starting. The state of the workout session
224        /// will transition to HKWorkoutSessionStatePrepared. A prepared session will put the system in session mode,
225        /// but will not start the session activity.
226        /// You might call this methods, for example, prior to displaying a countdown on your application while waiting
227        /// for the activity to start.
228        #[unsafe(method(prepare))]
229        #[unsafe(method_family = none)]
230        pub unsafe fn prepare(&self);
231
232        /// Parameter `date`: Start date for the workout session activity
233        ///
234        /// Starts the workout session activity.
235        ///
236        /// This method will asynchronously begin the workout session activity. The state of the workout session will
237        /// transition to HKWorkoutSessionStateRunning. Once a session activity is started the system will be in session
238        /// mode and sensor algorithms will be applied to generate data for the workout activity.
239        #[unsafe(method(startActivityWithDate:))]
240        #[unsafe(method_family = none)]
241        pub unsafe fn startActivityWithDate(&self, date: Option<&NSDate>);
242
243        /// Parameter `date`: Stop date for the workout session activity
244        ///
245        /// Stops the workout session activity.
246        ///
247        /// This method will asynchronously stop the session activity if it is currently running. The state of the workout
248        /// session will transition to HKWorkoutSessionStateStopped. Once a workout session is stopped, it cannot be reused to
249        /// start a new workout session. Sensor algorithms will be stopped and no new data will be generated for this session.
250        /// However, the system will remain in session mode.
251        #[unsafe(method(stopActivityWithDate:))]
252        #[unsafe(method_family = none)]
253        pub unsafe fn stopActivityWithDate(&self, date: Option<&NSDate>);
254
255        /// Ends the workout session.
256        ///
257        /// This method will end the session if it is currently running or stopped. The state of the workout session will
258        /// transition to HKWorkoutSessionStateEnded. Once a workout session is ended, it cannot be reused to start a new
259        /// workout session. Sensor algorithms will be stopped, no new data will be generated for this session, and the
260        /// system will exit session mode.
261        #[unsafe(method(end))]
262        #[unsafe(method_family = none)]
263        pub unsafe fn end(&self);
264
265        /// Pauses the workout session.
266        ///
267        /// This method will pause the session if it is currently running. The state of the workout session
268        /// will transition to HKWorkoutSessionStatePaused. An HKWorkoutEventTypePause will be generated and
269        /// delivered to the workout session's delegate.
270        #[unsafe(method(pause))]
271        #[unsafe(method_family = none)]
272        pub unsafe fn pause(&self);
273
274        /// Resumes the workout session.
275        ///
276        /// This method will resume the session if it is currently paused. The state of the workout session
277        /// will transition to HKWorkoutSessionStateRunning. An HKWorkoutEventTypeResume will be generated and
278        /// delivered to the workout session's delegate.
279        #[unsafe(method(resume))]
280        #[unsafe(method_family = none)]
281        pub unsafe fn resume(&self);
282
283        #[cfg(all(feature = "HKLiveWorkoutBuilder", feature = "HKWorkoutBuilder"))]
284        /// Retrieves (and creates if necessary) an HKLiveWorkoutBuilder associated with this session.
285        ///
286        /// A session may have associated with it an HKLiveWorkoutBuilder that will be used to record the workout
287        /// for this session. This method will return the session's associated builder, creating it if needed.
288        /// Calling this method more than once will return the previously-created builder. If this session was not
289        /// initialized with initWithHealthStore:configuration:error:, an exception will be thrown.
290        #[unsafe(method(associatedWorkoutBuilder))]
291        #[unsafe(method_family = none)]
292        pub unsafe fn associatedWorkoutBuilder(&self) -> Retained<HKLiveWorkoutBuilder>;
293
294        #[cfg(feature = "HKWorkoutConfiguration")]
295        /// Begins a new workout activity for this session.
296        ///
297        /// This method will asynchronously begin the workout activity. The delegate for this session would be
298        /// informed once the activity effectively begins.  Sensor algorithms to generate data would be updated
299        /// to match the new activity.
300        ///
301        /// # Safety
302        ///
303        /// `metadata` generic should be of the correct type.
304        #[unsafe(method(beginNewActivityWithConfiguration:date:metadata:))]
305        #[unsafe(method_family = none)]
306        pub unsafe fn beginNewActivityWithConfiguration_date_metadata(
307            &self,
308            workout_configuration: &HKWorkoutConfiguration,
309            date: &NSDate,
310            metadata: Option<&NSDictionary<NSString, AnyObject>>,
311        );
312
313        /// Ends the current workout activity.
314        ///
315        /// This method will end the current activity, reverting to the main session activity. The delegate for this session
316        /// would be informed once the activity effectively ends. Sensor algorithms to generate data would be updated to
317        /// match the main session activity.
318        #[unsafe(method(endCurrentActivityOnDate:))]
319        #[unsafe(method_family = none)]
320        pub unsafe fn endCurrentActivityOnDate(&self, date: &NSDate);
321
322        #[cfg(feature = "block2")]
323        /// Starts mirroring the session to the companion device.
324        ///
325        /// Calling this method will result in your app on the companion device being launched in the background.
326        /// When your app is launched set the `HKHealthStore` `workoutSessionMirroringStartHandler` property to retrieve
327        /// the mirrored session.
328        /// This method will fail if called for a session that is ended.
329        /// The completion handler will be executed on an arbitrary background queue.
330        ///
331        /// # Safety
332        ///
333        /// `completion` block must be sendable.
334        #[unsafe(method(startMirroringToCompanionDeviceWithCompletion:))]
335        #[unsafe(method_family = none)]
336        pub unsafe fn startMirroringToCompanionDeviceWithCompletion(
337            &self,
338            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
339        );
340
341        #[cfg(feature = "block2")]
342        /// Stops mirroring the session to the companion device.
343        ///
344        /// Calling this method will stop sending data to the companion device. The mirrored session's delegate method
345        /// `didDisconnectFromRemoteDeviceWithError:` will be called to indicate that.
346        /// When a workout session is ended, mirroring is automatically stopped.
347        /// The completion handler will be executed on an arbitrary background queue.
348        ///
349        /// # Safety
350        ///
351        /// `completion` block must be sendable.
352        #[unsafe(method(stopMirroringToCompanionDeviceWithCompletion:))]
353        #[unsafe(method_family = none)]
354        pub unsafe fn stopMirroringToCompanionDeviceWithCompletion(
355            &self,
356            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
357        );
358
359        #[cfg(feature = "block2")]
360        /// Sends an NSData object to the connected remote workout session.
361        ///
362        /// This method can be called to send data from a primary session to its mirrored counterpart and vice-versa.
363        /// It's only intended to be used for data that describes the current state of the workout, such as accumulated metrics, and any
364        /// data needed to keep your app on both devices in sync.
365        /// The maximum amount of data that can be sent is 100 KB in any given 10-second time window.
366        /// If this limit is exceeded, an error will be returned in the completion handler.
367        /// An error will also be returned if the session is not mirroring.
368        /// The completion handler will be executed on an arbitrary background queue.
369        ///
370        /// # Safety
371        ///
372        /// `completion` block must be sendable.
373        #[unsafe(method(sendDataToRemoteWorkoutSession:completion:))]
374        #[unsafe(method_family = none)]
375        pub unsafe fn sendDataToRemoteWorkoutSession_completion(
376            &self,
377            data: &NSData,
378            completion: &block2::DynBlock<dyn Fn(Bool, *mut NSError)>,
379        );
380    );
381}
382
383/// Methods declared on superclass `NSObject`.
384impl HKWorkoutSession {
385    extern_methods!(
386        #[unsafe(method(new))]
387        #[unsafe(method_family = new)]
388        pub unsafe fn new() -> Retained<Self>;
389    );
390}
391
392extern_protocol!(
393    /// This protocol should be implemented to be notified when a workout session's state changes.
394    ///
395    /// The methods on this protocol are called on an anonymous serial background queue.
396    ///
397    /// See also [Apple's documentation](https://developer.apple.com/documentation/healthkit/hkworkoutsessiondelegate?language=objc)
398    pub unsafe trait HKWorkoutSessionDelegate: NSObjectProtocol {
399        /// This method is called when a workout session transitions to a new state.
400        ///
401        /// The date is provided to indicate when the state change actually happened.  If your application is
402        /// suspended then the delegate will receive this call once the application resumes, which may be much later
403        /// than when the original state change ocurred.
404        #[unsafe(method(workoutSession:didChangeToState:fromState:date:))]
405        #[unsafe(method_family = none)]
406        unsafe fn workoutSession_didChangeToState_fromState_date(
407            &self,
408            workout_session: &HKWorkoutSession,
409            to_state: HKWorkoutSessionState,
410            from_state: HKWorkoutSessionState,
411            date: &NSDate,
412        );
413
414        /// This method is called when an error occurs that stops a workout session.
415        ///
416        /// When the state of the workout session changes due to an error occurring, this method is always called
417        /// before workoutSession:didChangeToState:fromState:date:.
418        #[unsafe(method(workoutSession:didFailWithError:))]
419        #[unsafe(method_family = none)]
420        unsafe fn workoutSession_didFailWithError(
421            &self,
422            workout_session: &HKWorkoutSession,
423            error: &NSError,
424        );
425
426        #[cfg(feature = "HKWorkout")]
427        /// This method is called whenever the system generates a workout event.
428        ///
429        /// Whenever a workout event is generated, such as pause or resume detection, the event will be passed
430        /// to the session delegate via this method. Clients may save the generated events to use when creating an
431        /// HKWorkout object.
432        #[optional]
433        #[unsafe(method(workoutSession:didGenerateEvent:))]
434        #[unsafe(method_family = none)]
435        unsafe fn workoutSession_didGenerateEvent(
436            &self,
437            workout_session: &HKWorkoutSession,
438            event: &HKWorkoutEvent,
439        );
440
441        #[cfg(feature = "HKWorkoutConfiguration")]
442        /// This method is called whenever a new activity begins.
443        ///
444        /// Whenever a new workout activity begins, the new configuration would be passed to the session delegate
445        /// via this method. Clients may use this data to create a workout activity to save when building an HKWorkout
446        /// object.
447        #[optional]
448        #[unsafe(method(workoutSession:didBeginActivityWithConfiguration:date:))]
449        #[unsafe(method_family = none)]
450        unsafe fn workoutSession_didBeginActivityWithConfiguration_date(
451            &self,
452            workout_session: &HKWorkoutSession,
453            workout_configuration: &HKWorkoutConfiguration,
454            date: &NSDate,
455        );
456
457        #[cfg(feature = "HKWorkoutConfiguration")]
458        /// This method is called whenever the current workout activity ends.
459        ///
460        /// Whenever a new workout activity ends, the new configuration would be passed to the session delegate
461        /// via this method. Clients may use this data to create a workout activity to save when building an HKWorkout
462        /// object.
463        #[optional]
464        #[unsafe(method(workoutSession:didEndActivityWithConfiguration:date:))]
465        #[unsafe(method_family = none)]
466        unsafe fn workoutSession_didEndActivityWithConfiguration_date(
467            &self,
468            workout_session: &HKWorkoutSession,
469            workout_configuration: &HKWorkoutConfiguration,
470            date: &NSDate,
471        );
472
473        /// This method is called when the session receives data from its remote counterpart.
474        #[optional]
475        #[unsafe(method(workoutSession:didReceiveDataFromRemoteWorkoutSession:))]
476        #[unsafe(method_family = none)]
477        unsafe fn workoutSession_didReceiveDataFromRemoteWorkoutSession(
478            &self,
479            workout_session: &HKWorkoutSession,
480            data: &NSArray<NSData>,
481        );
482
483        /// This method is called when the session is disconnected from its remote counterpart.
484        ///
485        /// After this method is called for a mirrored workout session, it is no longer considered valid.
486        #[optional]
487        #[unsafe(method(workoutSession:didDisconnectFromRemoteDeviceWithError:))]
488        #[unsafe(method_family = none)]
489        unsafe fn workoutSession_didDisconnectFromRemoteDeviceWithError(
490            &self,
491            workout_session: &HKWorkoutSession,
492            error: Option<&NSError>,
493        );
494    }
495);