Skip to content

Commit 8e00734

Browse files
author
Toby Brennan
committed
Animate scrollPositionSetter if withAnimation used
Other minor fixes
1 parent 234c008 commit 8e00734

File tree

7 files changed

+60
-57
lines changed

7 files changed

+60
-57
lines changed

ASCollectionView-SwiftUI.podspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = 'ASCollectionView-SwiftUI'
4-
s.version = '1.7.1'
4+
s.version = '1.8.0'
55
s.summary = 'A SwiftUI collection view with support for custom layouts, preloading, and more. '
66

77
s.description = <<-DESC
@@ -11,7 +11,6 @@ Pod::Spec.new do |s|
1111
- supports autosizing of cells.
1212
- supports the new UICollectionViewCompositionalLayout, and any other UICollectionViewLayout
1313
- supports removing separators for ASTableView.
14-
- supports directly using FetchedResults as a data source
1514
DESC
1615

1716
s.homepage = 'https://github.com/apptekstudios/ASCollectionView'

Demo/ASCollectionViewDemo/Screens/PhotoGrid/PhotoGridScreen.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ struct PhotoGridScreen: View
3636
.aspectRatio(1, contentMode: .fill)
3737
.frame(width: geom.size.width, height: geom.size.height)
3838
.clipped()
39+
.opacity(self.isEditing ? (state.isSelected ? 1 : 0.7) : 1)
3940
}
40-
.buttonStyle(PlainButtonStyle())
41+
.buttonStyle(NeutralButtonStyle())
4142
.disabled(self.isEditing)
4243
}
4344

@@ -195,3 +196,11 @@ struct GridView_Previews: PreviewProvider
195196
PhotoGridScreen()
196197
}
197198
}
199+
200+
struct NeutralButtonStyle: ButtonStyle
201+
{
202+
func makeBody(configuration: Configuration) -> some View
203+
{
204+
configuration.label
205+
}
206+
}

Sources/ASCollectionView/Delegate/ASCollectionViewDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extension ASCollectionViewDelegate: UICollectionViewDragDelegate, UICollectionVi
9494
coordinator?.collectionView(collectionView, dropSessionDidUpdate: session, withDestinationIndexPath: destinationIndexPath) ?? UICollectionViewDropProposal(operation: .cancel)
9595
}
9696

97-
// UICollectionView doesn't support dropping multiple items :( http://www.openradar.me/42068699
97+
// UICollectionView doesn't support dropping multiple items :( [http://www.openradar.me/42068699]
9898
open func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator)
9999
{
100100
self.coordinator?.collectionView(collectionView, performDropWith: coordinator)

Sources/ASCollectionView/Implementation/ASCollectionView.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
132132
// MARK: Private tracking variables
133133

134134
private var hasDoneInitialSetup = false
135+
private var shouldAnimateScrollPositionSet = false
135136

136137
private var hasFiredBoundaryNotificationForBoundary: Set<Boundary> = []
137138
private var haveRegisteredForSupplementaryOfKind: Set<String> = []
@@ -293,6 +294,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
293294
invalidateLayoutOnNextUpdate = false
294295
}
295296
dataSource?.applySnapshot(snapshot, animated: animated)
297+
shouldAnimateScrollPositionSet = animated
296298

297299
collectionViewController.map { updateSelectionBindings($0.collectionView) }
298300
refreshVisibleCells(transaction: transaction, updateAll: false)
@@ -492,16 +494,17 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
492494
collectionViewController?.collectionView.scrollToItem(at: indexPath, at: position, animated: true)
493495
CATransaction.commit()
494496
}
495-
496-
func applyScrollPosition(animated: Bool) {
497-
if let scrollPositionToSet = self.parent.scrollPositionSetter?.wrappedValue
498-
{
499-
self.scrollToPosition(scrollPositionToSet, animated: animated)
500-
DispatchQueue.main.async {
501-
self.parent.scrollPositionSetter?.wrappedValue = nil
502-
}
503-
}
504-
}
497+
498+
func applyScrollPosition(animated: Bool)
499+
{
500+
if let scrollPositionToSet = parent.scrollPositionSetter?.wrappedValue
501+
{
502+
scrollToPosition(scrollPositionToSet, animated: animated)
503+
DispatchQueue.main.async {
504+
self.parent.scrollPositionSetter?.wrappedValue = nil
505+
}
506+
}
507+
}
505508

506509
func scrollToPosition(_ scrollPosition: ASCollectionViewScrollPosition, animated: Bool = false)
507510
{
@@ -849,9 +852,8 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
849852
DispatchQueue.main.async {
850853
self.parent.invalidateParentCellLayout?(!firstSize)
851854
}
852-
applyScrollPosition(animated: false)
855+
applyScrollPosition(animated: shouldAnimateScrollPositionSet)
853856
}
854-
#warning("TODO: get animation state")
855857
}
856858

857859
// MARK: Variables used for the custom prefetching implementation

Sources/ASCollectionView/Implementation/ASSectionDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ internal struct ASSectionDataSource<DataCollection: RandomAccessCollection, Data
237237

238238
func applyMove(from: Int, to: Int) -> Bool
239239
{
240+
// dragDropConfig.dataBinding?.wrappedValue.move(fromOffsets: [from], toOffset: to) //This is not behaving as expected
240241
// NOTE: Binding seemingly not updated until next runloop. Any change must be done in one move; hence the var array
241-
// dragDropConfig.dataBinding?.wrappedValue.move(fromOffsets: [from], toOffset: to) //This is not behaving as expected
242242
guard from != to,
243243
dragDropConfig.shouldMoveItem?(from, to) ?? true,
244244
var array = dragDropConfig.dataBinding?.wrappedValue

Sources/ASCollectionView/Implementation/ASTableView.swift

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
111111
// MARK: Private tracking variables
112112

113113
private var hasDoneInitialSetup = false
114+
private var shouldAnimateScrollPositionSet = false
114115

115116
typealias Cell = ASTableViewCell
116117

@@ -146,8 +147,8 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
146147
assignIfChanged(tableView, \.showsVerticalScrollIndicator, newValue: parent.scrollIndicatorEnabled)
147148
assignIfChanged(tableView, \.showsHorizontalScrollIndicator, newValue: parent.scrollIndicatorEnabled)
148149
assignIfChanged(tableView, \.keyboardDismissMode, newValue: .interactive)
149-
150-
updateTableViewContentInsets(tableView)
150+
151+
updateTableViewContentInsets(tableView)
151152

152153
assignIfChanged(tableView, \.allowsSelection, newValue: true)
153154
assignIfChanged(tableView, \.allowsMultipleSelectionDuringEditing, newValue: true)
@@ -203,7 +204,6 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
203204
cell.isSelected = self.isIndexPathSelected(indexPath)
204205

205206
cell.setContent(itemID: itemID, content: section.dataSource.content(forItemID: itemID, isSelected: cell.isSelected, isHighlighted: cell.isHighlighted))
206-
// cell.skipNextRefresh = true // Avoid setting this again when we refresh old cells in a moment
207207

208208
cell.disableSwiftUIDropInteraction = section.dataSource.dropEnabled
209209
cell.disableSwiftUIDragInteraction = section.dataSource.dragEnabled
@@ -251,6 +251,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
251251
)
252252

253253
dataSource?.applySnapshot(snapshot, animated: animated)
254+
shouldAnimateScrollPositionSet = animated
254255

255256
tableViewController.map { updateSelectionBindings($0.tableView) }
256257
refreshVisibleCells(transaction: transaction, updateAll: false)
@@ -306,27 +307,28 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
306307
{
307308
tableViewController?.tableView.scrollToRow(at: indexPath, at: position, animated: true)
308309
}
309-
310-
func applyScrollPosition(animated: Bool) {
311-
if let scrollPositionToSet = self.parent.scrollPositionSetter?.wrappedValue
312-
{
313-
switch scrollPositionToSet
314-
{
315-
case let .indexPath(indexPath):
316-
self.tableViewController?.tableView.scrollToRow(at: indexPath, at: .none, animated: animated)
317-
case .top:
318-
let contentInsets = self.tableViewController?.tableView.contentInset ?? .zero
319-
self.tableViewController?.tableView.setContentOffset(CGPoint(x: 0, y: contentInsets.top), animated: animated)
320-
case .bottom:
321-
let contentSize = self.tableViewController?.tableView.contentSizePlusInsets ?? .zero
322-
let visibleHeight = self.tableViewController?.tableView.frame.height ?? .zero
323-
self.tableViewController?.tableView.setContentOffset(CGPoint(x: 0, y: contentSize.height - visibleHeight), animated: animated)
324-
}
325-
DispatchQueue.main.async {
326-
self.parent.scrollPositionSetter?.wrappedValue = nil
327-
}
328-
}
329-
}
310+
311+
func applyScrollPosition(animated: Bool)
312+
{
313+
if let scrollPositionToSet = parent.scrollPositionSetter?.wrappedValue
314+
{
315+
switch scrollPositionToSet
316+
{
317+
case let .indexPath(indexPath):
318+
tableViewController?.tableView.scrollToRow(at: indexPath, at: .none, animated: animated)
319+
case .top:
320+
let contentInsets = tableViewController?.tableView.contentInset ?? .zero
321+
tableViewController?.tableView.setContentOffset(CGPoint(x: 0, y: contentInsets.top), animated: animated)
322+
case .bottom:
323+
let contentSize = tableViewController?.tableView.contentSizePlusInsets ?? .zero
324+
let visibleHeight = tableViewController?.tableView.frame.height ?? .zero
325+
tableViewController?.tableView.setContentOffset(CGPoint(x: 0, y: contentSize.height - visibleHeight), animated: animated)
326+
}
327+
DispatchQueue.main.async {
328+
self.parent.scrollPositionSetter?.wrappedValue = nil
329+
}
330+
}
331+
}
330332

331333
func onMoveToParent()
332334
{
@@ -355,9 +357,8 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
355357
DispatchQueue.main.async {
356358
self.parent.invalidateParentCellLayout?(!firstSize)
357359
}
358-
359-
applyScrollPosition(animated: false)
360-
#warning("TODO: get animation state")
360+
361+
applyScrollPosition(animated: shouldAnimateScrollPositionSet)
361362
}
362363

363364
func configureRefreshControl(for tv: UITableView)
@@ -399,24 +400,16 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
399400
}
400401

401402
public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
402-
{
403-
// guard let view = (view as? ASTableViewSupplementaryView) else { return }
404-
}
403+
{}
405404

406405
public func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int)
407-
{
408-
// guard let view = (view as? ASTableViewSupplementaryView) else { return }
409-
}
406+
{}
410407

411408
public func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
412-
{
413-
// guard let view = (view as? ASTableViewSupplementaryView) else { return }
414-
}
409+
{}
415410

416411
public func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)
417-
{
418-
// guard let view = (view as? ASTableViewSupplementaryView) else { return }
419-
}
412+
{}
420413

421414
public func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])
422415
{

Sources/ASCollectionView/UIKitExtensions/UIScrollView+Convenience.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extension UIScrollView
88
{
99
CGSize(
1010
width: contentSize.width + adjustedContentInset.left + adjustedContentInset.right,
11-
height: contentSize.height + adjustedContentInset.bottom + contentInset.top) // NOTE: the adjusted top inset intentionally left out, as SwiftUI uses a negative contentOffset to display the nav bar (doesn't affect content size)
11+
height: contentSize.height + adjustedContentInset.bottom + adjustedContentInset.top)
1212
}
1313

1414
var maxContentOffset: CGPoint

0 commit comments

Comments
 (0)