import SwiftUI import SFSafeSymbols 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(Color.white.opacity(0.8)) .frame(width: 30, height: 30) .background(Color.black.opacity(0.7).clipShape(Circle())) } } struct PostImageGalleryView: View { @ObservedObject var post: LocalizedPost @State private var currentIndex = 0 @State private var showImagePicker = false private var imageAtCurrentIndex: Image? { guard !post.images.isEmpty else { return nil } guard currentIndex < post.images.count else { return post.images.last?.imageToDisplay } return post.images[currentIndex].imageToDisplay } var body: some View { ZStack(alignment: .center) { ZStack(alignment: .bottomTrailing) { ZStack(alignment: .bottom) { if let imageAtCurrentIndex { imageAtCurrentIndex .resizable() .scaledToFit() } if post.images.count > 1 { HStack(spacing: 8) { ForEach(0.. 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) { image in post.images.append(image) } } } 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 } } private func shiftBack() { guard currentIndex > 0 else { return } post.images.swapAt(currentIndex, currentIndex-1) currentIndex -= 1 } private func shiftForward() { guard currentIndex < post.images.count - 1 else { return } post.images.swapAt(currentIndex, currentIndex+1) currentIndex += 1 } private func removeImage() { post.images.remove(at: currentIndex) if currentIndex >= post.images.count { currentIndex = post.images.count - 1 } } } #Preview(traits: .fixedLayout(width: 300, height: 250)) { PostImageGalleryView(post: .german) }