fix cache
This commit is contained in:
parent
4fb871ddb9
commit
5083375194
@ -118,8 +118,8 @@ struct API {
|
|||||||
}
|
}
|
||||||
|
|
||||||
print("request url: \(request.url!.absoluteString)")
|
print("request url: \(request.url!.absoluteString)")
|
||||||
// let x = String(data: data, encoding: .utf8)!
|
let x = String(data: data, encoding: .utf8)!
|
||||||
// print("url: \(request.url!.path()), data is: \(x)")
|
print("url: \(request.url!.path()), data is: \(x)")
|
||||||
do {
|
do {
|
||||||
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
||||||
return .result(result.result)
|
return .result(result.result)
|
||||||
|
|||||||
@ -23,6 +23,10 @@ final class CacheManager {
|
|||||||
func preloadImages(urls: [String]) async throws {
|
func preloadImages(urls: [String]) async throws {
|
||||||
let preloadUrls = urls.filter { url in !self.fileExists(urlString: url)}
|
let preloadUrls = urls.filter { url in !self.fileExists(urlString: url)}
|
||||||
|
|
||||||
|
guard preloadUrls.count > 0 else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 批量异步加载
|
// 批量异步加载
|
||||||
try await withThrowingTaskGroup(of: Void.self) { group in
|
try await withThrowingTaskGroup(of: Void.self) { group in
|
||||||
for url in preloadUrls {
|
for url in preloadUrls {
|
||||||
|
|||||||
@ -142,22 +142,6 @@ struct IndexMainView: View {
|
|||||||
.onPreferenceChange(DramaGroupElementPreferenceKey.self) { frames in
|
.onPreferenceChange(DramaGroupElementPreferenceKey.self) { frames in
|
||||||
indexModel.visiblePublisher.send(frames)
|
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):
|
case .error(let code, let message):
|
||||||
print("index load data get error_code: \(code), message: \(message)")
|
print("index load data get error_code: \(code), message: \(message)")
|
||||||
case .result(let result):
|
case .result(let result):
|
||||||
|
preloadGroupImages(groups: result.update_dramas)
|
||||||
|
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
self.updateDramaGroups = result.update_dramas
|
self.updateDramaGroups = result.update_dramas
|
||||||
self.fixedDramaGroup = result.update_dramas.first
|
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)
|
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: firstId, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
if groups.count > 0 {
|
if groups.count > 0 {
|
||||||
|
preloadGroupImages(groups: groups)
|
||||||
|
|
||||||
displayDramaGroups(self.updateDramaGroups, label: "before")
|
displayDramaGroups(self.updateDramaGroups, label: "before")
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
@ -178,6 +181,8 @@ final class IndexModel {
|
|||||||
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self)
|
let response = await API.loadMoreUpdateDramas(userId: userId, mode: mode, id: lastId, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
if groups.count > 0 {
|
if groups.count > 0 {
|
||||||
|
preloadGroupImages(groups: groups)
|
||||||
|
|
||||||
displayDramaGroups(self.updateDramaGroups, label: "before")
|
displayDramaGroups(self.updateDramaGroups, label: "before")
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
self.updateDramaGroups = appendMergeDramaGroups(groups: self.updateDramaGroups, mergeGroups: groups)
|
||||||
@ -197,6 +202,8 @@ final class IndexModel {
|
|||||||
self.updateDramaGroups.removeAll()
|
self.updateDramaGroups.removeAll()
|
||||||
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
let response = await API.loadDateUpdateDramas(userId: userId, date: date, as: [UpdateDramaGroup].self)
|
||||||
if case let .result(groups) = response {
|
if case let .result(groups) = response {
|
||||||
|
preloadGroupImages(groups: groups)
|
||||||
|
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
self.updateDramaGroups = groups
|
self.updateDramaGroups = groups
|
||||||
self.fixedDramaGroup = groups.first
|
self.fixedDramaGroup = groups.first
|
||||||
@ -239,6 +246,21 @@ final class IndexModel {
|
|||||||
return sortDramaGroups(groups: targetGroups)
|
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] {
|
private func sortDramaGroups(groups: [UpdateDramaGroup]) -> [UpdateDramaGroup] {
|
||||||
return groups.sorted { g0, g1 in
|
return groups.sorted { g0, g1 in
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user