Skip to content

Commit e91dd44

Browse files
committed
Refactor formattedRebootContent
1 parent 327860d commit e91dd44

File tree

7 files changed

+98
-72
lines changed

7 files changed

+98
-72
lines changed

SupportCompanion/Components/CardData.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,7 @@ struct CardData: View {
104104
}
105105

106106
private func rebootContent(value: Int) -> some View {
107-
var formattedLastRestart: String {
108-
if value >= 1440 { // 1440 minutes in a day
109-
let days = value / 1440
110-
return "\(days) \(Constants.General.daysAgo)"
111-
} else if value >= 60 { // More than an hour
112-
let hours = value / 60
113-
return "\(hours) \(Constants.General.hours)"
114-
} else { // Less than an hour
115-
return "\(value) \(Constants.General.minutes)"
116-
}
117-
}
107+
let formattedLastRestart = formattedRebootContent(value: value)
118108
return Text(formattedLastRestart)
119109
.foregroundColor(colorForLastRestart(value: value))
120110
.font(.system(size: fontSize ?? 14))

SupportCompanion/Constants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +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 dayAgo = String(localized: "General.DayAgo", defaultValue: "Day Ago", comment: "Number of day ago")
3940
static let daysAgo = String(localized: "General.DaysAgo", defaultValue: "Days Ago", comment: "Number of days ago")
4041
static let hours = String(localized: "General.Hours", defaultValue: "Hours", comment: "Number of hours")
4142
static let hour = String(localized: "General.Hour", defaultValue: "Hour", comment: "Number of hour")

SupportCompanion/Helpers/DeviceInfoHelpers.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,30 @@ class IPAddressMonitor {
198198
}
199199
}
200200

201+
func formattedRebootContent(value: Int) -> String {
202+
var formattedLastRestart: String {
203+
if value >= 1440 { // 1440 minutes in a day
204+
let days = value / 1440
205+
if days == 1 {
206+
return "\(days) \(Constants.General.dayAgo)"
207+
}
208+
return "\(days) \(Constants.General.daysAgo)"
209+
} else if value >= 60 { // More than an hour
210+
let hours = value / 60
211+
if hours == 1 {
212+
return "\(hours) \(Constants.General.hour)"
213+
}
214+
return "\(hours) \(Constants.General.hours)"
215+
} else { // Less than an hour
216+
if value == 1 {
217+
return "\(value) \(Constants.General.minute)"
218+
}
219+
return "\(value) \(Constants.General.minutes)"
220+
}
221+
}
222+
return formattedLastRestart
223+
}
224+
201225
class LastRebootMonitor {
202226
static let shared = LastRebootMonitor()
203227
private var updateHandler: ((Int) -> Void)?

SupportCompanion/Localizable.xcstrings

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,14 +2040,56 @@
20402040
}
20412041
}
20422042
},
2043+
"General.DayAgo" : {
2044+
"comment" : "Number of day ago",
2045+
"extractionState" : "extracted_with_value",
2046+
"localizations" : {
2047+
"de" : {
2048+
"stringUnit" : {
2049+
"state" : "translated",
2050+
"value" : "tag zuvor"
2051+
}
2052+
},
2053+
"en" : {
2054+
"stringUnit" : {
2055+
"state" : "new",
2056+
"value" : "Day Ago"
2057+
}
2058+
},
2059+
"fr" : {
2060+
"stringUnit" : {
2061+
"state" : "translated",
2062+
"value" : "jour avant"
2063+
}
2064+
},
2065+
"ja" : {
2066+
"stringUnit" : {
2067+
"state" : "translated",
2068+
"value" : "日前"
2069+
}
2070+
},
2071+
"nb" : {
2072+
"stringUnit" : {
2073+
"state" : "translated",
2074+
"value" : "dag siden"
2075+
}
2076+
},
2077+
"sv" : {
2078+
"stringUnit" : {
2079+
"state" : "translated",
2080+
"value" : "dag sedan"
2081+
}
2082+
}
2083+
}
2084+
},
20432085
"General.Days" : {
20442086
"comment" : "Number of days",
20452087
"extractionState" : "manual",
20462088
"localizations" : {
20472089
"de" : {
20482090
"stringUnit" : {
20492091
"state" : "translated",
2050-
"value" : "Tage"
2092+
"value" : "tage"
20512093
}
20522094
},
20532095
"en" : {
@@ -2059,7 +2101,7 @@
20592101
"fr" : {
20602102
"stringUnit" : {
20612103
"state" : "translated",
2062-
"value" : "Jours"
2104+
"value" : "jours"
20632105
}
20642106
},
20652107
"ja" : {
@@ -2071,7 +2113,7 @@
20712113
"nb" : {
20722114
"stringUnit" : {
20732115
"state" : "translated",
2074-
"value" : "Dager"
2116+
"value" : "dager"
20752117
}
20762118
},
20772119
"sv" : {
@@ -2089,7 +2131,7 @@
20892131
"de" : {
20902132
"stringUnit" : {
20912133
"state" : "translated",
2092-
"value" : "Vor Tagen"
2134+
"value" : "vor tagen"
20932135
}
20942136
},
20952137
"en" : {
@@ -2101,7 +2143,7 @@
21012143
"fr" : {
21022144
"stringUnit" : {
21032145
"state" : "translated",
2104-
"value" : "Jours avant"
2146+
"value" : "jours avant"
21052147
}
21062148
},
21072149
"ja" : {
@@ -2173,7 +2215,7 @@
21732215
"de" : {
21742216
"stringUnit" : {
21752217
"state" : "translated",
2176-
"value" : "Stunde"
2218+
"value" : "stunde"
21772219
}
21782220
},
21792221
"en" : {
@@ -2185,7 +2227,7 @@
21852227
"fr" : {
21862228
"stringUnit" : {
21872229
"state" : "translated",
2188-
"value" : "Heure"
2230+
"value" : "heure"
21892231
}
21902232
},
21912233
"ja" : {
@@ -2197,13 +2239,13 @@
21972239
"nb" : {
21982240
"stringUnit" : {
21992241
"state" : "translated",
2200-
"value" : "Time"
2242+
"value" : "time"
22012243
}
22022244
},
22032245
"sv" : {
22042246
"stringUnit" : {
22052247
"state" : "translated",
2206-
"value" : "Timme"
2248+
"value" : "timme"
22072249
}
22082250
}
22092251
}
@@ -2215,7 +2257,7 @@
22152257
"de" : {
22162258
"stringUnit" : {
22172259
"state" : "translated",
2218-
"value" : "Stunden"
2260+
"value" : "stunden"
22192261
}
22202262
},
22212263
"en" : {
@@ -2227,7 +2269,7 @@
22272269
"fr" : {
22282270
"stringUnit" : {
22292271
"state" : "translated",
2230-
"value" : "Heures"
2272+
"value" : "heures"
22312273
}
22322274
},
22332275
"ja" : {
@@ -2239,13 +2281,13 @@
22392281
"nb" : {
22402282
"stringUnit" : {
22412283
"state" : "translated",
2242-
"value" : "Timer"
2284+
"value" : "timer"
22432285
}
22442286
},
22452287
"sv" : {
22462288
"stringUnit" : {
22472289
"state" : "translated",
2248-
"value" : "Timmar"
2290+
"value" : "timmar"
22492291
}
22502292
}
22512293
}
@@ -2299,7 +2341,7 @@
22992341
"de" : {
23002342
"stringUnit" : {
23012343
"state" : "translated",
2302-
"value" : "Minuten"
2344+
"value" : "minuten"
23032345
}
23042346
},
23052347
"en" : {
@@ -2317,13 +2359,13 @@
23172359
"nb" : {
23182360
"stringUnit" : {
23192361
"state" : "translated",
2320-
"value" : "Minutt"
2362+
"value" : "minutt"
23212363
}
23222364
},
23232365
"sv" : {
23242366
"stringUnit" : {
23252367
"state" : "translated",
2326-
"value" : "Minut"
2368+
"value" : "minut"
23272369
}
23282370
}
23292371
}
@@ -2335,7 +2377,7 @@
23352377
"de" : {
23362378
"stringUnit" : {
23372379
"state" : "translated",
2338-
"value" : "Minuten"
2380+
"value" : "minuten"
23392381
}
23402382
},
23412383
"en" : {
@@ -2353,7 +2395,7 @@
23532395
"nb" : {
23542396
"stringUnit" : {
23552397
"state" : "translated",
2356-
"value" : "Minutter"
2398+
"value" : "minutter"
23572399
}
23582400
},
23592401
"sv" : {
@@ -2371,7 +2413,7 @@
23712413
"de" : {
23722414
"stringUnit" : {
23732415
"state" : "translated",
2374-
"value" : "Sekunde"
2416+
"value" : "sekunde"
23752417
}
23762418
},
23772419
"en" : {
@@ -2383,7 +2425,7 @@
23832425
"fr" : {
23842426
"stringUnit" : {
23852427
"state" : "translated",
2386-
"value" : "Seconde"
2428+
"value" : "seconde"
23872429
}
23882430
},
23892431
"ja" : {
@@ -2395,13 +2437,13 @@
23952437
"nb" : {
23962438
"stringUnit" : {
23972439
"state" : "translated",
2398-
"value" : "Sekund"
2440+
"value" : "sekund"
23992441
}
24002442
},
24012443
"sv" : {
24022444
"stringUnit" : {
24032445
"state" : "translated",
2404-
"value" : "Sekund"
2446+
"value" : "sekund"
24052447
}
24062448
}
24072449
}
@@ -2413,7 +2455,7 @@
24132455
"de" : {
24142456
"stringUnit" : {
24152457
"state" : "translated",
2416-
"value" : "Sekunden"
2458+
"value" : "sekunden"
24172459
}
24182460
},
24192461
"en" : {
@@ -2425,7 +2467,7 @@
24252467
"fr" : {
24262468
"stringUnit" : {
24272469
"state" : "translated",
2428-
"value" : "Secondes"
2470+
"value" : "secondes"
24292471
}
24302472
},
24312473
"ja" : {
@@ -2437,13 +2479,13 @@
24372479
"nb" : {
24382480
"stringUnit" : {
24392481
"state" : "translated",
2440-
"value" : "Sekunder"
2482+
"value" : "sekunder"
24412483
}
24422484
},
24432485
"sv" : {
24442486
"stringUnit" : {
24452487
"state" : "translated",
2446-
"value" : "Sekunder"
2488+
"value" : "sekunder"
24472489
}
24482490
}
24492491
}

SupportCompanion/Views/Cards/DeviceInformationCard.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,8 @@ 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-
164152
var body: some View {
153+
let formattedLastRestart = formattedRebootContent(value: value)
165154
let color = colorForLastRestart(value: value)
166155

167156
HStack {

SupportCompanion/Views/TransparentView.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,19 +351,9 @@ struct LastRestartRowTransparent: View {
351351
let value: Int // Days since last restart
352352
let fontSize: CGFloat
353353

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-
}
365-
366354
var body: some View {
355+
let formattedLastRestart = formattedRebootContent(value: value)
356+
367357
HStack {
368358
Text(label)
369359
.font(.system(size: fontSize))

SupportCompanionCLI/main.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,8 @@ struct SupportCompanionCLI {
139139
let lastReboot = getLastRestartMinutes() ?? 0
140140
let osVersion = getOSVersion()
141141
let osBuild = getOSBuild()
142+
let formattedLastRestart = formattedRebootContent(value: lastReboot)
142143

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-
}
154144

155145
print("""
156146
💻 Device Information

0 commit comments

Comments
 (0)