Skip to content

Add carbs to healthkit#165

Closed
dyjaks wants to merge 9 commits intoivalkou:devfrom
dyjaks:dev
Closed

Add carbs to healthkit#165
dyjaks wants to merge 9 commits intoivalkou:devfrom
dyjaks:dev

Conversation

@dyjaks
Copy link
Copy Markdown

@dyjaks dyjaks commented Dec 31, 2021

Add carbs to healthkit
Should probably refactor carbs/tmp targets to their own files/subscriptions
Entires show up twice in health but health freeapx knows both don't count? All seems to load properly.
Carbs deleted in health don't delete from FreeAPS. Carbs deleted from freeAPS do delete from health.

Most of HealthKitManager.swift is now duplicate code and should be made better but as I don't know swift or ios I'm hitting a limit here of what I can do

Carbs in the future don't show. This is an easy fix but I feel is more a design decision. OpenAPS is not carb forward looking so should they be shown?

@dyjaks dyjaks changed the title [WIP] Add carbs to healthkit Add carbs to healthkit Jan 11, 2022
@dyjaks
Copy link
Copy Markdown
Author

dyjaks commented Jan 11, 2022

I think this is ready enough for review. Health will sync now with FreeAPS and FreeAPS will read carbs from health but it will not read deleted carbs from health, if that makes sense.

More simply put, carbs can be read and entered from health but if you want to delete it must be done from faps.

@ivalkou
Copy link
Copy Markdown
Owner

ivalkou commented Jan 17, 2022

@DobbyWanKenoby PTAL

@ivalkou
Copy link
Copy Markdown
Owner

ivalkou commented Jan 19, 2022

I think it's OK, but we need FAX to Health carbs transfer also

@dyjaks
Copy link
Copy Markdown
Author

dyjaks commented Jan 19, 2022

I think it's OK, but we need FAX to Health carbs transfer also

Added. Wanted to put in in carb storage but that caused a circular dependency.

@DobbyWanKenoby
Copy link
Copy Markdown
Collaborator

DobbyWanKenoby commented Jan 28, 2022

@dyjaks Good job! You did very good job! We need to add some fix.

I finded 2 problem:

  1. Dublicates of items in Health. https://ibb.co/rvX0Pnf
    I think there are 2 reasons. First: id property of CarbEntry. Each time id different. And after get data from NS they are dublicated. Second: need filtered healthkit data before save it to healthkit again (see comments in code).

I keep checking this problem

  1. Manual entered carbs data from FAX don't transfer to Health.

CarbsEntry(
_id: sample.healthKitId,
createdAt: sample.date,
carbs: sample.carb as? Decimal ?? 0.0,
Copy link
Copy Markdown
Collaborator

@DobbyWanKenoby DobbyWanKenoby Jan 28, 2022

Choose a reason for hiding this comment

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

Conditional downcast from Decimal? to Decimal does nothing
sample.carb is Decimal already

Edit to
carbs: sample.carb ?? 0.0

@DobbyWanKenoby
Copy link
Copy Markdown
Collaborator

DobbyWanKenoby commented Jan 31, 2022

Houston we have a problem
Carbs loading from NS doesn't work now

Problem with CarbEntry. You added id property, but NS send _id. As a result decoding doesn't work. I will add comments to code

@DobbyWanKenoby
Copy link
Copy Markdown
Collaborator

After a lot of different tests with adding/removing carbs and different situation (such as bad signal), I think, that we shouldn't add carbs to HealthKit in FreeAPS/Sources/APS/FetchTreatmentsManager.swift subscribe method.
All we need for HealthKit support is use CarbObserver protocol in BaseHealthKitManager like Ivan did it in BaseNightscoutManager. After that all new carbs (from FAX, from NS, and others) will send to HealthKitManager.

To BaseHealthKitManager add:

private func subscribe() {
    broadcaster.register(CarbsObserver.self, observer: self)
}

To BaseHealthKitManager.init(resolver:) add

init(resolver: Resolver) {
    // ...
    subscribe()
}

At the end of file add

extension BaseHealthKitManager: CarbsObserver {
    func carbsDidUpdate(_ carbs: [CarbsEntry]) {
        saveIfNeeded(carbs: carbs)
    }
}

@DobbyWanKenoby
Copy link
Copy Markdown
Collaborator

Ok finished review.
So, we edit model (CarbsEntity) and now have problem with old carbs entities.
In several days I will release new MigrationManager and resolve this problem

isa = PBXGroup;
children = (
38F3783A2613555C009DB701 /* Config.xcconfig */,
3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again

3811DEE725CA063400A708ED /* PersistedProperty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PersistedProperty.swift; sourceTree = "<group>"; };
3811DF0125CA9FEA00A708ED /* Credentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Credentials.swift; sourceTree = "<group>"; };
3811DF0F25CAAAE200A708ED /* APSManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APSManager.swift; sourceTree = "<group>"; };
3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigOverride.xcconfig; sourceTree = "<group>"; };
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I had same problem in past, when Ivan added ConfigOverride.xcconfig file.
You deleted wrong copy of the file.

Previous version of PR you have 2 files ConfigOverride.xcconfig, one was old (original), and second - new (your).
We need only old file, but now you deleted original.

See checksum (left side) and compare it with original in FreeAPS.xcodeproj/project.pbxproj

3818AA42274BBC1100843DB3 /* ConfigOverride.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigOverride.xcconfig; sourceTree = "<group>"; };

You have to return this file, and delete next with checksum '29AC4F65277F48C100766404'

CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "${DEVELOPER_TEAM}";
DEVELOPMENT_TEAM = 4W28235M63;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please return back "${DEVELOPER_TEAM}"
This change automatically with ConfigOverride.xcconfig

CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = "${DEVELOPER_TEAM}";
DEVELOPMENT_TEAM = 4W28235M63;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again

CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = "$(BUILD_VERSION)";
DEVELOPMENT_TEAM = "${DEVELOPER_TEAM}";
DEVELOPMENT_TEAM = 4W28235M63;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Again

CarbsEntry(createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)
])
let carbArray = [
CarbsEntry(id: ID, createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

replace to CarbsEntry(createdAt: date, carbs: carbs, enteredBy: CarbsEntry.manual)

In CarbsEntry we added id property with initial value

import Foundation

struct CarbsEntry: JSON, Equatable, Hashable {
let id: String
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

replace to var id = UUID().uuidString

But after adding new property (id) old carbs will not display, and it's a problem.
The reason is because old carbs entities (storing at carbs.json) don't contain id and cant decode.

In several days I will release new MigrationManager and resolve this problem


extension CarbsEntry {
private enum CodingKeys: String, CodingKey {
case id
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Answer (JSON) from NS have _id field. This is reason because import from NS doesn't work now.
Replace to case id = "_id"

broadcaster.notify(CarbsObserver.self, on: processQueue) {
$0.carbsDidUpdate(uniqEvents)

DispatchQueue.main.async {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why did you fix this code?
I think it must be edit back

CarbsEntry(id: UUID().uuidString, createdAt: Date(), carbs: Decimal(carbs), enteredBy: CarbsEntry.manual)
]
carbsStorage.storeCarbs(carbArray)
healthKitManager.saveIfNeeded(carbs: carbArray)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please read comment, where I tell about CarbObserver.
After that these edits are unnecessary

@ivalkou ivalkou added enhancement New feature or request not ready yet labels Mar 14, 2022
@DobbyWanKenoby
Copy link
Copy Markdown
Collaborator

New realization in #188

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request not ready yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants