Notes.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // LoopFollow
  2. // Notes.swift
  3. import Foundation
  4. extension MainViewController {
  5. // NS Note Response Processor
  6. func processNotes(entries: [[String: AnyObject]]) {
  7. // because it's a small array, we're going to destroy and reload every time.
  8. noteGraphData.removeAll()
  9. var lastFoundIndex = 0
  10. for currentEntry in entries.reversed() {
  11. guard let currentEntry = currentEntry as? [String: AnyObject] else { continue }
  12. var date: String
  13. if currentEntry["timestamp"] != nil {
  14. date = currentEntry["timestamp"] as! String
  15. } else if currentEntry["created_at"] != nil {
  16. date = currentEntry["created_at"] as! String
  17. } else {
  18. continue
  19. }
  20. if let parsedDate = NightscoutUtils.parseDate(date) {
  21. let dateTimeStamp = parsedDate.timeIntervalSince1970
  22. let sgv = findNearestBGbyTime(needle: dateTimeStamp, haystack: bgData, startingIndex: lastFoundIndex)
  23. lastFoundIndex = sgv.foundIndex
  24. guard let thisNote = currentEntry["notes"] as? String else { continue }
  25. if dateTimeStamp < (dateTimeUtils.getNowTimeIntervalUTC() + (60 * 60)) {
  26. let dot = DataStructs.noteStruct(date: Double(dateTimeStamp), sgv: Int(sgv.sgv), note: thisNote)
  27. noteGraphData.append(dot)
  28. }
  29. } else {
  30. print("Failed to parse date")
  31. }
  32. }
  33. if Storage.shared.graphOtherTreatments.value {
  34. updateNotes()
  35. }
  36. }
  37. }