HistoryDeletionTarget.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import CoreData
  2. import Foundation
  3. extension History {
  4. enum DeletionTarget: Identifiable {
  5. case glucose(GlucoseStored)
  6. case insulin(PumpEventStored)
  7. case carbs(CarbEntryStored)
  8. var id: NSManagedObjectID {
  9. switch self {
  10. case let .glucose(glucose): return glucose.objectID
  11. case let .insulin(pumpEvent): return pumpEvent.objectID
  12. case let .carbs(carbEntry): return carbEntry.objectID
  13. }
  14. }
  15. func title(units _: GlucoseUnits) -> String {
  16. switch self {
  17. case .glucose:
  18. return String(localized: "Delete Glucose?", comment: "Alert title for deleting glucose")
  19. case .insulin:
  20. return String(localized: "Delete Insulin?", comment: "Alert title for deleting insulin")
  21. case let .carbs(carbEntry):
  22. if carbEntry.fpuID == nil {
  23. return String(localized: "Delete Carbs?", comment: "Alert title for deleting carbs")
  24. }
  25. return carbEntry.isFPU
  26. ? String(localized: "Delete Carbs Equivalents?", comment: "Alert title for deleting carb equivalents")
  27. : String(localized: "Delete Carbs?", comment: "Alert title for deleting carbs")
  28. }
  29. }
  30. func message(units: GlucoseUnits) -> String? {
  31. switch self {
  32. case let .glucose(glucose):
  33. let glucoseToDisplay = units == .mgdL
  34. ? glucose.glucose.description
  35. : Int(glucose.glucose).formattedAsMmolL
  36. return Formatter.dateFormatter.string(from: glucose.date ?? Date())
  37. + ", " + glucoseToDisplay + " " + units.rawValue
  38. case let .insulin(pumpEvent):
  39. var text = Formatter.dateFormatter.string(from: pumpEvent.timestamp ?? Date())
  40. + ", "
  41. + (Formatter.decimalFormatterWithThreeFractionDigits.string(from: pumpEvent.bolus?.amount ?? 0) ?? "0")
  42. + String(localized: " U", comment: "Insulin unit")
  43. if let bolus = pumpEvent.bolus, bolus.isSMB {
  44. text += String(localized: " SMB", comment: "Super Micro Bolus indicator in delete alert")
  45. }
  46. return text
  47. case let .carbs(carbEntry):
  48. if carbEntry.fpuID == nil {
  49. return Formatter.dateFormatter.string(from: carbEntry.date ?? Date())
  50. + ", "
  51. + (Formatter.decimalFormatterWithTwoFractionDigits.string(for: carbEntry.carbs) ?? "0")
  52. + String(localized: " g", comment: "gram of carbs")
  53. }
  54. return String(
  55. localized: "All FPUs and the carbs of the meal will be deleted.",
  56. comment: "Alert message for meal deletion"
  57. )
  58. }
  59. }
  60. }
  61. }