Repository navigation
Fix stuttery iOS workspace refresh animation #8397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
c15b812
dcf5ad0
95213b6
617738c
0fa999f
e0356ff
336b36f
f8b16a6
b829e0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| #if os(iOS) | ||
| import Foundation | ||
|
|
||
| /// Orders refresh-driven table snapshots ahead of the native refresh collapse. | ||
| struct WorkspaceListRefreshLifecycle { | ||
| struct RefreshID: Equatable { | ||
| fileprivate let rawValue: UInt64 | ||
| } | ||
|
|
||
| struct SnapshotApplyID: Equatable { | ||
| fileprivate let rawValue: UInt64 | ||
| } | ||
|
|
||
| private enum Phase: Equatable { | ||
| case idle | ||
| case refreshing(id: RefreshID, targetGeneration: UInt64) | ||
| case awaitingFinalSnapshot( | ||
| id: RefreshID, | ||
| targetGeneration: UInt64, | ||
| latestApplyID: SnapshotApplyID? | ||
| ) | ||
| case collapsing(id: RefreshID) | ||
| } | ||
|
|
||
| private var phase: Phase = .idle | ||
| private var nextRefreshRawValue: UInt64 = 0 | ||
| private var nextApplyRawValue: UInt64 = 0 | ||
|
|
||
| var suppressesSnapshotAnimations: Bool { | ||
| phase != .idle | ||
| } | ||
|
|
||
| mutating func begin(currentGeneration: UInt64) -> RefreshID? { | ||
| guard phase == .idle else { return nil } | ||
| nextRefreshRawValue &+= 1 | ||
| let id = RefreshID(rawValue: nextRefreshRawValue) | ||
| phase = .refreshing(id: id, targetGeneration: currentGeneration &+ 1) | ||
| return id | ||
| } | ||
|
|
||
| mutating func refreshActionCompleted(_ id: RefreshID) -> Bool { | ||
| guard case .refreshing(let activeID, let targetGeneration) = phase, | ||
| activeID == id else { | ||
| return false | ||
| } | ||
| phase = .awaitingFinalSnapshot( | ||
| id: id, | ||
| targetGeneration: targetGeneration, | ||
| latestApplyID: nil | ||
| ) | ||
| return true | ||
| } | ||
|
|
||
| mutating func snapshotApplyStarted( | ||
| refreshCompletionGeneration: UInt64 | ||
| ) -> SnapshotApplyID? { | ||
| guard case .awaitingFinalSnapshot( | ||
| let id, | ||
| let targetGeneration, | ||
| _ | ||
| ) = phase, refreshCompletionGeneration == targetGeneration else { | ||
| return nil | ||
| } | ||
| nextApplyRawValue &+= 1 | ||
| let applyID = SnapshotApplyID(rawValue: nextApplyRawValue) | ||
| phase = .awaitingFinalSnapshot( | ||
| id: id, | ||
| targetGeneration: targetGeneration, | ||
| latestApplyID: applyID | ||
| ) | ||
| return applyID | ||
| } | ||
|
|
||
| mutating func snapshotApplyCompleted(_ applyID: SnapshotApplyID) -> Bool { | ||
| guard case .awaitingFinalSnapshot( | ||
| let id, | ||
| _, | ||
| let latestApplyID | ||
| ) = phase, latestApplyID == applyID else { | ||
| return false | ||
| } | ||
| phase = .collapsing(id: id) | ||
| return true | ||
| } | ||
|
|
||
| mutating func observeCollapse( | ||
| refreshControlIsRefreshing: Bool, | ||
| scrollViewIsTracking: Bool, | ||
| contentOffsetY: Double, | ||
| restingTopY: Double | ||
| ) { | ||
| guard case .collapsing = phase, | ||
| !refreshControlIsRefreshing, | ||
| !scrollViewIsTracking, | ||
| abs(contentOffsetY - restingTopY) <= 0.5 | ||
| else { return } | ||
| phase = .idle | ||
| } | ||
|
|
||
| mutating func reset() { | ||
| phase = .idle | ||
| } | ||
| } | ||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,13 +36,17 @@ final class WorkspaceListTableCoordinator: NSObject, UITableViewDelegate, | |
| private let sizingCell = UITableViewCell(style: .default, reuseIdentifier: nil) | ||
| private var heightCache: [HeightCacheKey: CGFloat] = [:] | ||
| private var dropJustCompleted = false | ||
| private var refreshLifecycle = WorkspaceListRefreshLifecycle() | ||
| private weak var activeRefreshControl: UIRefreshControl? | ||
| private weak var tableView: WorkspaceListUITableView? | ||
|
|
||
| init(configuration: WorkspaceListTable) { | ||
| self.configuration = configuration | ||
| super.init() | ||
| } | ||
|
|
||
| func attach(to tableView: WorkspaceListUITableView) { | ||
| self.tableView = tableView | ||
| tableView.delegate = self | ||
| tableView.dragDelegate = self | ||
| tableView.dropDelegate = self | ||
|
|
@@ -95,9 +99,17 @@ final class WorkspaceListTableCoordinator: NSObject, UITableViewDelegate, | |
| snapshot.reconfigureItems(changed) | ||
| } | ||
| previousConfiguration = next | ||
| let animatesSnapshot = tableView.window != nil && !dropJustCompleted | ||
| let refreshApplyID = refreshLifecycle.snapshotApplyStarted( | ||
| refreshCompletionGeneration: next.refreshCompletionGeneration | ||
| ) | ||
| let animatesSnapshot = tableView.window != nil | ||
| && !dropJustCompleted | ||
| && !refreshLifecycle.suppressesSnapshotAnimations | ||
| dropJustCompleted = false | ||
| dataSource?.apply(snapshot, animatingDifferences: animatesSnapshot) | ||
| dataSource?.apply(snapshot, animatingDifferences: animatesSnapshot) { [weak self] in | ||
| guard let refreshApplyID else { return } | ||
| self?.refreshSnapshotApplyCompleted(refreshApplyID) | ||
| } | ||
| } | ||
|
|
||
| func tableView( | ||
|
|
@@ -308,9 +320,23 @@ final class WorkspaceListTableCoordinator: NSObject, UITableViewDelegate, | |
| refreshControl.endRefreshing() | ||
| return | ||
| } | ||
| Task { @MainActor in | ||
| guard let refreshID = refreshLifecycle.begin( | ||
| currentGeneration: configuration.refreshCompletionGeneration | ||
| ) else { return } | ||
| activeRefreshControl = refreshControl | ||
| let refreshDidComplete = configuration.refreshDidComplete | ||
| Task { @MainActor [weak self, weak refreshControl] in | ||
| await refresh() | ||
| refreshControl.endRefreshing() | ||
| guard let self else { return } | ||
| guard let refreshControl, refreshControl === self.activeRefreshControl else { | ||
| self.refreshLifecycle.reset() | ||
| self.activeRefreshControl = nil | ||
| return | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| guard self.refreshLifecycle.refreshActionCompleted(refreshID) else { | ||
| return | ||
| } | ||
| refreshDidComplete() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -325,10 +351,44 @@ final class WorkspaceListTableCoordinator: NSObject, UITableViewDelegate, | |
| ) | ||
| tableView.refreshControl = refreshControl | ||
| } else { | ||
| activeRefreshControl?.endRefreshing() | ||
| activeRefreshControl = nil | ||
| refreshLifecycle.reset() | ||
| tableView.refreshControl = nil | ||
| } | ||
| } | ||
|
|
||
| func scrollViewDidScroll(_ scrollView: UIScrollView) { | ||
| settleRefreshCollapse(in: scrollView) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
file='Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceListTableCoordinator.swift'
# Map the file structure, then inspect the relevant section.
ast-grep outline "$file" --view expanded || true
echo '---'
sed -n '1,520p' "$file" | cat -n | sed -n '300,430p'Repository: manaflow-ai/cmux Length of output: 7419 🏁 Script executed: #!/bin/bash
set -euo pipefail
file='Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceListTableCoordinator.swift'
# Inspect the implementation and nearby delegate methods.
grep -n "settleRefreshCollapse\|scrollViewDid\|endRefreshing\|isTracking\|refresh" "$file" || true
echo '---'
sed -n '1,260p' "$file" | cat -n | sed -n '1,260p'Repository: manaflow-ai/cmux Length of output: 15126 🏁 Script executed: #!/bin/bash
set -euo pipefail
file='Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceListTableCoordinator.swift'
nl -ba "$file" | sed -n '320,420p'Repository: manaflow-ai/cmux Length of output: 194 🏁 Script executed: #!/bin/bash
set -euo pipefail
file='Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceListTableCoordinator.swift'
echo "[delegate methods]"
grep -nE 'func scrollViewDid|func settleRefreshCollapse|endRefreshing|isTracking' "$file" || true
echo
echo "[context]"
nl -ba "$file" | sed -n '340,390p'Repository: manaflow-ai/cmux Length of output: 573 Call 🤖 Prompt for AI AgentsSource: Coding guidelines
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| private func refreshSnapshotApplyCompleted( | ||
| _ applyID: WorkspaceListRefreshLifecycle.SnapshotApplyID | ||
| ) { | ||
| guard refreshLifecycle.snapshotApplyCompleted(applyID) else { return } | ||
| guard let activeRefreshControl else { | ||
| refreshLifecycle.reset() | ||
| return | ||
| } | ||
| activeRefreshControl.endRefreshing() | ||
| if let tableView { | ||
| settleRefreshCollapse(in: tableView) | ||
| } | ||
| } | ||
|
|
||
| private func settleRefreshCollapse(in scrollView: UIScrollView) { | ||
| guard refreshLifecycle.suppressesSnapshotAnimations else { return } | ||
| refreshLifecycle.observeCollapse( | ||
| refreshControlIsRefreshing: activeRefreshControl?.isRefreshing ?? false, | ||
| scrollViewIsTracking: scrollView.isTracking, | ||
| contentOffsetY: scrollView.contentOffset.y, | ||
| restingTopY: -scrollView.adjustedContentInset.top | ||
| ) | ||
| if !refreshLifecycle.suppressesSnapshotAnimations { | ||
| activeRefreshControl = nil | ||
| } | ||
| } | ||
|
|
||
| private func workspace(at indexPath: IndexPath) -> MobileWorkspacePreview? { | ||
| guard | ||
| let item = dataSource?.itemIdentifier(for: indexPath), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
collapsingis silently swallowedIf
refreshLifecycle.begin()returnsnil(phase is anything other than.idle), the method returns early without callingrefreshControl.endRefreshing(). During the.collapsingphaseisRefreshingis alreadyfalseso UIKit won't normally let the user initiate a new pull, but if the refresh action is extremely fast (action completes andendRefreshingfires before the user releases their pull gesture), a secondUIControlEvents.valueChangedcan fire on the same control. In that case therefreshControlis left spinning with no lifecycle entry, no Task, and no futureendRefreshingcall. AddingrefreshControl.endRefreshing()in theelse { return }path would prevent the control from hanging.