fix: 修复新增availability字段导致JSON解码失败
自定义init(from decoder:),为新增字段提供默认值: - availability默认.unknown - 兼容旧版JSON数据,避免解码失败清空媒体库
This commit is contained in:
parent
af710e4e4f
commit
b1937bfc16
@ -24,6 +24,11 @@ struct LibraryItem: Identifiable, Codable, Equatable {
|
||||
var logoURL: String? // M3U tvg-logo
|
||||
var availability: StreamAvailability // 流可用性
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id, url, name, type, lastPosition, liked, disliked, tags
|
||||
case addedAt, group, logoURL, availability
|
||||
}
|
||||
|
||||
init(id: String = UUID().uuidString, url: String, name: String,
|
||||
type: String = "url", lastPosition: Double = 0,
|
||||
liked: Bool = false, disliked: Bool = false,
|
||||
@ -43,6 +48,22 @@ struct LibraryItem: Identifiable, Codable, Equatable {
|
||||
self.availability = availability
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decode(String.self, forKey: .id)
|
||||
url = try container.decode(String.self, forKey: .url)
|
||||
name = try container.decode(String.self, forKey: .name)
|
||||
type = try container.decode(String.self, forKey: .type)
|
||||
lastPosition = try container.decodeIfPresent(Double.self, forKey: .lastPosition) ?? 0
|
||||
liked = try container.decodeIfPresent(Bool.self, forKey: .liked) ?? false
|
||||
disliked = try container.decodeIfPresent(Bool.self, forKey: .disliked) ?? false
|
||||
tags = try container.decodeIfPresent([String].self, forKey: .tags) ?? []
|
||||
addedAt = try container.decode(Date.self, forKey: .addedAt)
|
||||
group = try container.decodeIfPresent(String.self, forKey: .group)
|
||||
logoURL = try container.decodeIfPresent(String.self, forKey: .logoURL)
|
||||
availability = try container.decodeIfPresent(StreamAvailability.self, forKey: .availability) ?? .unknown
|
||||
}
|
||||
|
||||
static func == (lhs: LibraryItem, rhs: LibraryItem) -> Bool { lhs.id == rhs.id }
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user