carbBolusArrays.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // LoopFollow
  2. // carbBolusArrays.swift
  3. // Created by Jon Fawcett on 2020-06-17.
  4. import Foundation
  5. extension MainViewController {
  6. func findNearestBGbyTime(needle: TimeInterval, haystack: [ShareGlucoseData], startingIndex: Int) -> (sgv: Double, foundIndex: Int) {
  7. // If we can't find a match or things fail, put it at 100 BG
  8. if startingIndex > haystack.count { return (100.00, 0) }
  9. for i in startingIndex ..< haystack.count {
  10. // i has reached the end without a result. Put the dot at 100
  11. if i == haystack.count - 1 { return (100.00, 0) }
  12. if needle >= haystack[i].date, needle < haystack[i + 1].date {
  13. return (Double(haystack[i].sgv), i)
  14. }
  15. }
  16. return (100.00, 0)
  17. }
  18. func findNearestBolusbyTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) {
  19. // If we can't find a match or things fail, put it at 100 BG
  20. for i in startingIndex ..< haystack.count {
  21. // i has reached the end without a result. return 0
  22. let timeDiff = needle - haystack[i].date
  23. if timeDiff <= Double(timeWithin), timeDiff >= Double(-timeWithin) { return (true, i) }
  24. if i == haystack.count - 1 { return (false, 0) }
  25. if timeDiff < Double(-timeWithin) { return (false, 0) }
  26. }
  27. return (false, 0)
  28. }
  29. func findNextCarbTime(timeWithin: Int, needle: TimeInterval, haystack: [carbGraphStruct], startingIndex: Int) -> Bool {
  30. if startingIndex > haystack.count - 2 { return false }
  31. if haystack[startingIndex + 1].date - needle < Double(timeWithin) {
  32. return true
  33. }
  34. return false
  35. }
  36. func findNextBolusTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> Bool {
  37. var last = false
  38. var next = true
  39. if startingIndex > haystack.count - 2 { return false }
  40. if startingIndex == 0 { return false }
  41. // Nothing to right that requires shift
  42. if haystack[startingIndex + 1].date - needle > Double(timeWithin) {
  43. return false
  44. } else {
  45. // Nothing to left preventing shift
  46. if needle - haystack[startingIndex - 1].date > Double(timeWithin) {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. }