Skip to content

Commit 05cc42a

Browse files
author
DevelopLab
committed
1. Improve widget refresh data process
2. Improve data refresh
1 parent 13a4f1b commit 05cc42a

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

BatteryInfo/AppDelegate.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
window!.makeKeyAndVisible()
1919
return true
2020
}
21+
22+
// app从后台返回的时候就自动刷新下数据
23+
func applicationDidBecomeActive(_ application: UIApplication) {
24+
BatteryDataController.getInstance.refreshBatteryInfo()
25+
}
2126

2227
}

BatteryInfo/Controller/BatteryDataController.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,26 @@ class BatteryDataController {
2525
batteryInfo = provider.fetchBatteryInfo()
2626

2727
// 顺便就记录电池的历史记录了
28+
if recordBatteryData(manualRecord: false) {
29+
NSLog("历史记录增加新的记录成功")
30+
}
31+
32+
// 刷新widget的数据
33+
refreshWidgetData(forceReload: false)
34+
}
35+
36+
// 新增一个方法这样可以允许用户强制刷新widget
37+
func refreshWidgetData(forceReload: Bool) {
38+
2839
if let cycleCount = batteryInfo?.cycleCount {
29-
30-
if recordBatteryData(manualRecord: false) {
31-
print("历史记录增加新的记录成功")
32-
}
33-
3440
// 提供给小组件的电池数据
3541
if #available(iOS 14.0, *) {
3642
if settingsUtils.getEnableWidget() {
37-
WidgetController.instance.setWidgetBatteryData(batteryData: WidgetBatteryData(maximumCapacity: self.calculateMaximumCapacity() ?? "--%", cycleCount: cycleCount))
43+
WidgetController.instance.setWidgetBatteryData(forceReload: forceReload, batteryData: WidgetBatteryData(maximumCapacity: self.calculateMaximumCapacity() ?? "--%", cycleCount: cycleCount))
3844
}
3945
}
40-
4146
}
47+
4248
}
4349

4450
func getBatteryRAWInfo() -> [String: Any]? {

BatteryInfo/Controller/WidgetController.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class WidgetController {
7979
}
8080

8181
// 给主程序保存电池数据
82-
func setWidgetBatteryData(batteryData: WidgetBatteryData) {
82+
func setWidgetBatteryData(forceReload: Bool, batteryData: WidgetBatteryData) {
8383

8484
// 主程序设置的实例
8585
let settingsUtils = SettingsUtils.instance
@@ -136,6 +136,11 @@ class WidgetController {
136136
shouldRefresh = true
137137
}
138138
}
139+
140+
if forceReload { // 给强制刷新widget做的优化
141+
shouldWrite = true
142+
shouldRefresh = true
143+
}
139144

140145
if shouldWrite {
141146
dataManager.setString(key: "maximumCapacity", value: batteryData.maximumCapacity)

BatteryInfo/ViewController/WidgetSettingsViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ class WidgetSettingsViewController: UIViewController, UITableViewDelegate, UITab
154154
// 设置当前的cell选中状态
155155
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
156156
} else if indexPath.section == 2 {
157-
// 请求系统刷新Widget
158-
widgetController.refreshWidget()
157+
// 重新写入最新数据并且请求系统刷新widget
158+
BatteryDataController.getInstance.refreshWidgetData(forceReload: true)
159159

160160
let alert = UIAlertController(
161161
title: NSLocalizedString("Alert", comment: ""),

0 commit comments

Comments
 (0)