fix follow list
This commit is contained in:
parent
605062c7a8
commit
66ee40c1a6
@ -51,8 +51,8 @@ struct API {
|
||||
}
|
||||
|
||||
// 获取用户收藏的数据列表
|
||||
static func getFavorData<T: Codable>(userId: String, id: Int, as: T.Type) async -> APIResponse<T> {
|
||||
let request = URLRequest(url: URL(string: baseUrl + "/api/favor?user_id=\(userId)&id=\(id)")!)
|
||||
static func getUserFollowList<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||||
let request = URLRequest(url: URL(string: baseUrl + "/api/follow_list?user_id=\(userId)")!)
|
||||
|
||||
return await doRequest(request: request, as: T.self)
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ import SwiftUI
|
||||
import Observation
|
||||
|
||||
@Observable
|
||||
final class FavorModel {
|
||||
final class FollowListModel {
|
||||
|
||||
struct DramaItem: Codable {
|
||||
struct Episode: Codable, Identifiable {
|
||||
@ -32,55 +32,31 @@ final class FavorModel {
|
||||
|
||||
struct FavorResponse: Codable {
|
||||
let dramas: [DramaItem]
|
||||
let has_more: Bool
|
||||
}
|
||||
|
||||
var dramas: [DramaItem]
|
||||
|
||||
// 标记是否还有新数据,避免空请求
|
||||
@ObservationIgnored
|
||||
private var hasMore: Bool
|
||||
|
||||
init() {
|
||||
self.dramas = []
|
||||
self.hasMore = true
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func loadData(userId: String) async {
|
||||
let response = await API.getFavorData(userId: userId, id: 0, as: FavorResponse.self)
|
||||
let response = await API.getUserFollowList(userId: userId, as: FavorResponse.self)
|
||||
switch response {
|
||||
case .error(let code, let message):
|
||||
print("index load data get error_code: \(code), message: \(message)")
|
||||
case .result(let result):
|
||||
self.dramas = result.dramas
|
||||
self.hasMore = result.has_more
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func loadMoreFavorDramas(userId: String) async {
|
||||
guard self.hasMore else {
|
||||
return
|
||||
}
|
||||
|
||||
let lastId = self.dramas.last?.id ?? 0
|
||||
let response = await API.getFavorData(userId: userId, id: lastId, as: FavorResponse.self)
|
||||
switch response {
|
||||
case .result(let result):
|
||||
self.dramas.append(contentsOf: result.dramas)
|
||||
self.hasMore = result.has_more
|
||||
case .error(let code, let string):
|
||||
print("load more favor get error code: \(code), message: \(string)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FavorView: View {
|
||||
struct FollowListView: View {
|
||||
@AppStorage("userId") private var userId: String = Utils.defaultUserId()
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
@State var favorModel = FavorModel()
|
||||
@State var followModel = FollowListModel()
|
||||
@State var isMoreLoading: Bool = false
|
||||
|
||||
var body: some View {
|
||||
@ -89,73 +65,55 @@ struct FavorView: View {
|
||||
HStack(alignment: .center) {
|
||||
Color.clear
|
||||
.overlay(alignment: .leading) {
|
||||
Text("< 番剧补完计划")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(Color(hex: "#999999"))
|
||||
.padding([.top, .bottom], 5)
|
||||
.padding([.leading], 15)
|
||||
.onTapGesture {
|
||||
dismiss()
|
||||
}
|
||||
HStack(alignment: .center, spacing: 3) {
|
||||
Image(systemName: "chevron.left")
|
||||
.border(Color.yellow)
|
||||
|
||||
Text("番剧补完计划")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(Color(hex: "#333333"))
|
||||
}
|
||||
.onTapGesture {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: 50)
|
||||
.background(Color(hex: "#F2F2F2"), ignoresSafeAreaEdges: .top)
|
||||
|
||||
if favorModel.dramas.count > 0 {
|
||||
VStack(alignment: .center) {
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
ForEach(favorModel.dramas, id: \.id) { drama in
|
||||
if followModel.dramas.count > 0 {
|
||||
ScrollView(.vertical, showsIndicators: false) {
|
||||
LazyVStack(alignment: .center) {
|
||||
ForEach(followModel.dramas, id: \.id) { drama in
|
||||
DramaCellView(dramaItem: drama)
|
||||
}
|
||||
|
||||
Rectangle()
|
||||
.frame(height: 0)
|
||||
.background(GeometryReader {
|
||||
geometry in
|
||||
Color.clear.onChange(of: geometry.frame(in: .global).minY) {_, offset in
|
||||
|
||||
let frame = geometry.frame(in: .global)
|
||||
let screenBounds = UIScreen.main.bounds
|
||||
let contextFrame = geometry.frame(in: .named("indexScrollView"))
|
||||
|
||||
if screenBounds.height - frame.minY > 50 && contextFrame.minY > 0 && !isMoreLoading {
|
||||
Task {
|
||||
self.isMoreLoading = true
|
||||
await self.favorModel.loadMoreFavorDramas(userId: userId)
|
||||
self.isMoreLoading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if self.isMoreLoading {
|
||||
ProgressView()
|
||||
}
|
||||
}
|
||||
.coordinateSpace(name: "indexScrollView")
|
||||
}
|
||||
.frame(width: 370)
|
||||
} else {
|
||||
VStack {
|
||||
Text("你还没有")
|
||||
Spacer()
|
||||
Text("这里什么都没有")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(.black)
|
||||
Spacer()
|
||||
}
|
||||
.frame(width: 370)
|
||||
}
|
||||
}
|
||||
.ignoresSafeArea(edges: .bottom)
|
||||
.navigationBarBackButtonHidden()
|
||||
.task {
|
||||
await self.favorModel.loadData(userId: self.userId)
|
||||
await self.followModel.loadData(userId: self.userId)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension FavorView {
|
||||
extension FollowListView {
|
||||
// 显示剧集的列表信息
|
||||
struct DramaCellView: View {
|
||||
let dramaItem: FavorModel.DramaItem
|
||||
let dramaItem: FollowListModel.DramaItem
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
@ -164,6 +122,7 @@ extension FavorView {
|
||||
Text(dramaItem.title)
|
||||
.font(.system(size: 20))
|
||||
.foregroundColor(Color(hex: "#333333"))
|
||||
.lineLimit(1)
|
||||
}
|
||||
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
@ -229,3 +188,7 @@ extension FavorView {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FollowListView()
|
||||
}
|
||||
@ -47,12 +47,14 @@ final class IndexModel {
|
||||
|
||||
struct IndexResponse: Codable {
|
||||
let update_dramas: [UpdateDramaGroup]
|
||||
let follow_num: Int
|
||||
}
|
||||
|
||||
var selectedDate: String
|
||||
|
||||
// 保存原始的更新数据
|
||||
var updateDramaGroups: [UpdateDramaGroup] = []
|
||||
var follow_num: String = "0"
|
||||
|
||||
@ObservationIgnored
|
||||
private var isLoaded = false
|
||||
@ -73,6 +75,7 @@ final class IndexModel {
|
||||
print("index load data get error_code: \(code), message: \(message)")
|
||||
case .result(let result):
|
||||
self.updateDramaGroups = result.update_dramas
|
||||
self.follow_num = result.follow_num >= 100 ? "99+" : "\(result.follow_num)"
|
||||
}
|
||||
self.isLoaded = true
|
||||
}
|
||||
@ -292,10 +295,11 @@ extension IndexView {
|
||||
.padding([.top, .bottom], 5)
|
||||
Spacer()
|
||||
|
||||
NavigationLink(destination: FavorView()) {
|
||||
NavigationLink(destination: FollowListView()) {
|
||||
HStack {
|
||||
Text("♡ 12")
|
||||
Text("♡ \(indexModel.follow_num)")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(.black)
|
||||
.padding([.top, .bottom], 5)
|
||||
}
|
||||
}
|
||||
@ -317,7 +321,7 @@ extension IndexView {
|
||||
}
|
||||
|
||||
// 基于日期的更新列表
|
||||
VStack(alignment: .center, spacing: 10) {
|
||||
LazyVStack(alignment: .center, spacing: 10) {
|
||||
ForEach(indexModel.updateDramaGroups, id: \.group_id) { group in
|
||||
DramaGroupView(group: group) {
|
||||
selectGroupId = group.group_id
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user