This commit is contained in:
anlicheng 2025-02-24 16:12:14 +08:00
parent 5ff82b10bb
commit 0ecb1dbb6a
5 changed files with 40 additions and 21 deletions

View File

@ -27,6 +27,12 @@ struct API {
case error(Int32, String)
}
//
enum LoadMode: String {
case prev = "prev"
case next = "next"
}
static let url = "http://localhost:18085/api/dimensionhub/"
//
@ -38,8 +44,11 @@ struct API {
return await doRequest(request: request, as: T.self)
}
static func loadMoreUpdateDramas<T: Codable>(as: T.Type) async -> APIResponse<T> {
let request = URLRequest(url: URL(string: url + "load_more_dramas")!)
//
static func loadMoreUpdateDramas<T: Codable>(userId: Int, mode: LoadMode, id: Int, as: T.Type) async -> APIResponse<T> {
// TODO
let request = URLRequest(url: URL(string: baseUrl + "/api/load_more_dramas?user_id=\(userId)&mode=\(mode.rawValue)&id=8030")!)
print(request)
return await doRequest(request: request, as: T.self)
}
@ -65,18 +74,17 @@ struct API {
}
let x = String(data: data, encoding: .utf8)!
print("url: \(request.url?.path()), data is: \(x)")
do {
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
return .result(result.result)
} catch let err {
print(err)
print("http request: \(request.url!.path()), get error: \(err)")
let apiError = try JSONDecoder().decode(APIErrorResponse.self, from: data)
return .error(apiError.error.code, apiError.error.message)
}
} catch let err {
print(err)
print("http error: \(err)")
return .error(-1, err.localizedDescription)
}
}

View File

@ -1,12 +0,0 @@
//
// DataModels.swift
// dimensionhub
// jsonmodel
// Created by on 2025/2/20.
//
//
//

View File

@ -0,0 +1,21 @@
//
// UserModel.swift
// dimensionhub
//
// Created by on 2025/2/24.
//
import Foundation
import SwiftData
@Model
final class UserModel {
var userId: Int
var username: String
init(userId: Int, username: String) {
self.userId = userId
self.username = username
}
}

View File

@ -87,8 +87,8 @@ final class IndexModel {
}
@MainActor
func loadMoreUpdateDramas() async {
let response = await API.loadMoreUpdateDramas(as: [UpdateDramaGroup].self)
func loadMoreUpdateDramas(userId: Int, mode: API.LoadMode, id: Int) async {
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: id, as: [UpdateDramaGroup].self)
if case let .result(groups) = response {
let showItems = groupUpdateDramas(updateDramaGroups: groups)
self.showUpdateDramas.append(contentsOf: showItems)
@ -111,6 +111,7 @@ final class IndexModel {
struct IndexView: View {
@Environment(\.modelContext) private var modelContext
@Query private var items: [Item]
@Query private var userModel: [UserModel]
@State var indexModel = IndexModel()
@State var isLoading: Bool = false
@ -207,7 +208,7 @@ struct IndexView: View {
if screenBounds.height - frame.minY > 50 && !isLoading {
Task {
self.isLoading = true
await self.indexModel.loadMoreUpdateDramas()
await self.indexModel.loadMoreUpdateDramas(userId: 1, mode: .next, id: 1234)
self.isLoading = false
}
}
@ -227,7 +228,7 @@ struct IndexView: View {
.frame(width: 370)
.ignoresSafeArea(edges: .bottom)
.task {
await self.indexModel.loadData()
//await self.indexModel.loadData()
}
}

View File

@ -13,6 +13,7 @@ struct dimensionhubApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
UserModel.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)