Improve a home timeline performance.

This commit is contained in:
Marcin Czachursk 2023-02-09 19:24:41 +01:00
parent 461df5e453
commit f8b996dae9
3 changed files with 17 additions and 21 deletions

View File

@ -17,11 +17,13 @@ extension AttachmentData {
self.text = attachment.description
self.type = attachment.type.rawValue
if let width = (attachment.meta as? ImageMetadata)?.original?.width {
// We can set image width only when it wasn't previusly recalculated.
if let width = (attachment.meta as? ImageMetadata)?.original?.width, self.metaImageWidth <= 0 && width > 0 {
self.metaImageWidth = Int32(width)
}
if let height = (attachment.meta as? ImageMetadata)?.original?.height {
// We can set image height only when it wasn't previusly recalculated.
if let height = (attachment.meta as? ImageMetadata)?.original?.height, self.metaImageHeight <= 0 && height > 0 {
self.metaImageHeight = Int32(height)
}
}

View File

@ -73,8 +73,10 @@ public class HomeTimelineService {
return statusData
}
public func update(attachment: AttachmentData, withData imageData: Data) {
public func update(attachment: AttachmentData, withData imageData: Data, imageWidth: Double, imageHeight: Double) {
attachment.data = imageData
attachment.metaImageWidth = Int32(imageWidth)
attachment.metaImageHeight = Int32(imageHeight)
self.setExifProperties(in: attachment, from: imageData)
CoreDataHandler.shared.save()

View File

@ -28,14 +28,6 @@ struct ImageRow: View {
if let attachmentData, let size = ImageSizeService.shared.get(for: attachmentData.url) {
self.imageWidth = size.width
self.imageHeight = size.height
} else if let attachmentData, let imageData = attachmentData.data, let uiImage = UIImage(data: imageData) {
self.uiImage = uiImage
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: uiImage.size.width,
height: uiImage.size.height)
self.imageWidth = size.width
self.imageHeight = size.height
} else if let attachmentData, attachmentData.metaImageWidth > 0 && attachmentData.metaImageHeight > 0 {
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: attachmentData.metaImageWidth,
@ -128,9 +120,8 @@ struct ImageRow: View {
private func downloadImage(attachmentData: AttachmentData) async {
do {
if let imageData = try await RemoteFileService.shared.fetchData(url: attachmentData.url) {
HomeTimelineService.shared.update(attachment: attachmentData, withData: imageData)
if let downloadedImage = UIImage(data: imageData) {
if let imageData = try await RemoteFileService.shared.fetchData(url: attachmentData.url),
let downloadedImage = UIImage(data: imageData) {
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: downloadedImage.size.width,
@ -138,7 +129,8 @@ struct ImageRow: View {
self.imageWidth = size.width
self.imageHeight = size.height
self.uiImage = downloadedImage
}
HomeTimelineService.shared.update(attachment: attachmentData, withData: imageData, imageWidth: size.width, imageHeight: size.height)
}
} catch {
ErrorService.shared.handle(error, message: "Cannot download the image.")