First version
This commit is contained in:
20
CHDataManagement/Views/Files/FilesView.swift
Normal file
20
CHDataManagement/Views/Files/FilesView.swift
Normal file
@ -0,0 +1,20 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FilesView: View {
|
||||
|
||||
@EnvironmentObject
|
||||
var content: Content
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack {
|
||||
|
||||
}
|
||||
}
|
||||
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
FilesView()
|
||||
}
|
53
CHDataManagement/Views/Generic/FlowHStack.swift
Normal file
53
CHDataManagement/Views/Generic/FlowHStack.swift
Normal file
@ -0,0 +1,53 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FlowHStack: Layout {
|
||||
|
||||
var horizontalSpacing: CGFloat = 8
|
||||
|
||||
var verticalSpacing: CGFloat = 8
|
||||
|
||||
func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
|
||||
let subviewSizes = subviews.map { $0.sizeThatFits(proposal) }
|
||||
let maxSubviewHeight = subviewSizes.map { $0.height }.max() ?? .zero
|
||||
var currentRowWidth: CGFloat = .zero
|
||||
var totalHeight: CGFloat = maxSubviewHeight
|
||||
var totalWidth: CGFloat = .zero
|
||||
|
||||
for size in subviewSizes {
|
||||
let requestedRowWidth = currentRowWidth + horizontalSpacing + size.width
|
||||
let availableRowWidth = proposal.width ?? .zero
|
||||
let willOverflow = requestedRowWidth > availableRowWidth
|
||||
|
||||
if willOverflow {
|
||||
totalHeight += verticalSpacing + maxSubviewHeight
|
||||
currentRowWidth = size.width
|
||||
} else {
|
||||
currentRowWidth = requestedRowWidth
|
||||
}
|
||||
|
||||
totalWidth = max(totalWidth, currentRowWidth)
|
||||
}
|
||||
|
||||
return CGSize(width: totalWidth, height: totalHeight)
|
||||
}
|
||||
|
||||
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
|
||||
let subviewSizes = subviews.map { $0.sizeThatFits(proposal) }
|
||||
let maxSubviewHeight = subviewSizes.map { $0.height }.max() ?? .zero
|
||||
var point = CGPoint(x: bounds.minX, y: bounds.minY)
|
||||
|
||||
for index in subviews.indices {
|
||||
let requestedWidth = point.x + subviewSizes[index].width
|
||||
let availableWidth = bounds.maxX
|
||||
let willOverflow = requestedWidth > availableWidth
|
||||
|
||||
if willOverflow {
|
||||
point.x = bounds.minX
|
||||
point.y += maxSubviewHeight + verticalSpacing
|
||||
}
|
||||
|
||||
subviews[index].place(at: point, proposal: ProposedViewSize(subviewSizes[index]))
|
||||
point.x += subviewSizes[index].width + horizontalSpacing
|
||||
}
|
||||
}
|
||||
}
|
33
CHDataManagement/Views/Generic/HorizontalCenter.swift
Normal file
33
CHDataManagement/Views/Generic/HorizontalCenter.swift
Normal file
@ -0,0 +1,33 @@
|
||||
import SwiftUI
|
||||
|
||||
/**
|
||||
A view that centers the content horizontally using an `HStack`
|
||||
*/
|
||||
struct HorizontalCenter<Content> : View where Content : View {
|
||||
|
||||
let alignment: VerticalAlignment
|
||||
|
||||
let spacing: CGFloat?
|
||||
|
||||
let content: Content
|
||||
|
||||
public init(alignment: VerticalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content) {
|
||||
self.alignment = alignment
|
||||
self.spacing = spacing
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: alignment, spacing: spacing) {
|
||||
Spacer()
|
||||
content
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
HorizontalCenter {
|
||||
Text("Test")
|
||||
}
|
||||
}
|
49
CHDataManagement/Views/Images/FlexibleColumnView.swift
Normal file
49
CHDataManagement/Views/Images/FlexibleColumnView.swift
Normal file
@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
}
|
59
CHDataManagement/Views/Images/ImageDetailsView.swift
Normal file
59
CHDataManagement/Views/Images/ImageDetailsView.swift
Normal file
@ -0,0 +1,59 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ImageDetailsView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language
|
||||
|
||||
let 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("Description")
|
||||
.font(.headline)
|
||||
TextField("", text: image.altText.text(for: language))
|
||||
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!)
|
||||
}
|
62
CHDataManagement/Views/Images/ImagesView.swift
Normal file
62
CHDataManagement/Views/Images/ImagesView.swift
Normal file
@ -0,0 +1,62 @@
|
||||
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 {
|
||||
FlexibleColumnView(items: $content.images) { image, width in
|
||||
let isSelected = selectedImage == image
|
||||
let borderColor: Color = isSelected ? .accentColor : .clear
|
||||
return image.imageToDisplay
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.border(borderColor, width: 5)
|
||||
.frame(width: width)
|
||||
.onTapGesture { didTap(image: image) }
|
||||
}
|
||||
.inspector(isPresented: $showImageDetails) {
|
||||
if let selectedImage {
|
||||
ImageDetailsView(image: selectedImage)
|
||||
} else {
|
||||
Text("Select an image to show its details")
|
||||
}
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
Button(action: { showImageDetails.toggle() }) {
|
||||
Label("Details", systemSymbol: .infoCircle)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func didTap(image: ImageResource) {
|
||||
if selectedImage == image {
|
||||
selectedImage = nil
|
||||
} else {
|
||||
selectedImage = image
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
let content = Content()
|
||||
content.images = MockImage.images
|
||||
return ImagesView()
|
||||
.environmentObject(content)
|
||||
}
|
17
CHDataManagement/Views/Pages/PageDetailView.swift
Normal file
17
CHDataManagement/Views/Pages/PageDetailView.swift
Normal file
@ -0,0 +1,17 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PageDetailView: View {
|
||||
|
||||
@ObservedObject var page: Page
|
||||
|
||||
@Binding
|
||||
var language: ContentLanguage
|
||||
|
||||
var body: some View {
|
||||
Text(page.metadata(for: language)?.headline ?? "No headline")
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PageDetailView(page: .empty, language: .constant(.english))
|
||||
}
|
49
CHDataManagement/Views/Posts/DatePickerView.swift
Normal file
49
CHDataManagement/Views/Posts/DatePickerView.swift
Normal file
@ -0,0 +1,49 @@
|
||||
import SwiftUI
|
||||
|
||||
struct DatePickerView: View {
|
||||
|
||||
@ObservedObject
|
||||
var post: Post
|
||||
|
||||
@Binding var showDatePicker: Bool
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
VStack {
|
||||
HStack(alignment: .top) {
|
||||
VStack {
|
||||
Text("Start date")
|
||||
.font(.headline)
|
||||
.padding(.vertical, 3)
|
||||
DatePicker("", selection: $post.startDate, displayedComponents: .date)
|
||||
.datePickerStyle(GraphicalDatePickerStyle())
|
||||
.labelsHidden()
|
||||
.padding()
|
||||
|
||||
}
|
||||
|
||||
VStack {
|
||||
Toggle("End date", isOn: $post.hasEndDate)
|
||||
.toggleStyle(.switch)
|
||||
.font(.headline)
|
||||
DatePicker("Select a date", selection: $post.startDate, displayedComponents: .date)
|
||||
.datePickerStyle(GraphicalDatePickerStyle())
|
||||
.labelsHidden()
|
||||
.padding()
|
||||
.disabled(!post.hasEndDate)
|
||||
}
|
||||
}
|
||||
Button("Done") {
|
||||
showDatePicker = false
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.navigationTitle("Pick a Date")
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
DatePickerView(post: .mock, showDatePicker: .constant(true))
|
||||
}
|
83
CHDataManagement/Views/Posts/PostImageGalleryView.swift
Normal file
83
CHDataManagement/Views/Posts/PostImageGalleryView.swift
Normal file
@ -0,0 +1,83 @@
|
||||
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 {
|
||||
|
||||
let images: [Image]
|
||||
|
||||
@State private var currentIndex = 0
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
images[currentIndex]
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
if 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()
|
||||
}
|
||||
VStack {
|
||||
Spacer()
|
||||
HStack(spacing: 8) {
|
||||
ForEach(0..<images.count, id: \.self) { index in
|
||||
Circle()
|
||||
.fill(index == currentIndex ? Color.white : Color.gray) // Change color based on current index
|
||||
.frame(width: 10, height: 10)
|
||||
}
|
||||
}
|
||||
.padding(.bottom, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func previous() {
|
||||
if currentIndex > 0 {
|
||||
currentIndex -= 1
|
||||
} else {
|
||||
currentIndex = images.count - 1
|
||||
}
|
||||
}
|
||||
|
||||
private func next() {
|
||||
if currentIndex < images.count - 1 {
|
||||
currentIndex += 1
|
||||
} else {
|
||||
currentIndex = 0 // Wrap to first image
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview(traits: .fixedLayout(width: 300, height: 300)) {
|
||||
PostImageGalleryView(images: MockImage.images.map { $0.imageToDisplay })
|
||||
}
|
75
CHDataManagement/Views/Posts/PostList.swift
Normal file
75
CHDataManagement/Views/Posts/PostList.swift
Normal file
@ -0,0 +1,75 @@
|
||||
import SwiftUI
|
||||
|
||||
|
||||
private struct CenteredPost<Content>: View where Content: View {
|
||||
|
||||
let content: Content
|
||||
|
||||
init(@ViewBuilder content: () -> Content) {
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HorizontalCenter {
|
||||
content
|
||||
}
|
||||
.listRowBackground(PostList.background)
|
||||
}
|
||||
}
|
||||
|
||||
struct PostList: View {
|
||||
|
||||
static let background = Color(r: 2, g: 15, b: 26)
|
||||
|
||||
@Binding
|
||||
var posts: [Post]
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
if posts.isEmpty {
|
||||
CenteredPost {
|
||||
Text("No posts yet.")
|
||||
.padding()
|
||||
}
|
||||
.listRowSeparator(.hidden)
|
||||
}
|
||||
CenteredPost {
|
||||
Button(action: addNewPost) {
|
||||
Text("Add post")
|
||||
}
|
||||
.padding()
|
||||
.listRowSeparator(.hidden)
|
||||
}
|
||||
ForEach(posts) { post in
|
||||
CenteredPost {
|
||||
PostView(post: post)
|
||||
.frame(maxWidth: 600)
|
||||
}
|
||||
.listRowSeparator(.hidden)
|
||||
.listRowInsets(.init(top: 0, leading: 0, bottom: 30, trailing: 0))
|
||||
}
|
||||
}
|
||||
.listStyle(.plain)
|
||||
.background(PostList.background)
|
||||
.scrollContentBackground(.hidden)
|
||||
}
|
||||
|
||||
private func addNewPost() {
|
||||
let largestId = posts.map { $0.id }.max() ?? 0
|
||||
|
||||
let post = Post(
|
||||
id: largestId + 1,
|
||||
isDraft: true,
|
||||
startDate: .now,
|
||||
endDate: nil,
|
||||
title: .init(en: "Title", de: "Titel"),
|
||||
text: .init(en: "Text", de: "Text"),
|
||||
tags: [],
|
||||
images: [])
|
||||
posts.insert(post, at: 0)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
PostList(posts: .constant([.mock, .fullMock]))
|
||||
}
|
88
CHDataManagement/Views/Posts/PostView.swift
Normal file
88
CHDataManagement/Views/Posts/PostView.swift
Normal file
@ -0,0 +1,88 @@
|
||||
import SwiftUI
|
||||
|
||||
struct PostView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language
|
||||
|
||||
@ObservedObject
|
||||
var post: Post
|
||||
|
||||
@State
|
||||
private var showDatePicker = false
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .center) {
|
||||
if !post.images.isEmpty {
|
||||
PostImageGalleryView(images: post.displayImages)
|
||||
.aspectRatio(1.33, contentMode: .fill)
|
||||
}
|
||||
VStack(alignment: .leading) {
|
||||
HStack(alignment: .center, spacing: 0) {
|
||||
Text(post.dateText(in: language))
|
||||
.font(.system(size: 19, weight: .semibold))
|
||||
.onTapGesture { showDatePicker = true }
|
||||
Spacer()
|
||||
Toggle("Draft", isOn: $post.isDraft)
|
||||
}
|
||||
.foregroundStyle(Color(r: 96, g: 186, b: 255))
|
||||
TextField("", text: post.title.text(for: language))
|
||||
.font(.system(size: 24, weight: .bold))
|
||||
.foregroundStyle(Color.white)
|
||||
.textFieldStyle(.plain)
|
||||
.lineLimit(2)
|
||||
FlowHStack {
|
||||
ForEach(post.tags, id: \.id) { tag in
|
||||
TagView(tag: tag.name)
|
||||
.onTapGesture {
|
||||
remove(tag: tag)
|
||||
}
|
||||
}
|
||||
Button(action: showTagList) {
|
||||
SwiftUI.Image(systemSymbol: .plusCircleFill)
|
||||
.resizable()
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.frame(height: 18)
|
||||
.foregroundColor(TagView.foreground)
|
||||
.opacity(0.7)
|
||||
.padding(.top, 3)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
TextEditor(text: post.text.text(for: language))
|
||||
.font(.body)
|
||||
.foregroundStyle(Color(r: 221, g: 221, b: 221))
|
||||
.textEditorStyle(.plain)
|
||||
.padding(.leading, -5)
|
||||
.scrollDisabled(true)
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.background(Color(r: 4, g: 31, b: 52))
|
||||
.cornerRadius(8)
|
||||
.sheet(isPresented: $showDatePicker) {
|
||||
DatePickerView(post: post, showDatePicker: $showDatePicker)
|
||||
}
|
||||
}
|
||||
|
||||
private func remove(tag: Tag) {
|
||||
post.tags = post.tags.filter {$0.id != tag.id }
|
||||
}
|
||||
|
||||
private func showTagList() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#Preview(traits: .fixedLayout(width: 450, height: 600)) {
|
||||
List {
|
||||
PostView(post: .fullMock)
|
||||
.listRowSeparator(.hidden)
|
||||
.listRowBackground(Color(r: 2, g: 15, b: 26))
|
||||
.environment(\.language, ContentLanguage.german)
|
||||
PostView(post: .mock)
|
||||
.listRowSeparator(.hidden)
|
||||
.listRowBackground(Color(r: 2, g: 15, b: 26))
|
||||
}
|
||||
.listStyle(.plain)
|
||||
}
|
57
CHDataManagement/Views/Posts/TagView.swift
Normal file
57
CHDataManagement/Views/Posts/TagView.swift
Normal file
@ -0,0 +1,57 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import SFSafeSymbols
|
||||
|
||||
struct TagView: View {
|
||||
|
||||
static let background = Color(r: 9, g: 62, b: 103)
|
||||
|
||||
static let foreground = Color(r: 96, g: 186, b: 255)
|
||||
|
||||
@Environment(\.language)
|
||||
var language: ContentLanguage
|
||||
|
||||
let tag: LocalizedText
|
||||
|
||||
let icon: SFSymbol
|
||||
|
||||
let iconSize: CGFloat
|
||||
|
||||
init(tag: LocalizedText, icon: SFSymbol = .xCircleFill, iconSize: CGFloat = 12.0) {
|
||||
self.tag = tag
|
||||
self.icon = icon
|
||||
self.iconSize = iconSize
|
||||
}
|
||||
|
||||
static var add: TagView {
|
||||
.init(tag: LocalizedText(en: "Add", de: "Mehr"), icon: .plusCircleFill)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Text(tag.getText(for: language))
|
||||
.font(.subheadline)
|
||||
.padding(.leading, 2)
|
||||
SwiftUI.Image(systemSymbol: icon)
|
||||
.font(.system(size: iconSize, weight: .black, design: .rounded))
|
||||
.opacity(0.7)
|
||||
.padding(.leading, -5)
|
||||
}
|
||||
.foregroundColor(TagView.foreground)
|
||||
.font(.caption2)
|
||||
.padding(.horizontal, 8)
|
||||
.padding(.vertical, 4)
|
||||
.background(TagView.background)
|
||||
.cornerRadius(8)
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
136
CHDataManagement/Views/Settings/SettingsView.swift
Normal file
136
CHDataManagement/Views/Settings/SettingsView.swift
Normal file
@ -0,0 +1,136 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
|
||||
@Environment(\.language)
|
||||
var language
|
||||
|
||||
@AppStorage("contentPath")
|
||||
var contentPath: String = ""
|
||||
|
||||
@AppStorage("outputPath")
|
||||
var outputPath: String = ""
|
||||
|
||||
@EnvironmentObject
|
||||
var content: Content
|
||||
|
||||
@State
|
||||
private var isSelectingContentFolder = false
|
||||
|
||||
@State
|
||||
private var showFileImporter = false
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading) {
|
||||
Text("Content Folder")
|
||||
.font(.headline)
|
||||
TextField("Content Folder", text: $contentPath)
|
||||
Button(action: selectContentFolder) {
|
||||
Text("Select folder")
|
||||
}
|
||||
Text("Output Folder")
|
||||
.font(.headline)
|
||||
TextField("Output Folder", text: $outputPath)
|
||||
Button(action: selectOutputFolder) {
|
||||
Text("Select folder")
|
||||
}
|
||||
Text("Feed")
|
||||
.font(.headline)
|
||||
Button(action: generateFeed) {
|
||||
Text("Generate")
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.fileImporter(
|
||||
isPresented: $showFileImporter,
|
||||
allowedContentTypes: [.folder],
|
||||
onCompletion: didSelectContentFolder)
|
||||
}
|
||||
|
||||
// MARK: Folder selection
|
||||
|
||||
private func selectContentFolder() {
|
||||
isSelectingContentFolder = true
|
||||
//showFileImporter = true
|
||||
savePanelUsingOpenPanel(key: "contentPathBookmark")
|
||||
}
|
||||
|
||||
private func selectOutputFolder() {
|
||||
isSelectingContentFolder = false
|
||||
//showFileImporter = true
|
||||
savePanelUsingOpenPanel(key: "outputPathBookmark")
|
||||
}
|
||||
|
||||
private func didSelectContentFolder(_ result: Result<URL, any Error>) {
|
||||
switch result {
|
||||
case .success(let url):
|
||||
didSelect(folder: url)
|
||||
case .failure(let error):
|
||||
print("Failed to select content folder: \(error)")
|
||||
}
|
||||
}
|
||||
|
||||
private func didSelect(folder: URL) {
|
||||
let path = folder.absoluteString
|
||||
.replacingOccurrences(of: "file://", with: "")
|
||||
if isSelectingContentFolder {
|
||||
self.contentPath = path
|
||||
saveSecurityScopedBookmark(folder, key: "contentPathBookmark")
|
||||
} else {
|
||||
self.outputPath = path
|
||||
saveSecurityScopedBookmark(folder, key: "outputPathBookmark")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Feed
|
||||
|
||||
private func generateFeed() {
|
||||
guard outputPath != "" else {
|
||||
print("Invalid output path")
|
||||
return
|
||||
}
|
||||
let url = URL(fileURLWithPath: outputPath)
|
||||
|
||||
guard FileManager.default.fileExists(atPath: url.path) else {
|
||||
print("Missing output folder")
|
||||
return
|
||||
}
|
||||
|
||||
content.generateFeed(for: language, bookmarkKey: "outputPathBookmark")
|
||||
}
|
||||
|
||||
func savePanelUsingOpenPanel(key: String) {
|
||||
let panel = NSOpenPanel()
|
||||
// Sets up so user can only select a single directory
|
||||
panel.canChooseFiles = false
|
||||
panel.canChooseDirectories = true
|
||||
panel.allowsMultipleSelection = false
|
||||
panel.showsHiddenFiles = false
|
||||
panel.title = "Select Save Directory"
|
||||
panel.prompt = "Select Save Directory"
|
||||
|
||||
let response = panel.runModal()
|
||||
guard response == .OK else {
|
||||
|
||||
return
|
||||
}
|
||||
guard let url = panel.url else {
|
||||
return
|
||||
}
|
||||
saveSecurityScopedBookmark(url, key: key)
|
||||
}
|
||||
|
||||
func saveSecurityScopedBookmark(_ url: URL, key: String) {
|
||||
do {
|
||||
let bookmarkData = try url.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
|
||||
UserDefaults.standard.set(bookmarkData, forKey: key)
|
||||
print("Security-scoped bookmark saved.")
|
||||
} catch {
|
||||
print("Failed to create security-scoped bookmark: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SettingsView()
|
||||
}
|
Reference in New Issue
Block a user