Skip to content

Commit 772e73f

Browse files
committed
Change last reboot to minutes, hours and days
1 parent b97acc5 commit 772e73f

File tree

8 files changed

+93
-31
lines changed

8 files changed

+93
-31
lines changed

SupportCompanion/Components/CardData.swift

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct CardData: View {
4949
case Constants.Battery.Keys.health:
5050
healthContent(value: value)
5151
case Constants.DeviceInfo.Keys.lastRestart:
52-
daysContent(value: value, suffix: " \(Constants.General.days)", color: colorForValue(key: key, value: value))
52+
rebootContent(value: value.rawValue as? Int ?? 0)
5353
case "FileVault":
5454
fileVaultContent(value: value)
5555
case Constants.KerberosSSO.Keys.expiryDays:
@@ -100,6 +100,35 @@ struct CardData: View {
100100
+ Text(suffix)
101101
.font(.system(size: fontSize ?? 14))
102102
}
103+
104+
private func rebootContent(value: Int) -> some View {
105+
var formattedLastRestart: String {
106+
if value >= 1440 { // 1440 minutes in a day
107+
let days = value / 1440
108+
return "\(days) \(Constants.General.daysAgo)"
109+
} else if value >= 60 { // More than an hour
110+
let hours = value / 60
111+
return "\(hours) \(Constants.General.hours)"
112+
} else { // Less than an hour
113+
return "\(value) \(Constants.General.minutes)"
114+
}
115+
}
116+
return Text(formattedLastRestart)
117+
.foregroundColor(colorForLastRestart(value: value))
118+
.font(.system(size: fontSize ?? 14))
119+
}
120+
121+
private func colorForLastRestart(value: Int) -> Color {
122+
let days = value / 1440
123+
switch days {
124+
case 0...2:
125+
return .ScGreen
126+
case 3...7:
127+
return colorScheme == .light ? .orangeLight : .orange
128+
default:
129+
return colorScheme == .light ? .redLight : .red
130+
}
131+
}
103132

104133
private func pssoRegistrationContent(value: InfoValue) -> some View {
105134
return Text(value.displayValue)
@@ -140,17 +169,6 @@ struct CardData: View {
140169
? (colorScheme == .light ? .orangeLight : .orange)
141170
: .ScGreen)
142171
}
143-
case "LastRestart":
144-
if let intValue = value.rawValue as? Int {
145-
switch intValue {
146-
case 0...2:
147-
return .ScGreen
148-
case 3...7:
149-
return colorScheme == .light ? .orangeLight : .orange
150-
default:
151-
return colorScheme == .light ? .redLight : .red
152-
}
153-
}
154172
case "FileVault":
155173
if let boolValue = value.rawValue as? Bool {
156174
return !boolValue ? (colorScheme == .light ? .redLight : .red) : .ScGreen

SupportCompanion/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ enum Constants {
3636

3737
enum General {
3838
static let days = String(localized: "General.Days", defaultValue: "Days", comment: "Number of days")
39-
static let daysAgo = String(localized: "General.DaysAgo", defaultValue: "Days ago", comment: "Number of days ago")
39+
static let daysAgo = String(localized: "General.DaysAgo", defaultValue: "Days Ago", comment: "Number of days ago")
4040
static let hours = String(localized: "General.Hours", defaultValue: "Hours", comment: "Number of hours")
4141
static let hour = String(localized: "General.Hour", defaultValue: "Hour", comment: "Number of hour")
4242
static let minute = String(localized: "General.Minute", defaultValue: "Minute", comment: "Number of minute")

SupportCompanion/Helpers/DeviceInfoHelpers.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ func getLastRebootDays() -> Int? {
8787
return daysSinceReboot
8888
}
8989

90+
func getLastRestartMinutes() -> Int? {
91+
var mib = [CTL_KERN, KERN_BOOTTIME]
92+
var bootTime = timeval()
93+
var size = MemoryLayout<timeval>.stride
94+
95+
let result = sysctl(&mib, 2, &bootTime, &size, nil, 0)
96+
guard result == 0 else {
97+
return nil
98+
}
99+
100+
let bootDate = Date(timeIntervalSince1970: TimeInterval(bootTime.tv_sec))
101+
let currentDate = Date()
102+
103+
let elapsedMinutes = Int(currentDate.timeIntervalSince(bootDate) / 60)
104+
return elapsedMinutes
105+
}
106+
90107
func getModelName() -> String {
91108
return getPropertyValue(forKey: "product-name", service: "product") ?? "Unknown"
92109
}
@@ -191,7 +208,7 @@ class LastRebootMonitor {
191208
self.updateHandler = onUpdate
192209

193210
// Perform the reboot check
194-
let lastRebootDays = getLastRebootDays()
211+
let lastRebootDays = getLastRestartMinutes()
195212
DispatchQueue.main.async {
196213
self.updateHandler?(lastRebootDays ?? 0)
197214
}

SupportCompanion/Localizable.xcstrings

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@
7777
},
7878
"%lld" : {
7979

80-
},
81-
"%lld %@" : {
82-
"localizations" : {
83-
"en" : {
84-
"stringUnit" : {
85-
"state" : "new",
86-
"value" : "%1$lld %2$@"
87-
}
88-
}
89-
}
9080
},
9181
"%lld%%" : {
9282

SupportCompanion/ViewModels/DeviceInfoManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DeviceInfoManager: ObservableObject {
3737
Logger.shared.logDebug("Starting device info monitoring")
3838
timer?.invalidate()
3939
refresh()
40-
timer = Timer.scheduledTimer(withTimeInterval: 28800, repeats: true) { _ in
40+
timer = Timer.scheduledTimer(withTimeInterval: 300, repeats: true) { _ in
4141
self.refresh()
4242
}
4343
}
@@ -59,7 +59,7 @@ class DeviceInfoManager: ObservableObject {
5959
ram: getRAMSize(),
6060
ipAddress: currentIPAddress,
6161
serialNumber: getSerialNumber(),
62-
lastRestart: getLastRebootDays() ?? 0,
62+
lastRestart: getLastRestartMinutes() ?? 0,
6363
model: getModelName()
6464
)
6565
}

SupportCompanion/Views/Cards/DeviceInformationCard.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ struct LastRestartRow: View {
149149
let value: Int // Days since last restart
150150
let colorScheme: ColorScheme
151151

152+
private var formattedLastRestart: String {
153+
if value >= 1440 { // 1440 minutes in a day
154+
let days = value / 1440
155+
return "\(days) \(Constants.General.daysAgo)"
156+
} else if value >= 60 { // More than an hour
157+
let hours = value / 60
158+
return "\(hours) \(Constants.General.hours)"
159+
} else { // Less than an hour
160+
return "\(value) \(Constants.General.minutes)"
161+
}
162+
}
163+
152164
var body: some View {
153165
let color = colorForLastRestart(value: value)
154166

@@ -160,7 +172,7 @@ struct LastRestartRow: View {
160172

161173
Spacer()
162174
HStack(spacing: 5) {
163-
Text("\(value) \(Constants.General.days)")
175+
Text(formattedLastRestart)
164176
.foregroundColor(color)
165177
Image(systemName: "clock.fill")
166178
.foregroundColor(color)
@@ -171,7 +183,8 @@ struct LastRestartRow: View {
171183
}
172184

173185
private func colorForLastRestart(value: Int) -> Color {
174-
switch value {
186+
let days = value / 1440
187+
switch days {
175188
case 0...2:
176189
return .ScGreen
177190
case 3...7:

SupportCompanion/Views/TransparentView.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,18 @@ struct LastRestartRowTransparent: View {
350350
let label: String
351351
let value: Int // Days since last restart
352352
let fontSize: CGFloat
353+
354+
private var formattedLastRestart: String {
355+
if value >= 1440 { // 1440 minutes in a day
356+
let days = value / 1440
357+
return "\(days) \(Constants.General.daysAgo)"
358+
} else if value >= 60 { // More than an hour
359+
let hours = value / 60
360+
return "\(hours) \(Constants.General.hours)"
361+
} else { // Less than an hour
362+
return "\(value) \(Constants.General.minutes)"
363+
}
364+
}
353365

354366
var body: some View {
355367
HStack {
@@ -358,7 +370,7 @@ struct LastRestartRowTransparent: View {
358370
.bold()
359371
Spacer()
360372
HStack(spacing: 5) {
361-
Text("\(value) \(Constants.General.days)")
373+
Text(formattedLastRestart)
362374
.shadow(radius: 2)
363375
Image(systemName: "clock.fill")
364376
}

SupportCompanionCLI/main.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,22 @@ struct SupportCompanionCLI {
136136
let serial = getSerialNumber() ?? "Unknown"
137137
let processor = getCPUName() ?? "Unknown"
138138
let ip = getAllIPAddresses().joined(separator: ", ")
139-
let lastReboot = getLastRebootDays() ?? 0
139+
let lastReboot = getLastRestartMinutes() ?? 0
140140
let osVersion = getOSVersion()
141141
let osBuild = getOSBuild()
142142

143+
var formattedLastRestart: String {
144+
if lastReboot >= 1440 { // 1440 minutes in a day
145+
let days = lastReboot / 1440
146+
return "\(days) \(Constants.General.daysAgo)"
147+
} else if lastReboot >= 60 { // More than an hour
148+
let hours = lastReboot / 60
149+
return "\(hours) \(Constants.General.hours)"
150+
} else { // Less than an hour
151+
return "\(lastReboot) \(Constants.General.minutes)"
152+
}
153+
}
154+
143155
print("""
144156
💻 Device Information
145157
-----------------------
@@ -149,7 +161,7 @@ struct SupportCompanionCLI {
149161
Processor: \(processor)
150162
Memory: \(ram)
151163
IP Address(es): \(ip)
152-
Last Reboot: \(lastReboot) days ago
164+
Last Reboot: \(formattedLastRestart)
153165
OS Version: \(osVersion)
154166
OS Build: \(osBuild)
155167
""")

0 commit comments

Comments
 (0)