频道的切换

This commit is contained in:
anlicheng 2025-08-15 16:51:55 +08:00
parent c2126eccd2
commit 6ca3c607b7
3 changed files with 12 additions and 6 deletions

View File

@ -96,7 +96,7 @@ struct DetailView: View {
if detailModel.selectedEpisodes.count >= 5 {
HStack(alignment: .center) {
NavigationLink(destination: ListView(id: self.id)) {
NavigationLink(destination: ListView(id: self.id, selectedChannelIdx: detailModel.selectedChannelIdx)) {
Rectangle()
.frame(width: 200, height: 25)
.foregroundColor(Color(hex: "#F2F2F2"))

View File

@ -45,7 +45,7 @@ final class ListModel {
var selectedChannelIdx: Int = 0
var selectedEpisodes: [Episode] = []
func loadData(userId: String, id: Int) async {
func loadData(userId: String, id: Int, selectedChannelIdx: Int) async {
let response = await API.getDramaDetail(userId: userId, id: id, as: DramaDetailResponse.self)
switch response {
case .error(let code, let message):
@ -60,11 +60,16 @@ final class ListModel {
self.thumb = detail.thumb
self.channels = detail.channels
if self.channels.count >= selectedChannelIdx {
self.selectedChannelIdx = selectedChannelIdx
self.selectedEpisodes = detail.channels[selectedChannelIdx].episodes
} else {
self.selectedChannelIdx = 0
self.selectedEpisodes = detail.channels[0].episodes
}
}
}
}
func toggleChannel(channelIdx: Int) {
self.selectedChannelIdx = channelIdx

View File

@ -11,6 +11,7 @@ struct ListView: View {
@State var detailModel = ListModel()
let id: Int
let selectedChannelIdx: Int
var body: some View {
VStack(alignment: .center, spacing: 20) {
@ -49,7 +50,7 @@ struct ListView: View {
}
.navigationTitle(detailModel.name)
.task {
await detailModel.loadData(userId: self.userId, id: self.id)
await detailModel.loadData(userId: self.userId, id: self.id, selectedChannelIdx: self.selectedChannelIdx)
}
}
@ -88,5 +89,5 @@ extension ListView {
#Preview {
ListView(id: 19625)
ListView(id: 19625, selectedChannelIdx: 0)
}