Skip to content

Commit

Permalink
Use Random carb entry seconds to avoid NS issues reflecting carbs
Browse files Browse the repository at this point in the history
  • Loading branch information
gestrich committed Jan 31, 2024
1 parent d26192b commit ce5bde8
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion LoopCaregiver/LoopCaregiver/Views/Actions/CarbInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ struct CarbInputView: View {
}

let now = Date()
let consumedDate = pickerConsumedDate
//We "randomize" the milliseconds to avoid issue with NS which
//doesn't allow entries at the same second.
let consumedDate = pickerConsumedDate.dateUsingCurrentSeconds()

let oldestAcceptedDate = now.addingTimeInterval(-60 * 60 * Double(maxPastCarbEntryHours))
let latestAcceptedDate = now.addingTimeInterval(60 * 60 * Double(maxFutureCarbEntryHours))
Expand Down Expand Up @@ -367,3 +369,25 @@ enum CarbInputViewError: LocalizedError {
let looperService = composer.accountServiceManager.createLooperService(looper: looper, settings: composer.settings)
return CarbInputView(looperService: looperService, showSheetView: showSheetBinding)
}

extension Date {
func dateUsingCurrentSeconds() -> Date {
let calendar = Calendar.current

// Extracting components from the original date
var components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: self)

// Getting the current seconds and milliseconds
let now = Date()
let nowSeconds = calendar.component(.second, from: now)
let nowMillisecond = calendar.component(.nanosecond, from: now) / 1_000_000

// Setting the seconds and millisecond components
components.second = nowSeconds
components.nanosecond = nowMillisecond * 1_000_000

// Creating a new date with these components
return calendar.date(from: components) ?? self
}

}

0 comments on commit ce5bde8

Please sign in to comment.