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

Fiddling around to add a Dynamic Island chart #9

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FreeAPS.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
{
"package": "SwiftCharts",
"repositoryURL": "https://github.com/ivanschuetz/SwiftCharts",
"repositoryURL": "https://github.com/ivanschuetz/SwiftCharts.git",
"state": {
"branch": "master",
"revision": "c354c1945bb35a1f01b665b22474f6db28cba4a2",
Expand Down
2 changes: 2 additions & 0 deletions FreeAPS/Sources/Services/LiveActivity/LiveActitiyShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct LiveActivityAttributes: ActivityAttributes {
let trendSystemImage: String?
let change: String
let date: Date
let sixHourReadingsGlucose: [Decimal?]
let sixHourReadingsDate: [Date?]
}

let startDate: Date
Expand Down
29 changes: 25 additions & 4 deletions FreeAPS/Sources/Services/LiveActivity/LiveActivityBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension LiveActivityAttributes.ContentState {
.string(from: mmol ? value.asMmolL as NSNumber : NSNumber(value: value))!
}

init?(new bg: BloodGlucose, prev: BloodGlucose?, mmol: Bool) {
init?(new bg: BloodGlucose, prev: BloodGlucose?, mmol: Bool, sixHourReadings: [BloodGlucose]) {
guard let glucose = bg.glucose,
bg.dateString.timeIntervalSinceNow > -TimeInterval(minutes: 6)
else {
Expand Down Expand Up @@ -62,7 +62,20 @@ extension LiveActivityAttributes.ContentState {
Self.formatGlucose(glucose - $0, mmol: mmol, forceSign: true)
}) ?? ""

self.init(bg: formattedBG, trendSystemImage: trendString, change: change, date: bg.dateString)
let sixHourReadingsGlucose: [Decimal?]
let sixHourReadingsDate: [Date?]

sixHourReadingsGlucose = sixHourReadings.map({ mmol ? $0.glucose?.asMmolL : Decimal(Double($0.glucose ?? 0)) })
sixHourReadingsDate = sixHourReadings.map(\.dateString)

self.init(
bg: formattedBG,
trendSystemImage: trendString,
change: change,
date: bg.dateString,
sixHourReadingsGlucose: sixHourReadingsGlucose,
sixHourReadingsDate: sixHourReadingsDate
)
}
}

Expand Down Expand Up @@ -97,6 +110,7 @@ extension LiveActivityAttributes.ContentState {

private var currentActivity: ActiveActivity?
private var latestGlucose: BloodGlucose?
private var sixHourReadings: [BloodGlucose]?

init(resolver: Resolver) {
injectServices(resolver)
Expand Down Expand Up @@ -189,15 +203,22 @@ extension LiveActivityBridge: GlucoseObserver {
// backfill latest glucose if contained in this update
if glucose.count > 1 {
latestGlucose = glucose[glucose.count - 2]

if glucose.count < 73 {
sixHourReadings = glucose.dropLast()
} else {
sixHourReadings = glucose.dropLast().suffix(72)
}
}
defer {
self.latestGlucose = glucose.last
}

guard let bg = glucose.last, let content = LiveActivityAttributes.ContentState(
guard let bg = glucose.last, let historicReadings = sixHourReadings, let content = LiveActivityAttributes.ContentState(
new: bg,
prev: latestGlucose,
mmol: settings.units == .mmolL
mmol: settings.units == .mmolL,
sixHourReadings: historicReadings
) else {
// no bg or value stale. Don't update the activity if there already is one, just let it turn stale so that it can still be used once current bg is available again
return
Expand Down
37 changes: 34 additions & 3 deletions LiveActivity/LiveActivity.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ActivityKit
import Charts
import SwiftUI
import WidgetKit

Expand Down Expand Up @@ -41,6 +42,21 @@ struct LiveActivity: Widget {
}
}

@ViewBuilder func chart(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
if context.isStale {
Text("--")
} else {
Chart {
ForEach(context.state.sixHourReadingsGlucose.indices, id: \.self) { index in
LineMark(
x: .value("Time", context.state.sixHourReadingsDate[index] ?? Date()),
y: .value("Value", context.state.sixHourReadingsGlucose[index] ?? 0)
).foregroundStyle(Color(red: 0.435, green: 0.812, blue: 0.592))
}
}
}
}

var body: some WidgetConfiguration {
ActivityConfiguration(for: LiveActivityAttributes.self) { context in
// Lock screen/banner UI goes here
Expand Down Expand Up @@ -74,8 +90,12 @@ struct LiveActivity: Widget {
changeLabel(context: context).font(.title).padding(.trailing, 5)
}
DynamicIslandExpandedRegion(.bottom) {
updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
.padding(.bottom, 5)
VStack {
updatedLabel(context: context).font(.caption).foregroundStyle(Color.secondary)
.padding(.bottom, 5)
Spacer()
chart(context: context)
}
}
} compactLeading: {
HStack(spacing: 1) {
Expand All @@ -100,7 +120,18 @@ private extension LiveActivityAttributes {

private extension LiveActivityAttributes.ContentState {
static var test: LiveActivityAttributes.ContentState {
LiveActivityAttributes.ContentState(bg: "100", trendSystemImage: "arrow.right", change: "+2", date: Date())
LiveActivityAttributes.ContentState(
bg: "100",
trendSystemImage: "arrow.right",
change: "+2",
date: Date(),
sixHourReadingsGlucose: [90, 95, 100],
sixHourReadingsDate: [
Calendar.current.date(byAdding: .minute, value: -15, to: Date()),
Calendar.current.date(byAdding: .minute, value: -10, to: Date()),
Calendar.current.date(byAdding: .minute, value: -5, to: Date())
]
)
}
}

Expand Down