-
Notifications
You must be signed in to change notification settings - Fork 453
feat: migrate pager view to SwiftUI #1020
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
okwasniewski
wants to merge
1
commit into
master
Choose a base branch
from
feat/swiftui
base: master
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.
+489
−378
Draft
Changes from all commits
Commits
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
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,39 @@ | ||
import Foundation | ||
import SwiftUI | ||
import UIKit | ||
|
||
/** | ||
Helper used to render UIView inside of SwiftUI. | ||
*/ | ||
struct RepresentableView: UIViewRepresentable { | ||
var view: UIView | ||
|
||
// Adding a wrapper UIView to avoid SwiftUI directly managing React Native views. | ||
// This fixes issues with incorrect layout rendering. | ||
func makeUIView(context: Context) -> UIView { | ||
let wrapper = UIView() | ||
wrapper.addSubview(view) | ||
return wrapper | ||
} | ||
|
||
func updateUIView(_ uiView: UIView, context: Context) {} | ||
} | ||
|
||
extension Collection { | ||
// Returns the element at the specified index if it is within bounds, otherwise nil. | ||
subscript(safe index: Index) -> Element? { | ||
okwasniewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
indices.contains(index) ? self[index] : nil | ||
} | ||
} | ||
|
||
extension UIView { | ||
func pinEdges(to other: UIView) { | ||
NSLayoutConstraint.activate([ | ||
leadingAnchor.constraint(equalTo: other.leadingAnchor), | ||
trailingAnchor.constraint(equalTo: other.trailingAnchor), | ||
topAnchor.constraint(equalTo: other.topAnchor), | ||
bottomAnchor.constraint(equalTo: other.bottomAnchor) | ||
]) | ||
} | ||
} | ||
|
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,98 @@ | ||
import UIKit | ||
|
||
/** | ||
Scroll delegate used to control underlying TabView's collection view. | ||
*/ | ||
class PagerScrollDelegate: NSObject, UIScrollViewDelegate, UICollectionViewDelegate { | ||
// Store the original delegate to forward calls | ||
weak var originalDelegate: UICollectionViewDelegate? | ||
weak var delegate: PagerViewProviderDelegate? | ||
var orientation: UICollectionView.ScrollDirection = .horizontal | ||
|
||
func scrollViewDidScroll(_ scrollView: UIScrollView) { | ||
let isHorizontal = orientation == .horizontal | ||
let pageSize = isHorizontal ? scrollView.frame.width : scrollView.frame.height | ||
let contentOffset = isHorizontal ? scrollView.contentOffset.x : scrollView.contentOffset.y | ||
|
||
guard pageSize > 0 else { return } | ||
|
||
let offset = contentOffset.truncatingRemainder(dividingBy: pageSize) / pageSize | ||
let position = round(contentOffset / pageSize - offset) | ||
|
||
let eventData = OnPageScrollEventData(position: position, offset: offset) | ||
delegate?.onPageScroll(data: eventData) | ||
originalDelegate?.scrollViewDidScroll?(scrollView) | ||
} | ||
|
||
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { | ||
delegate?.onPageScrollStateChanged(state: .dragging) | ||
originalDelegate?.scrollViewWillBeginDragging?(scrollView) | ||
} | ||
|
||
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { | ||
delegate?.onPageScrollStateChanged(state: .settling) | ||
originalDelegate?.scrollViewWillBeginDecelerating?(scrollView) | ||
} | ||
|
||
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { | ||
delegate?.onPageScrollStateChanged(state: .idle) | ||
originalDelegate?.scrollViewDidEndDecelerating?(scrollView) | ||
} | ||
|
||
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { | ||
delegate?.onPageScrollStateChanged(state: .idle) | ||
originalDelegate?.scrollViewDidEndScrollingAnimation?(scrollView) | ||
} | ||
|
||
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { | ||
if !decelerate { | ||
delegate?.onPageScrollStateChanged(state: .idle) | ||
} | ||
originalDelegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate) | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { | ||
originalDelegate?.collectionView?(collectionView, didEndDisplaying: cell, forItemAt: indexPath) | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { | ||
originalDelegate?.collectionView?(collectionView, willDisplay: cell, forItemAt: indexPath) | ||
} | ||
|
||
override func responds(to aSelector: Selector!) -> Bool { | ||
let handledSelectors: [Selector] = [ | ||
#selector(scrollViewDidScroll(_:)), | ||
#selector(scrollViewWillBeginDragging(_:)), | ||
#selector(scrollViewWillBeginDecelerating(_:)), | ||
#selector(scrollViewDidEndDecelerating(_:)), | ||
#selector(scrollViewDidEndScrollingAnimation(_:)), | ||
#selector(scrollViewDidEndDragging(_:willDecelerate:)), | ||
#selector(collectionView(_:didEndDisplaying:forItemAt:)), | ||
#selector(collectionView(_:willDisplay:forItemAt:)) | ||
] | ||
|
||
if handledSelectors.contains(aSelector) { | ||
return true | ||
} | ||
return originalDelegate?.responds(to: aSelector) ?? false | ||
} | ||
|
||
override func forwardingTarget(for aSelector: Selector!) -> Any? { | ||
let handledSelectors: [Selector] = [ | ||
#selector(scrollViewDidScroll(_:)), | ||
#selector(scrollViewWillBeginDragging(_:)), | ||
#selector(scrollViewWillBeginDecelerating(_:)), | ||
#selector(scrollViewDidEndDecelerating(_:)), | ||
#selector(scrollViewDidEndScrollingAnimation(_:)), | ||
#selector(scrollViewDidEndDragging(_:willDecelerate:)), | ||
#selector(collectionView(_:didEndDisplaying:forItemAt:)), | ||
#selector(collectionView(_:willDisplay:forItemAt:)) | ||
] | ||
|
||
if handledSelectors.contains(aSelector) { | ||
return nil | ||
} | ||
return originalDelegate | ||
} | ||
} | ||
|
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,61 @@ | ||
import SwiftUI | ||
@_spi(Advanced) import SwiftUIIntrospect | ||
|
||
struct PagerView: View { | ||
@ObservedObject var props: PagerViewProps | ||
@State private var scrollDelegate = PagerScrollDelegate() | ||
weak var delegate: PagerViewProviderDelegate? | ||
|
||
@Weak var collectionView: UICollectionView? | ||
|
||
var body: some View { | ||
TabView(selection: $props.currentPage) { | ||
ForEach(props.children) { child in | ||
if let index = props.children.firstIndex(of: child) { | ||
RepresentableView(view: child.view) | ||
.ignoresSafeArea(.container, edges: .vertical) | ||
.tag(index) | ||
} | ||
} | ||
} | ||
.id(props.children.count) | ||
.background(.clear) | ||
.tabViewStyle(.page(indexDisplayMode: .never)) | ||
.ignoresSafeArea(.all, edges: .all) | ||
.environment(\.layoutDirection, props.layoutDirection.converted) | ||
.introspect(.tabView(style: .page), on: .iOS(.v14...)) { collectionView in | ||
self.collectionView = collectionView | ||
collectionView.bounces = props.overdrag | ||
collectionView.isScrollEnabled = props.scrollEnabled | ||
collectionView.keyboardDismissMode = props.keyboardDismissMode | ||
|
||
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { | ||
layout.scrollDirection = props.orientation | ||
} | ||
|
||
if scrollDelegate.originalDelegate == nil { | ||
scrollDelegate.originalDelegate = collectionView.delegate | ||
scrollDelegate.delegate = delegate | ||
scrollDelegate.orientation = props.orientation | ||
collectionView.delegate = scrollDelegate | ||
} | ||
} | ||
.onChange(of: props.children) { newValue in | ||
if props.currentPage >= newValue.count && !newValue.isEmpty { | ||
props.currentPage = newValue.count - 1 | ||
} | ||
} | ||
.onChange(of: props.currentPage) { newValue in | ||
delegate?.onPageSelected(position: newValue) | ||
} | ||
okwasniewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.onChange(of: props.scrollEnabled) { newValue in | ||
collectionView?.isScrollEnabled = newValue | ||
} | ||
.onChange(of: props.overdrag) { newValue in | ||
collectionView?.bounces = newValue | ||
} | ||
.onChange(of: props.keyboardDismissMode) { newValue in | ||
collectionView?.keyboardDismissMode = newValue | ||
} | ||
} | ||
} |
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,35 @@ | ||
import SwiftUI | ||
import UIKit | ||
|
||
struct IdentifiablePlatformView: Identifiable, Equatable { | ||
let id = UUID() | ||
let view: UIView | ||
|
||
init(_ view: UIView) { | ||
self.view = view | ||
} | ||
} | ||
|
||
@objc public enum PagerLayoutDirection: Int { | ||
case ltr | ||
case rtl | ||
|
||
var converted: LayoutDirection { | ||
switch self { | ||
case .ltr: | ||
return .leftToRight | ||
case .rtl: | ||
return .rightToLeft | ||
} | ||
} | ||
} | ||
|
||
class PagerViewProps: ObservableObject { | ||
okwasniewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Published var children: [IdentifiablePlatformView] = [] | ||
@Published var currentPage: Int = -1 | ||
@Published var scrollEnabled: Bool = true | ||
@Published var overdrag: Bool = false | ||
@Published var keyboardDismissMode: UIScrollView.KeyboardDismissMode = .none | ||
@Published var layoutDirection: PagerLayoutDirection = .ltr | ||
@Published var orientation: UICollectionView.ScrollDirection = .horizontal | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.