Reset data after folder change

This commit is contained in:
Christoph Hagen 2024-12-17 23:24:07 +01:00
parent 9a53e020a7
commit 9c828ff80a
9 changed files with 51 additions and 16 deletions

View File

@ -52,10 +52,8 @@ struct InitialSetupView: View {
set(message: "Failed to set content path")
return
}
print("Selected folder, initializing storage")
DispatchQueue.main.async {
do {
print("Loading disk content")
try content.loadFromDisk()
} catch {
set(message: "Failed to load database: \(error)")

View File

@ -203,8 +203,6 @@ struct MainView: App {
}
private func loadContent() {
#warning("Remove")
content.storage.clearContentPath()
guard content.storage.hasContentFolders else {
showInitialSheet()
return

View File

@ -47,7 +47,6 @@ extension Content {
}
let settings = try storage.loadSettings() // Uses defaults if missing
print("Loaded settings")
let imageDescriptions = try storage.loadFileDescriptions().reduce(into: [:]) { descriptions, description in
descriptions[description.fileId] = description
}

View File

@ -26,7 +26,7 @@ final class Content: ObservableObject {
var tagOverview: TagOverviewPage?
@Published
private(set) var results: [ItemId : PageGenerationResults] = [:]
private(set) var results: [ItemId : PageGenerationResults]
@Published
private(set) var isGeneratingWebsite = false
@ -43,15 +43,27 @@ final class Content: ObservableObject {
self.tags = tags
self.files = files
self.tagOverview = tagOverview
self.results = [:]
}
init() {
self.settings = .mock
self.settings = .default
self.posts = []
self.pages = []
self.tags = []
self.files = []
self.tagOverview = nil
self.results = [:]
}
private func clear() {
self.settings = .default
self.posts = []
self.pages = []
self.tags = []
self.files = []
self.tagOverview = nil
self.results = [:]
}
var images: [FileResource] {
@ -73,4 +85,16 @@ final class Content: ObservableObject {
// TODO: Insert at correct index?
pages.insert(page, at: 0)
}
func update(contentPath: URL) {
guard storage.save(contentPath: contentPath) else {
return
}
clear()
do {
try loadFromDisk()
} catch {
print("Failed to reload content: \(error)")
}
}
}

View File

@ -15,7 +15,7 @@ extension Content {
private static let dbPath = FileManager.default.documentDirectory.appendingPathComponent("db").path()
static let mock: Content = Content(
settings: .mock,
settings: .default,
posts: [.empty, .mock, .fullMock],
pages: [.empty],
tags: [.hiking, .mountains, .nature, .sports],

View File

@ -2,7 +2,7 @@ import Foundation
extension Settings {
static let mock: Settings = .init(
static let `default`: Settings = .init(
paths: .default,
navigationItems: [],
posts: .default,

View File

@ -560,6 +560,7 @@ final class Storage: ObservableObject {
}
}
@discardableResult
func save(outputPath: URL) -> Bool {
guard let contentPath else { return false }
guard let bookmarkData = encode(url: outputPath) else { return false }

View File

@ -1,4 +1,5 @@
import SwiftUI
import SFSafeSymbols
struct FolderOnDiskPropertyView: View {
@ -7,21 +8,31 @@ struct FolderOnDiskPropertyView: View {
@Binding
var folder: URL?
@Binding
var isStale: Bool
let footer: LocalizedStringKey
let update: (URL) -> Void
init(title: LocalizedStringKey, folder: Binding<URL?>, footer: LocalizedStringKey, update: @escaping (URL) -> Void) {
init(title: LocalizedStringKey, folder: Binding<URL?>, isStale: Binding<Bool>, footer: LocalizedStringKey, update: @escaping (URL) -> Void) {
self.title = title
self._folder = folder
self._isStale = isStale
self.footer = footer
self.update = update
}
var body: some View {
GenericPropertyView(title: title, footer: footer) {
VStack(alignment: .leading) {
HStack(alignment: .firstTextBaseline) {
Text(folder?.path() ?? "No folder selected")
Text(title)
.font(.headline)
if isStale {
Image(systemSymbol: .exclamationmarkTriangle)
.foregroundStyle(.yellow)
}
Spacer()
Button("Select") {
guard let url = openFolderSelectionPanel() else {
@ -32,6 +43,11 @@ struct FolderOnDiskPropertyView: View {
}
}
}
Text(folder?.path() ?? "No folder selected")
.padding(.bottom, 5)
Text(footer)
.foregroundStyle(.secondary)
.padding(.bottom)
}
}

View File

@ -18,16 +18,15 @@ struct PathSettingsView: View {
FolderOnDiskPropertyView(
title: "Content Folder",
folder: $content.storage.contentPath,
isStale: $content.storage.contentPathUrlIsStale,
footer: "The folder where the raw content of the website is stored") { url in
guard content.storage.save(contentPath: url) else {
return
}
#warning("Reload database")
content.update(contentPath: url)
}
FolderOnDiskPropertyView(
title: "Output Folder",
folder: $content.storage.outputPath,
isStale: $content.storage.outputPathUrlIsStale,
footer: "The folder where the generated website is stored") { url in
content.storage.save(outputPath: url)
}