-
Notifications
You must be signed in to change notification settings - Fork 3
ALFMOB-61: Refactor PDP screen's details to use a ScrollView #28
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
Draft
JoaoPinhoMinder
wants to merge
2
commits into
main
Choose a base branch
from
techdebt/ALFMOB-61-refactor-pdp-screen-details-to-use-scroll-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Alfie/AlfieKit/Sources/Common/Extensions/Binding+Extension.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import SwiftUI | ||
|
|
||
| public extension Binding where Value == Bool { | ||
| var negate: Binding<Value> { | ||
| Binding<Value>( | ||
| get: { !self.wrappedValue }, | ||
| set: { self.wrappedValue = !$0 } | ||
| ) | ||
| } | ||
| } |
141 changes: 141 additions & 0 deletions
141
Alfie/AlfieKit/Sources/StyleGuide/Components/DraggableBottomSheet/DraggableBottomSheet.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import SwiftUI | ||
|
|
||
| @available(iOS 16.4, *) | ||
| public struct DraggableBottomSheet<Content: View>: View { | ||
| // MARK: Constants | ||
|
|
||
| private let minVelocity: CGFloat = 800 | ||
|
|
||
| // MARK: State Properties | ||
|
|
||
| @State private var scrollOffset: CGPoint = .zero | ||
| @State private var isScrollEnabled = true | ||
| @State private var contentViewSize: CGSize = .zero | ||
| @State private var dragOffset: CGFloat = 0 | ||
| @State private var lastDragOffset: CGFloat = 0 | ||
| @State private var isExpanded = false | ||
| @Binding private var expansionSignal: Bool | ||
|
|
||
| // MARK: Parameters | ||
|
|
||
| private let showCapsule: Bool | ||
| private let expandedHeight: CGFloat | ||
| private let collapsedHeight: CGFloat | ||
| private let dragStartOffset: CGFloat | ||
| private let content: Content | ||
|
|
||
| // MARK: Lifecycle | ||
|
|
||
| public init( | ||
| showCapsule: Bool = true, | ||
| expandedHeight: CGFloat, | ||
| collapsedHeight: CGFloat, | ||
| dragStartOffset: CGFloat, | ||
| expansionSignal: Binding<Bool>, | ||
| @ViewBuilder content: () -> Content | ||
| ) { | ||
| self.showCapsule = showCapsule | ||
| self.expandedHeight = expandedHeight | ||
| self.collapsedHeight = collapsedHeight | ||
| self.dragStartOffset = dragStartOffset | ||
| self._expansionSignal = expansionSignal | ||
| self.content = content() | ||
| } | ||
|
|
||
| public var body: some View { | ||
| let isScrolledToTop = scrollOffset.y == 0 | ||
|
|
||
| VStack { | ||
| if showCapsule { | ||
| Capsule() | ||
| .frame(width: 40, height: 6) | ||
| .foregroundColor(.gray) | ||
| .padding(.top, 8) | ||
| } | ||
|
|
||
| ScrollViewWithOffsetReader(offset: $scrollOffset) { | ||
| content | ||
| .writingSize(to: $contentViewSize) | ||
| } | ||
| .scrollBounceBehavior(.basedOnSize) | ||
| .scrollIndicators(.hidden) | ||
| .scrollDisabled(!isScrollEnabled) | ||
| } | ||
| .frame(alignment: .top) | ||
| .background(Colors.primary.white) | ||
| .clipShape( | ||
| .rect( | ||
| topLeadingRadius: CornerRadius.s, | ||
| bottomLeadingRadius: 0, | ||
| bottomTrailingRadius: 0, | ||
| topTrailingRadius: CornerRadius.s | ||
| ) | ||
| ) | ||
| .shadow(.softFloat5) | ||
| .offset(y: dragStartOffset + dragOffset) | ||
| .simultaneousGesture( | ||
| DragGesture() | ||
| .onChanged { value in | ||
| let newOffset = lastDragOffset + value.translation.height | ||
|
|
||
| if isExpanded { | ||
| // Only allow dragging if at the top of the scroll view | ||
| if newOffset > lastDragOffset && isScrolledToTop { | ||
| isScrollEnabled = false | ||
| dragOffset = max(min(newOffset, 0), collapsedHeight - expandedHeight) | ||
| } else { | ||
| isScrollEnabled = true | ||
| } | ||
| } else { | ||
| // Always allow dragging when collapsed | ||
| isScrollEnabled = false | ||
| dragOffset = max(min(newOffset, 0), collapsedHeight - expandedHeight) | ||
| } | ||
| } | ||
| .onEnded { value in | ||
| let velocity = value.velocity.height | ||
| let midPoint = (collapsedHeight - expandedHeight) / 2 | ||
|
|
||
| if velocity < -minVelocity { | ||
| // Case 1: Fast swipe up → Expand the sheet | ||
| expand() | ||
| } else if velocity > minVelocity && isScrolledToTop { | ||
| // Case 2: Fast swipe down → Collapse the sheet | ||
| collapse() | ||
| } else if dragOffset < midPoint { | ||
| // Case 3: Slow or no swipe, but sheet is past the midpoint → Expand | ||
| expand() | ||
| } else { | ||
| // Case 4: Slow or no swipe, but sheet is before the midpoint → Collapse | ||
| collapse() | ||
| } | ||
| } | ||
| ) | ||
| .onChange(of: expansionSignal) { shouldExpand in | ||
| shouldExpand ? expand() : collapse() | ||
| } | ||
| .onAppear { | ||
| isExpanded ? expand() : collapse() | ||
| } | ||
| } | ||
|
|
||
| // MARK: Private Methods | ||
|
|
||
| private func expand() { | ||
| withAnimation(.spring()) { | ||
| dragOffset = collapsedHeight - expandedHeight | ||
| isExpanded = true | ||
| isScrollEnabled = true | ||
| } | ||
| lastDragOffset = dragOffset | ||
| } | ||
|
|
||
| private func collapse() { | ||
| withAnimation(.spring()) { | ||
| dragOffset = 0 | ||
| isExpanded = false | ||
| isScrollEnabled = false | ||
| } | ||
| lastDragOffset = dragOffset | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hey 👋 @p4checo asked me to take a look and help.
The reason why we had a different view for iPad and iPhone pre iOS 16.4 is: