基于日期的加载
This commit is contained in:
parent
66f9f81a4a
commit
ef092bde46
@ -53,6 +53,13 @@ struct API {
|
||||
return await doRequest(request: request, as: T.self)
|
||||
}
|
||||
|
||||
// 指定时间索引
|
||||
static func loadDateUpdateDramas<T: Codable>(userId: Int, date: String, as: T.Type) async -> APIResponse<T> {
|
||||
let request = URLRequest(url: URL(string: baseUrl + "/api/load_date_dramas?user_id=\(userId)&date=\(date)")!)
|
||||
|
||||
return await doRequest(request: request, as: T.self)
|
||||
}
|
||||
|
||||
static func getDateIndex<T: Codable>(as: T.Type) async -> APIResponse<T> {
|
||||
let request = URLRequest(url: URL(string: url + "date_index")!)
|
||||
|
||||
@ -74,7 +81,7 @@ struct API {
|
||||
}
|
||||
|
||||
let x = String(data: data, encoding: .utf8)!
|
||||
print("url: \(request.url?.path()), data is: \(x)")
|
||||
print("url: \(request.url!.path()), data is: \(x)")
|
||||
do {
|
||||
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
||||
return .result(result.result)
|
||||
|
||||
@ -72,9 +72,13 @@ extension DateNavView {
|
||||
|
||||
}
|
||||
.onTapGesture {
|
||||
// 如果之前的状态就是选中状态,则不触发回调函数
|
||||
if !self.selected {
|
||||
onSelected(id)
|
||||
}
|
||||
|
||||
self.selected = true
|
||||
self.showDateNavPopover = false
|
||||
onSelected(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,6 +112,16 @@ final class IndexModel {
|
||||
}
|
||||
}
|
||||
|
||||
// 指定日期,并更新日期下对应的数据
|
||||
@MainActor
|
||||
func loadDateUpdateDramas(userId: Int, date: String) async {
|
||||
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
self.updateDramaGroups = preappendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||
self.showUpdateDramas = transformGroupUpdateDramas(updateDramaGroups: self.updateDramaGroups)
|
||||
}
|
||||
}
|
||||
|
||||
// 格式化
|
||||
private func transformGroupUpdateDramas(updateDramaGroups: [UpdateDramaGroup]) -> [UpdateDramaShowItem] {
|
||||
var updateItems: [UpdateDramaShowItem] = []
|
||||
@ -214,7 +224,6 @@ struct IndexView: View {
|
||||
.fontWeight(.regular)
|
||||
.onTapGesture {
|
||||
selectGroupId = group.group_id
|
||||
print("current group_id: \(self.selectGroupId)")
|
||||
indexModel.selectedDate = group.group_id
|
||||
showDateNavPopover = true
|
||||
}
|
||||
@ -248,9 +257,6 @@ struct IndexView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.onChange(of: indexModel.selectedDate) { oldValue, newValue in
|
||||
print("old date: \(oldValue), new value: \(newValue)")
|
||||
}
|
||||
|
||||
Rectangle()
|
||||
.frame(height: 0)
|
||||
@ -277,6 +283,9 @@ struct IndexView: View {
|
||||
.popover(isPresented: $showDateNavPopover) {
|
||||
DateNavView(selectGroupId: self.$selectGroupId, showDateNavPopover: $showDateNavPopover) { selectedDate in
|
||||
print("new selected date: " + selectedDate)
|
||||
Task {
|
||||
await indexModel.loadDateUpdateDramas(userId: 1, date: selectedDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
.refreshable {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user