diff --git a/.github/workflows/verify-ios.yml b/.github/workflows/verify-ios.yml new file mode 100644 index 0000000..4ec277a --- /dev/null +++ b/.github/workflows/verify-ios.yml @@ -0,0 +1,81 @@ +name: Verify iOS build + +on: + push: + branches: ['codex/video-detail-back-gesture'] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + build: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: '26.6' + - name: Test detail gestures on iOS Simulator + run: | + SIMULATOR_ID=$(xcrun simctl list devices available -j | python3 -c ' + import json, sys + devices = json.load(sys.stdin)["devices"] + for runtime in sorted(devices, reverse=True): + if "iOS" in runtime: + for device in devices[runtime]: + if device["name"].startswith("iPhone"): + print(device["udid"]) + sys.exit(0) + sys.exit("No available iPhone simulator")') + xcodebuild test \ + -project PiliPod.xcodeproj \ + -scheme PiliPod \ + -configuration Debug \ + -destination "platform=iOS Simulator,id=$SIMULATOR_ID" \ + -only-testing:PiliPodUITests/VideoDetailGestureUITests \ + -parallel-testing-enabled NO \ + -resultBundlePath build/GestureTests.xcresult \ + CODE_SIGNING_ALLOWED=NO + - name: Upload gesture test results + if: always() + uses: actions/upload-artifact@v4 + with: + name: Video-detail-gesture-test-results + path: build/GestureTests.xcresult + retention-days: 14 + + ipa: + if: github.ref == 'refs/heads/codex/video-detail-back-gesture' + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - uses: maxim-lobanov/setup-xcode@v1 + with: + xcode-version: '26.6' + - name: Archive unsigned iOS app + run: >- + xcodebuild archive + -project PiliPod.xcodeproj + -scheme PiliPod + -configuration Release + -destination 'generic/platform=iOS' + -archivePath build/PiliPod.xcarchive + CODE_SIGNING_ALLOWED=NO + CODE_SIGNING_REQUIRED=NO + CODE_SIGN_IDENTITY="" + PROVISIONING_PROFILE_SPECIFIER="" + - name: Package IPA + run: | + mkdir -p build/Payload + cp -R build/PiliPod.xcarchive/Products/Applications/PiliPod.app build/Payload/ + cd build + zip -qr PiliPod-back-gesture.ipa Payload + - name: Upload IPA + uses: actions/upload-artifact@v4 + with: + name: PiliPod-back-gesture-unsigned-IPA + path: build/PiliPod-back-gesture.ipa + retention-days: 14 + if-no-files-found: error diff --git a/PiliPod/Components/VideoDetailTabPanGesture.swift b/PiliPod/Components/VideoDetailTabPanGesture.swift new file mode 100644 index 0000000..8171e4c --- /dev/null +++ b/PiliPod/Components/VideoDetailTabPanGesture.swift @@ -0,0 +1,100 @@ +import SwiftUI +import UIKit + +/// Directional tab paging that yields unrelated drags to scrolling and navigation. +struct VideoDetailTabPanGesture: UIGestureRecognizerRepresentable { + let isEnabled: Bool + let isIntro: Bool + let leadingExclusionWidth: CGFloat + let onChanged: (CGFloat) -> Void + let onEnded: (CGFloat?) -> Void + + func makeCoordinator(converter: CoordinateSpaceConverter) -> Coordinator { + Coordinator(gesture: self, converter: converter) + } + + func makeUIGestureRecognizer(context: Context) -> UIPanGestureRecognizer { + let recognizer = UIPanGestureRecognizer() + recognizer.maximumNumberOfTouches = 1 + recognizer.delegate = context.coordinator + return recognizer + } + + func updateUIGestureRecognizer(_ recognizer: UIPanGestureRecognizer, context: Context) { + context.coordinator.gesture = self + context.coordinator.converter = context.converter + recognizer.isEnabled = isEnabled + } + + func handleUIGestureRecognizerAction(_ recognizer: UIPanGestureRecognizer, context: Context) { + let translation = context.converter.localTranslation?.x ?? recognizer.translation(in: recognizer.view).x + switch recognizer.state { + case .began, .changed: + onChanged(translation) + case .ended: + onEnded(translation) + case .cancelled, .failed: + onEnded(nil) + default: + break + } + } + + final class Coordinator: NSObject, UIGestureRecognizerDelegate { + var gesture: VideoDetailTabPanGesture + var converter: CoordinateSpaceConverter + + init(gesture: VideoDetailTabPanGesture, converter: CoordinateSpaceConverter) { + self.gesture = gesture + self.converter = converter + } + + func gestureRecognizer(_ recognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { + // Use the touch itself: the converter's current location need not + // have been updated when UIKit asks whether to receive a new touch. + guard gesture.isEnabled, let window = touch.window else { return false } + return touch.location(in: window).x > window.bounds.minX + gesture.leadingExclusionWidth + } + + func gestureRecognizerShouldBegin(_ recognizer: UIGestureRecognizer) -> Bool { + guard gesture.isEnabled, let pan = recognizer as? UIPanGestureRecognizer else { return false } + let velocity = pan.velocity(in: pan.view) + guard abs(velocity.x) > abs(velocity.y) else { return false } + // In the intro, a rightward pan belongs to native back navigation. + return gesture.isIntro ? velocity.x < 0 : velocity.x > 0 + } + + func gestureRecognizer( + _ recognizer: UIGestureRecognizer, + shouldRecognizeSimultaneouslyWith other: UIGestureRecognizer + ) -> Bool { + // Vertical scrolling remains responsive while UIKit decides direction. + guard let scrollView = other.view as? UIScrollView else { return false } + return other === scrollView.panGestureRecognizer + } + + func gestureRecognizer( + _ recognizer: UIGestureRecognizer, + shouldBeRequiredToFailBy other: UIGestureRecognizer + ) -> Bool { + // Content-back waits for this directional pan to fail. On the intro + // a rightward pan fails immediately; on comments it pages to intro. + guard #available(iOS 26.0, *), let navigation = navigationController(for: recognizer.view) else { + return false + } + return other === navigation.interactiveContentPopGestureRecognizer + } + + private func navigationController(for view: UIView?) -> UINavigationController? { + var responder: UIResponder? = view + while let current = responder { + if let controller = current as? UIViewController, + let navigation = controller.navigationController { + return navigation + } + responder = current.next + } + return nil + } + } +} diff --git a/PiliPod/Components/VideoTripleLikePressGesture.swift b/PiliPod/Components/VideoTripleLikePressGesture.swift new file mode 100644 index 0000000..850bdfb --- /dev/null +++ b/PiliPod/Components/VideoTripleLikePressGesture.swift @@ -0,0 +1,76 @@ +import SwiftUI +import UIKit + +/// A stationary hold, rather than a zero-distance drag that competes with pans. +struct VideoTripleLikePressGesture: UIGestureRecognizerRepresentable { + let isEnabled: Bool + let onStarted: () -> Void + let onFinished: () -> Void + + func makeCoordinator(converter: CoordinateSpaceConverter) -> Coordinator { + Coordinator(gesture: self) + } + + func makeUIGestureRecognizer(context: Context) -> UILongPressGestureRecognizer { + let recognizer = UILongPressGestureRecognizer() + recognizer.minimumPressDuration = 0.5 + recognizer.allowableMovement = 10 + recognizer.numberOfTouchesRequired = 1 + recognizer.delegate = context.coordinator + return recognizer + } + + func updateUIGestureRecognizer(_ recognizer: UILongPressGestureRecognizer, context: Context) { + context.coordinator.gesture = self + recognizer.isEnabled = isEnabled + } + + func handleUIGestureRecognizerAction(_ recognizer: UILongPressGestureRecognizer, context: Context) { + let coordinator = context.coordinator + switch recognizer.state { + case .began: + coordinator.isHolding = true + onStarted() + case .changed: + // UIKit's allowableMovement guards recognition. Also cancel movement + // during charging, using window coordinates unaffected by animation. + guard let start = coordinator.startLocation else { return } + let location = recognizer.location(in: recognizer.view?.window) + if hypot(location.x - start.x, location.y - start.y) > recognizer.allowableMovement { + coordinator.finish() + recognizer.isEnabled = false + recognizer.isEnabled = isEnabled + } + case .ended, .cancelled, .failed: + coordinator.finish() + default: + break + } + } + + final class Coordinator: NSObject, UIGestureRecognizerDelegate { + var gesture: VideoTripleLikePressGesture + var startLocation: CGPoint? + var isHolding = false + + init(gesture: VideoTripleLikePressGesture) { + self.gesture = gesture + } + + func gestureRecognizer(_ recognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { + guard gesture.isEnabled, let window = touch.window else { return false } + let location = touch.location(in: window) + // Reserve both physical edges for system navigation gestures. + guard location.x > window.bounds.minX + 32, + location.x < window.bounds.maxX - 32 else { return false } + startLocation = location + return true + } + + func finish() { + guard isHolding else { return } + isHolding = false + gesture.onFinished() + } + } +} diff --git a/PiliPod/PiliPodApp.swift b/PiliPod/PiliPodApp.swift index b9d2c50..91d1532 100644 --- a/PiliPod/PiliPodApp.swift +++ b/PiliPod/PiliPodApp.swift @@ -65,7 +65,15 @@ struct PiliPodApp: App { var body: some Scene { WindowGroup { +#if DEBUG + if ProcessInfo.processInfo.arguments.contains("--uitest-video-detail-gestures") { + VideoDetailGestureTestRoot() + } else { + MainTabView() + } +#else MainTabView() +#endif } } } diff --git a/PiliPod/Views/VideoDetail/VideoDetailPage.swift b/PiliPod/Views/VideoDetail/VideoDetailPage.swift index 722b461..ca2ba95 100644 --- a/PiliPod/Views/VideoDetail/VideoDetailPage.swift +++ b/PiliPod/Views/VideoDetail/VideoDetailPage.swift @@ -1987,7 +1987,7 @@ private struct TabPager: View { @ViewBuilder let introContent: () -> IntroContent @ViewBuilder let commentsContent: () -> CommentsContent - @GestureState private var dragTranslation: CGFloat = 0 + @State private var dragTranslation: CGFloat = 0 private var currentIndex: CGFloat { switch selectedTab { @@ -2008,15 +2008,14 @@ private struct TabPager: View { .frame(width: width) .frame(maxHeight: .infinity, alignment: .top) } + .offset(x: -currentIndex * width + dragOffset) .frame(width: width, alignment: .leading) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) - .offset(x: -currentIndex * width + dragOffset) .animation(.interactiveSpring(response: 0.32, dampingFraction: 0.86), value: selectedTab) .contentShape(Rectangle()) - // A transparent overlay here would intercept ScrollView and button - // touches. Keep the pager gesture simultaneous with the content - // instead, and leave the edge strip inert in the gesture callbacks. - .simultaneousGesture(pagerGesture) + // Reject unrelated pans before recognition, rather than ignoring their + // callbacks after a SwiftUI DragGesture has already claimed the touch. + .gesture(pagerGesture) .clipped() } @@ -2025,27 +2024,21 @@ private struct TabPager: View { return dragTranslation } - private var pagerGesture: some Gesture { - DragGesture(minimumDistance: 12) - .updating($dragTranslation) { value, state, _ in - guard isSwipeEnabled else { return } - guard value.startLocation.x > leadingSwipeExclusionWidth else { return } - guard abs(value.translation.width) > abs(value.translation.height) else { return } - - let translation = value.translation.width + private var pagerGesture: VideoDetailTabPanGesture { + VideoDetailTabPanGesture( + isEnabled: isSwipeEnabled, + isIntro: selectedTab == .intro, + leadingExclusionWidth: leadingSwipeExclusionWidth, + onChanged: { translation in if selectedTab == .intro { - state = max(-width, min(0, translation)) + dragTranslation = max(-width, min(0, translation)) } else { - state = max(0, min(width, translation)) + dragTranslation = max(0, min(width, translation)) } - } - .onEnded { value in - guard isSwipeEnabled else { return } - guard value.startLocation.x > leadingSwipeExclusionWidth else { return } - let dx = value.translation.width - let dy = value.translation.height - guard abs(dx) > abs(dy) else { return } - + }, + onEnded: { dx in + defer { dragTranslation = 0 } + guard isSwipeEnabled, let dx else { return } let threshold = width * 0.2 switch selectedTab { case .intro: @@ -2056,9 +2049,71 @@ private struct TabPager: View { selectedTab = .intro } } + ) } } +#if DEBUG +/// Network-free UI test surface using the production pager and navigation setup. +struct VideoDetailGestureTestRoot: View { + @State private var isPresented = false + @Namespace private var namespace + + var body: some View { + NavigationStack { + Button("Open detail") { isPresented = true } + .accessibilityIdentifier("gesture.open") + .matchedTransitionSource(id: "gesture.detail", in: namespace) + .navigationDestination(isPresented: $isPresented) { + VideoDetailGestureTestContent() + .navigationTransition(.zoom(sourceID: "gesture.detail", in: namespace)) + } + } + } +} + +private struct VideoDetailGestureTestContent: View { + @State private var selectedTab: VideoDetailPage.VideoDetailTab = .intro + + var body: some View { + GeometryReader { geometry in + VStack(spacing: 0) { + Color.black.frame(height: 160) + Text(selectedTab.rawValue) + .accessibilityIdentifier(selectedTab == .intro ? "gesture.tab.intro" : "gesture.tab.comments") + .frame(height: 44) + TabPager( + selectedTab: $selectedTab, + width: geometry.size.width, + isSwipeEnabled: true, + leadingSwipeExclusionWidth: 32, + introContent: { + ScrollView { + VStack { + ForEach(0..<80) { index in + Text("Intro row \(index)") + .frame(maxWidth: .infinity, minHeight: 48) + .accessibilityIdentifier("gesture.intro.\(index)") + } + } + } + }, + commentsContent: { + List(0..<80, id: \.self) { index in + Text("Comment row \(index)") + } + .listStyle(.plain) + } + ) + } + } + .background(NavigationPopGestureEnabler()) + .navigationBarBackButtonHidden(true) + .toolbar(.hidden, for: .navigationBar) + } +} +#endif + #if canImport(UIKit) struct SystemVolumeController { private let volumeView: MPVolumeView @@ -2224,18 +2279,15 @@ struct VideoActionBar: View { let pressID = UUID() activeTriplePressID = pressID isTripleTouching = true - hasTripleChargeStarted = false + hasTripleCompleted = false + hasTripleChargeStarted = true + isTripleCharging = true + withAnimation(.easeIn(duration: 1.4)) { + tripleChargeProgress = 1 + } + tripleChargeHaptics.start() triplePressTask = Task { @MainActor in - guard await waitForTriplePress(milliseconds: 500) else { return } - guard isTripleTouching, activeTriplePressID == pressID else { return } - - hasTripleChargeStarted = true - isTripleCharging = true - withAnimation(.easeIn(duration: 1.4)) { - tripleChargeProgress = 1 - } - tripleChargeHaptics.start() - + // UIKit already recognized a stationary 0.5-second long press. guard await waitForTriplePress(milliseconds: 1_400) else { return } guard isTripleTouching, activeTriplePressID == pressID else { return } @@ -2274,7 +2326,6 @@ struct VideoActionBar: View { return } - let shouldHandleAsTap = !hasTripleChargeStarted let shouldRestore = hasTripleChargeStarted && !hasTripleCompleted isTripleTouching = false @@ -2296,10 +2347,6 @@ struct VideoActionBar: View { tripleCompletionScale = 1 } } - - if shouldHandleAsTap { - onToggleLike() - } } private func cancelTriplePress() { @@ -2308,6 +2355,12 @@ struct VideoActionBar: View { triplePressTask?.cancel() triplePressTask = nil tripleChargeHaptics.stop() + isTripleCharging = false + isTripleCompletionAnimating = false + hasTripleChargeStarted = false + hasTripleCompleted = false + tripleChargeProgress = 0 + tripleCompletionScale = 1 } private func requestTripleLike() { @@ -2385,17 +2438,15 @@ private struct VideoTripleLikeButton: View { completionScale: completionScale ) ) - .gesture( - DragGesture(minimumDistance: 0) - .onChanged { _ in - guard !isDisabled else { return } - onPressStarted() - } - .onEnded { _ in - guard !isDisabled else { return } - onPressEnded() - } - ) + .onTapGesture { + guard !isDisabled else { return } + onTap() + } + .gesture(VideoTripleLikePressGesture( + isEnabled: !isDisabled, + onStarted: onPressStarted, + onFinished: onPressEnded + )) .opacity(isDisabled ? 0.6 : 1) } } diff --git a/PiliPodUITests/VideoDetailGestureUITests.swift b/PiliPodUITests/VideoDetailGestureUITests.swift new file mode 100644 index 0000000..6ece461 --- /dev/null +++ b/PiliPodUITests/VideoDetailGestureUITests.swift @@ -0,0 +1,52 @@ +import XCTest + +final class VideoDetailGestureUITests: XCTestCase { + override func setUpWithError() throws { + continueAfterFailure = false + } + + @MainActor + private func openDetail() -> XCUIApplication { + let app = XCUIApplication() + app.launchArguments = ["--uitest-video-detail-gestures"] + app.launch() + app.buttons["gesture.open"].tap() + XCTAssertTrue(app.staticTexts["gesture.tab.intro"].waitForExistence(timeout: 5)) + return app + } + + @MainActor + func testTabSwipesAndContentBack() { + let app = openDetail() + let left = app.coordinate(withNormalizedOffset: CGVector(dx: 0.15, dy: 0.65)) + let right = app.coordinate(withNormalizedOffset: CGVector(dx: 0.85, dy: 0.65)) + right.press(forDuration: 0.05, thenDragTo: left) + XCTAssertTrue(app.staticTexts["gesture.tab.comments"].waitForExistence(timeout: 5)) + left.press(forDuration: 0.05, thenDragTo: right) + XCTAssertTrue(app.staticTexts["gesture.tab.intro"].waitForExistence(timeout: 5)) + let backStart = app.coordinate(withNormalizedOffset: CGVector(dx: 0.25, dy: 0.65)) + let backEnd = app.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.68)) + backStart.press(forDuration: 0.05, thenDragTo: backEnd) + XCTAssertTrue(app.buttons["gesture.open"].waitForExistence(timeout: 5)) + XCTAssertFalse(app.staticTexts["gesture.tab.intro"].exists) + } + + @MainActor + func testEdgeBack() { + let app = openDetail() + let start = app.coordinate(withNormalizedOffset: CGVector(dx: 0.01, dy: 0.65)) + let end = app.coordinate(withNormalizedOffset: CGVector(dx: 0.9, dy: 0.65)) + start.press(forDuration: 0.05, thenDragTo: end) + XCTAssertTrue(app.buttons["gesture.open"].waitForExistence(timeout: 5)) + XCTAssertFalse(app.staticTexts["gesture.tab.intro"].exists) + } + + @MainActor + func testVerticalScrollDoesNotSwitchTabs() { + let app = openDetail() + XCTAssertTrue(app.staticTexts["gesture.intro.0"].isHittable) + app.swipeUp() + XCTAssertTrue(app.staticTexts["gesture.tab.intro"].exists) + XCTAssertFalse(app.staticTexts["gesture.intro.0"].isHittable) + } +}