-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create PaginatingLazyVStack as a generic scrollable and refreshable c…
…ontainer
- Loading branch information
1 parent
c558498
commit 0a3a633
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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() | ||
} | ||
} | ||
|
||
#Preview { | ||
PaginatingLazyVStackExampleView() | ||
} |