Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): a failed call says what the network did

David Lecd917c73959c
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Analytics.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Analytics.swift
index b4428532..cd463945 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Analytics.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Analytics.swift
@@ -275,15 +275,21 @@ public final class AnalyticsModel {
}
/// Fetches the rollup. A refresh over loaded data keeps the charts up
- /// while it runs and on failure; a first load surfaces the error, saying
+ /// while it runs and on failure; a retry over a failure spins, so the tap
+ /// shows something happening. A first load surfaces the error, saying
/// which one it was — a failure that only reads "try again" tells nobody
/// whether the phone, the session, or the server is at fault.
+ ///
+ /// A cancelled fetch is the screen closing, so it leaves the state alone.
public func refresh() async {
+ if case .failed = state { state = .loading }
do {
let rollup = try await service.fetchAnalyticsRollup()
state = .loaded(AnalyticsSummary(rollup: rollup))
} catch AnalyticsError.noRollup {
state = .empty
+ } catch is CancellationError {
+ return
} catch {
if case .loaded = state { return }
state = .failed(Self.reason(for: error))
@@ -296,10 +302,12 @@ public final class AnalyticsModel {
"This account can't read analytics. Sign out and back in."
case CloudSyncError.refused(let message):
message
+ case CloudSyncError.unreachable(let reach, let code):
+ "\(reach.sentence) (\(code))"
case AnalyticsError.unreadable:
"The rollup came back in a shape this build doesn't read."
default:
- "Couldn't reach donkeycut.com. Check your connection."
+ "The rollup didn't load. Try again."
}
}
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
index 13346e9e..ff7e9cab 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
@@ -217,6 +217,37 @@ nonisolated public enum RenderProgress: Equatable, Sendable {
case failed(String)
}
+/// Why a request never reached an answer. The client keeps the network's own
+/// verdict this far so a screen can say what happened; collapsing every
+/// network failure into one word leaves a phone with full bars being told to
+/// check its connection.
+nonisolated public enum NetworkReach: Sendable, Equatable {
+ /// The phone has no route out at all — airplane mode, no signal.
+ case offline
+ /// The link is up and the answer took longer than the client waits.
+ case timedOut
+ /// The connection died between the request and the answer.
+ case dropped
+ /// The host itself went unanswered: DNS, TLS, a refused socket.
+ case unreachable
+
+ /// Whether asking again in a moment could get a different answer. A phone
+ /// with no route out needs the person, so nothing retries there.
+ public var isWorthAnotherTry: Bool {
+ self != .offline
+ }
+
+ /// A sentence for the person holding the phone, naming this cause.
+ public var sentence: String {
+ switch self {
+ case .offline: "This phone is offline. Reconnect and try again."
+ case .timedOut: "donkeycut.com took too long to answer."
+ case .dropped: "The connection dropped before the answer arrived."
+ case .unreachable: "Couldn't reach donkeycut.com."
+ }
+ }
+}
+
nonisolated public enum CloudSyncError: Error, Equatable {
/// The server refused with a reason worth reading out loud — out of
/// renders for the day, out of room. The message is the server's own.
@@ -226,8 +257,12 @@ nonisolated public enum CloudSyncError: Error, Equatable {
case storageFull
/// The session is gone; syncing waits for the next sign-in.
case unauthorized
- /// Anything transient: network, 5xx. The item stays queued.
+ /// An answer came back and it was no good: a 5xx, a body that won't
+ /// decode. The item stays queued.
case transport
+ /// No answer came back at all, and the network said why. Transient like
+ /// `transport`; the item stays queued.
+ case unreachable(NetworkReach, code: Int)
}
/// What the app target's CutCloudClient does for the sync engine.
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index f8b5863a..22158473 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -995,6 +995,93 @@ import Testing
}
}
+@Suite struct AnalyticsModelTests {
+ final class Service: AnalyticsServicing, @unchecked Sendable {
+ var answer: Result<AnalyticsRollup, any Error> = .failure(CloudSyncError.transport)
+ var calls = 0
+
+ func fetchAnalyticsRollup() async throws -> AnalyticsRollup {
+ calls += 1
+ return try answer.get()
+ }
+ }
+
+ func rollup() -> AnalyticsRollup {
+ AnalyticsRollup(
+ generatedAt: "2026-08-21T04:00:00.000Z",
+ days: ["2026-08-20"],
+ sources: ["renders"],
+ missing: [],
+ billing: nil,
+ users: [.init(registeredAt: "2026-08-01T10:00:00.000Z", balanceMicros: "0", activity: [1])]
+ )
+ }
+
+ @Test func aDeadLinkNamesItselfAndCarriesItsCode() async {
+ let service = Service()
+ service.answer = .failure(CloudSyncError.unreachable(.dropped, code: -1005))
+ let model = AnalyticsModel(service: service)
+ await model.refresh()
+ guard case .failed(let reason) = model.state else {
+ Issue.record("expected a failed state")
+ return
+ }
+ #expect(reason.contains("connection dropped"))
+ #expect(reason.contains("-1005"))
+ }
+
+ @Test func anOfflinePhoneIsToldItIsOffline() async {
+ let service = Service()
+ service.answer = .failure(CloudSyncError.unreachable(.offline, code: -1009))
+ let model = AnalyticsModel(service: service)
+ await model.refresh()
+ guard case .failed(let reason) = model.state else {
+ Issue.record("expected a failed state")
+ return
+ }
+ #expect(reason.hasPrefix("This phone is offline"))
+ }
+
+ @Test func aCancelledFetchLeavesTheStateAlone() async {
+ let service = Service()
+ service.answer = .failure(CancellationError())
+ let model = AnalyticsModel(service: service)
+ await model.refresh()
+ guard case .loading = model.state else {
+ Issue.record("a cancelled fetch should leave the screen loading")
+ return
+ }
+ }
+
+ @Test func aRetryOverAFailureSpinsAgain() async {
+ let service = Service()
+ service.answer = .failure(CloudSyncError.unreachable(.timedOut, code: -1001))
+ let model = AnalyticsModel(service: service)
+ await model.refresh()
+ service.answer = .success(rollup())
+ await model.refresh()
+ guard case .loaded(let summary) = model.state else {
+ Issue.record("expected the retry to load")
+ return
+ }
+ #expect(summary.registered == 1)
+ #expect(service.calls == 2)
+ }
+
+ @Test func aFailedRefreshKeepsChartsThatAlreadyLoaded() async {
+ let service = Service()
+ service.answer = .success(rollup())
+ let model = AnalyticsModel(service: service)
+ await model.refresh()
+ service.answer = .failure(CloudSyncError.unreachable(.dropped, code: -1005))
+ await model.refresh()
+ guard case .loaded = model.state else {
+ Issue.record("a refresh failure should leave the loaded charts up")
+ return
+ }
+ }
+}
+
@Suite struct UserProfileDecodingTests {
@Test func profileCachedBeforeRoleFieldDecodesAsRegular() throws {
let data = Data(#"{"id":"u1","name":"Dana","email":"[email protected]"}"#.utf8)