@@ -201,7 +201,7 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
201201 case manualEntryDoses( [ DoseEntry ] )
202202 }
203203
204- private enum HistorySection : Int {
204+ fileprivate enum HistorySection : Int {
205205 case today
206206 case yesterday
207207 }
@@ -401,7 +401,7 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
401401 return 0
402402 case . display:
403403 switch self . values {
404- case . history( let values ) : return values . valuesBeforeToday . isEmpty ? 1 : 2
404+ case . history( let pumpEvents ) : return pumpEvents . pumpEventsBeforeToday . isEmpty ? 1 : 2
405405 default : return 1
406406 }
407407 }
@@ -411,10 +411,10 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
411411 switch values {
412412 case . reservoir( let values) :
413413 return values. count
414- case . history( let values ) :
414+ case . history( let pumpEvents ) :
415415 switch HistorySection ( rawValue: section) {
416- case . today: return values . valuesFromToday . count
417- case . yesterday: return values . valuesBeforeToday . count
416+ case . today: return pumpEvents . pumpEventsFromToday . count
417+ case . yesterday: return pumpEvents . pumpEventsBeforeToday . count
418418 case . none: return 0
419419 }
420420 case . manualEntryDoses( let values) :
@@ -426,13 +426,13 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
426426 switch state {
427427 case . display:
428428 switch self . values {
429- case . history( let values ) :
429+ case . history( let pumpEvents ) :
430430 switch HistorySection ( rawValue: section) {
431431 case . today:
432- guard let firstValue = values . valuesFromToday . first else { return nil }
432+ guard let firstValue = pumpEvents . pumpEventsFromToday . first else { return nil }
433433 return dateFormatter. string ( from: firstValue. date) . uppercased ( )
434434 case . yesterday:
435- guard let firstValue = values . valuesBeforeToday . first else { return nil }
435+ guard let firstValue = pumpEvents . pumpEventsBeforeToday . first else { return nil }
436436 return dateFormatter. string ( from: firstValue. date) . uppercased ( )
437437 case . none: return nil
438438 }
@@ -457,24 +457,18 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
457457 cell. detailTextLabel? . text = time
458458 cell. accessoryType = . none
459459 cell. selectionStyle = . none
460- case . history( let values) :
461- let filterValues : [ PersistedPumpEvent ]
462- if HistorySection ( rawValue: indexPath. section) == . today {
463- filterValues = values. valuesFromToday
464- } else {
465- filterValues = values. valuesBeforeToday
466- }
467- let entry = filterValues [ indexPath. row]
468- let time = timeFormatter. string ( from: entry. date)
460+ case . history( let pumpEvents) :
461+ let pumpEvent = pumpEvents. pumpEventForIndexPath ( indexPath)
462+ let time = timeFormatter. string ( from: pumpEvent. date)
469463
470- if let attributedText = entry . localizedAttributedDescription {
464+ if let attributedText = pumpEvent . localizedAttributedDescription {
471465 cell. textLabel? . attributedText = attributedText
472466 } else {
473467 cell. textLabel? . text = NSLocalizedString ( " Unknown " , comment: " The default description to use when an entry has no dose description " )
474468 }
475469
476470 cell. detailTextLabel? . text = time
477- cell. accessoryType = entry . isUploaded ? . checkmark : . none
471+ cell. accessoryType = pumpEvent . isUploaded ? . checkmark : . none
478472 cell. selectionStyle = . default
479473 case . manualEntryDoses( let values) :
480474 let entry = values [ indexPath. row]
@@ -517,14 +511,13 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
517511 }
518512 }
519513 }
520- case . history( let historyValues) :
521- var historyValues = historyValues
522- let value = historyValues. remove ( at: indexPath. row)
523- self . values = . history( historyValues)
514+ case . history( let pumpEvents) :
515+ let pumpEvent = pumpEvents. pumpEventForIndexPath ( indexPath)
516+ self . values = . history( pumpEvents. filter { $0. dose != pumpEvent. dose } )
524517
525518 tableView. deleteRows ( at: [ indexPath] , with: . automatic)
526519
527- doseStore? . deletePumpEvent ( value ) { ( error) -> Void in
520+ doseStore? . deletePumpEvent ( pumpEvent ) { ( error) -> Void in
528521 if let error = error {
529522 DispatchQueue . main. async {
530523 self . present ( UIAlertController ( with: error) , animated: true )
@@ -555,23 +548,23 @@ public final class InsulinDeliveryTableViewController: UITableViewController {
555548 }
556549
557550 public override func tableView( _ tableView: UITableView , didSelectRowAt indexPath: IndexPath ) {
558- if case . display = state, case . history( let history ) = values {
559- let entry = history [ indexPath . row ]
551+ if case . display = state, case . history( let pumpEvents ) = values {
552+ let pumpEvent = pumpEvents . pumpEventForIndexPath ( indexPath )
560553
561554 let vc = CommandResponseViewController ( command: { ( completionHandler) -> String in
562555 var description = [ String] ( )
563556
564- description. append ( self . timeFormatter. string ( from: entry . date) )
557+ description. append ( self . timeFormatter. string ( from: pumpEvent . date) )
565558
566- if let title = entry . title {
559+ if let title = pumpEvent . title {
567560 description. append ( title)
568561 }
569562
570- if let dose = entry . dose {
563+ if let dose = pumpEvent . dose {
571564 description. append ( String ( describing: dose) )
572565 }
573566
574- if let raw = entry . raw {
567+ if let raw = pumpEvent . raw {
575568 description. append ( raw. hexadecimalString)
576569 }
577570
@@ -688,13 +681,23 @@ extension PersistedPumpEvent {
688681extension InsulinDeliveryTableViewController : IdentifiableClass { }
689682
690683fileprivate extension Array where Element == PersistedPumpEvent {
691- var valuesFromToday : [ PersistedPumpEvent ] {
684+ var pumpEventsFromToday : [ PersistedPumpEvent ] {
692685 let startOfDay = Calendar . current. startOfDay ( for: Date ( ) )
693686 return self . filter ( { $0. date >= startOfDay} )
694687 }
695688
696- var valuesBeforeToday : [ PersistedPumpEvent ] {
689+ var pumpEventsBeforeToday : [ PersistedPumpEvent ] {
697690 let startOfDay = Calendar . current. startOfDay ( for: Date ( ) )
698691 return self . filter ( { $0. date < startOfDay} )
699692 }
693+
694+ func pumpEventForIndexPath( _ indexPath: IndexPath ) -> PersistedPumpEvent {
695+ let filterPumpEvents : [ PersistedPumpEvent ]
696+ if InsulinDeliveryTableViewController . HistorySection ( rawValue: indexPath. section) == . today {
697+ filterPumpEvents = self . pumpEventsFromToday
698+ } else {
699+ filterPumpEvents = self . pumpEventsBeforeToday
700+ }
701+ return filterPumpEvents [ indexPath. row]
702+ }
700703}
0 commit comments