149 lines
4.3 KiB
Swift
149 lines
4.3 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(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..<post.images.count, id: \.self) { index in
|
|
Circle()
|
|
.fill(index == currentIndex ? Color.white : Color.gray)
|
|
.frame(width: 10, height: 10)
|
|
}
|
|
}
|
|
.padding(.bottom, 10)
|
|
}
|
|
}
|
|
HStack(spacing: 5) {
|
|
Button(action: shiftBack) {
|
|
NavigationIcon(symbol: .arrowTurnUpLeft, edge: .trailing)
|
|
}
|
|
Button(action: shiftForward) {
|
|
NavigationIcon(symbol: .arrowTurnUpRight, edge: .leading)
|
|
}
|
|
Spacer()
|
|
Button(action: { showImagePicker = true }) {
|
|
NavigationIcon(symbol: .plus, edge: .all)
|
|
}
|
|
Button(action: removeImage) {
|
|
NavigationIcon(symbol: .trash, edge: .all)
|
|
}
|
|
}.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
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|