|
|
@@ -8,7 +8,7 @@ protocol CarbsObserver {
|
|
|
}
|
|
|
|
|
|
protocol CarbsStorage {
|
|
|
- func storeCarbs(_ carbs: [CarbsEntry])
|
|
|
+ func storeCarbs(_ carbs: [CarbsEntry]) async
|
|
|
func syncDate() -> Date
|
|
|
func recent() -> [CarbsEntry]
|
|
|
func getCarbsNotYetUploadedToNightscout() async -> [NightscoutTreatment]
|
|
|
@@ -28,11 +28,9 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
|
|
|
injectServices(resolver)
|
|
|
}
|
|
|
|
|
|
- func storeCarbs(_ entries: [CarbsEntry]) {
|
|
|
- processQueue.sync {
|
|
|
- self.storeCarbEquivalents(entries: entries)
|
|
|
- self.saveCarbsToCoreData(entries: entries)
|
|
|
- }
|
|
|
+ func storeCarbs(_ entries: [CarbsEntry]) async {
|
|
|
+ await storeCarbEquivalents(entries: entries)
|
|
|
+ await saveCarbsToCoreData(entries: entries)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -125,7 +123,7 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
|
|
|
return (futureCarbArray, carbEquivalents)
|
|
|
}
|
|
|
|
|
|
- private func storeCarbEquivalents(entries: [CarbsEntry]) {
|
|
|
+ private func storeCarbEquivalents(entries: [CarbsEntry]) async {
|
|
|
guard let lastEntry = entries.last else { return }
|
|
|
|
|
|
if let fat = lastEntry.fat, let protein = lastEntry.protein, fat > 0 || protein > 0 {
|
|
|
@@ -137,123 +135,15 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
|
|
|
)
|
|
|
|
|
|
if carbEquivalentCount > 0 {
|
|
|
- saveFPUToCoreDataAsBatchInsert(entries: futureCarbEquivalents)
|
|
|
+ await saveFPUToCoreDataAsBatchInsert(entries: futureCarbEquivalents)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//
|
|
|
-// private func handleFPUCalculations(entries: [CarbsEntry]) {
|
|
|
-// let file = OpenAPS.Monitor.carbHistory
|
|
|
-// var uniqEvents: [CarbsEntry] = []
|
|
|
-//
|
|
|
-// let fat = entries.last?.fat ?? 0
|
|
|
-// let protein = entries.last?.protein ?? 0
|
|
|
-//
|
|
|
-// if fat > 0 || protein > 0 {
|
|
|
-// // -------------------------- FPU--------------------------------------
|
|
|
-// let interval = settings.settings.minuteInterval // Interval betwwen carbs
|
|
|
-// let timeCap = settings.settings.timeCap // Max Duration
|
|
|
-// let adjustment = settings.settings.individualAdjustmentFactor
|
|
|
-// let delay = settings.settings.delay // Tme before first future carb entry
|
|
|
-// let kcal = protein * 4 + fat * 9
|
|
|
-// let carbEquivalents = (kcal / 10) * adjustment
|
|
|
-// let fpus = carbEquivalents / 10
|
|
|
-// // Duration in hours used for extended boluses with Warsaw Method. Here used for total duration of the computed carbquivalents instead, excluding the configurable delay.
|
|
|
-// var computedDuration = 0
|
|
|
-// switch fpus {
|
|
|
-// case ..<2:
|
|
|
-// computedDuration = 3
|
|
|
-// case 2 ..< 3:
|
|
|
-// computedDuration = 4
|
|
|
-// case 3 ..< 4:
|
|
|
-// computedDuration = 5
|
|
|
-// default:
|
|
|
-// computedDuration = timeCap
|
|
|
-// }
|
|
|
-// // Size of each created carb equivalent if 60 minutes interval
|
|
|
-// var equivalent: Decimal = carbEquivalents / Decimal(computedDuration)
|
|
|
-// // Adjust for interval setting other than 60 minutes
|
|
|
-// equivalent /= Decimal(60 / interval)
|
|
|
-// // Round to 1 fraction digit
|
|
|
-// // equivalent = Decimal(round(Double(equivalent * 10) / 10))
|
|
|
-// let roundedEquivalent: Double = round(Double(equivalent * 10)) / 10
|
|
|
-// equivalent = Decimal(roundedEquivalent)
|
|
|
-// // Number of equivalents
|
|
|
-// var numberOfEquivalents = carbEquivalents / equivalent
|
|
|
-// // Only use delay in first loop
|
|
|
-// var firstIndex = true
|
|
|
-// // New date for each carb equivalent
|
|
|
-// var useDate = entries.last?.actualDate ?? Date()
|
|
|
-// // Group and Identify all FPUs together
|
|
|
-// let fpuID = entries.last?.fpuID ?? ""
|
|
|
-// // Create an array of all future carb equivalents.
|
|
|
-// var futureCarbArray = [CarbsEntry]()
|
|
|
-// while carbEquivalents > 0, numberOfEquivalents > 0 {
|
|
|
-// if firstIndex {
|
|
|
-// useDate = useDate.addingTimeInterval(delay.minutes.timeInterval)
|
|
|
-// firstIndex = false
|
|
|
-// } else { useDate = useDate.addingTimeInterval(interval.minutes.timeInterval) }
|
|
|
-//
|
|
|
-// let eachCarbEntry = CarbsEntry(
|
|
|
-// id: UUID().uuidString, createdAt: entries.last?.createdAt ?? Date(), actualDate: useDate,
|
|
|
-// carbs: equivalent, fat: 0, protein: 0, note: nil,
|
|
|
-// enteredBy: CarbsEntry.manual, isFPU: true,
|
|
|
-// fpuID: fpuID
|
|
|
-// )
|
|
|
-// futureCarbArray.append(eachCarbEntry)
|
|
|
-// numberOfEquivalents -= 1
|
|
|
-// }
|
|
|
-// // Save the array
|
|
|
-// if carbEquivalents > 0 {
|
|
|
-// storage.transaction { storage in
|
|
|
-// storage.append(futureCarbArray, to: file, uniqBy: \.id)
|
|
|
-// uniqEvents = storage.retrieve(file, as: [CarbsEntry].self)?
|
|
|
-// .filter { $0.createdAt.addingTimeInterval(1.days.timeInterval) > Date() }
|
|
|
-// .sorted { $0.createdAt > $1.createdAt } ?? []
|
|
|
-// storage.save(Array(uniqEvents), as: file)
|
|
|
-// }
|
|
|
-//
|
|
|
-// // MARK: - save also to core data
|
|
|
-//
|
|
|
-// saveFPUToCoreDataAsBatchInsert(entries: futureCarbArray)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// private func storeNormalCarbs(entries: [CarbsEntry]) {
|
|
|
-// let file = OpenAPS.Monitor.carbHistory
|
|
|
-// var uniqEvents: [CarbsEntry] = []
|
|
|
-//
|
|
|
-// if let entry = entries.last, entry.carbs > 0 {
|
|
|
-// // uniqEvents = []
|
|
|
-// let onlyCarbs = CarbsEntry(
|
|
|
-// id: entry.id ?? "",
|
|
|
-// createdAt: entry.createdAt,
|
|
|
-// actualDate: entry.actualDate ?? entry.createdAt,
|
|
|
-// carbs: entry.carbs,
|
|
|
-// fat: entry.fat,
|
|
|
-// protein: entry.protein,
|
|
|
-// note: entry.note ?? "",
|
|
|
-// enteredBy: entry.enteredBy ?? "",
|
|
|
-// isFPU: false,
|
|
|
-// fpuID: ""
|
|
|
-// )
|
|
|
-//
|
|
|
-// storage.transaction { storage in
|
|
|
-// storage.append(onlyCarbs, to: file, uniqBy: \.id)
|
|
|
-// uniqEvents = storage.retrieve(file, as: [CarbsEntry].self)?
|
|
|
-// .filter { $0.createdAt.addingTimeInterval(1.days.timeInterval) > Date() }
|
|
|
-// .sorted { $0.createdAt > $1.createdAt } ?? []
|
|
|
-// storage.save(Array(uniqEvents), as: file)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- private func saveCarbsToCoreData(entries: [CarbsEntry]) {
|
|
|
+ private func saveCarbsToCoreData(entries: [CarbsEntry]) async {
|
|
|
guard let entry = entries.last, entry.carbs != 0 else { return }
|
|
|
|
|
|
- coredataContext.perform {
|
|
|
+ await coredataContext.perform {
|
|
|
let newItem = CarbEntryStored(context: self.coredataContext)
|
|
|
newItem.date = entry.actualDate ?? entry.createdAt
|
|
|
newItem.carbs = Double(truncating: NSDecimalNumber(decimal: entry.carbs))
|
|
|
@@ -272,7 +162,7 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private func saveFPUToCoreDataAsBatchInsert(entries: [CarbsEntry]) {
|
|
|
+ private func saveFPUToCoreDataAsBatchInsert(entries: [CarbsEntry]) async {
|
|
|
let commonFPUID =
|
|
|
UUID() // all fpus should only get ONE id per batch insert to be able to delete them referencing the fpuID
|
|
|
var entrySlice = ArraySlice(entries) // convert to ArraySlice
|
|
|
@@ -290,7 +180,7 @@ final class BaseCarbsStorage: CarbsStorage, Injectable {
|
|
|
carbEntry.isUploadedToNS = false
|
|
|
return false // return false to continue
|
|
|
}
|
|
|
- coredataContext.perform {
|
|
|
+ await coredataContext.perform {
|
|
|
do {
|
|
|
try self.coredataContext.execute(batchInsert)
|
|
|
debugPrint("Carbs Storage: \(DebuggingIdentifiers.succeeded) saved fpus to core data")
|