Files
hapi/ios/Packages/HapiKit/Sources/HapiClient/Settings/SessionListOrderPrefs.swift
T
weishu 628cb84bf5 feat(ios): web-parity session presence badges and list sort/filter
The home list showed presence only for connected sessions (a green dot,
pulsing while thinking) and left stopped sessions unmarked. Rows were
ordered by pins and the last message only, with no sort or status
choices.

- Presence badge follows web #2296: solid green pulsing while working,
  solid blue pulsing for background tasks only, a green ring when
  connected and waiting, and a power mark when stopped or archived.
  Activity moves off SessionRowStatus, which keeps only pending requests.
- Sort & Filter menu mirrors the web view menu: sort by last message or
  date created, Active first (on by default; manual pins still lead),
  show all / active only / archived only, plus the existing unread lens.
  Sort and Active first persist app-wide (SessionListOrderPrefs); the
  status filter stays per home like the other iOS filters and keeps the
  open session listed. Search keeps relevance with the base tie-break.
- Native parity docs now describe the actual base order.
2026-11-10 17:27:25 +08:00

58 lines
2.2 KiB
Swift

import Foundation
import HapiProtocol
import Observation
/// Session-list sort key and Active first, app-wide over `UserDefaults` (web
/// keeps them in `localStorage`, `useSessionListView.ts`). Defaults are stored
/// as absent keys, so a changed default reaches users who never chose.
/// Row-hiding filters stay transient on the home model; only ordering
/// persists. `shared` keeps every window's list in step.
@MainActor @Observable
public final class SessionListOrderPrefs {
public static let shared = SessionListOrderPrefs()
public private(set) var order: SessionListOrder
// `let` storage is inert under @Observable (no annotation needed).
private let defaults: UserDefaults?
public static let sortKey = "sessionList.sort"
public static let activeFirstKey = "sessionList.activeFirst"
/// `nil` defaults keep choices in memory only (previews and tests).
public init(defaults: UserDefaults? = .standard) {
self.defaults = defaults
let sort = defaults?.string(forKey: Self.sortKey).flatMap(SessionListSort.init(rawValue:))
let activeFirst = defaults.flatMap { defaults in
defaults.object(forKey: Self.activeFirstKey) == nil ? nil : defaults.bool(forKey: Self.activeFirstKey)
}
self.order = SessionListOrder(
sort: sort ?? SessionListOrder.default.sort,
activeFirst: activeFirst ?? SessionListOrder.default.activeFirst
)
}
public func setSort(_ sort: SessionListSort) {
order.sort = sort
if sort == SessionListOrder.default.sort {
defaults?.removeObject(forKey: Self.sortKey)
} else {
defaults?.set(sort.rawValue, forKey: Self.sortKey)
}
}
public func setActiveFirst(_ activeFirst: Bool) {
order.activeFirst = activeFirst
if activeFirst == SessionListOrder.default.activeFirst {
defaults?.removeObject(forKey: Self.activeFirstKey)
} else {
defaults?.set(activeFirst, forKey: Self.activeFirstKey)
}
}
public func reset() {
setSort(SessionListOrder.default.sort)
setActiveFirst(SessionListOrder.default.activeFirst)
}
}