Allow videos in posts, simplify post image view

This commit is contained in:
Christoph Hagen
2025-01-17 23:24:56 +01:00
parent 60716fca20
commit bc3f21e7e4
10 changed files with 175 additions and 172 deletions

View File

@ -15,7 +15,11 @@ struct PostContentView: View {
}
var body: some View {
LocalizedPostContentView(post: post)
LocalizedPostContentView(
post: post.localized(in: language),
other: post.localized(in: language.next),
tags: $post.tags,
page: $post.linkedPage)
}
}
@ -28,46 +32,6 @@ extension PostContentView: MainContentView {
static let itemDescription = "a post"
}
private struct LocalizedTitle: View {
@ObservedObject
private var post: LocalizedPost
init(post: LocalizedPost) {
self.post = post
}
var body: some View {
OptionalTextField("", text: $post.title)
.font(.system(size: 24, weight: .bold))
.foregroundStyle(Color.primary)
.textFieldStyle(.plain)
.lineLimit(2)
.frame(minHeight: 30)
}
}
private struct LocalizedContentEditor: View {
@ObservedObject
private var post: LocalizedPost
init(post: LocalizedPost) {
self.post = post
}
var body: some View {
TextEditor(text: $post.text)
.font(.body)
.frame(minHeight: 150)
.textEditorStyle(.plain)
.padding(.vertical, 8)
.padding(.leading, 3)
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
}
}
private struct LinkedPageTagView: View {
@ObservedObject
@ -80,42 +44,114 @@ private struct LinkedPageTagView: View {
struct LocalizedPostContentView: View {
@ObservedObject
var post: Post
@Environment(\.language)
private var language
@EnvironmentObject
private var content: Content
init(post: Post) {
@ObservedObject
var post: LocalizedPost
@ObservedObject
var other: LocalizedPost
@Binding
var tags: [Tag]
@Binding
var page: Page?
@State
private var fileTypeToSelect: FileTypeCategory = .image
@State
private var showImagePicker = false
init(post: LocalizedPost, other: LocalizedPost, tags: Binding<[Tag]>, page: Binding<Page?>) {
self.post = post
self.other = other
self._tags = tags
self._page = page
}
var body: some View {
VStack(alignment: .leading) {
HStack {
Text("Images")
Text("Images/Video")
.font(.headline)
Button("Transfer from \(language.next.text)", action: copyImagesFromOtherLanguage)
.disabled(post.localized(in: language.next).images.isEmpty)
Button("Images") {
fileTypeToSelect = .image
showImagePicker = true
}
.disabled(post.hasVideos)
Button("Videos") {
fileTypeToSelect = .video
showImagePicker = true
}
.disabled(post.hasImages)
Button("Transfer from \(language.next.text)") {
post.images = other.images
}
.disabled(other.images.isEmpty)
}
PostImagesView(post: post.localized(in: language))
LocalizedTitle(post: post.localized(in: language))
if let page = post.linkedPage {
ScrollView(.horizontal) {
HStack(alignment: .center, spacing: 8) {
ForEach(post.images) { image in
if image.type.isImage {
image.imageToDisplay
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: 300, maxHeight: 200)
.cornerRadius(8)
} else {
VStack {
Image(systemSymbol: .film)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 100)
Text(image.id)
.font(.title)
}
//.foregroundStyle(.secondary)
.frame(width: 300, height: 200)
.background(Color.gray)
.cornerRadius(8)
}
}
}
}
OptionalTextField("", text: $post.title)
.font(.system(size: 24, weight: .bold))
.foregroundStyle(Color.primary)
.textFieldStyle(.plain)
.lineLimit(2)
.frame(minHeight: 30)
if let page = page {
LinkedPageTagView(page: page)
} else {
TagDisplayView(tags: $post.tags)
TagDisplayView(tags: $tags)
}
LocalizedContentEditor(post: post.localized(in: language))
TextEditor(text: $post.text)
.font(.body)
.frame(minHeight: 150)
.textEditorStyle(.plain)
.padding(.vertical, 8)
.padding(.leading, 3)
.background(Color.gray.opacity(0.1))
.cornerRadius(8)
}
.padding()
.sheet(isPresented: $showImagePicker) {
MultiFileSelectionView(
selectedFiles: $post.images,
allowedType: fileTypeToSelect)
}
}
private func copyImagesFromOtherLanguage() {
let images = post.localized(in: language.next).images
post.localized(in: language).images = images
post.images = other.images
}
}

View File

@ -1,96 +0,0 @@
import SwiftUI
struct PostImagesView: View {
@ObservedObject
var post: LocalizedPost
@State
private var showImagePicker = false
var body: some View {
ScrollView(.horizontal) {
HStack(alignment: .center, spacing: 8) {
ForEach(post.images) { image in
ZStack {
image.imageToDisplay
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: 300, maxHeight: 200)
.cornerRadius(8)
.layoutPriority(1)
VStack {
HStack(alignment: .top) {
Button(action: { remove(image) }) {
NavigationIcon(symbol: .trash, edge: .all)
}
.buttonStyle(.plain)
Spacer()
Text(image.id)
.padding(4)
.foregroundStyle(Color.white.opacity(0.8))
.background(RoundedRectangle(cornerRadius: 8).fill(Color.black.opacity(0.7)))
}
Spacer()
HStack {
Button(action: { shiftLeft(image) }) {
NavigationIcon(symbol: .chevronLeft, edge: .trailing)
}
.buttonStyle(.plain)
Spacer()
Button(action: { shiftRight(image) }) {
NavigationIcon(symbol: .chevronRight, edge: .leading)
}
.buttonStyle(.plain)
}
}
.padding()
}
}
Button(action: { showImagePicker = true }) {
NavigationIcon(symbol: .plus, edge: .all)
}
.buttonStyle(.plain)
.padding()
}
}
.sheet(isPresented: $showImagePicker) {
MultiFileSelectionView(selectedFiles: $post.images, allowedType: .image)
}
}
private func shiftLeft(_ image: FileResource) {
guard let index = post.images.firstIndex(of: image) else {
return
}
guard index > 0 else {
return
}
post.images.swapAt(index, index - 1)
}
private func shiftRight(_ image: FileResource) {
guard let index = post.images.firstIndex(of: image) else {
return
}
guard index < post.images.count - 1 else {
return
}
post.images.swapAt(index, index + 1)
}
private func remove(_ image: FileResource) {
guard let index = post.images.firstIndex(of: image) else {
return
}
post.images.remove(at: index)
}
}
#Preview {
VStack(alignment: .leading) {
Text("Images")
.font(.headline)
PostImagesView(post: .english)
}
}