diff --git a/dimensionhub/Views/FollowList/FollowListModel.swift b/dimensionhub/Views/FollowList/FollowListModel.swift index 4349215..ac5d263 100644 --- a/dimensionhub/Views/FollowList/FollowListModel.swift +++ b/dimensionhub/Views/FollowList/FollowListModel.swift @@ -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) diff --git a/dimensionhub/Views/FollowList/FollowListView.swift b/dimensionhub/Views/FollowList/FollowListView.swift index 8a7ea6c..37786ab 100644 --- a/dimensionhub/Views/FollowList/FollowListView.swift +++ b/dimensionhub/Views/FollowList/FollowListView.swift @@ -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) {