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

Add IAGE Support and Migrate InfoDisplaySettings to SwiftUI #323

Merged
merged 6 commits into from
Aug 7, 2024
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
28 changes: 24 additions & 4 deletions LoopFollow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
DD13BC752C3FD6210062313B /* InfoType.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD13BC742C3FD6200062313B /* InfoType.swift */; };
DD13BC772C3FD64E0062313B /* InfoData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD13BC762C3FD64E0062313B /* InfoData.swift */; };
DD13BC792C3FE63A0062313B /* InfoManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD13BC782C3FE63A0062313B /* InfoManager.swift */; };
DD152D3B24C01B2300361FA2 /* InfoDisplaySettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD152D3A24C01B2300361FA2 /* InfoDisplaySettingsViewController.swift */; };
DD493AD52ACF2109009A6922 /* ResumePump.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AD42ACF2109009A6922 /* ResumePump.swift */; };
DD493AD72ACF2139009A6922 /* SuspendPump.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AD62ACF2139009A6922 /* SuspendPump.swift */; };
DD493AD92ACF2171009A6922 /* Carbs.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AD82ACF2171009A6922 /* Carbs.swift */; };
Expand All @@ -33,6 +32,10 @@
DD493AE52ACF2383009A6922 /* Treatments.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE42ACF2383009A6922 /* Treatments.swift */; };
DD493AE72ACF23CF009A6922 /* DeviceStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE62ACF23CF009A6922 /* DeviceStatus.swift */; };
DD493AE92ACF2445009A6922 /* BGData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD493AE82ACF2445009A6922 /* BGData.swift */; };
DD5334212C60EBEE00062F9D /* InsulinCartridgeChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5334202C60EBEE00062F9D /* InsulinCartridgeChange.swift */; };
DD5334232C60ED3600062F9D /* IAge.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5334222C60ED3600062F9D /* IAge.swift */; };
DD5334272C61668800062F9D /* InfoDisplaySettingsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5334262C61668700062F9D /* InfoDisplaySettingsViewModel.swift */; };
DD5334292C6166A500062F9D /* InfoDisplaySettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD5334282C6166A500062F9D /* InfoDisplaySettingsView.swift */; };
DD608A082C1F584900F91132 /* DeviceStatusLoop.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */; };
DD608A0A2C23593900F91132 /* SMB.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A092C23593900F91132 /* SMB.swift */; };
DD608A0C2C27415C00F91132 /* BackgroundAlertManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD608A0B2C27415C00F91132 /* BackgroundAlertManager.swift */; };
Expand Down Expand Up @@ -238,7 +241,6 @@
DD13BC742C3FD6200062313B /* InfoType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoType.swift; sourceTree = "<group>"; };
DD13BC762C3FD64E0062313B /* InfoData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoData.swift; sourceTree = "<group>"; };
DD13BC782C3FE63A0062313B /* InfoManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoManager.swift; sourceTree = "<group>"; };
DD152D3A24C01B2300361FA2 /* InfoDisplaySettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoDisplaySettingsViewController.swift; sourceTree = "<group>"; };
DD493AD42ACF2109009A6922 /* ResumePump.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResumePump.swift; sourceTree = "<group>"; };
DD493AD62ACF2139009A6922 /* SuspendPump.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuspendPump.swift; sourceTree = "<group>"; };
DD493AD82ACF2171009A6922 /* Carbs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Carbs.swift; sourceTree = "<group>"; };
Expand All @@ -250,6 +252,10 @@
DD493AE42ACF2383009A6922 /* Treatments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Treatments.swift; sourceTree = "<group>"; };
DD493AE62ACF23CF009A6922 /* DeviceStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatus.swift; sourceTree = "<group>"; };
DD493AE82ACF2445009A6922 /* BGData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BGData.swift; sourceTree = "<group>"; };
DD5334202C60EBEE00062F9D /* InsulinCartridgeChange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsulinCartridgeChange.swift; sourceTree = "<group>"; };
DD5334222C60ED3600062F9D /* IAge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAge.swift; sourceTree = "<group>"; };
DD5334262C61668700062F9D /* InfoDisplaySettingsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoDisplaySettingsViewModel.swift; sourceTree = "<group>"; };
DD5334282C6166A500062F9D /* InfoDisplaySettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoDisplaySettingsView.swift; sourceTree = "<group>"; };
DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceStatusLoop.swift; sourceTree = "<group>"; };
DD608A092C23593900F91132 /* SMB.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SMB.swift; sourceTree = "<group>"; };
DD608A0B2C27415C00F91132 /* BackgroundAlertManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundAlertManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -524,10 +530,20 @@
DDF9676D2AD08C6E00C5EB95 /* SiteChange.swift */,
DD608A092C23593900F91132 /* SMB.swift */,
DDD10F0A2C54192A00D76A8E /* TemporaryTarget.swift */,
DD5334202C60EBEE00062F9D /* InsulinCartridgeChange.swift */,
);
path = Treatments;
sourceTree = "<group>";
};
DD5334252C61667700062F9D /* InfoDisplaySettings */ = {
isa = PBXGroup;
children = (
DD5334262C61668700062F9D /* InfoDisplaySettingsViewModel.swift */,
DD5334282C6166A500062F9D /* InfoDisplaySettingsView.swift */,
);
path = InfoDisplaySettings;
sourceTree = "<group>";
};
DD7E19802ACDA0EA00DBD158 /* Nightscout */ = {
isa = PBXGroup;
children = (
Expand All @@ -542,6 +558,7 @@
DD608A072C1F584900F91132 /* DeviceStatusLoop.swift */,
DD0C0C5F2C415B9D00DBADDF /* ProfileManager.swift */,
DD0C0C612C4175FD00DBADDF /* NSProfile.swift */,
DD5334222C60ED3600062F9D /* IAge.swift */,
);
path = Nightscout;
sourceTree = "<group>";
Expand Down Expand Up @@ -746,6 +763,7 @@
FC8DEEE32485D1680075863F /* LoopFollow */ = {
isa = PBXGroup;
children = (
DD5334252C61667700062F9D /* InfoDisplaySettings */,
DD0C0C6E2C4AFFB800DBADDF /* Remote */,
DD0C0C692C4852A100DBADDF /* Metric */,
DD13BC732C3FD60E0062313B /* InfoTable */,
Expand Down Expand Up @@ -835,7 +853,6 @@
FCFEECA1248857A600402A7F /* SettingsViewController.swift */,
FCC6884F248935D800A0279D /* AlarmViewController.swift */,
FC7CE59E248D8D23001F83B8 /* SnoozeViewController.swift */,
DD152D3A24C01B2300361FA2 /* InfoDisplaySettingsViewController.swift */,
DDCF979524C1443C002C9752 /* GeneralSettingsViewController.swift */,
DDCF979724C1489C002C9752 /* GraphSettingsViewController.swift */,
DDCF979924C14DB4002C9752 /* WatchSettingsViewController.swift */,
Expand Down Expand Up @@ -1098,13 +1115,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DD5334292C6166A500062F9D /* InfoDisplaySettingsView.swift in Sources */,
FCC68850248935D800A0279D /* AlarmViewController.swift in Sources */,
FC7CE59F248D8D23001F83B8 /* SnoozeViewController.swift in Sources */,
DDF6999B2C5AA32E0058A8D9 /* TempTargetPreset.swift in Sources */,
DD7FFAFD2A72953000C3A304 /* EKEventStore+Extensions.swift in Sources */,
FCC6886724898F8000A0279D /* UserDefaultsValue.swift in Sources */,
DDF699942C555B310058A8D9 /* ViewControllerManager.swift in Sources */,
DDCF979E24C2382A002C9752 /* AppStateController.swift in Sources */,
DD5334212C60EBEE00062F9D /* InsulinCartridgeChange.swift in Sources */,
FC97881E2485969B00A7906C /* NightScoutViewController.swift in Sources */,
DD608A0A2C23593900F91132 /* SMB.swift in Sources */,
DDCF979824C1489C002C9752 /* GraphSettingsViewController.swift in Sources */,
Expand All @@ -1123,6 +1142,7 @@
DD0C0C622C4175FD00DBADDF /* NSProfile.swift in Sources */,
DDD10F032C518A6500D76A8E /* TreatmentResponse.swift in Sources */,
DDD10F0B2C54192A00D76A8E /* TemporaryTarget.swift in Sources */,
DD5334272C61668800062F9D /* InfoDisplaySettingsViewModel.swift in Sources */,
DD493AE72ACF23CF009A6922 /* DeviceStatus.swift in Sources */,
FCFEECA2248857A600402A7F /* SettingsViewController.swift in Sources */,
DD13BC792C3FE63A0062313B /* InfoManager.swift in Sources */,
Expand All @@ -1142,7 +1162,6 @@
DDF699992C5AA3060058A8D9 /* TempTargetPresetManager.swift in Sources */,
DD0C0C722C4B000800DBADDF /* RemoteView.swift in Sources */,
DD493ADB2ACF21A3009A6922 /* Bolus.swift in Sources */,
DD152D3B24C01B2300361FA2 /* InfoDisplaySettingsViewController.swift in Sources */,
DDF9676E2AD08C6E00C5EB95 /* SiteChange.swift in Sources */,
DD13BC772C3FD64E0062313B /* InfoData.swift in Sources */,
DD13BC752C3FD6210062313B /* InfoType.swift in Sources */,
Expand Down Expand Up @@ -1176,6 +1195,7 @@
DDD10F052C529DA200D76A8E /* ObservableValue.swift in Sources */,
FC1BDD2D24A23204001B652C /* StatsView.swift in Sources */,
FCE537BC249A4D7D00F80BF8 /* carbBolusArrays.swift in Sources */,
DD5334232C60ED3600062F9D /* IAge.swift in Sources */,
FCD2A27D24C9D044009F7B7B /* Globals.swift in Sources */,
DD0C0C642C45A59400DBADDF /* HKUnit+Extensions.swift in Sources */,
DDCF979A24C14DB4002C9752 /* WatchSettingsViewController.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions LoopFollow/Controllers/NightScout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extension MainViewController {
var created_at: String
}

struct iageData: Codable {
var created_at: String
}

//NS Basal Profile Struct
struct basalProfileStruct: Codable {
var value: Double
Expand Down
65 changes: 65 additions & 0 deletions LoopFollow/Controllers/Nightscout/IAge.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// IAge.swift
// LoopFollow
//
// Created by Jonas Björkert on 2024-08-05.
// Copyright © 2024 Jon Fawcett. All rights reserved.
//

import Foundation
extension MainViewController {
// NS Iage Web Call
func webLoadNSIage() {
let lastDateString = dateTimeUtils.getDateTimeString(addingDays: -60)
let currentTimeString = dateTimeUtils.getDateTimeString()

let parameters: [String: String] = [
"find[eventType]": NightscoutUtils.EventType.iage.rawValue,
"find[created_at][$gte]": lastDateString,
"find[created_at][$lte]": currentTimeString,
"count": "1"
]

NightscoutUtils.executeRequest(eventType: .iage, parameters: parameters) { (result: Result<[iageData], Error>) in
switch result {
case .success(let data):
DispatchQueue.main.async {
self.updateIage(data: data)
}
case .failure(let error):
print("Failed to fetch data: \(error.localizedDescription)")
}
}
}

// NS Sage Response Processor
func updateIage(data: [iageData]) {
infoManager.clearInfoData(type: .iage)

if data.count == 0 {
return
}
currentIage = data[0]
let lastIageString = data[0].created_at

let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate,
.withTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime]

if let iageTime = formatter.date(from: (lastIageString as! String))?.timeIntervalSince1970 {
let now = dateTimeUtils.getNowTimeIntervalUTC()
let secondsAgo = now - iageTime

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [ .day, .hour]
formatter.zeroFormattingBehavior = [ .pad ]

if let formattedDuration = formatter.string(from: secondsAgo) {
infoManager.updateInfoData(type: .iage, value: formattedDuration)
}
}
}
}
11 changes: 10 additions & 1 deletion LoopFollow/Controllers/Nightscout/Treatments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ extension MainViewController {
var resumePump: [[String:AnyObject]] = []
var pumpSiteChange: [cageData] = []
var cgmSensorStart: [sageData] = []

var insulinCartridge: [iageData] = []

for entry in entries {
guard let eventType = entry["eventType"] as? String else {
continue
Expand Down Expand Up @@ -96,6 +97,11 @@ extension MainViewController {
let newEntry = sageData(created_at: createdAt)
cgmSensorStart.append(newEntry)
}
case "Insulin Change":
if let createdAt = entry["created_at"] as? String {
let newEntry = iageData(created_at: createdAt)
insulinCartridge.append(newEntry)
}
default:
print("No Match: \(String(describing: entry))")
}
Expand Down Expand Up @@ -168,6 +174,9 @@ extension MainViewController {
clearOldSensor()
}
}

processIage(entries: insulinCartridge)

if note.count > 0 {
processNotes(entries: note)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// InsulinCartridgeChange.swift
// LoopFollow
//
// Created by Jonas Björkert on 2024-08-05.
// Copyright © 2024 Jon Fawcett. All rights reserved.
//

import Foundation
extension MainViewController {
func processIage(entries: [iageData]) {
if !entries.isEmpty {
updateIage(data: entries)
} else if let iage = currentIage {
updateIage(data: [iage])
} else if UserDefaultsRepository.infoVisible.value[InfoType.iage.rawValue] {
webLoadNSIage()
}
}
}
3 changes: 2 additions & 1 deletion LoopFollow/Helpers/NightscoutUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ class NightscoutUtils {
case profile
case treatments
case deviceStatus
case iage = "Insulin Change"

var endpoint: String {
switch self {
case .cage, .carbsToday, .sage, .treatments:
case .cage, .carbsToday, .sage, .treatments, .iage:
return "/api/v1/treatments.json"
case .sgv:
return "/api/v1/entries.json"
Expand Down
55 changes: 55 additions & 0 deletions LoopFollow/InfoDisplaySettings/InfoDisplaySettingsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// InfoDisplaySettingsView.swift
// LoopFollow
//
// Created by Jonas Björkert on 2024-08-05.
// Copyright © 2024 Jon Fawcett. All rights reserved.
//

import SwiftUI

struct InfoDisplaySettingsView: View {
@ObservedObject var viewModel: InfoDisplaySettingsViewModel
@Environment(\.presentationMode) var presentationMode

var body: some View {
NavigationView {
Form {
Section(header: Text("General")) {
Toggle(isOn: Binding(
get: { UserDefaultsRepository.hideInfoTable.value },
set: { UserDefaultsRepository.hideInfoTable.value = $0 }
)) {
Text("Hide Information Table")
}
}

Section(header: Text("Information Display Settings")) {
List {
ForEach(viewModel.infoSort, id: \.self) { sortedIndex in
HStack {
Text(viewModel.getName(for: sortedIndex))
Spacer()
Toggle("", isOn: Binding(
get: { viewModel.infoVisible[sortedIndex] },
set: { _ in
viewModel.toggleVisibility(for: sortedIndex)
}
))
.labelsHidden()
}
}
.onMove(perform: viewModel.move)
}
.environment(\.editMode, .constant(.active))
}
}
.navigationBarItems(trailing: Button("Done") {
presentationMode.wrappedValue.dismiss()
})
.onDisappear {
NotificationCenter.default.post(name: NSNotification.Name("refresh"), object: nil)
}
}
}
}
37 changes: 37 additions & 0 deletions LoopFollow/InfoDisplaySettings/InfoDisplaySettingsViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// InfoDisplaySettingsViewModel.swift
// LoopFollow
//
// Created by Jonas Björkert on 2024-08-05.
// Copyright © 2024 Jon Fawcett. All rights reserved.
//

import Foundation
import SwiftUI

class InfoDisplaySettingsViewModel: ObservableObject {
@Published var infoSort: [Int]
@Published var infoVisible: [Bool]

init() {
self.infoSort = UserDefaultsRepository.infoSort.value
self.infoVisible = UserDefaultsRepository.infoVisible.value
}

func toggleVisibility(for sortedIndex: Int) {
infoVisible[sortedIndex].toggle()
UserDefaultsRepository.infoVisible.value = infoVisible
}

func move(from source: IndexSet, to destination: Int) {
infoSort.move(fromOffsets: source, toOffset: destination)
UserDefaultsRepository.infoSort.value = infoSort
}

func getName(for index: Int) -> String {
guard let infoType = InfoType(rawValue: index) else {
return "Unknown"
}
return infoType.name
}
}
12 changes: 11 additions & 1 deletion LoopFollow/InfoTable/InfoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,19 @@ class InfoManager {
return UserDefaultsRepository.infoSort.value.filter { UserDefaultsRepository.infoVisible.value[$0] }.count
}

func dataForIndexPath(_ indexPath: IndexPath) -> InfoData {
func dataForIndexPath(_ indexPath: IndexPath) -> InfoData? {
let sortedAndVisibleIndexes = UserDefaultsRepository.infoSort.value.filter { UserDefaultsRepository.infoVisible.value[$0] }

guard indexPath.row < sortedAndVisibleIndexes.count else {
return nil
}

let infoIndex = sortedAndVisibleIndexes[indexPath.row]

guard infoIndex < tableData.count else {
return nil
}

return tableData[infoIndex]
}
}
3 changes: 2 additions & 1 deletion LoopFollow/InfoTable/InfoType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

enum InfoType: Int, CaseIterable {
case iob, cob, basal, override, battery, pump, sage, cage, recBolus, minMax, carbsToday, autosens, profile, target, isf, carbRatio, updated, tdd
case iob, cob, basal, override, battery, pump, sage, cage, recBolus, minMax, carbsToday, autosens, profile, target, isf, carbRatio, updated, tdd, iage

var name: String {
switch self {
Expand All @@ -31,6 +31,7 @@ enum InfoType: Int, CaseIterable {
case .carbRatio: return "CR"
case .updated: return "Updated"
case .tdd: return "TDD"
case .iage: return "IAGE"
}
}

Expand Down
Loading