dimensionhub/dimensionhub/Views/FollowList/FollowListModel.swift
2025-04-14 14:43:41 +08:00

68 lines
1.7 KiB
Swift

//
// FollowListModel.swift
// dimensionhub
//
// Created by on 2025/4/8.
//
import Foundation
import Observation
@Observable
final class FollowListModel {
struct DramaItem: Codable {
struct Episode: Codable, Identifiable {
let id = UUID().uuidString
let name: String
let thumb: String
let num_name: String
let play: String
enum CodingKeys: String, CodingKey {
case name, thumb, num_name, play
}
}
let id: Int
let title: String
let episodes: [Episode]
}
struct FavorResponse: Codable {
let dramas: [DramaItem]
}
var dramas: [DramaItem]
init() {
self.dramas = []
}
func loadData(userId: String) async {
let response = await API.getUserFollowList(userId: userId, as: FavorResponse.self)
switch response {
case .error(let code, let message):
print("index load data get error_code: \(code), message: \(message)")
case .result(let result):
self.preloadImages(dramas: result.dramas)
await MainActor.run {
self.dramas = result.dramas
}
}
}
private func preloadImages(dramas: [DramaItem]) {
let cacheManager = CacheManager.shared
dramas.forEach { dramaItem in
let urls = dramaItem.episodes.map { $0.thumb }
if urls.count > 0 {
Task.detached(priority: .medium) {
try? await cacheManager.preloadImages(urls: urls)
}
}
}
}
}