diff --git a/dimensionhub/Views/FollowList/FollowDramaModel.swift b/dimensionhub/Views/FollowList/FollowDramaModel.swift new file mode 100644 index 0000000..6d96f95 --- /dev/null +++ b/dimensionhub/Views/FollowList/FollowDramaModel.swift @@ -0,0 +1,40 @@ +// +// FollowListModel.swift +// dimensionhub +// +// Created by 安礼成 on 2025/4/8. +// + +import Foundation +import Observation + +@Observable +final class FollowDramaModel { + + @ObservationIgnored + var drama: FollowListModel.DramaItem + + // 选中的频道信息 + var checkedChannelId: Int = 0 + var episodes: [FollowListModel.DramaItem.Channel.Episode] + + init(drama: FollowListModel.DramaItem) { + self.drama = drama + self.checkedChannelId = 0 + if let channel = drama.channels.first { + self.episodes = channel.episodes + } else { + self.episodes = [] + } + } + + func changeChannel(channelId: Int) { + self.checkedChannelId = channelId + if drama.channels.indices.contains(channelId) { + self.episodes = drama.channels[channelId].episodes + } else { + self.episodes = [] + } + } + +} diff --git a/dimensionhub/Views/FollowList/FollowListModel.swift b/dimensionhub/Views/FollowList/FollowListModel.swift index ac5d263..ec8c0b6 100644 --- a/dimensionhub/Views/FollowList/FollowListModel.swift +++ b/dimensionhub/Views/FollowList/FollowListModel.swift @@ -40,8 +40,6 @@ final class FollowListModel { } var dramas: [DramaItem] - // 选中的频道信息 - var checkedChannels: [Int:Int] = [:] init() { self.dramas = [] @@ -56,17 +54,10 @@ 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 diff --git a/dimensionhub/Views/FollowList/FollowListView.swift b/dimensionhub/Views/FollowList/FollowListView.swift index 37786ab..12ebb8a 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, followModel: followModel) + DramaCellView(dramaModel: FollowDramaModel(drama: drama)) } } } @@ -54,14 +54,13 @@ extension FollowListView { // 显示剧集的列表信息 struct DramaCellView: View { - let dramaItem: FollowListModel.DramaItem - let followModel: FollowListModel + let dramaModel: FollowDramaModel var body: some View { - VStack(alignment: .leading) { + VStack(alignment: .leading, spacing: 8) { - NavigationLink(destination: DetailView(id: dramaItem.id)) { - Text(dramaItem.title) + NavigationLink(destination: DetailView(id: dramaModel.drama.id)) { + Text(dramaModel.drama.title) .font(.system(size: 20)) .foregroundColor(Color(hex: "#333333")) .lineLimit(1) @@ -69,12 +68,12 @@ extension FollowListView { // 渠道列表 HStack(alignment: .center, spacing: 15) { - ForEach(Array(dramaItem.channels.enumerated()), id: \.offset) { idx, channel in + ForEach(Array(dramaModel.drama.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")) + .font(.system(size: 13, weight: idx == dramaModel.checkedChannelId ? .medium : .regular)) + .foregroundColor(idx == dramaModel.checkedChannelId ? Color(hex: "#202020") : Color(hex: "#666666")) .onTapGesture { - followModel.changeChannel(dramaId: dramaItem.id, channelId: idx) + dramaModel.changeChannel(channelId: idx) } } Spacer() @@ -82,7 +81,7 @@ extension FollowListView { ScrollView(.horizontal, showsIndicators: false) { LazyHStack(alignment: .center, spacing: 5) { - ForEach(dramaItem.channels[followModel.checkedChannels[dramaItem.id]!].episodes) { item in + ForEach(dramaModel.episodes) { item in DramaCellEpisodeView(item: item) } }