Skip to content

Commit

Permalink
minor error handling improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepistrol committed Oct 18, 2024
1 parent 1e436bc commit da0c0ba
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ custom_rules:
message: "Use `SFSafeSymbols` via `systemSymbol` parameters for type safety."
regex: "(Image\\(systemName:)|(NSImage\\(symbolName:)|(Label[^,]+?,\\s*systemImage:)|(UIApplicationShortcutIcon\\(systemImageName:)"
severity: warning
no_print_statements:
name: "No print statements"
message: "Use the `Logging` package instead."
regex: "(print\\(.*\\))|(debugPrint\\(.*\\))"
severity: warning
2 changes: 1 addition & 1 deletion TimeMachineStatus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
28FAD5A62AFE3DBF00F642E7 /* CardViewModifier.swift */,
28FAD5AC2AFF0D7200F642E7 /* ExpandableSection.swift */,
28FAD5AE2AFF0D9600F642E7 /* UserfacingErrorView.swift */,
281C819D2CBC887D00CDFED6 /* HiddenViewModifier+.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -281,7 +282,6 @@
281822662AFD86AC0067E564 /* Color+RawRepresentable.swift */,
2888D17B2C99B3E80081FBBB /* KeyedDecodingContainer+.swift */,
28710D2E2CAF16AF00033855 /* Timer+Sendable.swift */,
281C819D2CBC887D00CDFED6 /* HiddenViewModifier+.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down
6 changes: 6 additions & 0 deletions TimeMachineStatus/Components/CustomLabeledContentStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ struct CustomLabeledContentStyle: LabeledContentStyle {
}
}
}

extension LabeledContentStyle where Self == CustomLabeledContentStyle {
static var custom: CustomLabeledContentStyle {
CustomLabeledContentStyle()
}
}
12 changes: 12 additions & 0 deletions TimeMachineStatus/Components/ExpandableSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ struct ExpandableSection<C: View, H: View>: View {
}
}
}

#Preview {
List {
ExpandableSection {
Text("Item 1")
Text("Item 2")
Text("Item 3")
} header: {
Text("Header")
}
}
}
59 changes: 55 additions & 4 deletions TimeMachineStatus/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,20 @@
}
},
"D" : {
"extractionState" : "manual",
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "D"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "D"
}
},
"it" : {
"stringUnit" : {
"state" : "translated",
Expand Down Expand Up @@ -464,6 +471,28 @@
}
}
},
"dest_info_error" : {
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Fehler Code"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Error Code"
}
},
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "Codice errore"
}
}
}
},
"dest_info_filesystem" : {
"extractionState" : "manual",
"localizations" : {
Expand Down Expand Up @@ -579,6 +608,28 @@
}
}
},
"dest_info_number_of_backups" : {
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Anzahl an Sicherungen"
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Number of backups"
}
},
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "Numero di backup"
}
}
}
},
"dest_info_quota" : {
"extractionState" : "manual",
"localizations" : {
Expand Down Expand Up @@ -671,25 +722,25 @@
}
}
},
"dest_label_last_backup_failed" : {
"dest_label_last_backup_failed_%lld" : {
"extractionState" : "manual",
"localizations" : {
"de" : {
"stringUnit" : {
"state" : "translated",
"value" : "Letztes Backup fehlgeschlagen. TimeMachine Einstellungen öffnen um mehr zu erfahren."
"value" : "Letztes Backup fehlgeschlagen (%lld). TimeMachine Einstellungen öffnen um mehr zu erfahren."
}
},
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Last Backup Failed. Open TimeMachine Settings for more information."
"value" : "Last Backup Failed (%lld). Open TimeMachine Settings for more information."
}
},
"it" : {
"stringUnit" : {
"state" : "translated",
"value" : "Ultimo backup non riuscito. Apri le Impostazioni di TimeMachine per ulteriori informazioni."
"value" : "Ultimo backup non riuscito (%lld). Apri le Impostazioni di TimeMachine per ulteriori informazioni."
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions TimeMachineStatus/Model/BackupState/BackupState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ enum BackupState {
if let state = try? _decodePlist(data, raw: result) {
return state
}

#if DEBUG
log.error("Unknown state: \(result)")
fatalError("Unknown state: \(result)")
#else
throw BackupStateError.invalidState(raw: result)
#endif

throw BackupStateError.invalidState(raw: result)
}
}

Expand Down Expand Up @@ -72,11 +72,8 @@ extension BackupState {
case ._thinning:
return try decoder.decode(BackupState.Thinning.self, from: data)
case ._unknown(let state):
#if DEBUG
throw BackupStateError.invalidState(raw: raw)
#else
log.warning("Unknown state: \(state), raw: \(raw)")
return BackupState.Unknown(title: state, raw: raw)
#endif
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion TimeMachineStatus/Model/Preferences/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct Destination: Decodable {
case referenceLocalSnapshotDate = "ReferenceLocalSnapshotDate"
case snapshotDates = "SnapshotDates"
case attemptDates = "AttemptDates"
case result = "RESULT"
}

let lastKnownVolumeName: String?
Expand All @@ -119,6 +120,7 @@ struct Destination: Decodable {
let referenceLocalSnapshotDate: Date?
let snapshotDates: [Date]?
let attemptDates: [Date]?
let result: Int?

var lastBackupFailed: Bool {
guard let snapshotDates,
Expand All @@ -129,6 +131,10 @@ struct Destination: Decodable {

return lastAttempt > last
}

var numberOfBackups: Int {
snapshotDates?.count ?? 0
}
}

extension Preferences {
Expand Down Expand Up @@ -160,7 +166,8 @@ extension Destination {
consistencyScanDate: .distantPast,
referenceLocalSnapshotDate: .now,
snapshotDates: [.distantPast, .now.addingTimeInterval(.random(in: -100_000...0))],
attemptDates: [.distantPast, .now.addingTimeInterval(.random(in: -100_000...0))]
attemptDates: [.distantPast, .now.addingTimeInterval(.random(in: -100_000...0))],
result: 0
)
}
}
5 changes: 4 additions & 1 deletion TimeMachineStatus/Views/DestinationCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct DestinationCell: View {
.contentShape(.rect)
.contextMenu { contextMenuActions }
.card(.background.secondary)
.onTapGesture {
showInfo.toggle()
}
.onHover { hovering in
withAnimation(.snappy) {
self.hovering = hovering
Expand Down Expand Up @@ -141,7 +144,7 @@ struct DestinationCell: View {
}
.buttonStyle(.plain)
.focusable(false)
.help("dest_label_last_backup_failed")
.help("dest_label_last_backup_failed_\(dest.result ?? -1)")
}
}

Expand Down
10 changes: 8 additions & 2 deletions TimeMachineStatus/Views/DestinationInfoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct DestinationInfoView: View {
if let lastKnownEncryptionState = dest.lastKnownEncryptionState {
LabeledContent("dest_info_encrypted", value: lastKnownEncryptionState)
}
if let networkURL = dest.networkURL {
if let networkURL = dest.networkURL?.removingPercentEncoding {
LabeledContent("dest_info_url", value: networkURL)
}
}
Expand All @@ -41,16 +41,22 @@ struct DestinationInfoView: View {
}
}
Section {
LabeledContent("dest_info_number_of_backups", value: "\(dest.numberOfBackups)")
if let last = dest.snapshotDates?.max() {
LabeledContent("dest_info_lastbackup", value: last.formatted(.relativeDate))
}
if let last = dest.attemptDates?.max() {
LabeledContent("dest_info_lastattempt", value: last.formatted(.relativeDate))
}
}
Section {
if let errorCode = dest.result, errorCode != 0 {
LabeledContent("dest_info_error", value: "\(errorCode)")
}
}
}
.formStyle(.grouped)
.labeledContentStyle(CustomLabeledContentStyle())
.labeledContentStyle(.custom)
.frame(width: 500)
}
}
2 changes: 1 addition & 1 deletion TimeMachineStatus/Views/MenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct MenuView: View {
} header: {
Text("section_general_info")
}
.labeledContentStyle(CustomLabeledContentStyle())
.labeledContentStyle(.custom)
}
}

Expand Down

0 comments on commit da0c0ba

Please sign in to comment.