Files
sodalive-ios/SodaLive/Sources/Content/Series/Detail/GetSeriesDetailResponse.swift
T

90 lines
2.8 KiB
Swift

//
// GetSeriesDetailResponse.swift
// SodaLive
//
// Created by klaus on 4/29/24.
//
import Foundation
struct GetSeriesDetailResponse: Decodable {
let seriesId: Int
let title: String
let coverImage: String
let introduction: String
let genre: String
let isAdult: Bool
let writer: String?
let studio: String?
let publishedDate: String
let publishedDateUtc: String
let creator: GetSeriesDetailCreator
let rentalMinPrice: Int
let rentalMaxPrice: Int
let rentalPeriod: Int
let minPrice: Int
let maxPrice: Int
let keywordList: [String]
let publishedDaysOfWeek: String
let contentList: [GetSeriesContentListItem]
let contentCount: Int
let translated: TranslatedSeries?
let translatedGenre: String?
}
struct GetSeriesDetailCreator: Decodable {
let creatorId: Int
let nickname: String
let profileImage: String
let isFollow: Bool
let isNotify: Bool
}
struct TranslatedSeries: Decodable {
let title: String
let introduction: String
let keywords: [String]
}
// 표시용 계산 프로퍼티: 번역 데이터가 있으면 번역을 우선 사용
extension GetSeriesDetailResponse {
// 번역된 제목 우선
var displayTitle: String { translated?.title ?? title }
// 번역된 소개 우선
var displayIntroduction: String { translated?.introduction ?? introduction }
// 번역된 키워드 우선
var displayKeywords: [String] { translated?.keywords ?? keywordList }
// 번역된 장르 우선
var displayGenre: String { translatedGenre ?? genre }
// ISO8601 UTC를 현재 타임존 기준 yyyy.MM.dd 포맷으로 변환
var displayPublishedDate: String {
// publishedDateUtc 예: ISO8601 문자열
let iso = ISO8601DateFormatter()
// .withInternetDateTime가 기본이지만, fractional seconds 등 다양한 포맷 대응
iso.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
func format(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .gregorian)
formatter.timeZone = .current
formatter.locale = Locale(identifier: Locale.current.identifier)
formatter.dateFormat = "yyyy.MM.dd"
return formatter.string(from: date)
}
if let date = iso.date(from: publishedDateUtc) {
return format(date)
} else {
// fractional seconds 미포함 대응
let isoNoFrac = ISO8601DateFormatter()
isoNoFrac.formatOptions = [.withInternetDateTime]
if let date = isoNoFrac.date(from: publishedDateUtc) {
return format(date)
}
}
// 파싱 실패 시 기존 표시값 폴백
return publishedDate
}
}