Skip to content

Commit 8b2751a

Browse files
committed
Add last reboot monitor
1 parent 22b308f commit 8b2751a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

SupportCompanion/Helpers/DeviceInfoHelpers.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func getLastRebootDays() -> Int? {
8383
// Calculate the difference in days
8484
let calendar = Calendar.current
8585
let daysSinceReboot = calendar.dateComponents([.day], from: bootDate, to: currentDate).day
86-
86+
8787
return daysSinceReboot
8888
}
8989

@@ -180,3 +180,33 @@ class IPAddressMonitor {
180180
monitor.cancel()
181181
}
182182
}
183+
184+
class LastRebootMonitor {
185+
static let shared = LastRebootMonitor()
186+
private var updateHandler: ((Int) -> Void)?
187+
188+
private init() {}
189+
190+
func startMonitoring(onUpdate: @escaping (Int) -> Void) {
191+
self.updateHandler = onUpdate
192+
193+
// Check if 24 hours have passed since the last update
194+
let lastRunKey = "LastRebootMonitorLastRun"
195+
let defaults = UserDefaults.standard
196+
let now = Date()
197+
198+
if let lastRun = defaults.object(forKey: lastRunKey) as? Date, now.timeIntervalSince(lastRun) < 86400 {
199+
// 24 hours haven't passed, skip this run
200+
return
201+
}
202+
203+
// Update the last run time
204+
defaults.set(now, forKey: lastRunKey)
205+
206+
// Perform the reboot check
207+
let lastRebootDays = getLastRebootDays()
208+
DispatchQueue.main.async {
209+
self.updateHandler?(lastRebootDays ?? 0)
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)