Skip to content

Commit

Permalink
#1171: Set aspect ratio to 3:4 for vertical media
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatbrown committed Aug 22, 2024
1 parent 0f45d61 commit d4a275d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Nos/Views/Components/Media/ImageButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ struct ImageButton: View {
/// Whether the image viewer is presented or not.
@State private var isViewerPresented = false

/// The size of the image that was loaded.
@State private var imageSize: CGSize?

var body: some View {
Button {
isViewerPresented = true
} label: {
WebImage(url: url)
.onSuccess { image, _, _ in
imageSize = image.size
}
.resizable()
.indicator(.activity)
.aspectRatio(contentMode: .fill)
Expand All @@ -23,13 +29,22 @@ struct ImageButton: View {
minHeight: 0,
maxHeight: .infinity
)
.aspectRatio(4 / 3, contentMode: .fit)
.aspectRatio(aspectRatio, contentMode: .fit)
.clipShape(.rect)
}
.sheet(isPresented: $isViewerPresented) {
ImageViewer(url: url)
}
}

/// The aspect ratio of the view. If the image is loaded and is taller than wide, this returns 3/4. Otherwise, 4/3.
var aspectRatio: CGFloat {
if let imageSize, imageSize.height > imageSize.width {
return 3 / 4
} else {
return 4 / 3
}
}
}

#Preview {
Expand Down

0 comments on commit d4a275d

Please sign in to comment.