Consolidate images and files
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
enum ColorPalette {
|
||||
|
||||
static let tagBackground = Color(r: 188, g: 188, b: 188) // Color(r: 9, g: 62, b: 103)
|
||||
|
||||
static let tagForeground = Color.primary // Color(r: 96, g: 186, b: 255)
|
||||
|
||||
static let listBackground = Color(r: 2, g: 15, b: 26)
|
||||
|
||||
static let postBackground = Color(r: 222, g: 222, b: 222) // Color(r: 4, g: 31, b: 52)
|
||||
|
||||
static let postText = Color(r: 221, g: 221, b: 221)
|
||||
|
||||
static let postDate = tagForeground
|
||||
|
||||
static let link = Color.blue
|
||||
|
||||
}
|
||||
|
109
CHDataManagement/Views/Files/AddFileView.swift
Normal file
109
CHDataManagement/Views/Files/AddFileView.swift
Normal file
@ -0,0 +1,109 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AddFileView: View {
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss: DismissAction
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
var selectedFile: FileResource?
|
||||
|
||||
@Binding
|
||||
var selectedImage: ImageResource?
|
||||
|
||||
@State
|
||||
private var filesToAdd: [FileToAdd] = []
|
||||
|
||||
init(selectedImage: Binding<ImageResource?>, selectedFile: Binding<FileResource?>) {
|
||||
_selectedFile = selectedFile
|
||||
_selectedImage = selectedImage
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
Text("Select files to add")
|
||||
.foregroundStyle(.secondary)
|
||||
List {
|
||||
ForEach(filesToAdd) { file in
|
||||
FileToAddView(file: file, delete: delete)
|
||||
}
|
||||
}.frame(minHeight: 300)
|
||||
HStack {
|
||||
Button("Cancel", role: .cancel) { dismiss() }
|
||||
Button("Select more files", action: openFilePanel)
|
||||
Button("Add selected", action: importSelectedFiles)
|
||||
.disabled(filesToAdd.isEmpty)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
private func openFilePanel() {
|
||||
let panel = NSOpenPanel()
|
||||
|
||||
panel.canChooseFiles = true
|
||||
panel.canChooseDirectories = false
|
||||
panel.allowsMultipleSelection = true
|
||||
panel.showsHiddenFiles = false
|
||||
panel.title = "Select files to add"
|
||||
panel.prompt = ""
|
||||
|
||||
let response = panel.runModal()
|
||||
guard response == .OK else {
|
||||
print("Failed to select files to import")
|
||||
return
|
||||
}
|
||||
|
||||
for url in panel.urls {
|
||||
guard !filesToAdd.contains(where: { $0.url == url }) else {
|
||||
print("Skipping already selected file \(url.path())")
|
||||
continue
|
||||
}
|
||||
print("Selected file \(url.path())")
|
||||
let newFile = FileToAdd(content: content, url: url)
|
||||
filesToAdd.append(newFile)
|
||||
}
|
||||
}
|
||||
|
||||
private func delete(file: FileToAdd) {
|
||||
guard let index = filesToAdd.firstIndex(of: file) else {
|
||||
return
|
||||
}
|
||||
filesToAdd.remove(at: index)
|
||||
}
|
||||
|
||||
private func importSelectedFiles() {
|
||||
for file in filesToAdd {
|
||||
guard file.isSelected else {
|
||||
print("Skipping unselected file \(file.uniqueId)")
|
||||
continue
|
||||
}
|
||||
guard !file.idAlreadyExists else {
|
||||
print("Skipping existing file \(file.uniqueId)")
|
||||
continue
|
||||
}
|
||||
|
||||
guard content.storage.copyFile(at: file.url, fileId: file.uniqueId) else {
|
||||
print("Failed to import file '\(file.uniqueId)' at \(file.url.path())")
|
||||
return
|
||||
}
|
||||
|
||||
let resource = FileResource(
|
||||
content: content,
|
||||
id: file.uniqueId,
|
||||
en: "", de: "")
|
||||
// TODO: Insert at correct index?
|
||||
content.files.insert(resource, at: 0)
|
||||
selectedFile = resource
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AddFileView(selectedImage: .constant(nil),
|
||||
selectedFile: .constant(nil))
|
||||
}
|
@ -1,37 +1,68 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct FileContentView: View {
|
||||
|
||||
private let iconSize: CGFloat = 150
|
||||
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var fileContent: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
if fileContent != "" {
|
||||
TextEditor(text: $fileContent)
|
||||
.font(.body.monospaced())
|
||||
.textEditorStyle(.plain)
|
||||
} else {
|
||||
Text("The file is not a text file")
|
||||
.onAppear(perform: loadFileContent)
|
||||
switch file.type {
|
||||
case .image:
|
||||
file.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
case .model:
|
||||
VStack {
|
||||
Image(systemSymbol: .cubeTransparent)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .text, .code:
|
||||
TextFileContentView(file: file)
|
||||
.id(file.id)
|
||||
case .video:
|
||||
VStack {
|
||||
Image(systemSymbol: .film)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
case .other:
|
||||
VStack {
|
||||
Image(systemSymbol: .docQuestionmark)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: iconSize)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}.padding()
|
||||
}
|
||||
}
|
||||
|
||||
private func loadFileContent() {
|
||||
do {
|
||||
fileContent = try content.storage.fileContent(for: file.uniqueId)
|
||||
} catch {
|
||||
print(error)
|
||||
fileContent = ""
|
||||
}
|
||||
extension FileContentView: MainContentView {
|
||||
|
||||
init(item: FileResource) {
|
||||
self.file = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a file"
|
||||
}
|
||||
|
||||
#Preview {
|
||||
|
@ -5,22 +5,75 @@ struct FileDetailView: View {
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
@State
|
||||
private var newId: String
|
||||
|
||||
init(file: FileResource) {
|
||||
self.file = file
|
||||
self.newId = file.id
|
||||
}
|
||||
|
||||
private let allowedCharactersInPostId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-.")).inverted
|
||||
|
||||
private var idExists: Bool {
|
||||
file.content.files.contains { $0.id == newId }
|
||||
}
|
||||
|
||||
private var containsInvalidCharacters: Bool {
|
||||
newId.rangeOfCharacter(from: allowedCharactersInPostId) != nil
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text("File Name")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.uniqueId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.bottom)
|
||||
.disabled(true)
|
||||
Text("Description")
|
||||
HStack {
|
||||
TextField("", text: $newId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
Button(action: setNewId) {
|
||||
Text("Update")
|
||||
}
|
||||
.disabled(newId.isEmpty || containsInvalidCharacters || idExists)
|
||||
}
|
||||
Text("German Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.description)
|
||||
TextField("", text: $file.germanDescription)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
Text("English Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $file.englishDescription)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
if file.type.isImage {
|
||||
Text("Image size")
|
||||
.font(.headline)
|
||||
Text("\(Int(file.size.width)) x \(Int(file.size.height)) (\(file.aspectRatio))")
|
||||
.foregroundStyle(.secondary)
|
||||
#warning("Add button to show image versions")
|
||||
}
|
||||
Spacer()
|
||||
}.padding()
|
||||
}
|
||||
|
||||
private func setNewId() {
|
||||
guard file.content.storage.move(file: file.id, to: newId) else {
|
||||
print("Failed to move file \(file.id)")
|
||||
newId = file.id
|
||||
return
|
||||
}
|
||||
file.id = newId
|
||||
}
|
||||
}
|
||||
|
||||
extension FileDetailView: MainContentView {
|
||||
|
||||
init(item: FileResource) {
|
||||
self.init(file: item)
|
||||
}
|
||||
|
||||
static let itemDescription = "a file"
|
||||
}
|
||||
|
||||
|
||||
#Preview {
|
||||
FileDetailView(file: .mock)
|
||||
}
|
||||
|
100
CHDataManagement/Views/Files/FileListView.swift
Normal file
100
CHDataManagement/Views/Files/FileListView.swift
Normal file
@ -0,0 +1,100 @@
|
||||
import SwiftUI
|
||||
|
||||
private enum FileFilterType: String, Hashable, CaseIterable, Identifiable {
|
||||
case images
|
||||
case text
|
||||
case videos
|
||||
case other
|
||||
|
||||
var text: String {
|
||||
switch self {
|
||||
case .images: return "Image"
|
||||
case .text: return "Text"
|
||||
case .videos: return "Video"
|
||||
case .other: return "Other"
|
||||
}
|
||||
}
|
||||
|
||||
var id: String {
|
||||
rawValue
|
||||
}
|
||||
|
||||
func matches(_ type: FileType) -> Bool {
|
||||
switch self {
|
||||
case .images: return type.isImage
|
||||
case .text: return type.isTextFile
|
||||
case .videos: return type.isVideo
|
||||
case .other: return type.isOtherFile
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FileListView: View {
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
var selectedFile: FileResource?
|
||||
|
||||
@State
|
||||
private var selectedFileType: FileFilterType = .images
|
||||
|
||||
@State
|
||||
private var searchString = ""
|
||||
|
||||
var filesBySelectedType: [FileResource] {
|
||||
content.files.filter { selectedFileType.matches($0.type) }
|
||||
}
|
||||
|
||||
var filteredFiles: [FileResource] {
|
||||
guard !searchString.isEmpty else {
|
||||
return filesBySelectedType
|
||||
}
|
||||
return filesBySelectedType.filter { $0.id.contains(searchString) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .center) {
|
||||
Picker("", selection: $selectedFileType) {
|
||||
ForEach(FileFilterType.allCases) { type in
|
||||
Text(type.text).tag(type)
|
||||
}
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
.padding(.trailing, 7)
|
||||
TextField("", text: $searchString, prompt: Text("Search"))
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.horizontal, 8)
|
||||
List(filteredFiles, selection: $selectedFile) { file in
|
||||
Text(file.id).tag(file)
|
||||
}
|
||||
.onChange(of: selectedFileType) { oldValue, newValue in
|
||||
guard oldValue != newValue else {
|
||||
return
|
||||
}
|
||||
if let selectedFile,
|
||||
newValue.matches(selectedFile.type) {
|
||||
return
|
||||
}
|
||||
selectedFile = filteredFiles.first
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if selectedFile == nil {
|
||||
selectedFile = content.files.first
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
NavigationSplitView {
|
||||
FileListView(selectedFile: .constant(nil))
|
||||
.environmentObject(Content.mock)
|
||||
.navigationSplitViewColumnWidth(250)
|
||||
} detail: {
|
||||
Text("")
|
||||
.frame(width: 50)
|
||||
}
|
||||
}
|
46
CHDataManagement/Views/Files/FileToAdd.swift
Normal file
46
CHDataManagement/Views/Files/FileToAdd.swift
Normal file
@ -0,0 +1,46 @@
|
||||
import Foundation
|
||||
|
||||
final class FileToAdd: ObservableObject {
|
||||
|
||||
unowned let content: Content
|
||||
|
||||
let url: URL
|
||||
|
||||
@Published
|
||||
var uniqueId: String
|
||||
|
||||
@Published
|
||||
var isSelected: Bool = true
|
||||
|
||||
init(content: Content, url: URL) {
|
||||
self.content = content
|
||||
self.url = url
|
||||
self.uniqueId = url.lastPathComponent
|
||||
}
|
||||
|
||||
var idAlreadyExists: Bool {
|
||||
content.files.contains { $0.id == uniqueId }
|
||||
}
|
||||
}
|
||||
|
||||
extension FileToAdd: Identifiable {
|
||||
|
||||
var id: URL {
|
||||
url
|
||||
}
|
||||
}
|
||||
|
||||
extension FileToAdd: Equatable {
|
||||
|
||||
static func == (lhs: FileToAdd, rhs: FileToAdd) -> Bool {
|
||||
lhs.url == rhs.url
|
||||
}
|
||||
}
|
||||
|
||||
extension FileToAdd: Hashable {
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(url)
|
||||
}
|
||||
}
|
||||
|
45
CHDataManagement/Views/Files/FileToAddView.swift
Normal file
45
CHDataManagement/Views/Files/FileToAddView.swift
Normal file
@ -0,0 +1,45 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct FileToAddView: View {
|
||||
|
||||
@ObservedObject
|
||||
var file: FileToAdd
|
||||
|
||||
let delete: (FileToAdd) -> Void
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
HStack {
|
||||
Image(systemSymbol: file.isSelected ? .checkmarkCircleFill : .circle)
|
||||
.resizable()
|
||||
.frame(width: 20, height: 20)
|
||||
.foregroundStyle(.blue)
|
||||
.onTapGesture {
|
||||
file.isSelected.toggle()
|
||||
}
|
||||
Image(systemSymbol: .trashCircleFill)
|
||||
.resizable()
|
||||
.frame(width: 20, height: 20)
|
||||
.foregroundStyle(.red)
|
||||
.onTapGesture {
|
||||
delete(file)
|
||||
}
|
||||
TextField("", text: $file.uniqueId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.frame(maxWidth: 200)
|
||||
|
||||
}
|
||||
Text(file.url.path())
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
List {
|
||||
FileToAddView(file: .init(content: .mock, url: URL(fileURLWithPath: "/path/to/file.swift")), delete: { _ in })
|
||||
FileToAddView(file: .init(content: .mock, url: URL(fileURLWithPath: "/path/to/file2.swift")), delete: { _ in })
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FilesView: View {
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var selected: FileResource? = nil
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(content.files, selection: $selected) { file in
|
||||
Text(file.uniqueId)
|
||||
.tag(file)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: openFilePanel) {
|
||||
Label("Add file", systemSymbol: .plus)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationSplitViewColumnWidth(min: 250, ideal: 250, max: 250)
|
||||
} content: {
|
||||
if let selected {
|
||||
FileContentView(file: selected)
|
||||
.id(selected.uniqueId)
|
||||
} else {
|
||||
Text("Select a file")
|
||||
}
|
||||
} detail: {
|
||||
if let selected {
|
||||
FileDetailView(file: selected)
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func openFilePanel() {
|
||||
let panel = NSOpenPanel()
|
||||
// Sets up so user can only select a single directory
|
||||
panel.canChooseFiles = true
|
||||
panel.canChooseDirectories = false
|
||||
panel.allowsMultipleSelection = true
|
||||
panel.showsHiddenFiles = false
|
||||
panel.title = "Select files to add"
|
||||
panel.prompt = ""
|
||||
|
||||
let response = panel.runModal()
|
||||
guard response == .OK else {
|
||||
print("Failed to select files to import")
|
||||
return
|
||||
}
|
||||
|
||||
for url in panel.urls {
|
||||
let fileId = url.lastPathComponent
|
||||
guard !content.files.contains(where: { $0.uniqueId == fileId }) else {
|
||||
print("A file '\(fileId)' already exists")
|
||||
continue
|
||||
}
|
||||
let type = FileType(fileExtension: fileId.fileExtension)
|
||||
let file = FileResource(type: type, uniqueId: fileId, description: "")
|
||||
guard content.storage.copyFile(at: url, fileId: fileId) else {
|
||||
print("Failed to import file '\(fileId)'")
|
||||
continue
|
||||
}
|
||||
content.files.insert(file, at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FilesView()
|
||||
.environmentObject(Content.mock)
|
||||
}
|
38
CHDataManagement/Views/Files/TextFileContentView.swift
Normal file
38
CHDataManagement/Views/Files/TextFileContentView.swift
Normal file
@ -0,0 +1,38 @@
|
||||
import SwiftUI
|
||||
|
||||
struct TextFileContentView: View {
|
||||
|
||||
@ObservedObject
|
||||
var file: FileResource
|
||||
|
||||
@State
|
||||
private var fileContent: String = ""
|
||||
|
||||
var body: some View {
|
||||
if fileContent != "" {
|
||||
TextEditor(text: $fileContent)
|
||||
.font(.body.monospaced())
|
||||
.textEditorStyle(.plain)
|
||||
//.background(.clear)
|
||||
} else {
|
||||
VStack {
|
||||
Image(systemSymbol: .docText)
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: 150)
|
||||
Text("No preview available")
|
||||
.font(.title)
|
||||
}
|
||||
.foregroundStyle(.secondary)
|
||||
.onAppear(perform: loadFileContent)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadFileContent() {
|
||||
guard fileContent == "" else {
|
||||
return
|
||||
}
|
||||
fileContent = file.textContent()
|
||||
print("Loaded content of file \(file.id)")
|
||||
}
|
||||
}
|
21
CHDataManagement/Views/Generic/NavigationIcon.swift
Normal file
21
CHDataManagement/Views/Generic/NavigationIcon.swift
Normal file
@ -0,0 +1,21 @@
|
||||
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()))
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FlexibleColumnView<Content, Inner>: View where Content: Identifiable, Inner: View {
|
||||
|
||||
@Binding
|
||||
var items: [Content]
|
||||
|
||||
let maximumItemWidth: CGFloat
|
||||
|
||||
let spacing: CGFloat
|
||||
|
||||
private let content: (_ item: Content, _ width: CGFloat) -> Inner
|
||||
|
||||
init(items: Binding<[Content]>, maximumItemWidth: CGFloat = 300, spacing: CGFloat = 20, content: @escaping (_ item: Content, _ width: CGFloat) -> Inner) {
|
||||
self._items = items
|
||||
self.maximumItemWidth = maximumItemWidth
|
||||
self.spacing = spacing
|
||||
self.content = content
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geometry in
|
||||
let totalWidth = geometry.size.width
|
||||
let columnCount = max(Int((totalWidth + spacing) / (maximumItemWidth + spacing)), 1)
|
||||
let totalSpacing = spacing * CGFloat(columnCount + 1)
|
||||
let trueItemWidth = (totalWidth - totalSpacing) / CGFloat(columnCount)
|
||||
|
||||
let columns = Array(repeating: GridItem(.flexible(), spacing: spacing), count: columnCount)
|
||||
|
||||
ScrollView {
|
||||
LazyVGrid(columns: columns, spacing: spacing) {
|
||||
ForEach(items) { item in
|
||||
content(item, trueItemWidth)
|
||||
}
|
||||
}
|
||||
.padding(spacing)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FlexibleColumnView(items: .constant(MockImage.images), maximumItemWidth: 150) { image, width in
|
||||
image.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.frame(width: width)
|
||||
}
|
||||
}
|
26
CHDataManagement/Views/Images/ImageContentView.swift
Normal file
26
CHDataManagement/Views/Images/ImageContentView.swift
Normal file
@ -0,0 +1,26 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ImageContentView: View {
|
||||
|
||||
@ObservedObject
|
||||
var image: FileResource
|
||||
|
||||
var body: some View {
|
||||
image.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageContentView: MainContentView {
|
||||
|
||||
init(item: FileResource) {
|
||||
self.image = item
|
||||
}
|
||||
|
||||
static let itemDescription = "an image"
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ImageContentView(image: .init(resourceImage: "image1", type: .jpg))
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ImageDetailsView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language
|
||||
|
||||
@ObservedObject
|
||||
var image: ImageResource
|
||||
|
||||
@State
|
||||
private var newId: String
|
||||
|
||||
init(image: ImageResource) {
|
||||
self.image = image
|
||||
self.newId = image.id
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text("Unique identifier")
|
||||
.font(.headline)
|
||||
HStack {
|
||||
TextField("", text: $newId)
|
||||
Button(action: setNewId) {
|
||||
Text("Update")
|
||||
}
|
||||
}
|
||||
Text("German Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $image.germanDescription)
|
||||
Text("English Description")
|
||||
.font(.headline)
|
||||
TextField("", text: $image.englishDescription)
|
||||
Text("Info")
|
||||
.font(.headline)
|
||||
HStack(alignment: .top) {
|
||||
VStack(alignment: .leading) {
|
||||
Text("Original Size")
|
||||
Text("Aspect ratio")
|
||||
}
|
||||
VStack(alignment: .trailing) {
|
||||
Text("\(Int(image.size.width)) x \(Int(image.size.height))")
|
||||
Text("\(image.aspectRatio)")
|
||||
}
|
||||
}.padding(.vertical)
|
||||
Text("Versions")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
private func setNewId() {
|
||||
#warning("Check if ID is unique")
|
||||
// TODO: Clean id
|
||||
image.id = newId
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ImageDetailsView(image: MockImage.images.first!)
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ImagesContentView: View {
|
||||
|
||||
@ObservedObject
|
||||
var image: ImageResource
|
||||
|
||||
var body: some View {
|
||||
image.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ImagesContentView(image: .init(resourceName: "image1", type: .jpg))
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct ImagesView: View {
|
||||
|
||||
@EnvironmentObject
|
||||
var content: Content
|
||||
|
||||
let maximumItemWidth: CGFloat = 300
|
||||
|
||||
let aspectRatio: CGFloat = 1.5
|
||||
|
||||
let spacing: CGFloat = 20
|
||||
|
||||
@State
|
||||
private var selectedImage: ImageResource?
|
||||
|
||||
@State
|
||||
private var showImageDetails = false
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(content.images, selection: $selectedImage) { image in
|
||||
Text(image.id)
|
||||
.tag(image)
|
||||
}
|
||||
} content: {
|
||||
if let selectedImage {
|
||||
ImagesContentView(image: selectedImage)
|
||||
.layoutPriority(1)
|
||||
} else {
|
||||
Text("Select an image in the sidebar")
|
||||
}
|
||||
} detail: {
|
||||
if let selectedImage {
|
||||
ImageDetailsView(image: selectedImage)
|
||||
.frame(maxWidth: 350)
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
let content = Content()
|
||||
content.images = MockImage.images
|
||||
return ImagesView()
|
||||
.environmentObject(content)
|
||||
}
|
93
CHDataManagement/Views/Pages/AddPageView.swift
Normal file
93
CHDataManagement/Views/Pages/AddPageView.swift
Normal file
@ -0,0 +1,93 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AddPageView: View {
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss: DismissAction
|
||||
|
||||
@Environment(\.language)
|
||||
private var language: ContentLanguage
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
var selectedPage: Page?
|
||||
|
||||
@State
|
||||
private var newPageId = ""
|
||||
|
||||
private let allowedCharactersInPageId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
|
||||
|
||||
init(selected: Binding<Page?>) {
|
||||
self._selectedPage = selected
|
||||
}
|
||||
|
||||
private var idExists: Bool {
|
||||
content.pages.contains { $0.id == newPageId }
|
||||
}
|
||||
|
||||
private var containsInvalidCharacters: Bool {
|
||||
newPageId.rangeOfCharacter(from: allowedCharactersInPageId) != nil
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("New page")
|
||||
.font(.headline)
|
||||
|
||||
TextField("", text: $newPageId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.frame(maxWidth: 350)
|
||||
if newPageId.isEmpty {
|
||||
Text("Enter the id of the new page to create")
|
||||
.foregroundStyle(.secondary)
|
||||
} else if idExists {
|
||||
Text("A page with the same id already exists")
|
||||
.foregroundStyle(Color.red)
|
||||
} else if containsInvalidCharacters {
|
||||
Text("The id contains invalid characters")
|
||||
.foregroundStyle(Color.red)
|
||||
} else {
|
||||
Text("Create a new page with the id")
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
HStack {
|
||||
Button(role: .cancel, action: dismissSheet) {
|
||||
Text("Cancel")
|
||||
}
|
||||
Button(action: addNewPage) {
|
||||
Text("Create")
|
||||
}
|
||||
.disabled(newPageId.isEmpty || containsInvalidCharacters || idExists)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
private func addNewPage() {
|
||||
let page = Page(
|
||||
id: newPageId,
|
||||
isDraft: true,
|
||||
createdDate: .now,
|
||||
startDate: .now,
|
||||
endDate: nil,
|
||||
german: .init(urlString: "seite",
|
||||
title: "Ein Titel"),
|
||||
english: .init(urlString: "page",
|
||||
title: "A Title"),
|
||||
tags: [])
|
||||
content.pages.insert(page, at: 0)
|
||||
selectedPage = page
|
||||
dismissSheet()
|
||||
}
|
||||
|
||||
private func dismissSheet() {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AddPageView(selected: .constant(nil))
|
||||
.environmentObject(Content.mock)
|
||||
}
|
67
CHDataManagement/Views/Pages/LocalizedPageContentView.swift
Normal file
67
CHDataManagement/Views/Pages/LocalizedPageContentView.swift
Normal file
@ -0,0 +1,67 @@
|
||||
import SwiftUI
|
||||
import HighlightedTextEditor
|
||||
|
||||
struct LocalizedPageContentView: View {
|
||||
|
||||
let pageId: String
|
||||
|
||||
@ObservedObject
|
||||
var page: LocalizedPage
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var isGeneratingWebsite = false
|
||||
|
||||
@State
|
||||
private var pageContent: String = ""
|
||||
|
||||
init(pageId: String, page: LocalizedPage) {
|
||||
self.pageId = pageId
|
||||
self.page = page
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
TextField("", text: $page.title)
|
||||
.font(.title)
|
||||
.textFieldStyle(.plain)
|
||||
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Button(action: loadContent) {
|
||||
Text("Load")
|
||||
}
|
||||
Button(action: saveContent) {
|
||||
Text("Save")
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
HighlightedTextEditor(
|
||||
text: $pageContent,
|
||||
highlightRules: .markdown)
|
||||
}
|
||||
.padding()
|
||||
.onAppear(perform: loadContent)
|
||||
.onDisappear(perform: saveContent)
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
let content = content.storage.pageContent(for: pageId, language: language)
|
||||
guard content != "" else {
|
||||
pageContent = "New file"
|
||||
return
|
||||
}
|
||||
pageContent = content
|
||||
}
|
||||
|
||||
private func saveContent() {
|
||||
guard pageContent != "", pageContent != "New file" else {
|
||||
return
|
||||
}
|
||||
content.storage.save(pageContent: pageContent, for: pageId, language: language)
|
||||
}
|
||||
}
|
@ -36,7 +36,7 @@ struct LocalizedPageDetailView: View {
|
||||
size: 22,
|
||||
color: .red) {
|
||||
item.linkPreviewImage = nil
|
||||
}
|
||||
}.disabled(item.linkPreviewImage == nil)
|
||||
Spacer()
|
||||
}
|
||||
|
||||
|
@ -1,15 +1,25 @@
|
||||
import SwiftUI
|
||||
import HighlightedTextEditor
|
||||
|
||||
struct PageTitleView: View {
|
||||
|
||||
@ObservedObject
|
||||
var page: LocalizedPage
|
||||
|
||||
var body: some View {
|
||||
TextField("", text: $page.title)
|
||||
.font(.title)
|
||||
.textFieldStyle(.plain)
|
||||
}
|
||||
}
|
||||
|
||||
struct PageContentView: View {
|
||||
|
||||
@ObservedObject
|
||||
var page: Page
|
||||
|
||||
@ObservedObject
|
||||
private var localized: LocalizedPage
|
||||
|
||||
let language: ContentLanguage
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
@ -17,87 +27,26 @@ struct PageContentView: View {
|
||||
@State
|
||||
private var isGeneratingWebsite = false
|
||||
|
||||
@State
|
||||
private var pageContent: String = ""
|
||||
|
||||
init(page: Page, language: ContentLanguage) {
|
||||
init(page: Page) {
|
||||
self.page = page
|
||||
self.localized = page.localized(in: language)
|
||||
self.language = language
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
TextField("", text: $localized.title)
|
||||
.font(.title)
|
||||
.textFieldStyle(.plain)
|
||||
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Button(action: loadContent) {
|
||||
Text("Load")
|
||||
}
|
||||
Button(action: saveContent) {
|
||||
Text("Save")
|
||||
}
|
||||
Button(action: generate) {
|
||||
Text("Generate")
|
||||
}
|
||||
.disabled(isGeneratingWebsite)
|
||||
Spacer()
|
||||
}
|
||||
HighlightedTextEditor(
|
||||
text: $pageContent,
|
||||
highlightRules: .markdown)
|
||||
}
|
||||
.padding()
|
||||
.onAppear(perform: loadContent)
|
||||
.onDisappear(perform: saveContent)
|
||||
LocalizedPageContentView(pageId: page.id, page: page.localized(in: language))
|
||||
.id(page.id + language.rawValue)
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
let content = content.storage.pageContent(for: page.id, language: language)
|
||||
guard content != "" else {
|
||||
pageContent = "New file"
|
||||
return
|
||||
}
|
||||
pageContent = content
|
||||
}
|
||||
|
||||
extension PageContentView: MainContentView {
|
||||
|
||||
init(item: Page) {
|
||||
self.page = item
|
||||
}
|
||||
|
||||
private func saveContent() {
|
||||
guard pageContent != "", pageContent != "New file" else {
|
||||
return
|
||||
}
|
||||
content.storage.save(pageContent: pageContent, for: page.id, language: language)
|
||||
}
|
||||
|
||||
private func generate() {
|
||||
guard content.settings.outputDirectoryPath != "" else {
|
||||
print("Invalid output path")
|
||||
return
|
||||
}
|
||||
let url = URL(fileURLWithPath: content.settings.outputDirectoryPath)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
print("Missing output folder")
|
||||
return
|
||||
}
|
||||
isGeneratingWebsite = true
|
||||
print("Generating page")
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
let generator = WebsiteGenerator(
|
||||
content: content,
|
||||
language: language)
|
||||
if !generator.generate(page: page) {
|
||||
print("Generation failed")
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
isGeneratingWebsite = false
|
||||
print("Done")
|
||||
}
|
||||
}
|
||||
}
|
||||
static let itemDescription = "a page"
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PageContentView(page: .empty, language: .english)
|
||||
PageContentView(page: .empty)
|
||||
}
|
||||
|
@ -5,19 +5,29 @@ struct PageDetailView: View {
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@ObservedObject
|
||||
private var item: Page
|
||||
private var page: Page
|
||||
|
||||
@State
|
||||
private var isGeneratingWebsite = false
|
||||
|
||||
init(page: Page) {
|
||||
self.item = page
|
||||
self.page = page
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading) {
|
||||
Button(action: generate) {
|
||||
Text("Generate")
|
||||
}
|
||||
.disabled(isGeneratingWebsite)
|
||||
Text("ID")
|
||||
.font(.headline)
|
||||
TextField("", text: $item.id)
|
||||
TextField("", text: $page.id)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.bottom)
|
||||
|
||||
@ -25,7 +35,7 @@ struct PageDetailView: View {
|
||||
Text("Draft")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
Toggle("", isOn: $item.isDraft)
|
||||
Toggle("", isOn: $page.isDraft)
|
||||
.toggleStyle(.switch)
|
||||
}
|
||||
.padding(.bottom)
|
||||
@ -34,7 +44,7 @@ struct PageDetailView: View {
|
||||
Text("Start")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
DatePicker("", selection: $item.startDate, displayedComponents: .date)
|
||||
DatePicker("", selection: $page.startDate, displayedComponents: .date)
|
||||
.datePickerStyle(.compact)
|
||||
.padding(.bottom)
|
||||
}
|
||||
@ -43,30 +53,67 @@ struct PageDetailView: View {
|
||||
Text("Has end date")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
Toggle("", isOn: $item.hasEndDate)
|
||||
Toggle("", isOn: $page.hasEndDate)
|
||||
.toggleStyle(.switch)
|
||||
.padding(.bottom)
|
||||
}
|
||||
|
||||
if item.hasEndDate {
|
||||
if page.hasEndDate {
|
||||
HStack(alignment: .firstTextBaseline) {
|
||||
Text("End date")
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
DatePicker("", selection: $item.endDate, displayedComponents: .date)
|
||||
DatePicker("", selection: $page.endDate, displayedComponents: .date)
|
||||
.datePickerStyle(.compact)
|
||||
.padding(.bottom)
|
||||
}
|
||||
}
|
||||
|
||||
LocalizedPageDetailView(page: item.localized(in: language))
|
||||
LocalizedPageDetailView(page: page.localized(in: language))
|
||||
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
private func generate() {
|
||||
guard content.settings.outputDirectoryPath != "" else {
|
||||
print("Invalid output path")
|
||||
return
|
||||
}
|
||||
let url = URL(fileURLWithPath: content.settings.outputDirectoryPath)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
print("Missing output folder")
|
||||
return
|
||||
}
|
||||
isGeneratingWebsite = true
|
||||
print("Generating page")
|
||||
DispatchQueue.global(qos: .userInitiated).async {
|
||||
let generator = WebsiteGenerator(
|
||||
content: content,
|
||||
language: language)
|
||||
if !generator.generate(page: page) {
|
||||
print("Generation failed")
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
isGeneratingWebsite = false
|
||||
print("Done")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PageDetailView: MainContentView {
|
||||
|
||||
init(item: Page) {
|
||||
self.page = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a page"
|
||||
}
|
||||
|
||||
|
||||
#Preview {
|
||||
PageDetailView(page: .empty)
|
||||
}
|
||||
|
@ -8,108 +8,41 @@ struct PageListView: View {
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var selected: Page?
|
||||
@Binding
|
||||
private var selectedPage: Page?
|
||||
|
||||
@State
|
||||
private var showNewPageView = false
|
||||
private var searchString = ""
|
||||
|
||||
@State
|
||||
private var newPageId = ""
|
||||
init(selectedPage: Binding<Page?>) {
|
||||
self._selectedPage = selectedPage
|
||||
}
|
||||
|
||||
@State
|
||||
private var newPageIdIsValid = false
|
||||
|
||||
private let allowedCharactersInPageId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
|
||||
|
||||
private var cleanPageId: String {
|
||||
newPageId.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
private var filteredPages: [Page] {
|
||||
guard !searchString.isEmpty else {
|
||||
return content.pages
|
||||
}
|
||||
return content.pages.filter { $0.localized(in: language).title.contains(searchString) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(content.pages, selection: $selected) { page in
|
||||
Text(page.localized(in: language).title)
|
||||
.tag(page)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: { showNewPageView = true }) {
|
||||
Label("New post", systemSymbol: .plus)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationSplitViewColumnWidth(min: 300, ideal: 300, max: 300)
|
||||
} content: {
|
||||
if let selected {
|
||||
PageContentView(page: selected, language: language)
|
||||
.id(selected.id + language.rawValue)
|
||||
.layoutPriority(1)
|
||||
} else {
|
||||
// Fallback if no item is selected
|
||||
Text("Select a page from the list")
|
||||
.font(.largeTitle)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
} detail: {
|
||||
if let selected {
|
||||
PageDetailView(page: selected)
|
||||
.frame(maxWidth: 350)
|
||||
} else {
|
||||
EmptyView()
|
||||
.frame(maxWidth: 350)
|
||||
VStack {
|
||||
TextField("", text: $searchString, prompt: Text("Search"))
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.horizontal, 8)
|
||||
List(filteredPages, selection: $selectedPage) { page in
|
||||
Text(page.localized(in: language).title).tag(page)
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if selected == nil {
|
||||
selected = content.pages.first
|
||||
if selectedPage == nil {
|
||||
selectedPage = content.pages.first
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showNewPageView,
|
||||
onDismiss: addNewPage) {
|
||||
TextEntrySheet(
|
||||
title: "Enter the id for the new page",
|
||||
text: $newPageId,
|
||||
isValid: $newPageIdIsValid)
|
||||
}
|
||||
}
|
||||
|
||||
private func isValid(id: String) -> Bool {
|
||||
let id = cleanPageId
|
||||
guard id != "" else {
|
||||
return false
|
||||
}
|
||||
|
||||
guard !content.pages.contains(where: { $0.id == id }) else {
|
||||
return false
|
||||
}
|
||||
// Only allow alphanumeric characters and hyphens
|
||||
return id.rangeOfCharacter(from: allowedCharactersInPageId) == nil
|
||||
}
|
||||
|
||||
private func addNewPage() {
|
||||
let id = cleanPageId
|
||||
guard isValid(id: id) else {
|
||||
return
|
||||
}
|
||||
|
||||
let page = Page(
|
||||
id: id,
|
||||
isDraft: true,
|
||||
createdDate: .now,
|
||||
startDate: .now,
|
||||
endDate: nil,
|
||||
german: .init(urlString: "seite",
|
||||
title: "Ein Titel"),
|
||||
english: .init(urlString: "page",
|
||||
title: "A Title"),
|
||||
tags: [])
|
||||
content.pages.insert(page, at: 0)
|
||||
selected = page
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PageListView()
|
||||
PageListView(selectedPage: .constant(nil))
|
||||
.environmentObject(Content.mock)
|
||||
}
|
||||
|
91
CHDataManagement/Views/Posts/AddPostView.swift
Normal file
91
CHDataManagement/Views/Posts/AddPostView.swift
Normal file
@ -0,0 +1,91 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AddPostView: View {
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss: DismissAction
|
||||
|
||||
@Environment(\.language)
|
||||
private var language: ContentLanguage
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
var selectedPost: Post?
|
||||
|
||||
@State
|
||||
private var newPostId = ""
|
||||
|
||||
private let allowedCharactersInPostId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
|
||||
|
||||
init(selected: Binding<Post?>) {
|
||||
self._selectedPost = selected
|
||||
}
|
||||
|
||||
private var idExists: Bool {
|
||||
content.posts.contains { $0.id == newPostId }
|
||||
}
|
||||
|
||||
private var containsInvalidCharacters: Bool {
|
||||
newPostId.rangeOfCharacter(from: allowedCharactersInPostId) != nil
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("New post")
|
||||
.font(.headline)
|
||||
|
||||
TextField("", text: $newPostId)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.frame(maxWidth: 350)
|
||||
if newPostId.isEmpty {
|
||||
Text("Enter the id of the new post to create")
|
||||
.foregroundStyle(.secondary)
|
||||
} else if idExists {
|
||||
Text("A post with the same id already exists")
|
||||
.foregroundStyle(Color.red)
|
||||
} else if containsInvalidCharacters {
|
||||
Text("The id contains invalid characters")
|
||||
.foregroundStyle(Color.red)
|
||||
} else {
|
||||
Text("Create a new post with the id")
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
HStack {
|
||||
Button(role: .cancel, action: dismissSheet) {
|
||||
Text("Cancel")
|
||||
}
|
||||
Button(action: addNewPost) {
|
||||
Text("Create")
|
||||
}
|
||||
.disabled(newPostId.isEmpty || containsInvalidCharacters || idExists)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
|
||||
private func addNewPost() {
|
||||
let post = Post(
|
||||
id: newPostId,
|
||||
isDraft: true,
|
||||
createdDate: .now,
|
||||
startDate: .now,
|
||||
endDate: nil,
|
||||
tags: [],
|
||||
german: .init(title: "Titel", content: "Text"),
|
||||
english: .init(title: "Title", content: "Text"))
|
||||
content.posts.insert(post, at: 0)
|
||||
selectedPost = post
|
||||
dismissSheet()
|
||||
}
|
||||
|
||||
private func dismissSheet() {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
AddPostView(selected: .constant(nil))
|
||||
.environmentObject(Content.mock)
|
||||
}
|
@ -5,7 +5,7 @@ struct ImagePickerView: View {
|
||||
@Binding
|
||||
var showImagePicker: Bool
|
||||
|
||||
private let selected: (ImageResource) -> Void
|
||||
private let selected: (FileResource) -> Void
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
@ -13,13 +13,13 @@ struct ImagePickerView: View {
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
init(showImagePicker: Binding<Bool>, selected: @escaping (ImageResource) -> Void) {
|
||||
init(showImagePicker: Binding<Bool>, selected: @escaping (FileResource) -> Void) {
|
||||
self._showImagePicker = showImagePicker
|
||||
self.selected = selected
|
||||
}
|
||||
|
||||
@State
|
||||
private var selectedImage: ImageResource?
|
||||
private var selectedImage: FileResource?
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
|
@ -35,7 +35,7 @@ struct LocalizedPostDetailView: View {
|
||||
size: 22,
|
||||
color: .red) {
|
||||
item.linkPreviewImage = nil
|
||||
}
|
||||
}.disabled(item.linkPreviewImage == nil)
|
||||
Spacer()
|
||||
}
|
||||
|
||||
|
@ -10,11 +10,24 @@ struct PostContentView: View {
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
init(post: Post) {
|
||||
self.post = post
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
LocalizedPostContentView(post: post)
|
||||
}
|
||||
}
|
||||
|
||||
extension PostContentView: MainContentView {
|
||||
|
||||
init(item: Post) {
|
||||
self.post = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a post"
|
||||
}
|
||||
|
||||
private struct LocalizedTitle: View {
|
||||
|
||||
@ObservedObject
|
||||
@ -44,9 +57,13 @@ private struct LocalizedContentEditor: View {
|
||||
|
||||
var body: some View {
|
||||
TextEditor(text: $post.content)
|
||||
// HighlightedTextEditor(
|
||||
// text: $post.content,
|
||||
// highlightRules: .markdown)
|
||||
.font(.body)
|
||||
.frame(minHeight: 150)
|
||||
.textEditorStyle(.plain)
|
||||
.padding(.vertical, 8)
|
||||
.padding(.leading, 3)
|
||||
.background(Color.gray.opacity(0.1))
|
||||
.cornerRadius(8)
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,10 +93,7 @@ struct LocalizedPostContentView: View {
|
||||
LocalizedTitle(post: post.localized(in: language))
|
||||
FlowHStack {
|
||||
ForEach(post.tags, id: \.id) { tag in
|
||||
TagView(tag: .init(
|
||||
en: tag.english.name,
|
||||
de: tag.german.name)
|
||||
)
|
||||
TagView(text: tag.localized(in: language).name)
|
||||
.foregroundStyle(.white)
|
||||
}
|
||||
Button(action: { showTagPicker = true }) {
|
||||
|
@ -95,6 +95,16 @@ struct PostDetailView: View {
|
||||
}
|
||||
}
|
||||
|
||||
extension PostDetailView: MainContentView {
|
||||
|
||||
init(item: Post) {
|
||||
self.item = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a post"
|
||||
}
|
||||
|
||||
|
||||
#Preview(traits: .fixedLayout(width: 270, height: 500)) {
|
||||
PostDetailView(post: .fullMock)
|
||||
}
|
||||
|
@ -1,147 +0,0 @@
|
||||
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..<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) { 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)
|
||||
}
|
@ -57,7 +57,7 @@ struct PostImagesView: View {
|
||||
}
|
||||
}
|
||||
|
||||
private func shiftLeft(_ image: ImageResource) {
|
||||
private func shiftLeft(_ image: FileResource) {
|
||||
guard let index = post.images.firstIndex(of: image) else {
|
||||
return
|
||||
}
|
||||
@ -67,7 +67,7 @@ struct PostImagesView: View {
|
||||
post.images.swapAt(index, index - 1)
|
||||
}
|
||||
|
||||
private func shiftRight(_ image: ImageResource) {
|
||||
private func shiftRight(_ image: FileResource) {
|
||||
guard let index = post.images.firstIndex(of: image) else {
|
||||
return
|
||||
}
|
||||
@ -77,7 +77,7 @@ struct PostImagesView: View {
|
||||
post.images.swapAt(index, index + 1)
|
||||
}
|
||||
|
||||
private func remove(_ image: ImageResource) {
|
||||
private func remove(_ image: FileResource) {
|
||||
guard let index = post.images.firstIndex(of: image) else {
|
||||
return
|
||||
}
|
||||
|
@ -1,118 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostList: View {
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Environment(\.language)
|
||||
private var language: ContentLanguage
|
||||
|
||||
@State
|
||||
private var selected: Post? = nil
|
||||
|
||||
@State
|
||||
private var showNewPostView = false
|
||||
|
||||
@State
|
||||
private var newPostId = ""
|
||||
|
||||
@State
|
||||
private var newPostIdIsValid = false
|
||||
|
||||
private let allowedCharactersInPostId = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
|
||||
|
||||
private var cleanPostId: String {
|
||||
newPostId.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(content.posts, selection: $selected) { post in
|
||||
Text(post.localized(in: language).title)
|
||||
.tag(post)
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: { showNewPostView = true }) {
|
||||
Label("New post", systemSymbol: .plus)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationSplitViewColumnWidth(min: 250, ideal: 250, max: 250)
|
||||
} content: {
|
||||
if let selected {
|
||||
PostContentView(post: selected)
|
||||
.layoutPriority(1)
|
||||
} else {
|
||||
HStack {
|
||||
Spacer()
|
||||
Text("Select a post to show the content")
|
||||
.font(.largeTitle)
|
||||
.foregroundColor(.secondary)
|
||||
Spacer()
|
||||
}.layoutPriority(1)
|
||||
}
|
||||
} detail: {
|
||||
if let selected {
|
||||
PostDetailView(post: selected)
|
||||
.frame(minWidth: 280)
|
||||
} else {
|
||||
Text("No post selected")
|
||||
.frame(minWidth: 280)
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showNewPostView,
|
||||
onDismiss: addNewPost) {
|
||||
TextEntrySheet(
|
||||
title: "Enter the id for the new post",
|
||||
text: $newPostId,
|
||||
isValid: $newPostIdIsValid)
|
||||
}
|
||||
.onChange(of: newPostId) { _, newValue in
|
||||
newPostIdIsValid = isValid(id: newValue)
|
||||
}
|
||||
.onAppear {
|
||||
if selected == nil {
|
||||
selected = content.posts.first
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func isValid(id: String) -> Bool {
|
||||
let id = cleanPostId
|
||||
guard id != "" else {
|
||||
return false
|
||||
}
|
||||
|
||||
guard !content.posts.contains(where: { $0.id == id }) else {
|
||||
return false
|
||||
}
|
||||
// Only allow alphanumeric characters and hyphens
|
||||
return id.rangeOfCharacter(from: allowedCharactersInPostId) == nil
|
||||
}
|
||||
|
||||
private func addNewPost() {
|
||||
let id = cleanPostId
|
||||
guard isValid(id: id) else {
|
||||
return
|
||||
}
|
||||
|
||||
let post = Post(
|
||||
id: id,
|
||||
isDraft: true,
|
||||
createdDate: .now,
|
||||
startDate: .now,
|
||||
endDate: nil,
|
||||
tags: [],
|
||||
german: .init(title: "Titel", content: "Text"),
|
||||
english: .init(title: "Title", content: "Text"))
|
||||
content.posts.insert(post, at: 0)
|
||||
selected = post
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PostList()
|
||||
.environmentObject(Content())
|
||||
}
|
42
CHDataManagement/Views/Posts/PostListView.swift
Normal file
42
CHDataManagement/Views/Posts/PostListView.swift
Normal file
@ -0,0 +1,42 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostListView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
private var selectedPost: Post?
|
||||
|
||||
@State
|
||||
private var searchString = ""
|
||||
|
||||
init(selectedPost: Binding<Post?>) {
|
||||
self._selectedPost = selectedPost
|
||||
}
|
||||
|
||||
private var filteredPosts: [Post] {
|
||||
guard !searchString.isEmpty else {
|
||||
return content.posts
|
||||
}
|
||||
return content.posts.filter { $0.localized(in: language).title.contains(searchString) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
TextField("", text: $searchString, prompt: Text("Search"))
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.horizontal, 8)
|
||||
List(filteredPosts, selection: $selectedPost) { post in
|
||||
Text(post.localized(in: language).title).tag(post)
|
||||
}
|
||||
}.onAppear {
|
||||
if selectedPost == nil {
|
||||
selectedPost = content.posts.first
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct PostView: View {
|
||||
|
||||
@ObservedObject
|
||||
var post: Post
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
var body: some View {
|
||||
LocalizedPostView(post: post, localized: post.localized(in: language))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct LocalizedPostView: View {
|
||||
|
||||
@ObservedObject
|
||||
var post: Post
|
||||
|
||||
@ObservedObject
|
||||
var localized: LocalizedPost
|
||||
|
||||
@State
|
||||
private var showDatePicker = false
|
||||
|
||||
@State
|
||||
private var showImagePicker = false
|
||||
|
||||
@State
|
||||
private var showTagPicker = false
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .center) {
|
||||
if localized.images.isEmpty {
|
||||
Button(action: { showImagePicker = true }) {
|
||||
Text("Add image")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.foregroundStyle(.blue)
|
||||
.padding(.top)
|
||||
} else {
|
||||
PostImageGalleryView(post: localized)
|
||||
.aspectRatio(1.33, contentMode: .fill)
|
||||
}
|
||||
VStack(alignment: .leading) {
|
||||
Text(post.dateText(in: language))
|
||||
.font(.system(size: 19, weight: .semibold))
|
||||
.foregroundStyle(.secondary)
|
||||
TextField("", text: $localized.title)
|
||||
.font(.system(size: 24, weight: .bold))
|
||||
.foregroundStyle(Color.primary)
|
||||
.textFieldStyle(.plain)
|
||||
.lineLimit(2)
|
||||
FlowHStack {
|
||||
ForEach(post.tags, id: \.id) { tag in
|
||||
TagView(tag: .init(
|
||||
en: tag.english.name,
|
||||
de: tag.german.name)
|
||||
)
|
||||
.foregroundStyle(.white)
|
||||
}
|
||||
Button(action: { showTagPicker = true }) {
|
||||
Image(systemSymbol: .squareAndPencilCircleFill)
|
||||
.resizable()
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.frame(height: 22)
|
||||
.foregroundColor(Color.blue)
|
||||
.background(Circle()
|
||||
.fill(Color.white)
|
||||
.padding(1))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
TextEditor(text: $localized.content)
|
||||
.font(.body)
|
||||
.foregroundStyle(.secondary)
|
||||
.textEditorStyle(.plain)
|
||||
.padding(.leading, -5)
|
||||
.scrollDisabled(true)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.background(Color(NSColor.windowBackgroundColor))
|
||||
.cornerRadius(8)
|
||||
.sheet(isPresented: $showDatePicker) {
|
||||
DatePickerView(
|
||||
post: post,
|
||||
showDatePicker: $showDatePicker)
|
||||
}
|
||||
.sheet(isPresented: $showImagePicker) {
|
||||
ImagePickerView(showImagePicker: $showImagePicker) { image in
|
||||
localized.images.append(image)
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showTagPicker) {
|
||||
TagSelectionView(
|
||||
presented: $showTagPicker,
|
||||
selected: $post.tags,
|
||||
tags: $content.tags)
|
||||
}
|
||||
}
|
||||
|
||||
private func remove(tag: Tag) {
|
||||
post.tags = post.tags.filter {$0.id != tag.id }
|
||||
}
|
||||
}
|
||||
|
||||
#Preview(traits: .fixedLayout(width: 450, height: 600)) {
|
||||
List {
|
||||
PostView(post: .fullMock)
|
||||
.listRowSeparator(.hidden)
|
||||
.environment(\.language, ContentLanguage.german)
|
||||
PostView(post: .mock)
|
||||
.listRowSeparator(.hidden)
|
||||
}
|
||||
.environmentObject(Content.mock)
|
||||
//.listStyle(.plain)
|
||||
}
|
@ -4,22 +4,15 @@ import SFSafeSymbols
|
||||
|
||||
struct TagView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language: ContentLanguage
|
||||
let text: String
|
||||
|
||||
let tag: LocalizedText
|
||||
|
||||
init(tag: LocalizedText) {
|
||||
self.tag = tag
|
||||
}
|
||||
|
||||
static var add: TagView {
|
||||
.init(tag: LocalizedText(en: "Add", de: "Mehr"))
|
||||
init(text: String) {
|
||||
self.text = text
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(tag.getText(for: language))
|
||||
Text(text)
|
||||
.font(.subheadline)
|
||||
.padding(.leading, 2)
|
||||
}
|
||||
@ -33,10 +26,7 @@ struct TagView: View {
|
||||
|
||||
#Preview {
|
||||
HStack {
|
||||
TagView(tag: LocalizedText(en: "Some", de: "Etwas"))
|
||||
.environment(\.language, ContentLanguage.german)
|
||||
TagView(tag: LocalizedText(en: "Some", de: "Etwas"))
|
||||
.environment(\.language, ContentLanguage.english)
|
||||
TagView.add
|
||||
TagView(text: "Some")
|
||||
TagView(text: "Etwas")
|
||||
}.background(Color.secondary)
|
||||
}
|
||||
|
@ -53,10 +53,7 @@ struct NavigationBarSettingsView: View {
|
||||
.font(.headline)
|
||||
FlowHStack {
|
||||
ForEach(content.settings.navigationBar.tags, id: \.id) { tag in
|
||||
TagView(tag: .init(
|
||||
en: tag.english.name,
|
||||
de: tag.german.name)
|
||||
)
|
||||
TagView(text: tag.localized(in: language).name)
|
||||
.foregroundStyle(.white)
|
||||
}
|
||||
Button(action: { showTagPicker = true }) {
|
||||
|
@ -10,13 +10,13 @@ struct SectionedSettingsView: View {
|
||||
SettingsSidebar(selectedSection: $selectedSection)
|
||||
.frame(minWidth: 200, idealWidth: 200, maxWidth: 200)
|
||||
} detail: {
|
||||
DetailView(section: selectedSection)
|
||||
GenerationDetailView(section: selectedSection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct DetailView: View {
|
||||
struct GenerationDetailView: View {
|
||||
|
||||
let section: SettingsSection?
|
||||
|
||||
@ -40,7 +40,7 @@ struct DetailView: View {
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
.padding()
|
||||
.navigationTitle(section?.rawValue ?? "")
|
||||
.navigationTitle("")
|
||||
}
|
||||
}
|
||||
|
||||
|
34
CHDataManagement/Views/Tags/AddTagView.swift
Normal file
34
CHDataManagement/Views/Tags/AddTagView.swift
Normal file
@ -0,0 +1,34 @@
|
||||
import SwiftUI
|
||||
|
||||
struct AddTagView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss
|
||||
|
||||
@Binding
|
||||
var selectedTag: Tag?
|
||||
|
||||
init(selected: Binding<Tag?>) {
|
||||
self._selectedTag = selected
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Text("Creating tag...")
|
||||
.onAppear(perform: addNewTag)
|
||||
}
|
||||
|
||||
private func addNewTag() {
|
||||
let newTag = Tag(isVisible: true,
|
||||
german: .init(urlComponent: "tag", name: "Neuer Tag"),
|
||||
english: .init(urlComponent: "tag-en", name: "New Tag"))
|
||||
// Add to top of the list, and resort when changing the name
|
||||
content.tags.insert(newTag, at: 0)
|
||||
dismiss()
|
||||
}
|
||||
}
|
75
CHDataManagement/Views/Tags/LocalizedTagDetailView.swift
Normal file
75
CHDataManagement/Views/Tags/LocalizedTagDetailView.swift
Normal file
@ -0,0 +1,75 @@
|
||||
import SwiftUI
|
||||
|
||||
struct LocalizedTagDetailView: View {
|
||||
|
||||
@Binding
|
||||
var tagIsVisible: Bool
|
||||
|
||||
@ObservedObject
|
||||
var tag: LocalizedTag
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var showImagePicker = false
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading) {
|
||||
Toggle("Appears in overviews", isOn: $tagIsVisible)
|
||||
.toggleStyle(.switch)
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
Text("Name")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
TextField("", text: $tag.name)
|
||||
|
||||
Text("URL String")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
TextField("", text: $tag.urlComponent)
|
||||
|
||||
Text("Original url")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
Text(tag.originalUrl ?? "-")
|
||||
.padding(.top, 1)
|
||||
.padding(.bottom)
|
||||
|
||||
Text("Subtitle")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
OptionalTextField("", text: $tag.subtitle)
|
||||
|
||||
Text("Description")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
OptionalTextField("", text: $tag.description)
|
||||
|
||||
Text("Thumbnail")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
Button(action: { showImagePicker = true }) {
|
||||
Text(tag.thumbnail?.id ?? "Select")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.foregroundStyle(.blue)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.sheet(isPresented: $showImagePicker) {
|
||||
ImagePickerView(showImagePicker: $showImagePicker) { image in
|
||||
tag.thumbnail = image
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
LocalizedTagDetailView(
|
||||
tagIsVisible: .constant(true),
|
||||
tag: Tag.mock.english)
|
||||
}
|
@ -57,6 +57,15 @@ struct TagContentView: View {
|
||||
}
|
||||
}
|
||||
|
||||
extension TagContentView: MainContentView {
|
||||
|
||||
init(item: Tag) {
|
||||
self.tag = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a tag"
|
||||
}
|
||||
|
||||
#Preview {
|
||||
TagContentView(tag: .hiking)
|
||||
.environmentObject(Content.mock)
|
||||
|
@ -1,75 +1,26 @@
|
||||
import SwiftUI
|
||||
|
||||
|
||||
struct TagDetailView: View {
|
||||
|
||||
@Binding
|
||||
var tagIsVisible: Bool
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@ObservedObject
|
||||
var tag: LocalizedTag
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@State
|
||||
private var showImagePicker = false
|
||||
var tag: Tag
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading) {
|
||||
Toggle("Appears in overviews", isOn: $tagIsVisible)
|
||||
.toggleStyle(.switch)
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
Text("Name")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
TextField("", text: $tag.name)
|
||||
|
||||
Text("URL String")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
TextField("", text: $tag.urlComponent)
|
||||
|
||||
Text("Original url")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
Text(tag.originalUrl ?? "-")
|
||||
.padding(.top, 1)
|
||||
.padding(.bottom)
|
||||
|
||||
Text("Subtitle")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
OptionalTextField("", text: $tag.subtitle)
|
||||
|
||||
Text("Description")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
OptionalTextField("", text: $tag.description)
|
||||
|
||||
Text("Thumbnail")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
Button(action: { showImagePicker = true }) {
|
||||
Text(tag.thumbnail?.id ?? "Select")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.foregroundStyle(.blue)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.sheet(isPresented: $showImagePicker) {
|
||||
ImagePickerView(showImagePicker: $showImagePicker) { image in
|
||||
tag.thumbnail = image
|
||||
}
|
||||
}
|
||||
LocalizedTagDetailView(
|
||||
tagIsVisible: $tag.isVisible,
|
||||
tag: tag.localized(in: language))
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
TagDetailView(
|
||||
tagIsVisible: .constant(true),
|
||||
tag: Tag.mock.english)
|
||||
extension TagDetailView: MainContentView {
|
||||
|
||||
init(item: Tag) {
|
||||
self.tag = item
|
||||
}
|
||||
|
||||
static let itemDescription = "a tag"
|
||||
}
|
||||
|
42
CHDataManagement/Views/Tags/TagListView.swift
Normal file
42
CHDataManagement/Views/Tags/TagListView.swift
Normal file
@ -0,0 +1,42 @@
|
||||
import SwiftUI
|
||||
|
||||
struct TagListView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Binding
|
||||
var selectedTag: Tag?
|
||||
|
||||
@State
|
||||
private var searchString = ""
|
||||
|
||||
init(selectedTag: Binding<Tag?>) {
|
||||
_selectedTag = selectedTag
|
||||
}
|
||||
|
||||
private var filteredTags: [Tag] {
|
||||
guard !searchString.isEmpty else {
|
||||
return content.tags
|
||||
}
|
||||
return content.tags.filter { $0.localized(in: language).name.contains(searchString) }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
TextField("", text: $searchString, prompt: Text("Search"))
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.padding(.horizontal, 8)
|
||||
List(filteredTags, selection: $selectedTag) { tag in
|
||||
Text(tag.localized(in: language).name).tag(tag)
|
||||
}
|
||||
}.onAppear {
|
||||
if selectedTag == nil {
|
||||
selectedTag = content.tags.first
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
private struct SelectedTagView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
private var language
|
||||
|
||||
@ObservedObject
|
||||
var tag: Tag
|
||||
|
||||
var body: some View {
|
||||
TagDetailView(
|
||||
tagIsVisible: $tag.isVisible,
|
||||
tag: tag.localized(in: language))
|
||||
}
|
||||
}
|
||||
|
||||
struct TagsListView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language
|
||||
|
||||
@EnvironmentObject
|
||||
var content: Content
|
||||
|
||||
@State
|
||||
var selectedTag: Tag?
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(content.tags.sorted(), selection: $selectedTag) { tag in
|
||||
Text(tag.localized(in: language).name)
|
||||
.tag(tag)
|
||||
}
|
||||
} content: {
|
||||
if let selectedTag {
|
||||
TagContentView(tag: selectedTag)
|
||||
.layoutPriority(1)
|
||||
} else {
|
||||
Text("Select a tag to show the details")
|
||||
.font(.largeTitle)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
} detail: {
|
||||
if let selectedTag {
|
||||
SelectedTagView(tag: selectedTag)
|
||||
.frame(maxWidth: 350)
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
if selectedTag == nil {
|
||||
selectedTag = content.tags.first
|
||||
}
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: addNewTag) {
|
||||
Label("New tag", systemSymbol: .plus)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func addNewTag() {
|
||||
let newTag = Tag(isVisible: true,
|
||||
german: .init(urlComponent: "tag", name: "Neuer Tag"),
|
||||
english: .init(urlComponent: "tag-en", name: "New Tag"))
|
||||
// Add to top of the list, and resort when changing the name
|
||||
content.tags.insert(newTag, at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PageListView()
|
||||
.environmentObject(Content())
|
||||
}
|
Reference in New Issue
Block a user