Skip to content
Open
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
42 changes: 8 additions & 34 deletions LoopFollow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@
656F8C122E49F3780008DC1D /* QRCodeGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 656F8C112E49F3780008DC1D /* QRCodeGenerator.swift */; };
656F8C142E49F3D20008DC1D /* RemoteCommandSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 656F8C132E49F3D20008DC1D /* RemoteCommandSettings.swift */; };
6584B1012E4A263900135D4D /* TOTPService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6584B1002E4A263900135D4D /* TOTPService.swift */; };
6589CC622E9E7D1600BB18FE /* ImportExportSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC532E9E7D1600BB18FE /* ImportExportSettingsView.swift */; };
6589CC632E9E7D1600BB18FE /* GeneralSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5D2E9E7D1600BB18FE /* GeneralSettingsView.swift */; };
6589CC642E9E7D1600BB18FE /* ContactSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC592E9E7D1600BB18FE /* ContactSettingsView.swift */; };
6589CC652E9E7D1600BB18FE /* DexcomSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5C2E9E7D1600BB18FE /* DexcomSettingsViewModel.swift */; };
6589CC662E9E7D1600BB18FE /* AdvancedSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC562E9E7D1600BB18FE /* AdvancedSettingsView.swift */; };
6589CC672E9E7D1600BB18FE /* ImportExportSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC542E9E7D1600BB18FE /* ImportExportSettingsViewModel.swift */; };
6589CC682E9E7D1600BB18FE /* ExportableSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC522E9E7D1600BB18FE /* ExportableSettings.swift */; };
6589CC692E9E7D1600BB18FE /* ContactSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5A2E9E7D1600BB18FE /* ContactSettingsViewModel.swift */; };
6589CC6A2E9E7D1600BB18FE /* DexcomSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5B2E9E7D1600BB18FE /* DexcomSettingsView.swift */; };
6589CC6B2E9E7D1600BB18FE /* TabCustomizationModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC602E9E7D1600BB18FE /* TabCustomizationModal.swift */; };
6589CC6C2E9E7D1600BB18FE /* GraphSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5E2E9E7D1600BB18FE /* GraphSettingsView.swift */; };
6589CC6D2E9E7D1600BB18FE /* CalendarSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC582E9E7D1600BB18FE /* CalendarSettingsView.swift */; };
6589CC6E2E9E7D1600BB18FE /* SettingsMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC5F2E9E7D1600BB18FE /* SettingsMenuView.swift */; };
6589CC6F2E9E7D1600BB18FE /* AdvancedSettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC572E9E7D1600BB18FE /* AdvancedSettingsViewModel.swift */; };
6589CC712E9E814F00BB18FE /* AlarmSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC702E9E814F00BB18FE /* AlarmSelectionView.swift */; };
6589CC752E9EAFB700BB18FE /* SettingsMigrationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6589CC742E9EAFB700BB18FE /* SettingsMigrationManager.swift */; };
65E153C32E4BB69100693A4F /* URLTokenValidationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E153C22E4BB69100693A4F /* URLTokenValidationView.swift */; };
65E153C32E4BB69100693A4F /* URLTokenValidationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E153C22E4BB69100693A4F /* URLTokenValidationView.swift */; };
65E8A2862E44B0300065037B /* VolumeButtonHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65E8A2852E44B0300065037B /* VolumeButtonHandler.swift */; };
DD0247592DB2E89600FCADF6 /* AlarmCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD0247582DB2E89600FCADF6 /* AlarmCondition.swift */; };
Expand Down Expand Up @@ -417,23 +400,6 @@
656F8C112E49F3780008DC1D /* QRCodeGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeGenerator.swift; sourceTree = "<group>"; };
656F8C132E49F3D20008DC1D /* RemoteCommandSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteCommandSettings.swift; sourceTree = "<group>"; };
6584B1002E4A263900135D4D /* TOTPService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TOTPService.swift; sourceTree = "<group>"; };
6589CC522E9E7D1600BB18FE /* ExportableSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExportableSettings.swift; sourceTree = "<group>"; };
6589CC532E9E7D1600BB18FE /* ImportExportSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImportExportSettingsView.swift; sourceTree = "<group>"; };
6589CC542E9E7D1600BB18FE /* ImportExportSettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImportExportSettingsViewModel.swift; sourceTree = "<group>"; };
6589CC562E9E7D1600BB18FE /* AdvancedSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedSettingsView.swift; sourceTree = "<group>"; };
6589CC572E9E7D1600BB18FE /* AdvancedSettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedSettingsViewModel.swift; sourceTree = "<group>"; };
6589CC582E9E7D1600BB18FE /* CalendarSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarSettingsView.swift; sourceTree = "<group>"; };
6589CC592E9E7D1600BB18FE /* ContactSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactSettingsView.swift; sourceTree = "<group>"; };
6589CC5A2E9E7D1600BB18FE /* ContactSettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactSettingsViewModel.swift; sourceTree = "<group>"; };
6589CC5B2E9E7D1600BB18FE /* DexcomSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DexcomSettingsView.swift; sourceTree = "<group>"; };
6589CC5C2E9E7D1600BB18FE /* DexcomSettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DexcomSettingsViewModel.swift; sourceTree = "<group>"; };
6589CC5D2E9E7D1600BB18FE /* GeneralSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingsView.swift; sourceTree = "<group>"; };
6589CC5E2E9E7D1600BB18FE /* GraphSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GraphSettingsView.swift; sourceTree = "<group>"; };
6589CC5F2E9E7D1600BB18FE /* SettingsMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsMenuView.swift; sourceTree = "<group>"; };
6589CC602E9E7D1600BB18FE /* TabCustomizationModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabCustomizationModal.swift; sourceTree = "<group>"; };
6589CC702E9E814F00BB18FE /* AlarmSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlarmSelectionView.swift; sourceTree = "<group>"; };
6589CC742E9EAFB700BB18FE /* SettingsMigrationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsMigrationManager.swift; sourceTree = "<group>"; };
65E153C22E4BB69100693A4F /* URLTokenValidationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLTokenValidationView.swift; sourceTree = "<group>"; };
65E153C22E4BB69100693A4F /* URLTokenValidationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLTokenValidationView.swift; sourceTree = "<group>"; };
65E8A2852E44B0300065037B /* VolumeButtonHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VolumeButtonHandler.swift; sourceTree = "<group>"; };
A7D55B42A22051DAD69E89D0 /* Pods_LoopFollow.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LoopFollow.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -804,6 +770,8 @@
/* End PBXFileReference section */

/* Begin PBXFileSystemSynchronizedRootGroup section */
65AC25F52ECFD5E800421360 /* Stats */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Stats; sourceTree = "<group>"; };
65AC26702ED245DF00421360 /* Treatments */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Treatments; sourceTree = "<group>"; };
DDCC3AD72DDE1790006F1C10 /* Tests */ = {isa = PBXFileSystemSynchronizedRootGroup; explicitFileTypes = {}; explicitFolders = (); path = Tests; sourceTree = "<group>"; };
/* End PBXFileSystemSynchronizedRootGroup section */

Expand Down Expand Up @@ -1467,6 +1435,8 @@
FC8DEEE32485D1680075863F /* LoopFollow */ = {
isa = PBXGroup;
children = (
65AC26702ED245DF00421360 /* Treatments */,
65AC25F52ECFD5E800421360 /* Stats */,
6589CC612E9E7D1600BB18FE /* Settings */,
DDCF9A7E2D85FCE6004DF4DD /* Alarm */,
FC16A97624995FEE003D6245 /* Application */,
Expand Down Expand Up @@ -1623,6 +1593,10 @@
);
dependencies = (
);
fileSystemSynchronizedGroups = (
65AC25F52ECFD5E800421360 /* Stats */,
65AC26702ED245DF00421360 /* Treatments */,
);
name = LoopFollow;
packageProductDependencies = (
DD48781B2C7DAF140048F05C /* SwiftJWT */,
Expand Down
14 changes: 7 additions & 7 deletions LoopFollow/Controllers/MainViewController+updateStats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ extension MainViewController {

let stats = StatsData(bgData: lastDayOfData)

statsLowPercent.text = String(format: "%.1f%", stats.percentLow) + "%"
statsInRangePercent.text = String(format: "%.1f%", stats.percentRange) + "%"
statsHighPercent.text = String(format: "%.1f%", stats.percentHigh) + "%"
statsAvgBG.text = Localizer.toDisplayUnits(String(format: "%.0f%", stats.avgBG))
statsLowPercent.text = String(format: "%.1f%%", stats.percentLow)
statsInRangePercent.text = String(format: "%.1f%%", stats.percentRange)
statsHighPercent.text = String(format: "%.1f%%", stats.percentHigh)
statsAvgBG.text = Localizer.toDisplayUnits(String(format: "%.0f", stats.avgBG))
if Storage.shared.useIFCC.value {
statsEstA1C.text = String(format: "%.0f%", stats.a1C)
statsEstA1C.text = String(format: "%.0f", stats.a1C)
} else {
statsEstA1C.text = String(format: "%.1f%", stats.a1C)
statsEstA1C.text = String(format: "%.1f%%", stats.a1C)
}
statsStdDev.text = String(format: "%.2f%", stats.stdDev)
statsStdDev.text = String(format: "%.2f", stats.stdDev)

createStatsPie(pieData: stats.pie)
}
Expand Down
4 changes: 3 additions & 1 deletion LoopFollow/Controllers/Nightscout/Treatments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ extension MainViewController {
if !Storage.shared.downloadTreatments.value { return }

let startTimeString = dateTimeUtils.getDateTimeString(addingDays: -1 * Storage.shared.downloadDays.value)
let currentTimeString = dateTimeUtils.getDateTimeString(addingHours: 6)
let currentTimeString = dateTimeUtils.getDateTimeString()
let estimatedCount = max(Storage.shared.downloadDays.value * 100, 5000)
let parameters: [String: String] = [
"find[created_at][$gte]": startTimeString,
"find[created_at][$lte]": currentTimeString,
"count": "\(estimatedCount)",
]
NightscoutUtils.executeDynamicRequest(eventType: .treatments, parameters: parameters) { (result: Result<Any, Error>) in
switch result {
Expand Down
55 changes: 55 additions & 0 deletions LoopFollow/Settings/SettingsMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ private enum Sheet: Hashable, Identifiable {
case calendar, contact
case advanced
case viewLog
case aggregatedStats

var id: Self { self }

Expand All @@ -303,10 +304,64 @@ private enum Sheet: Hashable, Identifiable {
case .contact: ContactSettingsView(viewModel: .init())
case .advanced: AdvancedSettingsView(viewModel: .init())
case .viewLog: LogView(viewModel: .init())
case .aggregatedStats:
AggregatedStatsViewWrapper()
}
}
}

// Helper view to access MainViewController
struct AggregatedStatsViewWrapper: View {
@State private var mainViewController: MainViewController?

var body: some View {
Group {
if let mainVC = mainViewController {
AggregatedStatsView(viewModel: AggregatedStatsViewModel(mainViewController: mainVC))
} else {
Text("Loading stats...")
.onAppear {
mainViewController = getMainViewController()
}
}
}
}

private func getMainViewController() -> MainViewController? {
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first,
let rootVC = window.rootViewController
else {
return nil
}

if let mainVC = rootVC as? MainViewController {
return mainVC
}

if let navVC = rootVC as? UINavigationController,
let mainVC = navVC.viewControllers.first as? MainViewController
{
return mainVC
}

if let tabVC = rootVC as? UITabBarController {
for vc in tabVC.viewControllers ?? [] {
if let mainVC = vc as? MainViewController {
return mainVC
}
if let navVC = vc as? UINavigationController,
let mainVC = navVC.viewControllers.first as? MainViewController
{
return mainVC
}
}
}

return nil
}
}

// MARK: – UIKit helpers (unchanged)

import UIKit
Expand Down
70 changes: 70 additions & 0 deletions LoopFollow/Stats/AGP/AGPCalculator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// LoopFollow
// AGPCalculator.swift

import Foundation

class AGPCalculator {
static func calculate(bgData: [ShareGlucoseData]) -> [AGPDataPoint] {
guard !bgData.isEmpty else { return [] }

var hourData: [Int: [Double]] = [:]
let calendar = Calendar.current

for reading in bgData {
let date = Date(timeIntervalSince1970: reading.date)
let components = calendar.dateComponents([.hour], from: date)
let hour = components.hour ?? 0

let glucose = Double(reading.sgv)
let glucoseMgdL = Storage.shared.units.value == "mg/dL" ? glucose : glucose * GlucoseConversion.mmolToMgDl

if hourData[hour] == nil {
hourData[hour] = []
}
hourData[hour]?.append(glucoseMgdL)
}

var agpPoints: [AGPDataPoint] = []
for hour in 0 ..< 24 {
guard let values = hourData[hour], !values.isEmpty else { continue }

let sorted = values.sorted()
let p5 = PercentileCalculator.percentile(sorted, p: 0.05)
let p25 = PercentileCalculator.percentile(sorted, p: 0.25)
let p50 = PercentileCalculator.percentile(sorted, p: 0.50)
let p75 = PercentileCalculator.percentile(sorted, p: 0.75)
let p95 = PercentileCalculator.percentile(sorted, p: 0.95)

let convert: (Double) -> Double = { value in
Storage.shared.units.value == "mg/dL" ? value : value * GlucoseConversion.mgDlToMmolL
}

let minutesSinceMidnight = hour * 60

agpPoints.append(AGPDataPoint(
timeOfDay: minutesSinceMidnight,
p5: convert(p5),
p25: convert(p25),
p50: convert(p50),
p75: convert(p75),
p95: convert(p95)
))
}

return agpPoints.sorted { $0.timeOfDay < $1.timeOfDay }
}
}

class PercentileCalculator {
static func percentile(_ sorted: [Double], p: Double) -> Double {
guard !sorted.isEmpty else { return 0.0 }
if sorted.count == 1 { return sorted[0] }

let index = p * Double(sorted.count - 1)
let lower = Int(index.rounded(.down))
let upper = min(lower + 1, sorted.count - 1)
let weight = index - Double(lower)

return sorted[lower] * (1.0 - weight) + sorted[upper] * weight
}
}
13 changes: 13 additions & 0 deletions LoopFollow/Stats/AGP/AGPDataPoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// LoopFollow
// AGPDataPoint.swift

import Foundation

struct AGPDataPoint {
let timeOfDay: Int
let p5: Double
let p25: Double
let p50: Double
let p75: Double
let p95: Double
}
Loading