mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-05 07:07:04 +00:00
fix(ios): defer list and transcript churn
This commit is contained in:
@@ -275,7 +275,10 @@ final class SessionListModel {
|
||||
/// All session groups, including historical / unidentified machines.
|
||||
/// The online roster is only a source of labels, never filter membership.
|
||||
var machineFilterIds: Set<String> {
|
||||
Set(sessionStore.sessions.map { $0.metadata?.machineId ?? unknownMachineFilterId })
|
||||
if let store = sessionStore as? SessionListStore {
|
||||
return store.machineFilterIds
|
||||
}
|
||||
return Set(sessionStore.sessions.map { $0.metadata?.machineId ?? unknownMachineFilterId })
|
||||
}
|
||||
|
||||
/// Counts are pre-filter. Names, not live counts, determine menu order.
|
||||
|
||||
@@ -83,6 +83,9 @@ public final class SessionListStore: SessionListStoring {
|
||||
/// real change also bumps ``listRevision`` and schedules a snapshot.
|
||||
public private(set) var sessions: [SessionSummary]
|
||||
|
||||
/// Cached machine membership for the session filter menu.
|
||||
public private(set) var machineFilterIds: Set<String>
|
||||
|
||||
/// All cached details, keyed by session id.
|
||||
public private(set) var details: [String: Session] = [:]
|
||||
|
||||
@@ -138,7 +141,9 @@ public final class SessionListStore: SessionListStoring {
|
||||
self.api = api
|
||||
self.snapshot = cache
|
||||
self.refreshBatch = refreshBatch
|
||||
self.sessions = cache?.load().map(sortSessionSummaries) ?? []
|
||||
let loadedSessions = cache?.load().map(sortSessionSummaries) ?? []
|
||||
self.sessions = loadedSessions
|
||||
self.machineFilterIds = Set(loadedSessions.map { $0.metadata?.machineId ?? "__unknown__" })
|
||||
}
|
||||
|
||||
// MARK: - Details
|
||||
@@ -494,6 +499,7 @@ public final class SessionListStore: SessionListStoring {
|
||||
private func setSessions(_ next: [SessionSummary]) {
|
||||
guard next != sessions else { return }
|
||||
sessions = next
|
||||
machineFilterIds = Set(next.map { $0.metadata?.machineId ?? "__unknown__" })
|
||||
listRevision += 1
|
||||
snapshot?.scheduleWrite(next)
|
||||
onSessionsChanged?(next)
|
||||
|
||||
@@ -116,6 +116,11 @@ public enum WidgetSnapshotBuilder {
|
||||
public static let defaultLimit = 20
|
||||
public static let defaultRecentWindowMs = 15 * 60 * 1000
|
||||
|
||||
private struct Candidate {
|
||||
let summary: SessionSummary
|
||||
let status: LiveActivityContentState.Status
|
||||
}
|
||||
|
||||
public static func build(
|
||||
sessions: [SessionSummary],
|
||||
hubUrl: String,
|
||||
@@ -125,7 +130,7 @@ public enum WidgetSnapshotBuilder {
|
||||
limit: Int = defaultLimit
|
||||
) -> WidgetSnapshot {
|
||||
let cutoff = now - recentWindowMs
|
||||
let rows = sessions.compactMap { summary -> WidgetSnapshot.Session? in
|
||||
let candidates = sessions.compactMap { summary -> Candidate? in
|
||||
let status: LiveActivityContentState.Status
|
||||
if summary.active {
|
||||
if summary.pendingRequestsCount > 0 {
|
||||
@@ -147,6 +152,17 @@ public enum WidgetSnapshotBuilder {
|
||||
guard summary.updatedAt >= cutoff else { return nil }
|
||||
status = .done
|
||||
}
|
||||
return Candidate(summary: summary, status: status)
|
||||
}
|
||||
.sorted { lhs, rhs in
|
||||
let lp = priority(lhs.status)
|
||||
let rp = priority(rhs.status)
|
||||
if lp != rp { return lp > rp }
|
||||
return lhs.summary.updatedAt > rhs.summary.updatedAt
|
||||
}
|
||||
|
||||
let limited = candidates.prefix(max(0, limit)).map { candidate in
|
||||
let summary = candidate.summary
|
||||
let preview = lastMessage(for: summary)
|
||||
let result = lastResult(for: summary)
|
||||
return WidgetSnapshot.Session(
|
||||
@@ -154,7 +170,7 @@ public enum WidgetSnapshotBuilder {
|
||||
title: title(for: summary),
|
||||
project: projectName(for: summary),
|
||||
flavor: summary.metadata?.flavor,
|
||||
status: status,
|
||||
status: candidate.status,
|
||||
startedAt: summary.lastUserMessageAt,
|
||||
updatedAt: summary.updatedAt,
|
||||
pendingCount: summary.pendingRequestsCount,
|
||||
@@ -166,23 +182,15 @@ public enum WidgetSnapshotBuilder {
|
||||
isUnread: unreadSessionIds.contains(summary.id) ? true : nil
|
||||
)
|
||||
}
|
||||
.sorted { lhs, rhs in
|
||||
let lp = priority(lhs.status)
|
||||
let rp = priority(rhs.status)
|
||||
if lp != rp { return lp > rp }
|
||||
return lhs.updatedAt > rhs.updatedAt
|
||||
}
|
||||
|
||||
let limited = Array(rows.prefix(max(0, limit)))
|
||||
return WidgetSnapshot(
|
||||
generatedAt: now,
|
||||
hubUrl: hubUrl,
|
||||
sessions: limited,
|
||||
runningCount: rows.filter { $0.status == .working }.count,
|
||||
needsApprovalCount: rows.filter { $0.status == .needsApproval }.count,
|
||||
needsInputCount: rows.filter { $0.status == .needsInput }.count,
|
||||
needsAttentionCount: rows.filter { $0.status == .needsAttention }.count,
|
||||
idleCount: rows.filter { $0.status == .idle }.count
|
||||
runningCount: candidates.filter { $0.status == .working }.count,
|
||||
needsApprovalCount: candidates.filter { $0.status == .needsApproval }.count,
|
||||
needsInputCount: candidates.filter { $0.status == .needsInput }.count,
|
||||
needsAttentionCount: candidates.filter { $0.status == .needsAttention }.count,
|
||||
idleCount: candidates.filter { $0.status == .idle }.count
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -396,10 +396,11 @@ public final class TranscriptCollectionController<Item: Identifiable & Equatable
|
||||
public override func viewDidLayoutSubviews() {
|
||||
super.viewDidLayoutSubviews()
|
||||
guard collection.bounds.width > 24 else { return }
|
||||
if abs(configuredWidth - collection.bounds.width) > 0.5, !applying {
|
||||
if abs(configuredWidth - collection.bounds.width) > 0.5, !applying,
|
||||
!collection.isDragging, !collection.isDecelerating {
|
||||
if pending == nil { pending = configuration }
|
||||
drain()
|
||||
} else if layout.followsTail, !applying, !collection.isDragging {
|
||||
} else if layout.followsTail, !applying, !collection.isDragging, !collection.isDecelerating {
|
||||
layout.correctOffset(to: layout.bottomOffset(collection))
|
||||
}
|
||||
reportViewport()
|
||||
@@ -410,6 +411,10 @@ public final class TranscriptCollectionController<Item: Identifiable & Equatable
|
||||
pending = configuration
|
||||
// The first SwiftUI update may arrive before UIKit supplies bounds.
|
||||
guard collection.bounds.width > 24 else { return }
|
||||
// Keep the pan/deceleration path focused on UIKit's scroll work. SwiftUI
|
||||
// may deliver several transcript revisions while the reader is moving.
|
||||
// The latest one remains in `pending` and is committed after the gesture.
|
||||
guard !collection.isDragging, !collection.isDecelerating else { return }
|
||||
drain()
|
||||
}
|
||||
|
||||
@@ -583,6 +588,7 @@ public final class TranscriptCollectionController<Item: Identifiable & Equatable
|
||||
|
||||
private func finishGesture() {
|
||||
layout.followsTail = configuration?.isInspectionPresented != true && layout.bottomOffset(collection) - collection.contentOffset.y <= 1
|
||||
drain()
|
||||
reportViewport(deferred: false)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user