SwiftSpinner
SwiftSpinner copied to clipboard
addTapHandler after SwiftSpinner.show(delay: …)
It would be great if you can change the following function func show(delay: Double, title: String, animated: Bool = true) to return a SwiftSpinner as other show functions to allow us adding a tapHandler after a specific delay…
I'm trying to find a workaround but couldn't make one working for now…
Is there a reason you didn't returned a SwiftSpinner with that specific function ?
If anyone has a solution, go ahead and tell me 👍
The demo project has lots of examples of this. Take a look at the code in viewWillAppear inside the ViewController.swift. It has lots of examples using a delays to call SwiftSpinner.show all of which could have the tapHandler bolted onto them if desired (and some of them do).
Yeah, I tried doing something like this :
`func showSpinner() {
SwiftSpinner.show(NSLocalizedString("Presenter.noDelay", value: "Connecting to server", comment: "Indicates that the application is connecting to the server"))
SwiftSpinner.show(delay: 0.7, title: NSLocalizedString("Presenter.delayOf1.0", value: "Fetching data from server", comment: "Indicates that the application is fetching the data from the server"))
delay(seconds: 10.0, completion: {
SwiftSpinner.show(NSLocalizedString("Presenter.delayOf10.0", value: "Server is taking too long to respond or is'nt responding…", comment: "Indicates that the server is not responding properly"))
.addTapHandler({
SwiftSpinner.hide()
}, subtitle: NSLocalizedString("TapHandler.comment", value: "You can tap the loader to dismiss it", comment: "Tells the user can tap to hide the loading spinner"))
})
}
func delay(seconds: Double, completion: @escaping () -> ()) {
let popTime = DispatchTime.now() + Double(Int64( Double(NSEC_PER_SEC) * seconds )) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: popTime) {
completion()
}
}`
But even after calling this method
SwiftSpinner.hide({ SwiftSpinner.hideCancelsScheduledSpinners = true })
the spinner shows.. this is my biggest problem actually :/ It wouldn't if the delay function allowed me to add a tapHandler though.
Could you tidy that reply up, I think you'd double pasted some parts?
I'm calling this SwiftSpinner.hide({ SwiftSpinner.hideCancelsScheduledSpinners = true }) in another function not showed above.
For what's showed up there, I'm not duplicating any code cause I'm assigning different strings to the Spinner.
So your .addTapHandler({SwiftSpinner.hide()} block isn't hiding the spinner when the user taps?
Yes it does, but the
delay(seconds: 10.0, completion: { SwiftSpinner.show(NSLocalizedString("Presenter.delayOf10.0", value: "Server is taking too long to respond or is'nt responding…", comment: "Indicates that the server is not responding properly")) .addTapHandler({ SwiftSpinner.hide() }, subtitle: NSLocalizedString("TapHandler.comment", value: "You can tap the loader to dismiss it", comment: "Tells the user can tap to hide the loading spinner")) })
block is showing even after calling the .addTapHandler({SwiftSpinner.hide()} block..
(sorry I can't make it properly indented.. :/)
That's the same block of code I was referring too. In the code you've provided, that block will always be show. There is no check in between to see if it does actually need to be shown.
Okay.. I'll try to find a way then.. thanks for you help buddy :)
To break it down a little more clearly.
This is shown first:
SwiftSpinner.show(NSLocalizedString("Presenter.noDelay", value: "Connecting to server", comment: "Indicates that the application is connecting to the server"))
Then this:
SwiftSpinner.show(delay: 0.7, title: NSLocalizedString("Presenter.delayOf1.0", value: "Fetching data from server", comment: "Indicates that the application is fetching the data from the server"))
Then this:
delay(seconds: 10.0, completion: {
SwiftSpinner.show(NSLocalizedString("Presenter.delayOf10.0", value: "Server is taking too long to respond or is'nt responding…", comment: "Indicates that the server is not responding properly"))
.addTapHandler({
SwiftSpinner.hide()
}, subtitle: NSLocalizedString("TapHandler.comment", value: "You can tap the loader to dismiss it", comment: "Tells the user can tap to hide the loading spinner"))
})
That sequence will always run to the conclusion of the 'server is taking too long' stage. If you don't want that part showing, because your networking was a success, you need a check in place to prevent that. I would suggest breaking this up into separate parts and then only calling the bits you need at appropriate times in the blocks that handle your networking.
Thank you for your explanation, I may do as you suggest to answer my problem the best way :)