Files
Outpost/Outpost/Root/RootView.swift
T
Puranjay Savar Mattas 4769645489 feat(offline): write queue with sync-on-reconnect + settings redesign
Extends CachingOutlineAPIClient with a scoped offline write queue:
updateDocument, updateCollection, pin/unpin, star/unstar, and
subscribe/unsubscribe now apply optimistically and queue via a new
PendingOperation (SwiftData) when they fail (or when a new manual
"Offline Mode" toggle forces it), then replay on reconnect via
flushPendingOperations(). Same-target edits coalesce into one queued
operation; a pin/unpin pair that never syncs cancels out instead of
queuing a delete the server never saw. Actions that would invent new
tree structure (create/move/archive/delete/duplicate) stay live-only —
reconciling a locally-invented id against the server's real one is a
separate, harder problem this pass doesn't take on. Sharing,
permissions, search, and export also stay live-only.

Added a "Full Local Sync" toggle that eagerly walks and caches the
whole workspace instead of only what's been opened, running
immediately on enable and every 20 minutes after while online.

Settings moved from a popup (Settings {} scene / PreferencesView) to
a full-page view rendered inside the root window (AppNavigation),
including the ⌘, shortcut. Folds in offline/sync management (storage
size, clear cache, pending-sync list with per-item retry) and the
About window's content (version, check for updates) so it's all in
one place.

8 new OutlineKit tests covering coalescing, cancel-out, flush
success/failure, and manual offline mode — 51/51 passing.
2026-08-15 00:12:05 +01:00

78 lines
2.9 KiB
Swift

import SwiftUI
import OutlineKit
struct RootView: View {
@Environment(SessionStore.self) private var session
@Environment(AppNavigation.self) private var navigation
@State private var welcomeName: String?
@State private var starStore = StarStore()
@AppStorage("outpost.fullLocalSyncEnabled") private var isFullLocalSyncEnabled = false
var body: some View {
ZStack {
if session.isSignedIn {
ContentView()
} else {
AuthView(onSigningIn: startWelcomeTransition)
}
if let welcomeName {
WelcomeSplashView(name: welcomeName)
.transition(.opacity)
.zIndex(1)
}
#if os(macOS)
if navigation.isShowingSettings {
SettingsView(onDone: { navigation.isShowingSettings = false })
.transition(.opacity)
.zIndex(2)
}
#endif
}
.environment(starStore)
.animation(.easeInOut(duration: 0.45), value: welcomeName != nil)
.animation(.easeInOut(duration: 0.2), value: navigation.isShowingSettings)
.task {
await session.refreshTeamInfoIfNeeded()
}
.task(id: session.isSignedIn) {
if session.isSignedIn, let apiClient = session.apiClient {
await starStore.load(apiClient: apiClient)
} else {
starStore.reset()
}
}
// Replay whatever queued up while offline the moment the network's
// back — no need to wait for the user to open Settings and hit Retry.
.task(id: session.networkMonitor.isOnline) {
guard session.networkMonitor.isOnline, let cachingClient = session.cachingClient else { return }
_ = await cachingClient.flushPendingOperations()
}
// Full Local Sync re-walks the whole workspace periodically while
// enabled, in addition to the immediate sync Settings kicks off when
// the toggle is first switched on — keeps a long-running session from
// slowly drifting stale.
.task(id: isFullLocalSyncEnabled) {
guard isFullLocalSyncEnabled else { return }
while !Task.isCancelled {
try? await Task.sleep(for: .seconds(1200))
guard !Task.isCancelled, session.networkMonitor.isOnline, let cachingClient = session.cachingClient else { continue }
_ = await cachingClient.performFullSync()
}
}
}
private func startWelcomeTransition(_ result: AuthViewModel.AuthResult) {
welcomeName = result.user.name
session.signIn(serverURL: result.serverURL, user: result.user, team: result.team)
Task {
try? await Task.sleep(for: .seconds(1.1))
withAnimation(.easeInOut(duration: 0.45)) {
welcomeName = nil
}
}
}
}