import Foundation import SwiftUI import Combine final class Content: ObservableObject { @Published var settings: Settings @Published var posts: [Post] @Published var pages: [Page] @Published var tags: [Tag] @Published var files: [FileResource] @AppStorage("contentPath") private var storedContentPath: String = "" @Published var contentPath: String = "" { didSet { storedContentPath = contentPath } } let storage: Storage private var cancellables = Set() init(settings: Settings, posts: [Post], pages: [Page], tags: [Tag], files: [FileResource], storedContentPath: String) { self.settings = settings self.posts = posts self.pages = pages self.tags = tags self.files = files self.storedContentPath = storedContentPath self.contentPath = storedContentPath self.storage = Storage(baseFolder: URL(filePath: storedContentPath)) do { try storage.createFolderStructure() } catch { print(error) return } observeContentPath() } init() { self.storage = Storage(baseFolder: URL(filePath: "")) self.settings = .mock self.posts = [] self.pages = [] self.tags = [] self.files = [] contentPath = storedContentPath do { try storage.createFolderStructure() } catch { print(error) return } try? storage.update(baseFolder: URL(filePath: contentPath)) observeContentPath() } private func observeContentPath() { $contentPath.sink { newValue in let url = URL(filePath: newValue) do { try self.storage.update(baseFolder: url) try self.loadFromDisk() } catch { print("Failed to switch content path: \(error)") } } .store(in: &cancellables) } var images: [FileResource] { files.filter { $0.type.isImage } } }