Skip to content
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

make code example more generic #1

Merged
merged 1 commit into from
Jun 2, 2020
Merged
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
32 changes: 21 additions & 11 deletions SwiftUIPullToRefresh/SwiftUIPullToRefresh/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ struct Model: Identifiable {

struct ContentView: View {

var modelData = DataModel(modelData: [Model(title: "Item 1"), Model(title: "Item 2"), Model(title: "Item 3")])

var body: some View {
GeometryReader{
geometry in
NavigationView{

CustomScrollView(width: geometry.size.width, height: geometry.size.height)
CustomScrollView(width: geometry.size.width, height: geometry.size.height, handlePullToRefresh: {
self.modelData.addElement()
}) {
SwiftUIList(model: self.modelData)
}
.navigationBarTitle(Text("SwiftUI Pull To Refresh"), displayMode: .inline)

}
Expand Down Expand Up @@ -55,13 +61,14 @@ class DataModel: ObservableObject {
}


struct CustomScrollView : UIViewRepresentable {
struct CustomScrollView<ROOTVIEW>: UIViewRepresentable where ROOTVIEW: View {

var width : CGFloat, height : CGFloat
let modelData = DataModel(modelData: [Model(title: "Item 1"), Model(title: "Item 2"), Model(title: "Item 3")])
let handlePullToRefresh: () -> Void
let rootView: () -> ROOTVIEW

func makeCoordinator() -> Coordinator {
Coordinator(self, model: modelData)
func makeCoordinator() -> Coordinator<ROOTVIEW> {
Coordinator(self, rootView: rootView, handlePullToRefresh: handlePullToRefresh)
}

func makeUIView(context: Context) -> UIScrollView {
Expand All @@ -71,7 +78,7 @@ struct CustomScrollView : UIViewRepresentable {
#selector(Coordinator.handleRefreshControl),
for: .valueChanged)

let childView = UIHostingController(rootView: SwiftUIList(model: modelData))
let childView = UIHostingController(rootView: rootView() )
childView.view.frame = CGRect(x: 0, y: 0, width: width, height: height)

control.addSubview(childView.view)
Expand All @@ -80,19 +87,22 @@ struct CustomScrollView : UIViewRepresentable {

func updateUIView(_ uiView: UIScrollView, context: Context) {}

class Coordinator: NSObject {
class Coordinator<ROOTVIEW>: NSObject where ROOTVIEW: View {
var control: CustomScrollView
var model : DataModel
var handlePullToRefresh: () -> Void
var rootView: () -> ROOTVIEW

init(_ control: CustomScrollView, model: DataModel) {
init(_ control: CustomScrollView, rootView: @escaping () -> ROOTVIEW, handlePullToRefresh: @escaping () -> Void) {
self.control = control
self.model = model
self.handlePullToRefresh = handlePullToRefresh
self.rootView = rootView
}

@objc func handleRefreshControl(sender: UIRefreshControl) {

sender.endRefreshing()
model.addElement()
handlePullToRefresh()

}
}
}
Expand Down