Rework path storage, add start screen
This commit is contained in:
@ -1,8 +1,73 @@
|
||||
import SwiftUI
|
||||
|
||||
struct InitialSetupView: View {
|
||||
|
||||
|
||||
@EnvironmentObject
|
||||
private var content: Content
|
||||
|
||||
@Environment(\.dismiss)
|
||||
private var dismiss
|
||||
|
||||
@State
|
||||
private var message: String?
|
||||
|
||||
var body: some View {
|
||||
/*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
|
||||
VStack {
|
||||
Text("No Database Loaded")
|
||||
.font(.title)
|
||||
.padding()
|
||||
Text("To start editing the content of a website, create a new database or load an existing one. Open a folder with an existing database, or choose an empty folder to create a new project.")
|
||||
.multilineTextAlignment(.center)
|
||||
Button("Select folder", action: selectContentPath)
|
||||
.padding()
|
||||
if let message {
|
||||
Text(message)
|
||||
.padding(.bottom)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
.frame(maxWidth: 350)
|
||||
}
|
||||
|
||||
private func selectContentPath() {
|
||||
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 the database folder"
|
||||
|
||||
let response = panel.runModal()
|
||||
guard response == .OK else {
|
||||
set(message: "Failed to select a folder: \(response)")
|
||||
return
|
||||
}
|
||||
guard let url = panel.url else {
|
||||
set(message: "No folder url found")
|
||||
return
|
||||
}
|
||||
|
||||
guard content.storage.save(contentPath: url) else {
|
||||
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)")
|
||||
return
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private func set(message: String) {
|
||||
DispatchQueue.main.async {
|
||||
self.message = message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -162,18 +162,15 @@ struct MainView: App {
|
||||
}.pickerStyle(.segmented)
|
||||
}
|
||||
ToolbarItem(placement: .primaryAction) {
|
||||
if content.storageIsInitialized {
|
||||
if content.storage.hasContentFolders {
|
||||
Button(action: save) {
|
||||
Text("Save")
|
||||
}
|
||||
} else {
|
||||
Button {
|
||||
selectedSection = .folders
|
||||
selectedTab = .generation
|
||||
} label: {
|
||||
Button(action: showInitialSheet) {
|
||||
Text("Setup")
|
||||
}
|
||||
.foregroundColor(.red)
|
||||
.background(RoundedRectangle(cornerRadius: 8).fill(Color.red))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -206,10 +203,10 @@ struct MainView: App {
|
||||
}
|
||||
|
||||
private func loadContent() {
|
||||
guard content.storageIsInitialized else {
|
||||
DispatchQueue.main.async {
|
||||
self.showInitialSetupSheet = true
|
||||
}
|
||||
#warning("Remove")
|
||||
content.storage.clearContentPath()
|
||||
guard content.storage.hasContentFolders else {
|
||||
showInitialSheet()
|
||||
return
|
||||
}
|
||||
do {
|
||||
@ -218,5 +215,13 @@ struct MainView: App {
|
||||
print("Failed to load content: \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
private func showInitialSheet() {
|
||||
DispatchQueue.main.async {
|
||||
selectedSection = .folders
|
||||
selectedTab = .generation
|
||||
showInitialSetupSheet = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user