Change size of images and add prefetch

This commit is contained in:
Marcin Czachurski 2023-10-22 18:51:55 +02:00
parent fc4d6f7f42
commit 9703d26656
1 changed files with 23 additions and 2 deletions

View File

@ -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)
}
}