globulus / swiftui-pull-to-refresh

Pull to refresh functionality for any ScrollView in SwiftUI!
MIT License
277 stars 51 forks source link

Instead of callback, can I use a @State to control refresh action? #11

Closed long-nguyen closed 2 years ago

long-nguyen commented 2 years ago

Hi, thank you for your great implementation. How could I achieve things like using @State var loading = false to control when the refreshing action should be finish. Thanks

gordan-glavas-codecons commented 2 years ago

Hi,

the library is completely agnostic in terms of how you handle your refreshing, which also includes how you cancel it. In other words, something like this would work:

RefreshableScrollView(onRefresh: { done in
           isLoading = true
           DispatchQueue(label: "background").async {
             while isLoading { }
             done()
           }
          },

As far as triggering the refresh action by setting isLoading to true, I need to think about it a bit more. It's certainly a possibility.

long-nguyen commented 2 years ago

@gordan-glavas-codecons thank you. But the done() event, how can I call it in another functions outside the onRefresh callback like above?

gordan-glavas-codecons commented 2 years ago

It's just a block you can pass it around any way you want. You can even store it in a state var:

@State private var refreshBlock: RefreshComplete? = nil
...
RefreshableScrollView(onRefresh: { done in
           refreshBlock = done
}

// call refreshBlock?() elsewhere
long-nguyen commented 2 years ago

@gordan-glavas-codecons Thank you, great!