@@ -53,37 +53,48 @@ class HistoryStatisticsViewController: UIViewController, UITableViewDelegate, UI
5353 let cell = UITableViewCell ( style: . default, reuseIdentifier: " Cell " )
5454 cell. textLabel? . numberOfLines = 0
5555
56- let records = BatteryRecordDatabaseManager . shared. fetchAllRecords ( )
57- guard records. count >= 2 else {
58- cell. textLabel? . text = NSLocalizedString ( " NotEnoughData " , comment: " " )
59- return cell
60- }
61-
62- let first = records. last!
63- let last = records. first!
56+ do {
57+ let records = BatteryRecordDatabaseManager . shared. fetchAllRecords ( )
58+ guard records. count >= 2 else {
59+ throw NSError ( domain: " BatteryStats " , code: 1 , userInfo: nil )
60+ }
6461
65- let totalRecords = records. count
66- let timeInterval = max ( 1 , last. createDate - first. createDate)
67- let days = Double ( timeInterval) / 86400.0
68- let totalDays = String ( Int ( days) )
62+ guard let first = records. last, let last = records. first else {
63+ throw NSError ( domain: " BatteryStats " , code: 3 , userInfo: nil )
64+ }
6965
70- let firstHealth = ( Double ( first. nominalChargeCapacity ?? 0 ) / Double( first. designCapacity ?? 1 ) ) * 100
71- let lastHealth = ( Double ( last. nominalChargeCapacity ?? 0 ) / Double( last. designCapacity ?? 1 ) ) * 100
72- let deltaHealth = lastHealth - firstHealth
66+ let totalRecords = records. count
67+ let timeInterval = max ( 1 , last. createDate - first. createDate)
68+ let days = Double ( timeInterval) / 86400.0
69+ let totalDays = String ( Int ( days) )
7370
74- let deltaCapacity = ( last. nominalChargeCapacity ?? 0 ) - ( first. nominalChargeCapacity ?? 0 )
75- let deltaCycles = last. cycleCount - first. cycleCount
71+ let healthValues = records. map { $0. nominalChargeCapacity ?? 0 }
72+ guard let minHealth = healthValues. min ( ) , let maxHealth = healthValues. max ( ) , let designCapacity = last. designCapacity else {
73+ throw NSError ( domain: " BatteryStats " , code: 2 , userInfo: nil )
74+ }
75+
76+ guard designCapacity != 0 else {
77+ throw NSError ( domain: " BatteryStats " , code: 4 , userInfo: nil )
78+ }
7679
77- let avgCyclePerDay = Double ( deltaCycles) / days
80+ let deltaHealth = Double ( minHealth - maxHealth) / Double( designCapacity) * 100
81+ let deltaCapacity = minHealth - maxHealth
82+ let deltaCycles = last. cycleCount - first. cycleCount
83+ let avgCyclePerDay = Double ( deltaCycles) / days
7884
79- cell. textLabel? . text = String ( format: NSLocalizedString ( " BatteryHistorySummaryContent " , comment: " " ) ,
80- String ( totalRecords) ,
81- String ( totalDays) ,
82- String ( format: " %.1f " , deltaHealth) ,
83- String ( deltaCapacity) ,
84- String ( deltaCycles) ,
85- String ( format: " %.2f " , avgCyclePerDay)
86- )
85+ cell. textLabel? . text = String ( format: NSLocalizedString ( " BatteryHistorySummaryContent " , comment: " " ) ,
86+ String ( totalRecords) ,
87+ String ( totalDays) ,
88+ String ( format: " %.2f " , deltaHealth) ,
89+ String ( deltaCapacity) ,
90+ String ( maxHealth) ,
91+ String ( minHealth) ,
92+ String ( deltaCycles) ,
93+ String ( format: " %.2f " , avgCyclePerDay)
94+ )
95+ } catch {
96+ cell. textLabel? . text = NSLocalizedString ( " NotEnoughData " , comment: " " )
97+ }
8798
8899 return cell
89100 }
0 commit comments