This commit is contained in:
anlicheng 2025-06-10 12:38:21 +08:00
parent 1d12ff5625
commit 73c4f260d3
2 changed files with 46 additions and 14 deletions

View File

@ -12,21 +12,27 @@ import Observation
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
struct Channel: 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 name: String
let episodes: [Episode]
}
let id: Int
let title: String
let episodes: [Episode]
let channels: [Channel]
}
struct FavorResponse: Codable {
@ -34,6 +40,8 @@ final class FollowListModel {
}
var dramas: [DramaItem]
//
var checkedChannels: [Int:Int] = [:]
init() {
self.dramas = []
@ -48,14 +56,24 @@ final class FollowListModel {
self.preloadImages(dramas: result.dramas)
await MainActor.run {
self.dramas = result.dramas
result.dramas.forEach {drama in
self.checkedChannels[drama.id] = 0
}
}
}
}
func changeChannel(dramaId: Int, channelId: Int) {
self.checkedChannels[dramaId] = channelId
}
private func preloadImages(dramas: [DramaItem]) {
let cacheManager = CacheManager.shared
dramas.forEach { dramaItem in
let urls = dramaItem.episodes.map { $0.thumb }
let urls = dramaItem.channels.flatMap { channel in
channel.episodes.map { $0.thumb }
}
if urls.count > 0 {
Task.detached(priority: .medium) {
try? await cacheManager.preloadImages(urls: urls)

View File

@ -25,7 +25,7 @@ struct FollowListView: View {
ScrollView(.vertical, showsIndicators: false) {
LazyVStack(alignment: .center) {
ForEach(followModel.dramas, id: \.id) { drama in
DramaCellView(dramaItem: drama)
DramaCellView(dramaItem: drama, followModel: followModel)
}
}
}
@ -55,6 +55,7 @@ extension FollowListView {
//
struct DramaCellView: View {
let dramaItem: FollowListModel.DramaItem
let followModel: FollowListModel
var body: some View {
VStack(alignment: .leading) {
@ -65,10 +66,23 @@ extension FollowListView {
.foregroundColor(Color(hex: "#333333"))
.lineLimit(1)
}
//
HStack(alignment: .center, spacing: 15) {
ForEach(Array(dramaItem.channels.enumerated()), id: \.offset) { idx, channel in
Text(channel.name)
.font(.system(size: 13))
.foregroundColor(idx == followModel.checkedChannels[dramaItem.id] ? Color(hex: "#169BD5") : Color(hex: "#666666"))
.onTapGesture {
followModel.changeChannel(dramaId: dramaItem.id, channelId: idx)
}
}
Spacer()
}
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(alignment: .center, spacing: 5) {
ForEach(dramaItem.episodes) { item in
ForEach(dramaItem.channels[followModel.checkedChannels[dramaItem.id]!].episodes) { item in
DramaCellEpisodeView(item: item)
}
}
@ -78,7 +92,7 @@ extension FollowListView {
}
struct DramaCellEpisodeView: View {
let item: FollowListModel.DramaItem.Episode
let item: FollowListModel.DramaItem.Channel.Episode
var body: some View {
VStack(alignment: .center) {