This commit is contained in:
anlicheng 2025-06-10 13:10:00 +08:00
parent 73c4f260d3
commit 4c0a63a1f4
3 changed files with 50 additions and 20 deletions

View File

@ -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 = []
}
}
}

View File

@ -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

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, 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)
}
}