ChWebsiteApp/CHDataManagement/Views/Posts/PostImageGalleryView.swift
2024-11-20 23:46:54 +01:00

108 lines
3.1 KiB
Swift

import SwiftUI
import SFSafeSymbols
private struct NavigationIcon: View {
let symbol: SFSymbol
let edge: Edge.Set
var body: some View {
SwiftUI.Image(systemSymbol: symbol)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(5)
.padding(edge, 2)
.fontWeight(.light)
.foregroundStyle(.white)
.frame(width: 30, height: 30)
.background(Color.black.opacity(0.6).clipShape(Circle()))
}
}
struct PostImageGalleryView: View {
@ObservedObject
var post: LocalizedPost
@State private var currentIndex = 0
@State
private var showImagePicker = false
var body: some View {
ZStack(alignment: .center) {
ZStack(alignment: .bottomTrailing) {
ZStack(alignment: .bottom) {
post.images[currentIndex]
.imageToDisplay
.resizable()
.scaledToFit()
if post.images.count > 1 {
HStack(spacing: 8) {
ForEach(0..<post.images.count, id: \.self) { index in
Circle()
.fill(index == currentIndex ? Color.white : Color.gray)
.frame(width: 10, height: 10)
}
}
.padding(.bottom, 10)
}
}
Button(action: { showImagePicker = true }) {
Image(systemSymbol: .plusCircleFill)
.resizable()
.renderingMode(.template)
.foregroundStyle(.blue.opacity(0.7))
.frame(width: 30, height: 30)
.padding()
}
}
.buttonStyle(.plain)
.foregroundStyle(.blue)
if post.images.count > 1 {
HStack {
Button(action: previous) {
NavigationIcon(symbol: .chevronLeft, edge: .trailing)
}
.buttonStyle(.plain)
.padding()
Spacer()
Button(action: next) {
NavigationIcon(symbol: .chevronRight, edge: .leading)
}
.buttonStyle(.plain)
.padding()
}
}
}
.sheet(isPresented: $showImagePicker) {
ImagePickerView(
showImagePicker: $showImagePicker,
post: post
)
}
}
private func previous() {
if currentIndex > 0 {
currentIndex -= 1
} else {
currentIndex = post.images.count - 1
}
}
private func next() {
if currentIndex < post.images.count - 1 {
currentIndex += 1
} else {
currentIndex = 0
}
}
}
#Preview(traits: .fixedLayout(width: 300, height: 250)) {
PostImageGalleryView(post: .german)
}