Make a second login on an instance reachable and orderable

An additional login could be created but never selected: the legacy
picker lists one entry per instance and always resolved to whichever
account signed in first. Account rows gain a "Use This Account" action,
and the subscriptions view reloads on the data context rather than on
the legacy setting — two logins on the same instance map onto the same
legacy value, so switching between them changed nothing on screen while
writes went to the other account.

Two devices migrating different legacy selections produced contexts with
identical epoch timestamps, and each refused the other's: a permanent,
silent divergence that both sides considered settled. Equal timestamps
are now broken by the selected account's ID, on the incoming side and in
the conflict resolver alike — a record that wins the upload must not be
refused on arrival.

Credential cleanup falls back to both managers whenever the backend
cannot be resolved, not only when the account is unknown here: an
instance deletion can arrive before the tombstone, and the credential
was then left in the Keychain.
This commit is contained in:
Arkadiusz Fal
2026-08-26 22:59:52 +02:00
parent 4f617b52e1
commit ae2fee2d4c
8 changed files with 104 additions and 16 deletions

View File

@@ -24,6 +24,16 @@ enum DataContext: Codable, Hashable, Sendable {
}
var isLocal: Bool { self == .local }
/// Total order used **only** to break a tie between two contexts chosen at
/// the same instant.
///
/// Migrated contexts are all deliberately stamped at the epoch, so two
/// devices migrating different legacy selections produce genuinely equal
/// timestamps. Without a tie-break each would keep its own and consider the
/// matter settled — a permanent, silent divergence. The value is arbitrary;
/// what matters is that every device computes the same one.
var tieBreakKey: String { accountID?.uuidString ?? "" }
}
/// A context together with the moment it was chosen.
@@ -51,6 +61,16 @@ struct StoredDataContext: Codable, Hashable, Sendable {
}
var isMigrated: Bool { changedAt == Date(timeIntervalSince1970: 0) }
/// Whether `other` should replace this selection.
///
/// Ordered by the app-managed timestamp, with `DataContext.tieBreakKey`
/// settling an exact tie so that every device converges on the same answer
/// rather than each keeping its own.
func isOutranked(by other: StoredDataContext) -> Bool {
if other.changedAt != changedAt { return other.changedAt > changedAt }
return other.context.tieBreakKey > context.tieBreakKey
}
}
// MARK: - Legacy migration