fix follow

This commit is contained in:
anlicheng 2025-02-25 12:55:30 +08:00
parent d74414a6c5
commit a8e3a68879
4 changed files with 66 additions and 14 deletions

View File

@ -72,6 +72,12 @@ struct API {
return await doRequest(request: request, as: T.self) return await doRequest(request: request, as: T.self)
} }
static func followDrama<T: Codable>(userId: Int, id: Int, status: String, as: T.Type) async -> APIResponse<T> {
let request = URLRequest(url: URL(string: baseUrl + "/api/follow?user_id=\(userId)&id=\(id)&status=\(status)")!)
return await doRequest(request: request, as: T.self)
}
// http // http
private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> { private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> {
do { do {

View File

@ -0,0 +1,28 @@
//
// Uitls.swift
// dimensionhub
//
// Created by on 2025/2/25.
//
import Foundation
struct Utils {
static func converHtmlToString(html: String) -> String? {
guard let data = html.data(using: .utf8) else {
return nil
}
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
guard let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) else {
return nil
}
return attributedString.string
}
}

View File

@ -90,7 +90,7 @@ final class DetailModel {
print(message) print(message)
case .result(let detail): case .result(let detail):
self.name = detail.name self.name = detail.name
self.summary = detail.summary self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
self.thumb = detail.thumb self.thumb = detail.thumb
self.statuses = detail.status.flatMap({ s in self.statuses = detail.status.flatMap({ s in
if let status = DramaStatus(s) { if let status = DramaStatus(s) {
@ -106,11 +106,30 @@ final class DetailModel {
} }
} }
@MainActor
func toggleChannel(channelIdx: Int) { func toggleChannel(channelIdx: Int) {
self.selectedChannelIdx = channelIdx self.selectedChannelIdx = channelIdx
self.selectedEpisodes = self.channels[channelIdx].episodes self.selectedEpisodes = self.channels[channelIdx].episodes
} }
@MainActor
func onTapFollowButton(userId: Int, id: Int, status: String) async {
let response = await API.followDrama(userId: userId, id: id, status: status, as: [String].self)
switch response {
case .error(let code, let message):
print(code)
print(message)
case .result(let newStatuses):
self.statuses = newStatuses.flatMap({ s in
if let status = DramaStatus(s) {
return [status]
} else {
return []
}
})
}
}
} }
struct DetailView: View { struct DetailView: View {
@ -129,8 +148,7 @@ struct DetailView: View {
Text(detailModel.summary) Text(detailModel.summary)
.lineLimit(3) .lineLimit(3)
.multilineTextAlignment(.leading) .multilineTextAlignment(.leading)
.padding() .frame(maxWidth: .infinity, alignment: .leading)
//.frame(maxWidth: .infinity, alignment: .leading)
} }
.padding([.top, .leading], 10) .padding([.top, .leading], 10)
.background(Color(hex: "#F2F2F2"), ignoresSafeAreaEdges: [.bottom]) .background(Color(hex: "#F2F2F2"), ignoresSafeAreaEdges: [.bottom])
@ -138,8 +156,10 @@ struct DetailView: View {
HStack(alignment: .center, spacing: 10) { HStack(alignment: .center, spacing: 10) {
Spacer() Spacer()
ForEach(detailModel.statuses, id: \.status) { status in ForEach(detailModel.statuses, id: \.status) { status in
FollowButtonView(title: status.config.name, bgColor: status.config.bgColor, fontColor: status.config.fontColor) { FollowButtonView(dramaStatus: status) { followStatus in
print("call me follow button clicked") Task {
await detailModel.onTapFollowButton(userId: 1, id: id, status: followStatus)
}
} }
} }
} }
@ -220,27 +240,25 @@ struct DetailView: View {
extension DetailView { extension DetailView {
struct FollowButtonView: View { struct FollowButtonView: View {
let title: String let dramaStatus: DetailModel.DramaStatus
let bgColor: Color let onTap: (String) -> Void
let fontColor: Color
let onTap: () -> Void
var body: some View { var body: some View {
Rectangle() Rectangle()
.frame(width: 140, height: 40) .frame(width: 140, height: 40)
.foregroundColor(bgColor) .foregroundColor(dramaStatus.config.bgColor)
.cornerRadius(5) .cornerRadius(5)
.overlay { .overlay {
RoundedRectangle(cornerRadius: 5) RoundedRectangle(cornerRadius: 5)
.stroke(Color.black, lineWidth: 1) .stroke(Color.black, lineWidth: 1)
Text(title) Text(dramaStatus.config.name)
.font(.system(size: 13)) .font(.system(size: 13))
.foregroundColor(fontColor) .foregroundColor(dramaStatus.config.fontColor)
.fontWeight(.regular) .fontWeight(.regular)
} }
.onTapGesture { .onTapGesture {
onTap() onTap(dramaStatus.status)
} }
} }
} }

View File

@ -54,7 +54,7 @@ final class ListModel {
print(message) print(message)
case .result(let detail): case .result(let detail):
self.name = detail.name self.name = detail.name
self.summary = detail.summary self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
self.thumb = detail.thumb self.thumb = detail.thumb
self.channels = detail.channels self.channels = detail.channels