Skip to content

Commit

Permalink
Remove scrollViewReader
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcHidalgo5 committed Sep 30, 2024
1 parent 31935ce commit 5b910b1
Showing 1 changed file with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import SwiftUI

@Previewable
@State
var pleaseScrollTo: Item.ID? = nil
var scrollPositionItemID: Item.ID? = nil

NavigationStack {
InfiniteVerticalScrollView(
direction: .downwards,
items: $items,
scrollPositionItemID: $pleaseScrollTo,
scrollPositionItemID: $scrollPositionItemID,
nextPageFetcher: { _ in
try await Task.sleep(for: .seconds(2))
return (Item.createItems(), true)
Expand Down Expand Up @@ -103,33 +103,32 @@ public struct InfiniteVerticalScrollView<Item: Identifiable & Sendable, ItemView
}
}
}

public var body: some View {
ScrollViewReader { proxy in
ScrollView(.vertical) {
if direction == .upwards, phase.isPaging {
ProgressView()
}

LazyVStack(alignment: alignment, spacing: spacing, pinnedViews: pinnedViews) {
ForEach(items) { item in
itemViewBuilder(item)
.id(item.id)
}
}
.scrollTargetLayout()

if direction == .downwards, phase.isPaging {
ProgressView()
ScrollView(.vertical) {
if direction == .upwards, phase.isPaging {
ProgressView()
}

LazyVStack(alignment: alignment, spacing: spacing, pinnedViews: pinnedViews) {
ForEach(items) { item in
itemViewBuilder(item)
.id(item.id)
}
}
.scrollPosition(
id: $scrollPositionItemID,
anchor: (direction == .downwards) ? .bottom : .top
)
.defaultScrollAnchor((direction == .downwards) ? .top : .bottom)
.scrollDismissesKeyboard(.interactively)
.scrollTargetLayout()

if direction == .downwards, phase.isPaging {
ProgressView()
}
}
.scrollPosition(
id: $scrollPositionItemID,
anchor: (direction == .downwards) ? .bottom : .top
)
.defaultScrollAnchor((direction == .downwards) ? .top : .bottom)
.scrollDismissesKeyboard(.interactively)

.onChange(of: scrollPositionItemID) { oldValue, newValue in
if let newValue, newValue == anchorItemID, phase == .idle {
self.phase = .paging(fromItem: newValue)
Expand All @@ -144,19 +143,20 @@ public struct InfiniteVerticalScrollView<Item: Identifiable & Sendable, ItemView
withAnimation {
self.phase = areThereMorePages ? .idle : .noMorePages
} completion: {
self.scrollPositionItemID = itemID
switch direction {
case .downwards:
self.items.append(contentsOf: newItems)
case .upwards:
self.items.insert(contentsOf: newItems, at: 0)
}
self.scrollPositionItemID = itemID
}
} catch {
self.phase = .idle
self.error = error
}
}
.errorAlert(error: $error)
}

private var anchorItemID: Item.ID? {
Expand Down

0 comments on commit 5b910b1

Please sign in to comment.