-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Create PaginatingLazyVStack as a generic scrollable list container #1983
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
// MARK: Implementation | ||
|
||
public struct PaginatingLazyVStack<Data: Identifiable, Cell: View>: View { | ||
@Binding var data: [Data] | ||
@Binding var canShowProgressView: Bool | ||
|
||
let onRefresh: () async -> Void | ||
let onDidShowProgressView: () -> Void | ||
let configureCell: (Data) -> Cell | ||
|
||
public init( | ||
data: Binding<[Data]>, | ||
canShowProgressView: Binding<Bool>, | ||
onRefresh: @escaping () async -> Void, | ||
onDidShowProgressView: @escaping () -> Void, | ||
configureCell: @escaping (Data) -> Cell | ||
) { | ||
self._data = data | ||
self._canShowProgressView = canShowProgressView | ||
self.onRefresh = onRefresh | ||
self.onDidShowProgressView = onDidShowProgressView | ||
self.configureCell = configureCell | ||
} | ||
|
||
public var body: some View { | ||
ScrollView { | ||
LazyVStack { | ||
ForEach(data) { | ||
configureCell($0) | ||
} | ||
.frame( | ||
maxWidth: .infinity | ||
) | ||
if canShowProgressView { | ||
HStack { | ||
Spacer() | ||
ProgressView() | ||
.progressViewStyle(.circular) | ||
.controlSize(.large) | ||
.onAppear { | ||
onDidShowProgressView() | ||
} | ||
Spacer() | ||
} | ||
} | ||
} | ||
} | ||
.refreshable { | ||
await onRefresh() | ||
} | ||
} | ||
} | ||
|
||
// MARK: Example view | ||
|
||
private struct PaginatingLazyVStackExampleCell: View { | ||
let title: String | ||
var body: some View { | ||
Text(title) | ||
.padding(.all, 10) | ||
} | ||
} | ||
|
||
struct PaginatingExampleModel: Identifiable { | ||
let title: String | ||
let id: String | ||
} | ||
|
||
struct PaginatingLazyVStackExampleView: View { | ||
@State var data: [PaginatingExampleModel] = [] | ||
@State var hasMore: Bool = true | ||
|
||
var body: some View { | ||
PaginatingLazyVStack( | ||
data: $data, | ||
canShowProgressView: $hasMore, | ||
onRefresh: { | ||
await refresh() | ||
}, | ||
onDidShowProgressView: { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { | ||
addMore() | ||
} | ||
}, | ||
configureCell: { model in | ||
PaginatingLazyVStackExampleCell(title: model.title) | ||
} | ||
) | ||
} | ||
|
||
private func addMore() { | ||
for _ in 0...15 { | ||
let uuid = UUID().uuidString | ||
self.data.append(PaginatingExampleModel( | ||
title: "Hello, world \(uuid)", | ||
id: uuid | ||
)) | ||
} | ||
|
||
if self.data.count > 100 { | ||
self.hasMore = false | ||
} | ||
} | ||
|
||
private func refresh() async { | ||
if #available(iOS 16.0, *) { | ||
try? await Task.sleep(for: .seconds(1)) | ||
} else {} | ||
|
||
self.data = [] | ||
self.addMore() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I learned some interesting things about |
||
} | ||
} | ||
|
||
#Preview { | ||
PaginatingLazyVStackExampleView() | ||
} |
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.
these should all be
private