fix cache
This commit is contained in:
parent
4fb871ddb9
commit
5083375194
@ -113,13 +113,13 @@ struct API {
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||
print("url: \(request.url!.absoluteString)")
|
||||
// print(String(data: data, encoding: .utf8)!)
|
||||
// print(String(data: data, encoding: .utf8)!)
|
||||
return .error(-1, "http status error")
|
||||
}
|
||||
|
||||
print("request url: \(request.url!.absoluteString)")
|
||||
// let x = String(data: data, encoding: .utf8)!
|
||||
// print("url: \(request.url!.path()), data is: \(x)")
|
||||
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)
|
||||
|
||||
@ -23,6 +23,10 @@ final class CacheManager {
|
||||
func preloadImages(urls: [String]) async throws {
|
||||
let preloadUrls = urls.filter { url in !self.fileExists(urlString: url)}
|
||||
|
||||
guard preloadUrls.count > 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
// 批量异步加载
|
||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||
for url in preloadUrls {
|
||||
|
||||
@ -18,7 +18,7 @@ struct IndexMainView: View {
|
||||
// 提示信息
|
||||
@State var showPrompt: Bool = false
|
||||
@State var promptMessage: String = ""
|
||||
|
||||
|
||||
// 是否显示日期弹出层
|
||||
@State private var selectGroupId: String = ""
|
||||
@State private var showDateNavPopover: Bool = false
|
||||
@ -71,7 +71,7 @@ struct IndexMainView: View {
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ProgressView()
|
||||
.background(GeometryReader { geometry in
|
||||
let minY = geometry.frame(in: .global).minY
|
||||
@ -142,22 +142,6 @@ struct IndexMainView: View {
|
||||
.onPreferenceChange(DramaGroupElementPreferenceKey.self) { frames in
|
||||
indexModel.visiblePublisher.send(frames)
|
||||
}
|
||||
.onAppear {
|
||||
Task {
|
||||
let imageUrl = "https://lain.bgm.tv/pic/cover/l/ea/c4/479788_3pcsw.jpg"
|
||||
let cacheManager = CacheManager.shared
|
||||
|
||||
try await cacheManager.preloadImages(urls: [imageUrl])
|
||||
|
||||
let result = cacheManager.fileExists(urlString: imageUrl)
|
||||
print("cache file exists: \(result)")
|
||||
|
||||
print("data: ")
|
||||
print(cacheManager.readFileContents(urlString: imageUrl))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -129,6 +129,8 @@ final class IndexModel {
|
||||
case .error(let code, let message):
|
||||
print("index load data get error_code: \(code), message: \(message)")
|
||||
case .result(let result):
|
||||
preloadGroupImages(groups: result.update_dramas)
|
||||
|
||||
await MainActor.run {
|
||||
self.updateDramaGroups = result.update_dramas
|
||||
self.fixedDramaGroup = result.update_dramas.first
|
||||
@ -162,6 +164,7 @@ final class IndexModel {
|
||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: firstId, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
if groups.count > 0 {
|
||||
preloadGroupImages(groups: groups)
|
||||
|
||||
displayDramaGroups(self.updateDramaGroups, label: "before")
|
||||
await MainActor.run {
|
||||
@ -178,6 +181,8 @@ final class IndexModel {
|
||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
if groups.count > 0 {
|
||||
preloadGroupImages(groups: groups)
|
||||
|
||||
displayDramaGroups(self.updateDramaGroups, label: "before")
|
||||
await MainActor.run {
|
||||
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||
@ -197,6 +202,8 @@ final class IndexModel {
|
||||
self.updateDramaGroups.removeAll()
|
||||
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
||||
if case let .result(groups) = response {
|
||||
preloadGroupImages(groups: groups)
|
||||
|
||||
await MainActor.run {
|
||||
self.updateDramaGroups = groups
|
||||
self.fixedDramaGroup = groups.first
|
||||
@ -239,6 +246,21 @@ final class IndexModel {
|
||||
return sortDramaGroups(groups: targetGroups)
|
||||
}
|
||||
|
||||
// 预加载图片信息
|
||||
private func preloadGroupImages(groups: [UpdateDramaGroup]) {
|
||||
let urls = groups.flatMap { group in
|
||||
return group.items.map {item in item.thumb}
|
||||
}
|
||||
|
||||
guard urls.count > 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
Task.detached {
|
||||
try? await CacheManager.shared.preloadImages(urls: urls)
|
||||
}
|
||||
}
|
||||
|
||||
// 按照日期进行排序
|
||||
private func sortDramaGroups(groups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
|
||||
return groups.sorted { g0, g1 in
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user