diff --git a/Vernissage/CoreData/StatusData+CoreDataProperties.swift b/Vernissage/CoreData/StatusData+CoreDataProperties.swift index 6130b71..649ed87 100644 --- a/Vernissage/CoreData/StatusData+CoreDataProperties.swift +++ b/Vernissage/CoreData/StatusData+CoreDataProperties.swift @@ -39,8 +39,7 @@ extension StatusData { @NSManaged public var uri: String? @NSManaged public var url: URL? @NSManaged public var visibility: String - @NSManaged public var attachmentRelation: NSSet? - + @NSManaged public var attachmentRelation: Set? } // MARK: Generated accessors for attachmentRelation @@ -63,3 +62,9 @@ extension StatusData { extension StatusData : Identifiable { } + +extension StatusData { + func attachments() -> [AttachmentData] { + return Array(self.attachmentRelation ?? []) + } +} diff --git a/Vernissage/Vernissage.xcdatamodeld/Vernissage.xcdatamodel/contents b/Vernissage/Vernissage.xcdatamodeld/Vernissage.xcdatamodel/contents index 1a9569d..9fdca4d 100644 --- a/Vernissage/Vernissage.xcdatamodeld/Vernissage.xcdatamodel/contents +++ b/Vernissage/Vernissage.xcdatamodeld/Vernissage.xcdatamodel/contents @@ -26,7 +26,7 @@ - + diff --git a/Vernissage/Views/DetailsView.swift b/Vernissage/Views/DetailsView.swift index 5265a17..ab6a06d 100644 --- a/Vernissage/Views/DetailsView.swift +++ b/Vernissage/Views/DetailsView.swift @@ -6,19 +6,28 @@ import SwiftUI import MastodonSwift +import AVFoundation struct DetailsView: View { @Environment(\.dismiss) private var dismiss + @State public var statusData: StatusData + @State private var height: Double = 0.0 var body: some View { ScrollView { VStack (alignment: .leading) { - if let attachmentData = statusData.attachmentRelation?.first(where: { elemet in true}) as? AttachmentData { - Image(uiImage: UIImage(data: attachmentData.data)!) - .resizable().aspectRatio(contentMode: .fit) - .frame(maxWidth: .infinity) + TabView { + ForEach(statusData.attachments(), id: \.self) { attachment in + if let image = UIImage(data: attachment.data) { + Image(uiImage: image) + .resizable() + .aspectRatio(contentMode: .fit) + } + } } + .frame(height: CGFloat(self.height)) + .tabViewStyle(PageTabViewStyle()) VStack(alignment: .leading) { HStack (alignment: .center) { @@ -88,6 +97,26 @@ struct DetailsView: View { } } .navigationBarTitle("Details") + .onAppear { + self.calculateImageHeight() + } + } + + private func calculateImageHeight() { + var imageHeight = 0.0 + var imageWidth = 0.0 + + for item in statusData.attachments() { + if let image = UIImage(data: item.data) { + if image.size.height > imageHeight { + imageHeight = image.size.height + imageWidth = image.size.width + } + } + } + + let divider = imageWidth / UIScreen.main.bounds.size.width + self.height = imageHeight / divider } } diff --git a/Vernissage/Views/HomeFeedView.swift b/Vernissage/Views/HomeFeedView.swift index 5b0f075..9b0b8da 100644 --- a/Vernissage/Views/HomeFeedView.swift +++ b/Vernissage/Views/HomeFeedView.swift @@ -3,7 +3,6 @@ // Copyright © 2022 Marcin Czachurski and the repository contributors. // Licensed under the MIT License. // - import SwiftUI import MastodonSwift @@ -25,14 +24,16 @@ struct HomeFeedView: View { ZStack { ScrollView { LazyVGrid(columns: gridColumns) { - ForEach(dbStatuses) { item in + ForEach(dbStatuses, id: \.self) { item in NavigationLink(destination: DetailsView(statusData: item)) { - if let attachmenData = item.attachmentRelation?.first(where: { element in true }) as? AttachmentData, + if let attachmenData = item.attachmentRelation?.first, let uiImage = UIImage(data: attachmenData.data) { ZStack { Image(uiImage: uiImage) - .resizable().aspectRatio(contentMode: .fit) + .resizable() + .aspectRatio(contentMode: .fit) + if let count = item.attachmentRelation?.count, count > 1 { VStack(alignment:.trailing) { Spacer() @@ -68,22 +69,6 @@ struct HomeFeedView: View { } } - VStack(alignment:.trailing) { - Spacer() - HStack { - Spacer() - Button { - - } label: { - Image(systemName: "plus") - .font(.body) - .padding(16) - .foregroundColor(.white) - .background(.ultraThinMaterial, in: Circle()) - } - } - }.padding() - if showLoading { ProgressView() .progressViewStyle(CircularProgressViewStyle())