Search code examples
iosswift3editcosmicmind

In CosmicMind / Material SnackbarController, how do I start (show) the snackbar?


In CosmicMind / Material library, how do I start (show) the snackbar?

I have tried to prepare the snackbarController and then show it whenever the user clicks on a button.

As shown in their example:

private var undoButton: FlatButton!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    prepareSnackbar()
    animateSnackbar()
}

@IBAction func loginBtnTapped(_ sender: AnyObject) {
    sc?.show(vc: UIViewControlle, sender: Any)//doesn't show
    more code... (which works)
}
private func prepareUndoButton() {
    undoButton = FlatButton(title: "Undo", titleColor: Color.yellow.base)
    undoButton.pulseAnimation = .backing
    undoButton.titleLabel?.font = RobotoFont.regular(with: 14)
}

private func prepareSnackbar() {
    guard let sc = snackbarController else {
        return
    }
    sc.snackbar.text = "Reminder saved."
    sc.snackbar.rightViews = [undoButton]

}

private func animateSnackbar() {
    guard let sc = snackbarController else {
        return
    }
    _ = sc.animate(snackbar: .visible, delay: 1)
    _ = sc.animate(snackbar: .hidden, delay: 4)
}

So I did try it on an empty project and the snackbar still doesn't work. Could you please point out what I am doing wrong?

 import Foundation
 import UIKit
 import Material

class MainViewController: UIViewController {

private var undoButton: FlatButton!


open override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = Color.red.accent1

    prepareUndoButton()
}

open override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //prepareSnackbar()
    //animateSnackbar()
}

private func prepareUndoButton() {
    undoButton = FlatButton(title: "Undo", titleColor: Color.yellow.base)
    undoButton.pulseAnimation = .backing
    undoButton.titleLabel?.font = RobotoFont.regular(with: 14)
}

private func prepareSnackbar() {
    guard let sc = snackbarController else {
        return
    }

    sc.snackbar.text = "Reminder saved."
    sc.snackbar.rightViews = [undoButton]
}

private func animateSnackbar() {
    guard let sc = snackbarController else {
        return
    }

    _ = sc.animate(snackbar: .visible, delay: 1)
    _ = sc.animate(snackbar: .hidden, delay: 4)
}


@IBAction func testBtn(_ sender: AnyObject) {
    print("TEST TEST TEST")

    prepareSnackbar()
    animateSnackbar()
}
}

Solution

  • If you notice in the viewDidAppear function you have an animateSnackbar call, which calls the lines

    _ = sc.animate(snackbar: .visible, delay: 1)
    _ = sc.animate(snackbar: .hidden, delay: 4)
    

    You can basically use the animateSnackbar function in your button handler, like so:

    @IBAction func loginBtnTapped(_ sender: AnyObject) {
        animateSnackbar()
    }
    

    That's it. There are two animations playing, one to show and one to hide the snackbar. Set the delay you would like to have them appear and hide automatically.

    You will probably want to remove the animateSnackbar call from the viewDidAppear method, as that was put there for the example.