Skip to content

Commit

Permalink
test pumpview
Browse files Browse the repository at this point in the history
  • Loading branch information
mountrcg committed Nov 8, 2024
1 parent be7825c commit 9afe453
Show file tree
Hide file tree
Showing 4 changed files with 539 additions and 190 deletions.
93 changes: 93 additions & 0 deletions FreeAPS/Sources/Modules/Home/HomeStateModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,97 @@ extension Home.StateModel: PumpManagerOnboardingDelegate {
func pumpManagerOnboarding(didPauseOnboarding _: PumpManagerUI) {
// TODO:
}

// fetch yesterdays TDD ID
private func fetchYtdTotalDailyDoseID() async -> [NSManagedObjectID] {
let results = await CoreDataStack.shared.fetchEntitiesAsync(
ofType: DailyTDD.self,
onContext: context,
predicate: NSPredicate.ytdTDD,
key: "date",
ascending: false,
fetchLimit: 1,
propertiesToFetch: ["date", "totalDailyDose"]
)

guard let fetchedResults = results as? [DailyTDD] else { return [] }

return await context.perform {
fetchedResults.map(\.objectID) }
}

// fetch TDD IDs
private func fetchDailyTotalDosesIDs(forDays days: Int, endDate: Date) async -> [NSManagedObjectID] {
guard let startDate = Calendar.current.date(byAdding: .day, value: -days + 1, to: endDate),
let adjustedStartDate = calendar.date(bySettingHour: 12, minute: 0, second: 0, of: startDate)
else {
debug(.businessLogic, "CoreData TDD: not enough data to fetch over \(days) days!")
return []
}

let results = await CoreDataStack.shared.fetchEntitiesAsync(
ofType: DailyTDD.self,
onContext: context,
predicate: NSPredicate.previousTDDs(from: adjustedStartDate, to: endDate),
key: "date",
ascending: false,
fetchLimit: days,
propertiesToFetch: ["date", "totalDailyDose"]
)

guard let fetchedResults = results as? [DailyTDD] else { return [] }

return await context.perform {
fetchedResults.map(\.objectID) }
}

// Setup Daily TDDs
func setupTDDValues(forDays days: Int, endDate: Date) {
Task {
let dailyTDDs: [DailyTDD] = await CoreDataStack.shared
.getNSManagedObject(with: fetchDailyTotalDosesIDs(forDays: days, endDate: endDate), context: viewContext)

// Map the DailyTDD objects to the expected tuple (date, dose)
let dailyDoses = dailyTDDs.map { dailyTDD in
(date: dailyTDD.date ?? Date(), dose: dailyTDD.totalDailyDose ?? NSDecimalNumber.zero)
}
await updateDailyTotalDoses(with: dailyDoses)
}
}

@MainActor private func updateDailyTotalDoses(with dailyDoses: [(date: Date, dose: NSDecimalNumber)]) {
dailyTotalDoses = dailyDoses
}

private func calculateTDDValues() {
guard !dailyTotalDoses.isEmpty else {
averageTDD = .zero
ytdTDD = .zero
return
}

let totalSum = dailyTotalDoses.reduce(NSDecimalNumber.zero) { $0.adding($1.dose) }
let count = NSDecimalNumber(value: dailyTotalDoses.count)

averageTDD = totalSum.dividing(by: count).rounding(accordingToBehavior: NSDecimalNumberHandler(
roundingMode: .plain,
scale: 1,
raiseOnExactness: false,
raiseOnOverflow: false,
raiseOnUnderflow: false,
raiseOnDivideByZero: false
))

// Fetch TDD for yesterday using the asynchronous method
Task {
let yesterdayTDD: [DailyTDD] = await CoreDataStack.shared
.getNSManagedObject(with: fetchYtdTotalDailyDoseID(), context: viewContext)
await MainActor.run {
ytdTDD = yesterdayTDD.first?.totalDailyDose ?? NSDecimalNumber.zero
debugPrint(
"CoreData TDD: calculated avg\(avgDays)TDD with \(dailyTotalDoses.count) values before \(tddEndDate) at \(averageTDD), while ytdTDD was \(ytdTDD)."
)
}
}
}
}
33 changes: 16 additions & 17 deletions FreeAPS/Sources/Modules/Home/View/Header/LoopView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,30 @@ struct LoopView: View {
return formatter
}

private let rect = CGRect(x: 0, y: 0, width: 27, height: 27)
private let rect = CGRect(x: 0, y: 0, width: 18, height: 18)

var body: some View {
VStack(alignment: .center) {
HStack(alignment: .center) {
ZStack {
if isLooping {
CircleProgress()
} else {
Circle()
.strokeBorder(color, lineWidth: 5)
.strokeBorder(color, lineWidth: 4)
.frame(width: rect.width, height: rect.height, alignment: .center)
.scaleEffect(1)
.mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
}
}
if isLooping {
/* Text("looping").font(.caption2) */
Text(timeString).font(.caption2)
.foregroundColor(.secondary)
} else if manualTempBasal {
Text("Manual").font(.caption2)
if manualTempBasal {
Text("Manual")
} else if actualSuggestion?.timestamp != nil {
Text(timeString).font(.caption2)
.foregroundColor(.secondary)
Text(timeString)
} else {
Text("--").font(.caption2).foregroundColor(.secondary)
Text("--")
}
}
.font(.system(size: 16))
}

private var timeString: String {
Expand Down Expand Up @@ -102,7 +98,7 @@ struct CircleProgress: View {
@State private var rotationAngle = 0.0
@State private var pulse = false

private let rect = CGRect(x: 0, y: 0, width: 27, height: 27)
private let rect = CGRect(x: 0, y: 0, width: 18, height: 18) // Same dimensions as in LoopView
private var backgroundGradient: AngularGradient {
AngularGradient(
gradient: Gradient(colors: [
Expand All @@ -122,26 +118,29 @@ struct CircleProgress: View {
)
}

let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
let timer = Timer.publish(every: 0.03, on: .main, in: .common).autoconnect()

var body: some View {
let rect = CGRect(x: 0, y: 0, width: 18, height: 18)

ZStack {
Circle()
.trim(from: 0, to: 1)
.stroke(backgroundGradient, style: StrokeStyle(lineWidth: pulse ? 10 : 5))
.scaleEffect(pulse ? 0.7 : 1)
// .stroke(backgroundGradient, style: StrokeStyle(lineWidth: 3))
.stroke(backgroundGradient, style: StrokeStyle(lineWidth: pulse ? 6 : 3.5))
.scaleEffect(pulse ? 0.5 : 1)
.animation(
Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true),
value: pulse
)
.frame(width: rect.width, height: rect.height, alignment: .center)
.onReceive(timer) { _ in
rotationAngle = (rotationAngle + 24).truncatingRemainder(dividingBy: 360)
}
.onAppear {
self.pulse = true
}
}
.frame(width: rect.width, height: rect.height, alignment: .center)
}
}

Expand Down
Loading

0 comments on commit 9afe453

Please sign in to comment.