integrate main: #193 PostsModel split (Watcher/Writes/Imports) into failed-flush departure guard
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s
# Conflicts: # Museion/PostsModel.swift # docs/devlog/INDEX.md
This commit is contained in:
commit
ffb9457a63
47 files changed
+3272
-1811
No files matched your search
@@ -0,0 +1,259 @@
|
||||
//
|
||||
// PostsModelWrites.swift
|
||||
// Museion
|
||||
//
|
||||
// #193: the #179 write rail and the structural library ops, moved from
|
||||
// PostsModel.swift (file-length ceiling) — performWrite, flushOrAbort,
|
||||
// rename/trash, the #108 project ops, and move/archive/restore, code
|
||||
// unchanged.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension PostsModel {
|
||||
// MARK: Own ops (route through the same rescan, #61)
|
||||
|
||||
// ponytail: the PostStore calls here run on MainActor — small single-file
|
||||
// writes and same-volume moves; if a big Home ever stalls the UI, wrap
|
||||
// them in Task.detached like rescan.
|
||||
|
||||
/// #179: the write rail — ONE seam pairs every library store write with
|
||||
/// exactly one rescan and owns the error strip. A verb passes its
|
||||
/// PostStore write (plus any post-write state surgery); a throw lands
|
||||
/// its reason in the strip with nothing rescanned, a clean run rescans
|
||||
/// once. The strip clears BEFORE the write, so a batch write that sets
|
||||
/// its own per-file failure messages mid-closure (importFiles) keeps
|
||||
/// them. The editor's debounced flush stays OUTSIDE the rail — its
|
||||
/// error semantics are the session's own (EditorSession.onError).
|
||||
/// Returns nil when the write landed; the failure reason otherwise
|
||||
/// (createProject hands that to its sheet).
|
||||
@discardableResult
|
||||
func performWrite(_ write: (URL) throws -> Void) -> String? {
|
||||
guard let home = watchedHome else {
|
||||
lastError = "No library is open yet — nothing was changed."
|
||||
return lastError
|
||||
}
|
||||
lastError = nil
|
||||
do {
|
||||
try write(home)
|
||||
Task { await rescan() }
|
||||
return nil
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
return lastError
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicit Rename = retitle + move (#60). Renaming the open post flushes
|
||||
/// first (rename reads the file from disk — unflushed edits must land
|
||||
/// before the move), then reopens a session at the new id. #179: the
|
||||
/// write rides the rail.
|
||||
func rename(_ post: Post, to title: String) {
|
||||
// #108 audit 2: rename reads the file from disk — a failed write
|
||||
// aborts (moving the folder would strand unflushed edits), and the
|
||||
// session stays up, dirty and honest.
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
performWrite { home in
|
||||
let moved = try PostStore.rename(post, to: title, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = moved.id
|
||||
if session?.postID == post.id {
|
||||
session = EditorSession(postID: moved.id, home: home)
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a Post (Trash, recoverable — #18 AC). Clears selection if it
|
||||
/// was the post; the session is torn down (cancel only — the folder is
|
||||
/// leaving, a flush would resurrect nothing but churn the watcher).
|
||||
/// #179: the write rides the rail.
|
||||
func trash(_ post: Post) {
|
||||
performWrite { home in
|
||||
try PostStore.trash(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
if let open = session, open.postID == post.id {
|
||||
open.teardown(flushFirst: false)
|
||||
session = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Projects (#108)
|
||||
|
||||
/// #108: New Project's confirmed sheet — the folder exists, the next
|
||||
/// draft targets it, and opens now.
|
||||
func newProjectPending(named name: String) {
|
||||
pendingProject = name
|
||||
newPost()
|
||||
}
|
||||
|
||||
/// #108: raw create — the sheet composes on it. #179: rides the write
|
||||
/// rail (one rescan pairs the new folder); the failure reason is BOTH
|
||||
/// returned for the sheet's inline display AND owned by the strip —
|
||||
/// the rail posture replaces the old sheet-only rule.
|
||||
func createProject(named name: String) -> String? {
|
||||
performWrite { try PostStore.createProject(named: name, home: $0) }
|
||||
}
|
||||
|
||||
/// #108: File ▸ New Project… — stage the ask (WorkspaceView presents).
|
||||
func stageNewProject() {
|
||||
askNewProject = true
|
||||
}
|
||||
|
||||
/// #108: the ask's Cancel.
|
||||
func cancelNewProject() {
|
||||
askNewProject = false
|
||||
}
|
||||
|
||||
/// #108 audit 2: the Move-to-Project list — every project on disk under
|
||||
/// projects/ plus the default (bind creates the roots, not posts/ — an
|
||||
/// empty Home must still offer it), localized-sorted to match the
|
||||
/// library's section order.
|
||||
func moveTargets() -> [String] {
|
||||
guard let home = watchedHome else { return [Post.defaultProject] }
|
||||
let root = home.appendingPathComponent("projects", isDirectory: true)
|
||||
let onDisk = (try? FileManager.default.contentsOfDirectory(atPath: root.path)) ?? []
|
||||
var names = Set(onDisk.filter { !$0.hasPrefix(".") })
|
||||
names.insert(Post.defaultProject)
|
||||
return names.sorted { $0.localizedStandardCompare($1) == .orderedAscending }
|
||||
}
|
||||
|
||||
/// #108 audit: flush the open piece, aborting when the write failed — a
|
||||
/// folder move must never carry away text that didn't land (the move
|
||||
/// itself succeeds exactly when a file write fails). Returns false with
|
||||
/// lastError set; the session stays up, dirty and honest.
|
||||
/// #33: internal — the Micropub draft flow (Micropub.swift) flushes
|
||||
/// through the same rail.
|
||||
func flushOrAbort(_ postID: String) -> Bool {
|
||||
guard let session, session.postID == postID else { return true }
|
||||
return sessionMayDepart(session)
|
||||
}
|
||||
|
||||
/// brief-25 (failed-flush-discarded-on-document-switch): the departure
|
||||
/// gate for ONE outgoing session — flushOrAbort's two gates lifted to
|
||||
/// document departure. An unanswered #67 conflict means disk holds
|
||||
/// foreign text with the session's version unwritten (#114 audit); a
|
||||
/// failed flush means the edits never landed. Either way the document
|
||||
/// may not leave: the caller aborts its navigation BEFORE clearing
|
||||
/// document-specific state, so the dirty session stays retained and
|
||||
/// honest while lastError says why (the structural rail's #108
|
||||
/// posture — now the navigation sites' too).
|
||||
/// #146: internal — PostsModelOutline's openUnguarded gates too.
|
||||
func sessionMayDepart(_ outgoing: EditorSession) -> Bool {
|
||||
if outgoing.conflictAlert {
|
||||
lastError = "This piece changed on disk underneath you — answer the reload question first."
|
||||
return false
|
||||
}
|
||||
if !outgoing.flush() {
|
||||
// Append, don't replace — the flush already routed the write's
|
||||
// localizedDescription into lastError; only say what was skipped.
|
||||
lastError = (lastError.map { "\($0) " } ?? "") + "— nothing was changed."
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// brief-25: the shared departure run for a full document switch —
|
||||
/// the external guest and the post session both leave (newPost,
|
||||
/// newOutline, openExternal). Each flushes through the departure gate
|
||||
/// first; a failure retains BOTH exactly as they are and the
|
||||
/// navigation aborts with nothing cleared.
|
||||
@discardableResult
|
||||
func departOpenDocuments() -> Bool {
|
||||
guard dismissExternal() else { return false }
|
||||
if let open = session {
|
||||
guard sessionMayDepart(open) else { return false }
|
||||
open.teardown(flushFirst: false) // the gate already flushed
|
||||
session = nil
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// #108: Move to Project — flush first (move reads disk), then the
|
||||
/// folder moves under projects/. Id stays the same; the session is
|
||||
/// reopened anyway so folderURL re-resolves to the new parent.
|
||||
/// #179: the write rides the rail.
|
||||
func moveToProject(_ post: Post, project: String) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
performWrite { home in
|
||||
try PostStore.move(post, to: project, home: home)
|
||||
if session?.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: Move to Archive. Archiving the open piece clears it — the
|
||||
/// editor is empty until the next selection (archived pieces open from
|
||||
/// the Archive pane, where they're listed). #179: the write rides the
|
||||
/// rail.
|
||||
func archivePost(_ post: Post) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
performWrite { home in
|
||||
try PostStore.archive(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
}
|
||||
if let open = session, open.postID == post.id {
|
||||
open.teardown(flushFirst: false) // already flushed above
|
||||
session = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: Move Back — the restored piece lands in its project and opens;
|
||||
/// the writer stays in the piece they just revived. A session already
|
||||
/// open on the piece survives `select` — reopen it so folderURL points
|
||||
/// at projects/, not the archive path it was born with.
|
||||
/// #179: the write rides the rail.
|
||||
func restorePost(_ post: Post) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
performWrite { home in
|
||||
try PostStore.restore(post, home: home)
|
||||
if let open = session, open.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
} else {
|
||||
select(post.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: whole-project unarchive from the Archive pane's section header.
|
||||
/// Flush-first like every folder move (a dirty open piece inside the
|
||||
/// project would otherwise race the move), and its session is reopened so
|
||||
/// folderURL re-resolves off archive/ (same reason as restorePost).
|
||||
/// #179: the write rides the rail.
|
||||
func unarchiveProject(_ name: String) {
|
||||
// #108 audit 2: disk-derived membership — the posts snapshot is stale
|
||||
// the moment a Finder move lands. Captured BEFORE the move: after it
|
||||
// the piece resolves under projects/ and its archive membership is
|
||||
// invisible. dir − 1 = project dir (…/archive/<name>), − 2 = root.
|
||||
guard let boundHome = watchedHome else { return }
|
||||
func openPieceInArchivedProject() -> String? {
|
||||
guard let id = session?.postID,
|
||||
let projectDir = PostStore.folder(of: id, home: boundHome)?.deletingLastPathComponent(),
|
||||
projectDir.lastPathComponent == name,
|
||||
projectDir.deletingLastPathComponent().lastPathComponent == "archive"
|
||||
else { return nil }
|
||||
return id
|
||||
}
|
||||
let openID = openPieceInArchivedProject()
|
||||
if let openID {
|
||||
// #108 audit 2: the only #108 folder move that skipped the gate.
|
||||
guard flushOrAbort(openID) else { return }
|
||||
}
|
||||
performWrite { home in
|
||||
try PostStore.unarchiveProject(name, home: home)
|
||||
if let openID {
|
||||
session = EditorSession(postID: openID, home: home)
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user