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

Run refresh programmatically #3

Open
Luur opened this issue Jan 16, 2020 · 3 comments
Open

Run refresh programmatically #3

Luur opened this issue Jan 16, 2020 · 3 comments

Comments

@Luur
Copy link

Luur commented Jan 16, 2020

Hello,

Thank you for great lib.
I would like to have opportunity to run refresh programmatically from .onAppear { } block. Could you please prompt me how to achieve it?

@Luur Luur changed the title Run refresh programatically Run refresh programmatically Jan 16, 2020
@J0ker98
Copy link

J0ker98 commented May 8, 2020

I think you can achieve that by setting the pullToRefresh "isShowing" binding var to true on the .onAppear code block

Something like this:

struct ContentView: View {

    @State var isRefreshing: Bool = false
    var body: some View {

        List { ... }
            .pullToRefresh(isShowing: $isRefreshing) { ... }

        Text("Refresh me")
            .onAppear() {
                self.isRefreshing = true
            }
    }
}

@Luur
Copy link
Author

Luur commented May 13, 2020

Thank you for your response. You are right, "isShowing" binding var should be set to true on the .onAppear code block. But it's not enough. You also need to modify func updateUIView.

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PullToRefreshRepresentation>) {
        
        DispatchQueue.main.asyncAfter(deadline: .now()) {
            
            guard let parentView = self.parentView(entry: uiView) else { return }
            
            if let refreshControl = parentView.refreshControl {
                if self.isShowing {
                    **if !refreshControl.isRefreshing {
                        parentView.setContentOffset(CGPoint(x: 0, y: parentView.contentOffset.y - refreshControl.frame.size.height), animated: true)
                    }**
                    refreshControl.beginRefreshing()
                } else {
                    refreshControl.endRefreshing()
                }
                return
            }
            
            let refreshControl = UIRefreshControl()
            refreshControl.addTarget(context.coordinator, action: #selector(Coordinator.onValueChanged), for: .valueChanged)
            parentView.refreshControl = refreshControl
        }
    }

Only after that it works as needed in my case.

@bart-kneepkens
Copy link

Should @Luur's changes go into a PR to master? Seems to me that this is a rather common use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants