From 5083375194318bc885287dd9f5a8fdfeacc83031 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 14 Apr 2025 11:39:57 +0800 Subject: [PATCH] fix cache --- dimensionhub/Core/API.swift | 6 +++--- dimensionhub/Core/CacheManager.swift | 4 ++++ dimensionhub/Views/Index/IndexMainView.swift | 20 ++---------------- dimensionhub/Views/Index/IndexModel.swift | 22 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/dimensionhub/Core/API.swift b/dimensionhub/Core/API.swift index 2a27b8e..2f907dd 100644 --- a/dimensionhub/Core/API.swift +++ b/dimensionhub/Core/API.swift @@ -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.self, from: data) return .result(result.result) diff --git a/dimensionhub/Core/CacheManager.swift b/dimensionhub/Core/CacheManager.swift index a78eaf7..ddeda8a 100644 --- a/dimensionhub/Core/CacheManager.swift +++ b/dimensionhub/Core/CacheManager.swift @@ -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 { diff --git a/dimensionhub/Views/Index/IndexMainView.swift b/dimensionhub/Views/Index/IndexMainView.swift index 789c80f..3576198 100644 --- a/dimensionhub/Views/Index/IndexMainView.swift +++ b/dimensionhub/Views/Index/IndexMainView.swift @@ -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)) - - } - - } } } diff --git a/dimensionhub/Views/Index/IndexModel.swift b/dimensionhub/Views/Index/IndexModel.swift index d4f6c3e..dce9f79 100644 --- a/dimensionhub/Views/Index/IndexModel.swift +++ b/dimensionhub/Views/Index/IndexModel.swift @@ -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