Reduce size of carousel change.

This commit is contained in:
Marcin Czachursk 2023-01-27 15:46:09 +01:00
parent c652b34430
commit 62e1995005
14 changed files with 74 additions and 26 deletions

View File

@ -102,6 +102,7 @@
F89992CE296D92E7005994BF /* AttachmentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89992CD296D92E7005994BF /* AttachmentViewModel.swift */; };
F89A46DC296EAACE0062125F /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89A46DB296EAACE0062125F /* SettingsView.swift */; };
F89A46DE296EABA20062125F /* StatusPlaceholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89A46DD296EABA20062125F /* StatusPlaceholder.swift */; };
F89CEB802984198600A1376F /* AttachmentData+HighestImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89CEB7F2984198600A1376F /* AttachmentData+HighestImage.swift */; };
F89D6C3F29716E41001DA3D4 /* Theme.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89D6C3E29716E41001DA3D4 /* Theme.swift */; };
F89D6C4229717FDC001DA3D4 /* AccountsSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89D6C4129717FDC001DA3D4 /* AccountsSection.swift */; };
F89D6C4429718092001DA3D4 /* AccentsSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = F89D6C4329718092001DA3D4 /* AccentsSection.swift */; };
@ -213,6 +214,7 @@
F89992CD296D92E7005994BF /* AttachmentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentViewModel.swift; sourceTree = "<group>"; };
F89A46DB296EAACE0062125F /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
F89A46DD296EABA20062125F /* StatusPlaceholder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusPlaceholder.swift; sourceTree = "<group>"; };
F89CEB7F2984198600A1376F /* AttachmentData+HighestImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AttachmentData+HighestImage.swift"; sourceTree = "<group>"; };
F89D6C3B29716DBC001DA3D4 /* Vernissage20230113-001.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Vernissage20230113-001.xcdatamodel"; sourceTree = "<group>"; };
F89D6C3E29716E41001DA3D4 /* Theme.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Theme.swift; sourceTree = "<group>"; };
F89D6C4129717FDC001DA3D4 /* AccountsSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSection.swift; sourceTree = "<group>"; };
@ -328,6 +330,7 @@
F80048002961850500E6868A /* AttachmentData+CoreDataProperties.swift */,
F85D498229642FAC00751DF7 /* AttachmentData+Comperable.swift */,
F8210DDE2966CFC7001D9973 /* AttachmentData+Attachment.swift */,
F89CEB7F2984198600A1376F /* AttachmentData+HighestImage.swift */,
F80048012961850500E6868A /* StatusData+CoreDataClass.swift */,
F80048022961850500E6868A /* StatusData+CoreDataProperties.swift */,
F85D49842964301800751DF7 /* StatusData+Attachments.swift */,
@ -674,6 +677,7 @@
F85D49852964301800751DF7 /* StatusData+Attachments.swift in Sources */,
F8210DE72966E1D1001D9973 /* Color+Assets.swift in Sources */,
F88ABD9429687CA4004EF61E /* ComposeView.swift in Sources */,
F89CEB802984198600A1376F /* AttachmentData+HighestImage.swift in Sources */,
F86B7214296BFDCE00EE59EC /* UserProfileHeader.swift in Sources */,
F88E4D46297E89DF0057491A /* TrendsService.swift in Sources */,
F85D497D29640D5900751DF7 /* InteractionRow.swift in Sources */,

View File

@ -13,9 +13,10 @@ extension View {
switch destination {
case .tag(let hashTag):
StatusesView(listType: .hashtag(tag: hashTag))
case .status(let id, let blurhash, let metaImageWidth, let metaImageHeight):
case .status(let id, let blurhash, let highestImageUrl, let metaImageWidth, let metaImageHeight):
StatusView(statusId: id,
imageBlurhash: blurhash,
highestImageUrl: highestImageUrl,
imageWidth: metaImageWidth,
imageHeight: metaImageHeight)
case .statuses(let listType):

View File

@ -0,0 +1,24 @@
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import Foundation
extension [AttachmentData] {
func getHighestImage() -> AttachmentData? {
var attachment = self.first
var imgHeight = 0.0
for item in self {
let attachmentheight = Double(item.metaImageHeight)
if attachmentheight > imgHeight {
attachment = item
imgHeight = attachmentheight
}
}
return attachment
}
}

View File

@ -10,7 +10,7 @@ import MastodonKit
enum RouteurDestinations: Hashable {
case tag(hashTag: String)
case status(id: String, blurhash: String? = nil, metaImageWidth: Int32? = nil, metaImageHeight: Int32? = nil)
case status(id: String, blurhash: String? = nil, highestImageUrl: URL? = nil, metaImageWidth: Int32? = nil, metaImageHeight: Int32? = nil)
case statuses(listType: StatusesView.ListType)
case userProfile(accountId: String, accountDisplayName: String?, accountUserName: String)
case accounts(entityId: String, listType: AccountsView.ListType)

View File

@ -116,3 +116,20 @@ public class AttachmentViewModel: ObservableObject {
}
}
}
extension [AttachmentViewModel] {
func getHighestImage() -> AttachmentViewModel? {
var attachment = self.first
var imgHeight = 0.0
for item in self {
let attachmentheight = Double((item.meta as? ImageMetadata)?.original?.height ?? 0)
if attachmentheight > imgHeight {
attachment = item
imgHeight = attachmentheight
}
}
return attachment
}
}

View File

@ -143,7 +143,8 @@ public class StatusViewModel: ObservableObject {
public extension StatusViewModel {
func getImageWidth() -> Int32? {
if let width = (self.mediaAttachments.first?.meta as? ImageMetadata)?.original?.width {
let highestImage = self.mediaAttachments.getHighestImage()
if let width = (highestImage?.meta as? ImageMetadata)?.original?.width {
return Int32(width)
} else {
return nil
@ -151,7 +152,8 @@ public extension StatusViewModel {
}
func getImageHeight() -> Int32? {
if let height = (self.mediaAttachments.first?.meta as? ImageMetadata)?.original?.height {
let highestImage = self.mediaAttachments.getHighestImage()
if let height = (highestImage?.meta as? ImageMetadata)?.original?.height {
return Int32(height)
} else {
return nil

View File

@ -32,6 +32,7 @@ struct HomeFeedView: View {
NavigationLink(value: RouteurDestinations.status(
id: item.rebloggedStatusId ?? item.id,
blurhash: item.attachments().first?.blurhash,
highestImageUrl: item.attachments().getHighestImage()?.url,
metaImageWidth: item.attachments().first?.metaImageWidth,
metaImageHeight: item.attachments().first?.metaImageHeight)
) {

View File

@ -16,6 +16,7 @@ struct StatusView: View {
@State var statusId: String
@State var imageBlurhash: String?
@State var highestImageUrl: URL?
@State var imageWidth: Int32?
@State var imageHeight: Int32?
@ -162,6 +163,10 @@ struct StatusView: View {
}
private func getImageHeight() -> Double {
if let highestImageUrl = self.highestImageUrl, let imageSize = ImageSizeService.shared.getImageSize(for: highestImageUrl) {
return imageSize.height
}
if let imageHeight = self.imageHeight, let imageWidth = self.imageWidth, imageHeight > 0 && imageWidth > 0 {
return self.calculateHeight(width: Double(imageWidth), height: Double(imageHeight))
}

View File

@ -36,6 +36,7 @@ struct StatusesView: View {
NavigationLink(value: RouteurDestinations.status(
id: item.id,
blurhash: item.mediaAttachments.first?.blurhash,
highestImageUrl: item.mediaAttachments.getHighestImage()?.url,
metaImageWidth: item.getImageWidth(),
metaImageHeight: item.getImageHeight())
) {

View File

@ -46,6 +46,7 @@ struct TrendStatusesView: View {
NavigationLink(value: RouteurDestinations.status(
id: item.id,
blurhash: item.mediaAttachments.first?.blurhash,
highestImageUrl: item.mediaAttachments.getHighestImage()?.url,
metaImageWidth: item.getImageWidth(),
metaImageHeight: item.getImageHeight())
) {

View File

@ -30,7 +30,7 @@ struct ImageCarouselPicture: View {
self.onImageDownloaded(attachment, imageData)
}
} catch {
ErrorService.shared.handle(error, message: "Connot download image for status")
ErrorService.shared.handle(error, message: "Cannot download image for status")
}
}
}

View File

@ -36,32 +36,18 @@ struct ImagesCarousel: View {
self.attachments = attachments
self.selected = String.empty()
var imgHeight = 0.0
var imgWidth = 0.0
var attachment: AttachmentViewModel?
for item in attachments {
let attachmentheight = Double((item.meta as? ImageMetadata)?.original?.height ?? 0)
if attachmentheight > imgHeight {
attachment = item
imgHeight = attachmentheight
imgWidth = Double((item.meta as? ImageMetadata)?.original?.width ?? 0)
}
}
// Attachments doesn't have any metadata with sizes, thus we have to use first one.
if attachment == nil {
attachment = attachments.first
}
let highestImage = attachments.getHighestImage()
let imgHeight = Double((highestImage?.meta as? ImageMetadata)?.original?.height ?? 0)
let imgWidth = Double((highestImage?.meta as? ImageMetadata)?.original?.width ?? 0)
// Calculate size of frame (first from cache, then from metadata).
if let attachment, let size = ImageSizeService.shared.getImageSize(for: attachment.url) {
if let highestImage, let size = ImageSizeService.shared.getImageSize(for: highestImage.url) {
self.imageWidth = size.width
self.imageHeight = size.height
self.heightWasPrecalculated = true
} else if let attachment, imgHeight > 0 && imgWidth > 0 {
let size = ImageSizeService.shared.calculateSize(for: attachment.url, width: imgWidth, height: imgHeight)
} else if let highestImage, imgHeight > 0 && imgWidth > 0 {
let size = ImageSizeService.shared.calculateSize(for: highestImage.url, width: imgWidth, height: imgHeight)
self.imageWidth = size.width
self.imageHeight = size.height
@ -71,6 +57,10 @@ struct ImagesCarousel: View {
self.imageHeight = UIScreen.main.bounds.width * 0.75
self.heightWasPrecalculated = false
}
print(self.heightWasPrecalculated)
print(self.imageWidth)
print(self.imageHeight)
}
var body: some View {
@ -89,7 +79,7 @@ struct ImagesCarousel: View {
.tag(attachment.id)
}
}
.frame(height: CGFloat(self.imageHeight))
.frame(height: self.imageHeight)
.tabViewStyle(PageTabViewStyle())
.onChange(of: selected, perform: { index in
self.selectedAttachmentId = selected

View File

@ -109,6 +109,7 @@ struct NotificationRow: View {
let statusViewModel = StatusViewModel(status: status)
self.routerPath.navigate(to: .status(id: statusViewModel.id,
blurhash: statusViewModel.mediaAttachments.first?.blurhash,
highestImageUrl: statusViewModel.mediaAttachments.getHighestImage()?.url,
metaImageWidth: statusViewModel.getImageWidth(),
metaImageHeight: statusViewModel.getImageHeight()))
}

View File

@ -24,6 +24,7 @@ struct UserProfileStatuses: View {
NavigationLink(value: RouteurDestinations.status(
id: item.id,
blurhash: item.mediaAttachments.first?.blurhash,
highestImageUrl: item.mediaAttachments.getHighestImage()?.url,
metaImageWidth: item.getImageWidth(),
metaImageHeight: item.getImageHeight())
) {