oficina / sheets Public

Native: the status says Saving… while edits are on their way, and they are let land

Review #72 finding 3. The status bar kept the previous "Saved" while edits
were queued or in flight, so on a hung network every edit looked saved.
It now reads Saving… (12 languages) until nothing is left to send, and
"Saved" only then; Close and Quit, which wait for edits on their way,
therefore show what they are waiting for.

While anything is sending, the model holds the system open: on iOS a
background task, so a swipe away or a switch to another app does not cut
the send off; on the Mac sudden termination is disabled.

Test: statusSaysSavingWhileEditsAreOnTheirWay.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7088fc336994ce53c17416eb12a682901baf4cb2 parent 6e73b13
16 files changed, +75 −2
  • native/apple/Sources/SheetsKit/Resources/ar.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/bn.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/en.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/es.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/ga.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/hi.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/ja.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/pt.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/ru.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/vi.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/yue.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/Resources/zh-Hans.lproj/Mac.strings +1 −0
  • native/apple/Sources/SheetsKit/SheetEditingControls.swift +1 −1
  • native/apple/Sources/SheetsKit/SheetModel.swift +4 −0
  • native/apple/Sources/SheetsKit/SheetWrites.swift +31 −1
  • native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift +27 −0
diff --git a/native/apple/Sources/SheetsKit/Resources/ar.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/ar.lproj/Mac.strings
index 21637c1..13bbfe3 100644
--- a/native/apple/Sources/SheetsKit/Resources/ar.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/ar.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "استعادة";
"Not saved" = "لم يُحفظ";
"Some cell edits weren’t saved." = "لم تُحفظ بعض تعديلات الخلايا.";
+"Saving…" = "جارٍ الحفظ…";
diff --git a/native/apple/Sources/SheetsKit/Resources/bn.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/bn.lproj/Mac.strings
index b2cb2b0..edcf304 100644
--- a/native/apple/Sources/SheetsKit/Resources/bn.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/bn.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "আগের অবস্থায় ফেরান";
"Not saved" = "সংরক্ষিত হয়নি";
"Some cell edits weren’t saved." = "কিছু ঘরের সম্পাদনা সংরক্ষিত হয়নি।";
+"Saving…" = "সংরক্ষণ করা হচ্ছে…";
diff --git a/native/apple/Sources/SheetsKit/Resources/en.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/en.lproj/Mac.strings
index a867788..22a9fc2 100644
--- a/native/apple/Sources/SheetsKit/Resources/en.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/en.lproj/Mac.strings
@@ -79,3 +79,4 @@
"Revert" = "Revert";
"Not saved" = "Not saved";
"Some cell edits weren’t saved." = "Some cell edits weren’t saved.";
+"Saving…" = "Saving…";
diff --git a/native/apple/Sources/SheetsKit/Resources/es.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/es.lproj/Mac.strings
index ff7095d..26c2511 100644
--- a/native/apple/Sources/SheetsKit/Resources/es.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/es.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "Revertir";
"Not saved" = "No guardado";
"Some cell edits weren’t saved." = "Algunos cambios en celdas no se guardaron.";
+"Saving…" = "Guardando…";
diff --git a/native/apple/Sources/SheetsKit/Resources/ga.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/ga.lproj/Mac.strings
index 69ed293..261c9f8 100644
--- a/native/apple/Sources/SheetsKit/Resources/ga.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/ga.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "Fill ar ais";
"Not saved" = "Níor sábháladh";
"Some cell edits weren’t saved." = "Níor sábháladh roinnt athruithe ar chealla.";
+"Saving…" = "Á shábháil…";
diff --git a/native/apple/Sources/SheetsKit/Resources/hi.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/hi.lproj/Mac.strings
index 35d95cf..3c27a55 100644
--- a/native/apple/Sources/SheetsKit/Resources/hi.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/hi.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "पहले जैसा करें";
"Not saved" = "सहेजा नहीं गया";
"Some cell edits weren’t saved." = "कुछ सेल के बदलाव सहेजे नहीं गए।";
+"Saving…" = "सहेजा जा रहा है…";
diff --git a/native/apple/Sources/SheetsKit/Resources/ja.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/ja.lproj/Mac.strings
index ebff5b3..df75272 100644
--- a/native/apple/Sources/SheetsKit/Resources/ja.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/ja.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "元に戻す";
"Not saved" = "保存されていません";
"Some cell edits weren’t saved." = "一部のセルの変更が保存されませんでした。";
+"Saving…" = "保存中…";
diff --git a/native/apple/Sources/SheetsKit/Resources/pt.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/pt.lproj/Mac.strings
index c4e9a52..e2ec7eb 100644
--- a/native/apple/Sources/SheetsKit/Resources/pt.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/pt.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "Reverter";
"Not saved" = "Não guardado";
"Some cell edits weren’t saved." = "Algumas alterações em células não foram guardadas.";
+"Saving…" = "A guardar…";
diff --git a/native/apple/Sources/SheetsKit/Resources/ru.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/ru.lproj/Mac.strings
index 8ab7b5f..5f19d46 100644
--- a/native/apple/Sources/SheetsKit/Resources/ru.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/ru.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "Вернуть";
"Not saved" = "Не сохранено";
"Some cell edits weren’t saved." = "Некоторые изменения ячеек не сохранены.";
+"Saving…" = "Сохранение…";
diff --git a/native/apple/Sources/SheetsKit/Resources/vi.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/vi.lproj/Mac.strings
index 4cc5dd7..d08d48a 100644
--- a/native/apple/Sources/SheetsKit/Resources/vi.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/vi.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "Hoàn nguyên";
"Not saved" = "Chưa lưu";
"Some cell edits weren’t saved." = "Một số chỉnh sửa ô chưa được lưu.";
+"Saving…" = "Đang lưu…";
diff --git a/native/apple/Sources/SheetsKit/Resources/yue.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/yue.lproj/Mac.strings
index e1b1112..438fdb8 100644
--- a/native/apple/Sources/SheetsKit/Resources/yue.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/yue.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "還原";
"Not saved" = "未儲存";
"Some cell edits weren’t saved." = "有啲儲存格嘅修改未儲存到。";
+"Saving…" = "儲存緊…";
diff --git a/native/apple/Sources/SheetsKit/Resources/zh-Hans.lproj/Mac.strings b/native/apple/Sources/SheetsKit/Resources/zh-Hans.lproj/Mac.strings
index a8ab488..a40219c 100644
--- a/native/apple/Sources/SheetsKit/Resources/zh-Hans.lproj/Mac.strings
+++ b/native/apple/Sources/SheetsKit/Resources/zh-Hans.lproj/Mac.strings
@@ -78,3 +78,4 @@
"Revert" = "还原";
"Not saved" = "未保存";
"Some cell edits weren’t saved." = "部分单元格修改未保存。";
+"Saving…" = "正在保存…";
diff --git a/native/apple/Sources/SheetsKit/SheetEditingControls.swift b/native/apple/Sources/SheetsKit/SheetEditingControls.swift
index 6e8da77..2cabdfd 100644
--- a/native/apple/Sources/SheetsKit/SheetEditingControls.swift
+++ b/native/apple/Sources/SheetsKit/SheetEditingControls.swift
@@ -58,7 +58,7 @@ struct SheetStatusBar: View {
Text(model.page?.Sheet.TabName ?? "Sheet")
Spacer()
if model.busy { ProgressView().controlSize(.small) }
- Text(model.dirty ? "Unsaved cell edit" : model.notice)
+ Text(model.saveStatus)
Text(model.live).foregroundStyle(.secondary)
}.font(.caption).padding(8)
}
diff --git a/native/apple/Sources/SheetsKit/SheetModel.swift b/native/apple/Sources/SheetsKit/SheetModel.swift
index bd9653b..cf3b1d6 100644
--- a/native/apple/Sources/SheetsKit/SheetModel.swift
+++ b/native/apple/Sources/SheetsKit/SheetModel.swift
@@ -22,6 +22,10 @@ import RastrilloApple
var pending: [Coordinate: PendingEdit] = [:]
@ObservationIgnored var editGeneration: UInt64 = 0
@ObservationIgnored var sender: Task<Void, Never>?
+ /// Held for as long as edits are on their way, so the system lets them land:
+ /// on iOS a background task (a swipe away or a switch to another app does
+ /// not cut the send off), on the Mac no sudden termination. Returns the end.
+ @ObservationIgnored var keepAliveWhileSending: @MainActor () -> (@MainActor () -> Void) = SheetModel.keepAlive
/// True while `open` replaces the sheet: no edit may start against either.
@ObservationIgnored var opening = false
/// An edit being typed, or one not yet acknowledged by the server.
diff --git a/native/apple/Sources/SheetsKit/SheetWrites.swift b/native/apple/Sources/SheetsKit/SheetWrites.swift
index 4e3984c..7053a55 100644
--- a/native/apple/Sources/SheetsKit/SheetWrites.swift
+++ b/native/apple/Sources/SheetsKit/SheetWrites.swift
@@ -1,5 +1,8 @@
import Foundation
import RastrilloApple
+#if os(iOS)
+import UIKit
+#endif
/// A value edit the server has not acknowledged yet.
///
@@ -53,6 +56,31 @@ struct PendingEdit: Sendable, Equatable {
var hasFailedEdits: Bool { pending.values.contains { $0.state == .failed } }
var isSending: Bool { pending.values.contains { $0.state != .failed } }
+ /// The status bar's words: an edit being typed, edits on their way, or the
+ /// last outcome. "Saved" only once nothing is left to send.
+ var saveStatus: String {
+ dirty ? "Unsaved cell edit" : isSending ? sheetsMacString("Saving…") : notice
+ }
+
+ static func keepAlive() -> (@MainActor () -> Void) {
+ #if os(iOS)
+ final class Holder { var id = UIBackgroundTaskIdentifier.invalid }
+ let app = UIApplication.shared
+ let task = Holder()
+ task.id = app.beginBackgroundTask(withName: "Saving cell edits") {
+ MainActor.assumeIsolated {
+ app.endBackgroundTask(task.id)
+ task.id = .invalid
+ }
+ }
+ return {
+ if task.id != .invalid { app.endBackgroundTask(task.id); task.id = .invalid }
+ }
+ #else
+ ProcessInfo.processInfo.disableSuddenTermination()
+ return { ProcessInfo.processInfo.enableSuddenTermination() }
+ #endif
+ }
/// The cell as it should look: a pending local value wins over the server's.
func displayCell(_ point: Coordinate) -> Cell? {
@@ -94,8 +122,10 @@ struct PendingEdit: Sendable, Equatable {
func startSending() {
guard sender == nil, pending.values.contains(where: { $0.state == .queued }) else { return }
+ let release = keepAliveWhileSending()
sender = Task { [weak self] in
await self?.sendQueued()
+ release()
self?.sender = nil
// An edit queued while the last answer was being applied.
self?.startSending()
@@ -129,7 +159,7 @@ struct PendingEdit: Sendable, Equatable {
offset += chunk.count
}
error = ""
- notice = "Saved"
+ if !isSending { notice = "Saved" }
if showTable { await loadTable() }
} catch {
guard grid == source else { return abandon(batch) }
diff --git a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift
index 901df57..65668ed 100644
--- a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift
+++ b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift
@@ -243,3 +243,30 @@ private let b1 = Coordinate(r: 0, c: 1)
#expect(model.displayCell(a1) == nil)
#expect(!model.hasUnconfirmedChanges)
}
+
+// Review #72, finding 3: "Saved" only once nothing is left to send, and the
+// system is asked to let edits on their way land.
+@MainActor @Test func statusSaysSavingWhileEditsAreOnTheirWay() async {
+ let server = SheetServerFixture()
+ server.hold()
+ let model = fixtureModel(server)
+ var held = 0
+ var released = 0
+ model.keepAliveWhileSending = {
+ held += 1
+ return { released += 1 }
+ }
+ model.notice = "Saved"
+ type(model, a1, "1")
+ #expect(model.saveStatus == "Saving…")
+ #expect(held == 1 && released == 0)
+ #expect(await eventually { server.heldCount == 1 })
+ type(model, a2, "2")
+ server.release()
+ #expect(await eventually { server.heldCount == 1 })
+ #expect(model.saveStatus == "Saving…", "the first answer is not the last")
+ server.release(stopHolding: true)
+ await model.settle()
+ #expect(model.saveStatus == "Saved")
+ #expect(held == released && held >= 1, "every hold on the system was let go")
+}