carbBolusArrays.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if startingIndex > haystack.count { return (100.00, 0) }
  13. for i in startingIndex..<haystack.count {
  14. // i has reached the end without a result. Put the dot at 100
  15. if i == haystack.count - 1 { return (100.00, 0) }
  16. if needle >= haystack[i].date && needle < haystack[i + 1].date {
  17. return (Double(haystack[i].sgv), i)
  18. }
  19. }
  20. return (100.00, 0)
  21. }
  22. func findNearestBolusbyTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) {
  23. // If we can't find a match or things fail, put it at 100 BG
  24. for i in startingIndex..<haystack.count {
  25. // i has reached the end without a result. return 0
  26. let timeDiff = needle - haystack[i].date
  27. if timeDiff <= Double(timeWithin) && timeDiff >= Double(-timeWithin) { return (true, i)}
  28. if i == haystack.count - 1 { return (false, 0) }
  29. if timeDiff < Double(-timeWithin) { return (false, 0)}
  30. }
  31. return (false, 0 )
  32. }
  33. func findNextCarbTime(timeWithin: Int, needle: TimeInterval, haystack: [carbGraphStruct], startingIndex: Int) -> Bool {
  34. if startingIndex > haystack.count - 2 { return false }
  35. if haystack[startingIndex + 1].date - needle < Double(timeWithin) {
  36. return true
  37. }
  38. return false
  39. }
  40. func findNextBolusTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> Bool {
  41. var last = false
  42. var next = true
  43. if startingIndex > haystack.count - 2 { return false }
  44. if startingIndex == 0 { return false }
  45. // Nothing to right that requires shift
  46. if haystack[startingIndex + 1].date - needle > Double(timeWithin) {
  47. return false
  48. } else {
  49. // Nothing to left preventing shift
  50. if needle - haystack[startingIndex - 1].date > Double(timeWithin) {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. }