Add images carousel

This commit is contained in:
Marcin Czachursk 2023-01-02 19:12:25 +01:00
parent 5d893a4b42
commit cc73acedd0
4 changed files with 46 additions and 27 deletions

View File

@ -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<AttachmentData>?
}
// MARK: Generated accessors for attachmentRelation
@ -63,3 +62,9 @@ extension StatusData {
extension StatusData : Identifiable {
}
extension StatusData {
func attachments() -> [AttachmentData] {
return Array(self.attachmentRelation ?? [])
}
}

View File

@ -26,7 +26,7 @@
</entity>
<entity name="AttachmentData" representedClassName="AttachmentData" syncable="YES">
<attribute name="blurhash" optional="YES" attributeType="String"/>
<attribute name="data" attributeType="Binary"/>
<attribute name="data" attributeType="Binary" allowsExternalBinaryDataStorage="YES"/>
<attribute name="id" attributeType="String"/>
<attribute name="previewUrl" optional="YES" attributeType="URI"/>
<attribute name="remoteUrl" optional="YES" attributeType="URI"/>

View File

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

View File

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