From dc43dacaee099f2e612f700419309ea13f8648e7 Mon Sep 17 00:00:00 2001
From: Paul Plant <37302780+paulplant@users.noreply.github.com>
Date: Mon, 30 May 2022 11:41:40 +0200
Subject: [PATCH] moved UIKit-dependant functions out of
TreatmentEntry+CoreDataClass and into TreatmentTableViewCell
---
.../TreatmentEntry+CoreDataClass.swift | 88 ---------------
xdrip/Storyboards/Base.lproj/Main.storyboard | 86 +++++++--------
.../Treatments/TreatmentTableViewCell.swift | 100 +++++++++++++++---
3 files changed, 129 insertions(+), 145 deletions(-)
diff --git a/xdrip/Core Data/classes/TreatmentEntry+CoreDataClass.swift b/xdrip/Core Data/classes/TreatmentEntry+CoreDataClass.swift
index 71a8f8e9..619eb0a1 100644
--- a/xdrip/Core Data/classes/TreatmentEntry+CoreDataClass.swift
+++ b/xdrip/Core Data/classes/TreatmentEntry+CoreDataClass.swift
@@ -8,7 +8,6 @@
import Foundation
import CoreData
-import UIKit
// @objc and Int16 allows enums to work with CoreData
@@ -113,56 +112,6 @@ import UIKit
}
}
-
- /// Returns the displayUnit: the .unit as required by the treatment type and the user settings
- public func iconColor() -> UIColor {
-
- switch self {
-
- case .Insulin:
- return ConstantsGlucoseChart.bolusTreatmentColor
-
- case .Carbs:
- return ConstantsGlucoseChart.carbsTreatmentColor
-
- case .Exercise:
- return UIColor.magenta
-
- case .BgCheck:
- return ConstantsGlucoseChart.bgCheckTreatmentColorInner
-
- }
-
- }
-
- /// Returns the displayUnit: the .unit as required by the treatment type and the user settings
- public func iconImage() -> UIImage? {
-
- if #available(iOS 13.0, *) {
-
- switch self {
-
- case .Insulin:
- return UIImage(systemName: "arrowtriangle.down.fill")!
-
- case .Carbs:
- return UIImage(systemName: "circle.fill")!
-
- case .Exercise:
- return UIImage(systemName: "heart.fill")!
-
- case .BgCheck:
- return UIImage(systemName: "drop.fill")!
-
- }
-
- } else {
-
- return nil
-
- }
-
- }
}
@@ -214,43 +163,6 @@ public class TreatmentEntry: NSManagedObject, Comparable {
super.init(entity: entity, insertInto: context)
}
- /// Returns the displayValue: the .value in the correct value if any changes are required
- public func displayValue() -> String {
-
- // if the treatmentType is a BG Check then convert the value to mmol/l if that is what the user is using. All BG checks are stored in coredata as mg/dl
- if self.treatmentType == .BgCheck {
-
- // save typing
- let isMgDl: Bool = UserDefaults.standard.bloodGlucoseUnitIsMgDl
-
- // convert to mmol/l if needed, round accordingly and add the correct units
- return self.value.mgdlToMmol(mgdl: isMgDl).bgValueRounded(mgdl: isMgDl).stringWithoutTrailingZeroes
-
- } else {
-
- return self.value.stringWithoutTrailingZeroes
-
- }
-
- }
-
- /// Returns the displayUnit: the .unit as required by the treatment type and the user settings
- public func displayUnit() -> String {
-
- // if the treatmentType is a BG Check then convert the value to mmol/l if that is what the user is using. All BG checks are stored in coredata as mg/dl
- if self.treatmentType == .BgCheck {
-
- // convert to mmol/l if needed, round accordingly and add the correct units
- return String(UserDefaults.standard.bloodGlucoseUnitIsMgDl ? Texts_Common.mgdl : Texts_Common.mmol)
-
- } else {
-
- return self.treatmentType.unit()
-
- }
-
- }
-
/// - get the dictionary representation required for creating a new treatment @ NighScout using POST or updating an existing treatment @ NightScout using PUT
/// - splits of "-carbs" "-insulin" or "-exercise" from the id
public func dictionaryRepresentationForNightScoutUpload() -> [String: Any] {
diff --git a/xdrip/Storyboards/Base.lproj/Main.storyboard b/xdrip/Storyboards/Base.lproj/Main.storyboard
index 523a5d7f..675895ae 100644
--- a/xdrip/Storyboards/Base.lproj/Main.storyboard
+++ b/xdrip/Storyboards/Base.lproj/Main.storyboard
@@ -6,7 +6,6 @@
-
@@ -1851,17 +1850,18 @@
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
+
+
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
diff --git a/xdrip/View Controllers/Treatments/TreatmentTableViewCell.swift b/xdrip/View Controllers/Treatments/TreatmentTableViewCell.swift
index e55eb0f7..1349faa2 100644
--- a/xdrip/View Controllers/Treatments/TreatmentTableViewCell.swift
+++ b/xdrip/View Controllers/Treatments/TreatmentTableViewCell.swift
@@ -10,21 +10,93 @@ import Foundation
class TreatmentTableViewCell: UITableViewCell {
@IBOutlet weak var typeLabel: UILabel!
- @IBOutlet weak var valueLabel: UILabel!
- @IBOutlet weak var dateLabel: UILabel!
+ @IBOutlet weak var valueLabel: UILabel!
+ @IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var unitLabel: UILabel!
@IBOutlet weak var iconImageView: UIImageView!
- public func setupWithTreatment(_ treatment: TreatmentEntry) {
- self.typeLabel.text = treatment.treatmentType.asString()
- self.valueLabel.text = treatment.displayValue()
- self.unitLabel.text = treatment.displayUnit()
- self.iconImageView.tintColor = treatment.treatmentType.iconColor()
- self.iconImageView.image = treatment.treatmentType.iconImage()
-
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
-
- self.dateLabel.text = formatter.string(from: treatment.date)
- }
+ public func setupWithTreatment(_ treatment: TreatmentEntry) {
+
+ // date label
+ let formatter = DateFormatter()
+ formatter.dateFormat = "HH:mm"
+
+ self.dateLabel.text = formatter.string(from: treatment.date)
+
+
+ // treatment type icon
+ switch treatment.treatmentType {
+
+ case .Insulin:
+ self.iconImageView.tintColor = ConstantsGlucoseChart.bolusTreatmentColor
+
+ case .Carbs:
+ self.iconImageView.tintColor = ConstantsGlucoseChart.carbsTreatmentColor
+
+ case .Exercise:
+ self.iconImageView.tintColor = UIColor.magenta
+
+ case .BgCheck:
+ self.iconImageView.tintColor = ConstantsGlucoseChart.bgCheckTreatmentColorInner
+
+ }
+
+ if #available(iOS 13.0, *) {
+
+ switch treatment.treatmentType {
+
+ case .Insulin:
+ self.iconImageView.image = UIImage(systemName: "arrowtriangle.down.fill")!
+
+ case .Carbs:
+ self.iconImageView.image = UIImage(systemName: "circle.fill")!
+
+ case .Exercise:
+ self.iconImageView.image = UIImage(systemName: "heart.fill")!
+
+ case .BgCheck:
+ self.iconImageView.image = UIImage(systemName: "drop.fill")!
+
+ }
+
+ } else {
+
+ self.iconImageView.image = nil
+
+ }
+
+
+ // treatment type label
+ self.typeLabel.text = treatment.treatmentType.asString()
+
+ // treatment value label
+ if treatment.treatmentType == .BgCheck {
+
+ // save typing
+ let isMgDl: Bool = UserDefaults.standard.bloodGlucoseUnitIsMgDl
+
+ // convert to mmol/l if needed, round accordingly and add the correct units
+ self.valueLabel.text = treatment.value.mgdlToMmol(mgdl: isMgDl).bgValueRounded(mgdl: isMgDl).stringWithoutTrailingZeroes
+
+ } else {
+
+ self.valueLabel.text = treatment.value.stringWithoutTrailingZeroes
+
+ }
+
+
+ // treatment unit label
+ if treatment.treatmentType == .BgCheck {
+
+ // convert to mmol/l if needed, round accordingly and add the correct units
+ self.unitLabel.text = String(UserDefaults.standard.bloodGlucoseUnitIsMgDl ? Texts_Common.mgdl : Texts_Common.mmol)
+
+ } else {
+
+ self.unitLabel.text = treatment.treatmentType.unit()
+
+ }
+
+ }
+
}