Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Sources/ASCollectionView/ASCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
@Environment(\.alwaysBounceHorizontal) private var alwaysBounceHorizontal
@Environment(\.alwaysBounceVertical) private var alwaysBounceVertical
@Environment(\.initialScrollPosition) private var initialScrollPosition
@Environment(\.onPullToRefresh) private var onPullToRefresh
@Environment(\.collectionViewOnReachedBoundary) private var onReachedBoundary
@Environment(\.animateOnDataRefresh) private var animateOnDataRefresh
@Environment(\.attemptToMaintainScrollPositionOnOrientationChange) private var attemptToMaintainScrollPositionOnOrientationChange
Expand Down Expand Up @@ -159,6 +160,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
updateCollectionViewSettings(collectionViewController.collectionView, delegate: context.coordinator.delegate)
context.coordinator.updateLayout()
context.coordinator.updateContent(collectionViewController.collectionView, animated: animateOnDataRefresh, refreshExistingCells: true)
context.coordinator.configureRefreshControl(for: collectionViewController.collectionView)
}

func updateCollectionViewSettings(_ collectionView: UICollectionView, delegate: ASCollectionViewDelegate?)
Expand Down Expand Up @@ -365,6 +367,35 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
parent.initialScrollPosition.map { scrollToPosition($0, animated: false) }
}
}

func configureRefreshControl(for cv: UICollectionView)
{
guard parent.onPullToRefresh != nil else
{
if cv.refreshControl != nil
{
cv.refreshControl = nil
}
return
}
if cv.refreshControl == nil
{
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(collectionViewDidPullToRefresh), for: .valueChanged)
cv.refreshControl = refreshControl
}
}

@objc
public func collectionViewDidPullToRefresh()
{
guard let collectionView = collectionViewController?.collectionView else { return }
let endRefreshing: (() -> Void) = { [weak collectionView] in
collectionView?.refreshControl?.endRefreshing()
}
parent.onPullToRefresh?(endRefreshing)
}


func onMoveFromParent() {
hasDoneInitialSetup = false
Expand Down
2 changes: 1 addition & 1 deletion Sources/ASCollectionView/ASTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable
// MARK: Environment variables

@Environment(\.tableViewSeparatorsEnabled) private var separatorsEnabled
@Environment(\.tableViewOnPullToRefresh) private var onPullToRefresh
@Environment(\.onPullToRefresh) private var onPullToRefresh
@Environment(\.tableViewOnReachedBottom) private var onReachedBottom
@Environment(\.scrollIndicatorsEnabled) private var scrollIndicatorsEnabled
@Environment(\.contentInsets) private var contentInsets
Expand Down
19 changes: 12 additions & 7 deletions Sources/ASCollectionView/EnvironmentKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public extension View
environment(\.tableViewSeparatorsEnabled, enabled)
}

/// Set a closure that is called when the tableView is pulled to refresh
func tableViewOnPullToRefresh(_ onPullToRefresh: ((_ endRefreshing: @escaping (() -> Void)) -> Void)?) -> some View
/// Set a closure that is called when the tableView or the collectionView is pulled to refresh
func onPullToRefresh(_ onPullToRefresh: ((_ endRefreshing: @escaping (() -> Void)) -> Void)?) -> some View
{
environment(\.tableViewOnPullToRefresh, onPullToRefresh)
environment(\.onPullToRefresh, onPullToRefresh)
}

/// Set a closure that is called whenever the tableView is scrolled to the bottom.
Expand Down Expand Up @@ -86,6 +86,11 @@ public extension View
{
environment(\.allowCellHeightToExceedCollectionContentSize, allowCellHeightToExceedCollectionContentSize)
}

func animateOnDataRefresh(_ animateOnDataRefresh: Bool = true) -> some View
{
environment(\.animateOnDataRefresh, animateOnDataRefresh)
}
}

//MARK: Internal Key Definitions
Expand Down Expand Up @@ -114,7 +119,7 @@ struct EnvironmentKeyASTableViewSeparatorsEnabled: EnvironmentKey
}

@available(iOS 13.0, *)
struct EnvironmentKeyASTableViewOnPullToRefresh: EnvironmentKey
struct EnvironmentKeyASViewOnPullToRefresh: EnvironmentKey
{
static let defaultValue: (((_ endRefreshing: @escaping (() -> Void)) -> Void)?) = nil
}
Expand Down Expand Up @@ -201,10 +206,10 @@ public extension EnvironmentValues
set { self[EnvironmentKeyASTableViewSeparatorsEnabled.self] = newValue }
}

var tableViewOnPullToRefresh: ((_ endRefreshing: @escaping (() -> Void)) -> Void)?
var onPullToRefresh: ((_ endRefreshing: @escaping (() -> Void)) -> Void)?
{
get { self[EnvironmentKeyASTableViewOnPullToRefresh.self] }
set { self[EnvironmentKeyASTableViewOnPullToRefresh.self] = newValue }
get { self[EnvironmentKeyASViewOnPullToRefresh.self] }
set { self[EnvironmentKeyASViewOnPullToRefresh.self] = newValue }
}

var tableViewOnReachedBottom: () -> Void
Expand Down