carbBolusArrays.swift 2.5 KB

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