WIP - Complete App refactoring
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user