Created
April 8, 2018 02:09
-
-
Save mitulmanish/f3fd9d0ae4dbea5f166bd6874435ef86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseContent: Decodable { | |
var id: Int? | |
var vimondID: Int? | |
var imageUrl: String? | |
var title: ContentDescription? | |
var description: ContentDescription? | |
var synopsis: ContentDescription? | |
var trailers: [String]? | |
var level: String? | |
var imagePackId: String? | |
var slug: String? | |
enum TVCodingKeys: String, CodingKey { | |
case id = "id" | |
case vimondID = "vimondID" | |
case imageUrl = "imageUrl" | |
case title = "title" | |
case description = "description" | |
case synopsis = "synopsis" | |
case trailers = "trailers" | |
case level = "level" | |
case imagePackId = "imagePackId" | |
case slug = "slug" | |
} | |
enum DescriptionCodingKeys: String, CodingKey { | |
case thai = "th_TH" | |
case english = "en_US" | |
case malaysian = "my_MM" | |
case vietnamese = "vi_VN" | |
} | |
required init(from decoder: Decoder) throws { | |
let container = try decoder.container(keyedBy: TVCodingKeys.self) | |
id = try container.decodeIfPresent(Int.self, forKey: .id) | |
vimondID = try container.decodeIfPresent(Int.self, forKey: .vimondID) | |
imageUrl = try container.decodeIfPresent(String.self, forKey: .imageUrl) | |
trailers = try container.decodeIfPresent([String].self, forKey: .trailers) | |
level = try container.decodeIfPresent(String.self, forKey: .level) | |
imagePackId = try container.decodeIfPresent(String.self, forKey: .imagePackId) | |
slug = try container.decodeIfPresent(String.self, forKey: .slug) | |
let titleContainer = try container.nestedContainer(keyedBy: DescriptionCodingKeys.self, forKey: .title) | |
let englishTitle = try titleContainer.decodeIfPresent(String.self, forKey: .english) | |
let malay = try titleContainer.decodeIfPresent(String.self, forKey: .malaysian) | |
let vietnamese = try titleContainer.decodeIfPresent(String.self, forKey: .vietnamese) | |
let thai = try titleContainer.decodeIfPresent(String.self, forKey: .thai) | |
let descriptionContainer = try container.nestedContainer(keyedBy: DescriptionCodingKeys.self, forKey: .description) | |
let englishDescription = try descriptionContainer.decodeIfPresent(String.self, forKey: .english) | |
let malayDescription = try descriptionContainer.decodeIfPresent(String.self, forKey: .malaysian) | |
let vietnameseDescription = try descriptionContainer.decodeIfPresent(String.self, forKey: .vietnamese) | |
let thaiDescription = try descriptionContainer.decodeIfPresent(String.self, forKey: .thai) | |
description = ContentDescription(english: englishDescription, thai: thaiDescription, malaysian: malayDescription, vietnamese: vietnameseDescription) | |
let synopsisContainer = try container.nestedContainer(keyedBy: DescriptionCodingKeys.self, forKey: .synopsis) | |
let englishSynopsis = try synopsisContainer.decodeIfPresent(String.self, forKey: .english) | |
let malaySynopsis = try synopsisContainer.decodeIfPresent(String.self, forKey: .malaysian) | |
let vietnameseSynopsis = try synopsisContainer.decodeIfPresent(String.self, forKey: .vietnamese) | |
let thaiSynopsis = try synopsisContainer.decodeIfPresent(String.self, forKey: .thai) | |
title = ContentDescription(english: englishTitle, thai: thai, malaysian: malay, vietnamese: vietnamese) | |
synopsis = ContentDescription(english: englishSynopsis, thai: thaiSynopsis, malaysian: malaySynopsis, vietnamese: vietnameseSynopsis) | |
} | |
var sanitizedImageUrlString: String? { | |
let urlPrefix = "https:" | |
guard let imageUrl = imageUrl else { | |
return nil | |
} | |
if imageUrl.starts(with: urlPrefix) { | |
return imageUrl | |
} | |
return "\(urlPrefix)\(imageUrl)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment