Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 WIP: V2 #334

Draft
wants to merge 37 commits into
base: react-native-health-v2
Choose a base branch
from
Draft

🚧 WIP: V2 #334

wants to merge 37 commits into from

Conversation

GGGava
Copy link
Collaborator

@GGGava GGGava commented Sep 7, 2023

This is draft PR for a complete overhaul of react-native-health library. This code is a work in progress and is subject to change at any moment. You are welcome to test it but this is not stable or production ready.

GGGava and others added 22 commits July 6, 2023 20:54
- Samples are now encodable to String for bridging to react native
- Moved queries to a different file
- Fixed issue with metadata predicate
- Deleted old rn bindings
- Created bindings for new core
- Moved sample type parameter to parameter object
- Interval and anchor date parameters are now available in react native
- Fixed typo
- Fixed issue when saving sample
- Added unit field to quantity sampl- Fixed typo
- Fixed issue when saving sample
- Added unit field to quantity sample
Copy link

@LucasAssisRo LucasAssisRo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far :D only had a few questions and nitpicks
Also found a potential issue, not sure if already on your radar

import Foundation
import HealthKit

public protocol HealthKitType: RawRepresentable {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is the rawValue always string? If yes I suggest changing it to RawRepresentable<String>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, nice, will do

public struct QuantityQuery {
let startDate: Date?
let endDate: Date?
let isUserEntered: Bool?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What does isUserEntered == nil represent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Samples manually added by the user on Health app. I'll think in a better name 😅

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but what's the difference between false and nil here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry lol

if set to false, it will only get samples that were not manually added. If nil it won't matter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh I see makes sense now, my instinct on those situations is to go with an OptionSet

struct FetchMode: OptionSet {
    let rawValue: Int
    static var userEntered: Self { .init(rawValue: 1 << 0) }
    static var automaticallyEntered: Self { .init(rawValue: 1 << 1) }
}

then you can use it like QuantityQuery(fetchMode: .userEntered) or QuantityQuery(fetchMode: [.userEntered, .automaticallyEntered])

Bool? is totally fine as well, in fact is probably a better choice if you plan on exposing this struct to objc

import HealthKit

public protocol HealthKitType: RawRepresentable {
var type: HKSampleType { get }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Since this protocol is not directly available in objective-c maybe you could add this HKSampleType as a primary associated type to the protocol. So you don't need to cast the type when using a concrete type.

I totally understand not going for it if the compiler is giving you grief for using PATs, an alternative could be having a computed property that does the type casting on the concrete types.

enum QuantityType {
/// Don't use this directly. Use `quantitySampleType` instead.
public var type: HKSampleType { ... } // I wonder if there's a way to use @available here to help 
public var quantitySampleType: HKQuantityType { type as! HKQuantityType }

var core: HealthKitCore?

@objc
func initHealthKit(_ read: Array<String>, write: Array<String>, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potential-issue: looks like is not possible to request workout permissions since this won't try to create it. I think you might have to go for the imperative code approach here

var readTypes: [any HealthKitType] = []
for identifier in read {
 guard let readType = QuantityType(rawValue: identifier) ?? WorkoutType(rawValue: identifier) else { continue }
 readTypes.append(readType)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workout is not properly exported yet, currently working on that 😅

var core: HealthKitCore?

@objc
func initHealthKit(_ read: Array<String>, write: Array<String>, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick:

nitpick:

Suggested change
func initHealthKit(_ read: Array<String>, write: Array<String>, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
func initHealthKit(_ read: [String], write: [String], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {

GGGava and others added 7 commits September 8, 2023 17:46
- Fixed typo on statistical query
- Addded config to fix error warning on exmaple app
- Exported ids filter for quantity query
- Added missing resolve on save quantity query
…-to-health-kit-core

chore: Add documentation to HealthKitCore module
@LucasReinaldo
Copy link

hey folks, I was just wondering if this is the current progress? https://github.com/agencyenterprise/react-native-health/projects/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants