diff --git a/Vernissage/Widgets/ImagesGrid.swift b/Vernissage/Widgets/ImagesGrid.swift index 04a7f55..74f1bcd 100644 --- a/Vernissage/Widgets/ImagesGrid.swift +++ b/Vernissage/Widgets/ImagesGrid.swift @@ -7,6 +7,7 @@ import SwiftUI import PixelfedKit import ClientKit +import Nuke import NukeUI import ServicesKit @@ -21,9 +22,10 @@ struct ImagesGrid: View { @Environment(RouterPath.self) var routerPath private let maxImages = 5 + private let imagePrefetcher = ImagePrefetcher(destination: .diskCache) @State public var gridType: GridType - @State public var maxHeight = UIDevice.isIPhone ? 120.0 : 240.0 + @State public var maxHeight = UIDevice.isIPhone ? 160.0 : 240.0 @State private var photoUrls: [PhotoUrl] = [ PhotoUrl(id: UUID().uuidString), @@ -50,7 +52,7 @@ struct ImagesGrid: View { } } .gallery { properties in - self.maxHeight = properties.horizontalSize == .compact ? 120.0 : 240.0 + self.maxHeight = properties.horizontalSize == .compact ? 160.0 : 240.0 } .frame(height: self.maxHeight) .onFirstAppear { @@ -74,6 +76,10 @@ struct ImagesGrid: View { let statusesFromApi = try await self.loadStatuses() let statusesWithImages = statusesFromApi.getStatusesWithImagesOnly() + + let photoUrls = self.getPhotoUrls(statuses: statusesWithImages) + self.prefetch(photoUrls: photoUrls) + self.updatePhotos(statusesWithImages: statusesWithImages) } catch { ErrorService.shared.handle(error, message: "global.error.errorDuringDataLoad", showToastr: !Task.isCancelled) @@ -124,4 +130,19 @@ struct ImagesGrid: View { return try await self.client.accounts?.statuses(createdBy: accountId, onlyMedia: true, limit: 10) ?? [] } } + + private func getPhotoUrls(statuses: [Status]) -> [URL] { + var photoUrls: [URL] = [] + for status in statuses { + if let photoUrl = status.getAllImageMediaAttachments().first?.url { + photoUrls.append(photoUrl) + } + } + + return photoUrls + } + + private func prefetch(photoUrls: [URL]) { + imagePrefetcher.startPrefetching(with: photoUrls) + } }