WIP - Complete App refactoring

This commit is contained in:
VincentMeilinger
2025-05-26 15:52:24 +02:00
parent 29fd3c668b
commit 5acf3b9c4f
49 changed files with 1996 additions and 543 deletions

View File

@@ -146,3 +146,33 @@ enum RequestAlert: UserAlert {
return [.OK]
}
}
enum PersistenceAlert: UserAlert {
case DECODING_FAILED,
ENCODING_FAILED,
SAVE_FAILED,
LOAD_FAILED
var localizedDescription: LocalizedStringKey {
switch self {
case .DECODING_FAILED: return "Unable to decode recipe data."
case .ENCODING_FAILED: return "Unable to encode recipe data."
case .SAVE_FAILED: return "Unable to save recipe."
case .LOAD_FAILED: return "Unable to load recipe."
}
}
var localizedTitle: LocalizedStringKey {
switch self {
case .DECODING_FAILED: return "Decoding Error"
case .ENCODING_FAILED: return "Encoding Error"
case .SAVE_FAILED: return "Error"
case .LOAD_FAILED: return "Error"
}
}
var alertButtons: [AlertButton] {
return [.OK]
}
}

View File

@@ -37,7 +37,28 @@ class DurationComponents: ObservableObject {
}
}
init() {
}
init(_ hours: Int, _ min: Int, _ sec: Int = 0) {
self.hourComponent = hours
self.minuteComponent = min
self.secondComponent = sec
}
required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let durationString = try container.decode(String.self)
let hourRegex = /([0-9]{1,2})H/
let minuteRegex = /([0-9]{1,2})M/
if let match = durationString.firstMatch(of: hourRegex) {
self.hourComponent = Int(match.1) ?? 0
}
if let match = durationString.firstMatch(of: minuteRegex) {
self.minuteComponent = Int(match.1) ?? 0
}
}
var displayString: String {
if hourComponent != 0 && minuteComponent != 0 {
@@ -144,3 +165,11 @@ class DurationComponents: ObservableObject {
return result
}
}
extension DurationComponents: Codable {
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
let durationString = toPTString()
try container.encode(durationString)
}
}