Skip to content

Commit 1f57fad

Browse files
authored
Use pump event title (#506)
1 parent 20eddc2 commit 1f57fad

File tree

1 file changed

+57
-51
lines changed

1 file changed

+57
-51
lines changed

Loop/View Controllers/InsulinDeliveryTableViewController.swift

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
451451
let entry = values[indexPath.row]
452452
let time = timeFormatter.string(from: entry.date)
453453

454-
if let attributedText = entry.dose?.localizedAttributedDescription {
455-
cell.textLabel?.attributedText = attributedText
454+
if let attributedText = entry.localizedAttributedDescription {
455+
cell.textLabel?.attributedText = attributedText
456456
} else {
457457
cell.textLabel?.text = NSLocalizedString("Unknown", comment: "The default description to use when an entry has no dose description")
458458
}
@@ -463,12 +463,12 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
463463
case .manualEntryDoses(let values):
464464
let entry = values[indexPath.row]
465465
let time = timeFormatter.string(from: entry.startDate)
466+
let font = UIFont.preferredFont(forTextStyle: .body)
466467

467-
if let attributedText = entry.localizedAttributedDescription {
468-
cell.textLabel?.attributedText = attributedText
469-
} else {
470-
cell.textLabel?.text = NSLocalizedString("Unknown", comment: "The default description to use when an entry has no dose description")
471-
}
468+
let description = String(format: NSLocalizedString("Manual Dose: <b>%1$@</b> %2$@", comment: "Description of a bolus dose entry (1: value (? if no value) in bold, 2: unit)"), numberFormatter.string(from: entry.programmedUnits) ?? "?", DoseEntry.units.shortLocalizedUnitString(avoidLineBreaking: false))
469+
470+
let attributedDescription = createAttributedDescription(from: description, with: font)
471+
cell.textLabel?.attributedText = attributedDescription
472472
cell.detailTextLabel?.text = time
473473
cell.selectionStyle = .default
474474
}
@@ -600,60 +600,66 @@ fileprivate extension UIAlertController {
600600
}
601601
}
602602

603-
extension DoseEntry {
604-
605-
fileprivate var numberFormatter: NumberFormatter {
606-
let numberFormatter = NumberFormatter()
607-
numberFormatter.maximumFractionDigits = DoseEntry.unitsPerHour.maxFractionDigits
608-
return numberFormatter
609-
}
603+
fileprivate var numberFormatter: NumberFormatter {
604+
let numberFormatter = NumberFormatter()
605+
numberFormatter.maximumFractionDigits = DoseEntry.unitsPerHour.maxFractionDigits
606+
return numberFormatter
607+
}
610608

611-
fileprivate var localizedAttributedDescription: NSAttributedString? {
612-
let font = UIFont.preferredFont(forTextStyle: .body)
609+
fileprivate func createAttributedDescription(from description: String, with font: UIFont) -> NSAttributedString? {
610+
let descriptionWithFont = String(format:"<style>body{font-family: '-apple-system', '\(font.fontName)'; font-size: \(font.pointSize);}</style>%@", description)
613611

614-
switch type {
615-
case .bolus:
616-
let description: String
617-
if let deliveredUnits = deliveredUnits,
618-
deliveredUnits != programmedUnits
619-
{
620-
description = String(format: NSLocalizedString("Interrupted %1$@: <b>%2$@</b> of %3$@ %4$@", comment: "Description of an interrupted bolus dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: programmed value (? if no value), 4: unit)"), type.localizedDescription, numberFormatter.string(from: deliveredUnits) ?? "?", numberFormatter.string(from: programmedUnits) ?? "?", DoseEntry.units.shortLocalizedUnitString())
621-
} else {
622-
description = String(format: NSLocalizedString("%1$@: <b>%2$@</b> %3$@", comment: "Description of a bolus dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: unit)"), type.localizedDescription, numberFormatter.string(from: programmedUnits) ?? "?", DoseEntry.units.shortLocalizedUnitString(avoidLineBreaking: false))
623-
}
612+
guard let attributedDescription = try? NSMutableAttributedString(data: Data(descriptionWithFont.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
613+
return nil
614+
}
624615

625-
return createAttributedDescription(from: description, with: font)
626-
case .basal, .tempBasal:
627-
let description = String(format: NSLocalizedString("%1$@: <b>%2$@</b> %3$@", comment: "Description of a basal temp basal dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: unit)"), type.localizedDescription, numberFormatter.string(from: unitsPerHour) ?? "?", DoseEntry.unitsPerHour.shortLocalizedUnitString(avoidLineBreaking: false))
628-
return createAttributedDescription(from: description, with: font)
629-
case .suspend, .resume:
630-
let attributes: [NSAttributedString.Key: Any] = [
631-
.font: font,
632-
.foregroundColor: UIColor.secondaryLabel
633-
]
634-
return NSAttributedString(string: type.localizedDescription, attributes: attributes)
616+
attributedDescription.enumerateAttribute(.font, in: NSRange(location: 0, length: attributedDescription.length)) { value, range, stop in
617+
// bold font items have a dominate colour
618+
if let font = value as? UIFont,
619+
font.fontDescriptor.symbolicTraits.contains(.traitBold)
620+
{
621+
attributedDescription.addAttributes([.foregroundColor: UIColor.label], range: range)
622+
} else {
623+
attributedDescription.addAttributes([.foregroundColor: UIColor.secondaryLabel], range: range)
635624
}
636625
}
637626

638-
fileprivate func createAttributedDescription(from description: String, with font: UIFont) -> NSAttributedString? {
639-
let descriptionWithFont = String(format:"<style>body{font-family: '-apple-system', '\(font.fontName)'; font-size: \(font.pointSize);}</style>%@", description)
627+
return attributedDescription
628+
}
640629

641-
guard let attributedDescription = try? NSMutableAttributedString(data: Data(descriptionWithFont.utf8), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
642-
return nil
643-
}
630+
extension PersistedPumpEvent {
644631

645-
attributedDescription.enumerateAttribute(.font, in: NSRange(location: 0, length: attributedDescription.length)) { value, range, stop in
646-
// bold font items have a dominate colour
647-
if let font = value as? UIFont,
648-
font.fontDescriptor.symbolicTraits.contains(.traitBold)
649-
{
650-
attributedDescription.addAttributes([.foregroundColor: UIColor.label], range: range)
651-
} else {
652-
attributedDescription.addAttributes([.foregroundColor: UIColor.secondaryLabel], range: range)
632+
fileprivate var localizedAttributedDescription: NSAttributedString? {
633+
let font = UIFont.preferredFont(forTextStyle: .body)
634+
635+
let eventTitle = title ?? NSLocalizedString("Unknown", comment: "Event title displayed when StoredPumpEvent.title is not set")
636+
637+
if let dose = dose {
638+
switch dose.type {
639+
case .bolus:
640+
let description: String
641+
if let deliveredUnits = dose.deliveredUnits,
642+
deliveredUnits != dose.programmedUnits
643+
{
644+
description = String(format: NSLocalizedString("Interrupted %1$@: <b>%2$@</b> of %3$@ %4$@", comment: "Description of an interrupted bolus dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: programmed value (? if no value), 4: unit)"), eventTitle, numberFormatter.string(from: deliveredUnits) ?? "?", numberFormatter.string(from: dose.programmedUnits) ?? "?", DoseEntry.units.shortLocalizedUnitString())
645+
} else {
646+
description = String(format: NSLocalizedString("%1$@: <b>%2$@</b> %3$@", comment: "Description of a bolus dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: unit)"), eventTitle, numberFormatter.string(from: dose.programmedUnits) ?? "?", DoseEntry.units.shortLocalizedUnitString(avoidLineBreaking: false))
647+
}
648+
649+
return createAttributedDescription(from: description, with: font)
650+
case .basal, .tempBasal:
651+
let description = String(format: NSLocalizedString("%1$@: <b>%2$@</b> %3$@", comment: "Description of a basal temp basal dose entry (1: title for dose type, 2: value (? if no value) in bold, 3: unit)"), eventTitle, numberFormatter.string(from: dose.unitsPerHour) ?? "?", DoseEntry.unitsPerHour.shortLocalizedUnitString(avoidLineBreaking: false))
652+
return createAttributedDescription(from: description, with: font)
653+
case .suspend, .resume:
654+
let attributes: [NSAttributedString.Key: Any] = [
655+
.font: font,
656+
.foregroundColor: UIColor.secondaryLabel
657+
]
658+
return NSAttributedString(string: eventTitle, attributes: attributes)
653659
}
660+
} else {
661+
return createAttributedDescription(from: eventTitle, with: font)
654662
}
655-
656-
return attributedDescription
657663
}
658664
}
659665

0 commit comments

Comments
 (0)