Git Product home page Git Product logo

Comments (6)

eyzuky avatar eyzuky commented on July 19, 2024 2

I figured a neat and easy to implement solution for this without you needing to change the API at all.
What I thought about (and tested and got it working perfectly) is to add the Toast to a different view, and when I want to hide the toast I simply remove the view from its superview and the Toast is removed along with it. Then If another toast needs to be displayed, I simply initialize the same view again and it is a clean view and then there is no toasts left. Some basic sample code:

    var viewForToast = UIView()
    func showToast(result: String) {
        self.viewForToast.removeFromSuperview()
        viewForToast = UIView()
        viewForToast.frame = self.view.frame
        
        self.viewForToast.makeToast(result, duration: 10.0, position: .center)
        self.view.addSubview(viewForToast)
    }

from toast-swift.

scalessec avatar scalessec commented on July 19, 2024 1

I'm only a year and a half late on this, but it's now possible in 3.0.0. You can have ToastManager.shared.isQueueEnabled set to true and then you can call self.view.hideToast() to immediately hide the active toast and present the next one (assuming there's multiple toasts in the queue).

from toast-swift.

scalessec avatar scalessec commented on July 19, 2024

I'd prefer to just make hideToast(_:) public. One of the reasons I never made this public in the first place is because it requires a reference to the toast view, which is a little inconvenient because none of the stock makeToast methods return a reference to the view they create. If you're presenting your own custom view as toast, this isn't a big deal. However if you were using makeToast, then it requires you to manually call toastViewForMessage to get the reference to the view, and then showToast(_:).

So, in the interim, I'll release a new version this week that just makes the existing hideToast(_:) method public. Ultimately though I want to think about how we can elegantly create a hideToast() method that doesn't require a reference to the toast view. This would be the ideal solution.

from toast-swift.

scalessec avatar scalessec commented on July 19, 2024

In addition, I may consider merging this into both Objective-C and Swift versions: scalessec/Toast#81

from toast-swift.

danielaRiesgo avatar danielaRiesgo commented on July 19, 2024

And what do you think of adding something like this in the framework?

In UIView's public extension:

private var currentToast: UIView?

public func hideToast() {
    if let currentToast = currentToast {
         if let timer = objc_getAssociatedObject(toast, &ToastKeys.Timer) as? NSTimer {
            timer.invalidate()
        }
        self.hideToast(toast, fromTap: true)
    }
}

And in makeToast method:

public func makeToast(message: String?, duration: NSTimeInterval, position: ToastPosition, title: String?, image: UIImage?, style: ToastStyle?, completion: ((didTap: Bool) -> Void)?) {
        var toastStyle = ToastManager.shared.style
        if let style = style {
            toastStyle = style
        }

        do {
            currentToast = try self.toastViewForMessage(message, title: title, image: image, style: toastStyle)
            self.showToast(currentToast, duration: duration, position: position, completion: completion)
        } catch ToastError.InsufficientData {
            print("Error: message, title, and image are all nil")
        } catch {}
    }

Add currentToast = nextToast and else clause in the hideToast method:

private func hideToast(toast: UIView, fromTap: Bool) {
        ....

            if let nextToast = self.queue.firstObject as? UIView, duration = objc_getAssociatedObject(nextToast, &ToastKeys.Duration) as? NSNumber, position = objc_getAssociatedObject(nextToast, &ToastKeys.Position) as? NSValue {
               currentToast = nextToast
                self.queue.removeObjectAtIndex(0)
                self.showToast(nextToast, duration: duration.doubleValue, position: position.CGPointValue())
            } else {
                currentToast = .None
            }
        }
    }

I don't know if I'm missing anything.

from toast-swift.

abrahamduran avatar abrahamduran commented on July 19, 2024

Is this still open? 🤔

from toast-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.