Notes.swift 1.7 KB

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