Save automatically, improve mocks

This commit is contained in:
Christoph Hagen
2025-02-05 12:24:33 +01:00
parent d41c54d174
commit 5abe6e1a9f
55 changed files with 701 additions and 381 deletions

View File

@ -58,7 +58,8 @@ struct InitialSetupView: View {
let loader = ModelLoader(content: content, storage: content.storage)
let result = loader.load()
guard result.errors.isEmpty else {
let message = "Failed to load database\n" + result.errors.sorted().joined(separator: "\n")
let message = "Failed to load database"
#warning("Show load errors")
set(message: message)
return
}

View File

@ -4,13 +4,11 @@ import SFSafeSymbols
/**
**Content**
- iPhone Backgrounds: Add page, html
- CV: Update PDF
**UI**
- Image search: Add view to see all images and filter
- Page Content: Show all results of `PageGenerationResults`
- Files: Show usages of file
- Buttons to insert special commands (images, page links, ...)
**Features**
- Posts: Generate separate pages for posts to link to
@ -26,9 +24,7 @@ import SFSafeSymbols
**Fixes**
- Files: Id change: Check all page contents for links to the renamed file and replace occurences
- Database: Show errors during loading
- Mock content: Clean and improve
- Investigate issue with spaces in content file names
- Check assignment of blog posts to tags
*/
@main
@ -54,10 +50,7 @@ struct MainView: App {
private var showInitialSetupSheet = false
@State
private var showLoadErrorSheet = false
@State
private var loadErrors: [String] = []
private var showStorageErrorSheet = false
@ViewBuilder
var sidebar: some View {
@ -158,15 +151,9 @@ struct MainView: App {
}.pickerStyle(.segmented)
}
ToolbarItem(placement: .primaryAction) {
if content.storage.contentScope != nil {
Button(action: save) {
Text("Save")
}
} else {
Button(action: showInitialSheet) {
Text("Setup")
}
.background(RoundedRectangle(cornerRadius: 8).fill(Color.red))
Button(action: saveButtonPressed) {
Image(systemSymbol: content.saveState.symbol)
.foregroundStyle(content.saveState.color)
}
}
}
@ -175,9 +162,6 @@ struct MainView: App {
.environmentObject(content)
.environmentObject(selection)
.onAppear(perform: loadContent)
.onReceive(Timer.publish(every: 60.0, on: .main, in: .common).autoconnect()) { _ in
save()
}
.sheet(isPresented: $showAddSheet) {
addItemSheet
.environment(\.language, language)
@ -190,30 +174,23 @@ struct MainView: App {
.environmentObject(content)
.environmentObject(selection)
}
.sheet(isPresented: $showLoadErrorSheet) {
VStack {
Text("Failed to load database")
.font(.headline)
List(loadErrors, id: \.self) { error in
HStack {
Text(error)
Spacer()
}
}
.frame(minHeight: 200)
Button("Dismiss", action: { showLoadErrorSheet = false })
.padding()
}
.padding()
.sheet(isPresented: $showStorageErrorSheet) {
StorageErrorView(isPresented: $showStorageErrorSheet)
.environmentObject(content)
}
}
}
private func save() {
guard content.saveToDisk() else {
print("Failed to save content")
#warning("Show error message")
return
private func saveButtonPressed() {
switch content.saveState {
case .storageNotInitialized:
showInitialSheet()
case .isSaved:
content.saveUnconditionally()
case .needsSave:
content.saveUnconditionally()
case .failedToSave:
showStorageErrorSheet = true
}
}
@ -222,13 +199,11 @@ struct MainView: App {
showInitialSheet()
return
}
content.loadFromDisk { errors in
content.loadFromDisk {
prepareAfterLoad()
guard !errors.isEmpty else {
return
if !content.storageErrors.isEmpty {
self.showStorageErrorSheet = true
}
self.loadErrors = errors
self.showLoadErrorSheet = true
}
}

View File

@ -0,0 +1,28 @@
import SwiftUI
struct StorageErrorView: View {
@EnvironmentObject
private var content: Content
@Binding
var isPresented: Bool
var body: some View {
VStack {
Text("Failed to load database")
.font(.headline)
List(content.storageErrors) { error in
VStack {
Text(error.message)
Text(error.date.formatted())
.font(.footnote)
}
}
.frame(minHeight: 300)
Button("Dismiss", action: { isPresented = false })
.padding()
}
.padding()
}
}