Skip to content

Commit f354145

Browse files
author
DevelopLab
committed
1. Improve History Statistics
2. Improve some UI
1 parent 66505bf commit f354145

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

BatteryInfo/Localizable.xcstrings

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,17 @@
260260
}
261261
},
262262
"BatteryHistorySummaryContent" : {
263-
"comment" : "Battery statistics summary with total records, health delta, capacity delta, cycle delta, and avg cycles per day.",
264263
"localizations" : {
265264
"en" : {
266265
"stringUnit" : {
267266
"state" : "translated",
268-
"value" : "Total Records: %@ items\nRecorded over %@ days\nBattery Health Change: %@%%\nCapacity Change: %@ mAh\nCycle Count Increase: %@ time(s)\nAvg. Cycles per Day: %@ time(s)"
267+
"value" : "Total Records: %@ items\nRecorded over %@ days\nBattery Health Change: %@%%\nCapacity Change: %@ mAh\nRecorded maximum capacity: %@ mAh\nRecorded minimum capacity: %@ mAh\nCycle Count Increase: %@ time(s)\nAvg. Cycles per Day: %@ time(s)"
269268
}
270269
},
271270
"zh-Hans" : {
272271
"stringUnit" : {
273272
"state" : "translated",
274-
"value" : "总记录数: %@条\n累计记录%@天\n电池健康度变化: %@%%\n电池容量变化: %@ mAh\n循环次数增加: %@次\n平均每日循环次数: %@次"
273+
"value" : "总记录数: %@条\n累计记录%@天\n电池健康度变化: %@%%\n电池容量变化: %@ mAh\n记录中最高容量: %@ mAh\n记录中最低容量: %@ mAh\n循环次数增加: %@次\n平均每日循环次数: %@次"
275274
}
276275
}
277276
}

BatteryInfo/ViewController/DisplaySettingsViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ class DisplaySettingsViewController: UIViewController, UITableViewDelegate, UITa
119119
} else if indexPath.row == 3 {
120120
switchView.isOn = SettingsUtils.instance.getUseHistoryRecordToCalculateSettingsBatteryInfoRefreshDate()
121121
switchView.isEnabled = SettingsUtils.instance.getEnableRecordBatteryData()
122+
if !SettingsUtils.instance.getEnableRecordBatteryData() {
123+
cell.textLabel?.textColor = .lightGray // 文本设置成灰色
124+
}
122125
cell.isUserInteractionEnabled = SettingsUtils.instance.getEnableRecordBatteryData()
123126
}else if indexPath.row == 4 {
124127
switchView.isOn = SettingsUtils.instance.getDoubleClickTabBarButtonToScrollToTop()

BatteryInfo/ViewController/HistoryStatisticsViewController.swift

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)