Skip to content

Instantly share code, notes, and snippets.

@srfunksensei
Created October 18, 2019 12:26
Show Gist options
  • Select an option

  • Save srfunksensei/2fe8464de72499727cf483029dfbd3d6 to your computer and use it in GitHub Desktop.

Select an option

Save srfunksensei/2fe8464de72499727cf483029dfbd3d6 to your computer and use it in GitHub Desktop.
import UIKit
class ExerciseOption: Decodable {
private enum ExerciseOptionCodingKeys: String, CodingKey {
case id
case appleHealthId = "apple_health_id"
case googleFitId = "google_fit_id"
case langsTranslation = "langs_translation"
}
let id: Int
let appleHealthId: String?
let googleFitId: String?
let langsTranslation: [String: String]
lazy var localizedName: String = {
for l in Locale.preferredLanguages {
let code = String(l.prefix(2))
if let translated = self.langsTranslation[code] {
return translated
}
}
// hardcoded fallback
return self.langsTranslation["en"] ?? "-"
}()
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ExerciseOptionCodingKeys.self)
let idString = try container.decode(String.self, forKey: .id)
self.id = Int(idString)!
self.appleHealthId = try container.decode(String.self, forKey: .appleHealthId)
self.googleFitId = try container.decode(String.self, forKey: .googleFitId)
self.langsTranslation = try container.decode([String:String].self, forKey: .langsTranslation)
}
}
class Exercise: Decodable {
private enum ExerciseCodingKeys: String, CodingKey {
case id
case langsTranslation = "langs_translation"
case preferredUnits = "preferred_units"
case options
}
let id: Int
let langsTranslation: [String: String]
let preferredUnits: [String]
let options: [ExerciseOption]
lazy var localizedName: String = {
for l in Locale.preferredLanguages {
let code = String(l.prefix(2))
if let translated = self.langsTranslation[code] {
return translated
}
}
// hardcoded fallback
return self.langsTranslation["en"] ?? "-"
}()
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ExerciseCodingKeys.self)
let idString = try container.decode(String.self, forKey: .id)
self.id = Int(idString)!
self.langsTranslation = try container.decode([String: String].self, forKey: .langsTranslation)
self.preferredUnits = try container.decode([String].self, forKey: .preferredUnits)
self.options = try container.decode([ExerciseOption].self, forKey: .options)
}
}
let exerciseOptionJson = """
{
"id": "100",
"apple_health_id": "",
"google_fit_id": "",
"langs_translation": {
}
}
"""
let exerciseOption = try JSONDecoder().decode(ExerciseOption.self, from: Data(exerciseOptionJson.utf8))
let exerciseJson = """
{
"id": "1",
"langs_translation": {
"en" : "Swimming",
"de" : "Schwimmen"
},
"preferred_units": [
"min", "h"
],
"options": [
{
"id": "100",
"apple_health_id": "",
"google_fit_id": "",
"langs_translation": {
"en" : "Freestyle"
}
},
{
"id": "101",
"apple_health_id": "",
"google_fit_id": "",
"langs_translation": {
"en" : "Backstroke"
}
}
]
}
"""
let exercise = try JSONDecoder().decode(Exercise.self, from: Data(exerciseJson.utf8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment