WIP
This commit is contained in:
@@ -15,17 +15,15 @@ struct Workout: Codable, Identifiable, Equatable {
|
||||
let id: Int
|
||||
let name: String
|
||||
let description: String?
|
||||
let exercises: [ExerciseElement]
|
||||
let supersets: [Superset]?
|
||||
let registeredUser: RegisteredUser?
|
||||
let femaleVideos, maleVideos, bothVideos: [String]?
|
||||
let muscles: [String]?
|
||||
let equipment: [String]?
|
||||
let exercise_count: Int?
|
||||
let maleVideos: [String]?
|
||||
let femaleVideos: [String]?
|
||||
let bothVideos: [String]?
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case name, description, exercises, id, muscles, equipment, exercise_count
|
||||
case id, name, description, supersets, exercise_count, muscles, equipment
|
||||
case registeredUser = "registered_user"
|
||||
case maleVideos = "male_videos"
|
||||
case femaleVideos = "female_videos"
|
||||
@@ -34,31 +32,18 @@ struct Workout: Codable, Identifiable, Equatable {
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
if let exercises = try container.decodeIfPresent([ExerciseElement].self, forKey: .exercises) {
|
||||
self.exercises = exercises
|
||||
} else {
|
||||
self.exercises = [ExerciseElement]()
|
||||
}
|
||||
|
||||
self.name = try container.decode(String.self, forKey: .name)
|
||||
|
||||
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? "NA"
|
||||
self.description = try container.decodeIfPresent(String.self, forKey: .description)
|
||||
self.registeredUser = try container.decodeIfPresent(RegisteredUser.self, forKey: .registeredUser)
|
||||
self.id = try container.decode(Int.self, forKey: .id)
|
||||
self.muscles = try container.decodeIfPresent([String].self, forKey: .muscles)
|
||||
self.equipment = try container.decodeIfPresent([String].self, forKey: .equipment)
|
||||
self.exercise_count = try container.decodeIfPresent(Int.self, forKey: .exercise_count)
|
||||
self.femaleVideos = try container.decodeIfPresent([String].self, forKey: .femaleVideos)
|
||||
self.maleVideos = try container.decodeIfPresent([String].self, forKey: .maleVideos)
|
||||
self.bothVideos = try container.decodeIfPresent([String].self, forKey: .bothVideos)
|
||||
}
|
||||
|
||||
var exercisesSortedByCreated_at: [ExerciseElement] {
|
||||
return self.exercises.sorted(by: {
|
||||
if let lhsDate = $0.createdAt.dateFromServerDate,
|
||||
let rhsDate = $1.createdAt.dateFromServerDate {
|
||||
return lhsDate < rhsDate
|
||||
}
|
||||
return false
|
||||
})
|
||||
self.supersets = try container.decodeIfPresent([Superset].self, forKey: .supersets)
|
||||
|
||||
self.equipment = try container.decodeIfPresent([String].self, forKey: .equipment)
|
||||
self.muscles = try container.decodeIfPresent([String].self, forKey: .muscles)
|
||||
self.exercise_count = try container.decodeIfPresent(Int.self, forKey: .exercise_count)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user