fix follow
This commit is contained in:
parent
d74414a6c5
commit
a8e3a68879
@ -72,6 +72,12 @@ struct API {
|
||||
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请求
|
||||
private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> {
|
||||
do {
|
||||
|
||||
28
dimensionhub/Core/Uitls.swift
Normal file
28
dimensionhub/Core/Uitls.swift
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
@ -90,7 +90,7 @@ final class DetailModel {
|
||||
print(message)
|
||||
case .result(let detail):
|
||||
self.name = detail.name
|
||||
self.summary = detail.summary
|
||||
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
||||
self.thumb = detail.thumb
|
||||
self.statuses = detail.status.flatMap({ s in
|
||||
if let status = DramaStatus(s) {
|
||||
@ -106,11 +106,30 @@ final class DetailModel {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func toggleChannel(channelIdx: Int) {
|
||||
self.selectedChannelIdx = channelIdx
|
||||
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 {
|
||||
@ -129,8 +148,7 @@ struct DetailView: View {
|
||||
Text(detailModel.summary)
|
||||
.lineLimit(3)
|
||||
.multilineTextAlignment(.leading)
|
||||
.padding()
|
||||
//.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.padding([.top, .leading], 10)
|
||||
.background(Color(hex: "#F2F2F2"), ignoresSafeAreaEdges: [.bottom])
|
||||
@ -138,8 +156,10 @@ struct DetailView: View {
|
||||
HStack(alignment: .center, spacing: 10) {
|
||||
Spacer()
|
||||
ForEach(detailModel.statuses, id: \.status) { status in
|
||||
FollowButtonView(title: status.config.name, bgColor: status.config.bgColor, fontColor: status.config.fontColor) {
|
||||
print("call me follow button clicked")
|
||||
FollowButtonView(dramaStatus: status) { followStatus in
|
||||
Task {
|
||||
await detailModel.onTapFollowButton(userId: 1, id: id, status: followStatus)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -220,27 +240,25 @@ struct DetailView: View {
|
||||
extension DetailView {
|
||||
|
||||
struct FollowButtonView: View {
|
||||
let title: String
|
||||
let bgColor: Color
|
||||
let fontColor: Color
|
||||
let onTap: () -> Void
|
||||
let dramaStatus: DetailModel.DramaStatus
|
||||
let onTap: (String) -> Void
|
||||
|
||||
var body: some View {
|
||||
Rectangle()
|
||||
.frame(width: 140, height: 40)
|
||||
.foregroundColor(bgColor)
|
||||
.foregroundColor(dramaStatus.config.bgColor)
|
||||
.cornerRadius(5)
|
||||
.overlay {
|
||||
RoundedRectangle(cornerRadius: 5)
|
||||
.stroke(Color.black, lineWidth: 1)
|
||||
|
||||
Text(title)
|
||||
Text(dramaStatus.config.name)
|
||||
.font(.system(size: 13))
|
||||
.foregroundColor(fontColor)
|
||||
.foregroundColor(dramaStatus.config.fontColor)
|
||||
.fontWeight(.regular)
|
||||
}
|
||||
.onTapGesture {
|
||||
onTap()
|
||||
onTap(dramaStatus.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ final class ListModel {
|
||||
print(message)
|
||||
case .result(let detail):
|
||||
self.name = detail.name
|
||||
self.summary = detail.summary
|
||||
self.summary = Utils.converHtmlToString(html: detail.summary) ?? ""
|
||||
self.thumb = detail.thumb
|
||||
self.channels = detail.channels
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user