andromedainiative
andromedainiative

Reputation: 4972

How can I use the MDCSnackbarMessage completion handler?

Im trying to do run some code on the end of the a snackbar message being shown from this repo: https://github.com/material-components/

But I really don't understand the syntax of a method enough to implement it. This one specifically: https://github.com/material-components/material-components-ios/blob/develop/components/Snackbar/src/MDCSnackbarMessage.h#L125

@property(nonatomic, copy, nullable) MDCSnackbarMessageCompletionHandler completionHandler;

// I've tried a'lot of different ways but nothing works:

let message = MDCSnackbarMessage()

message.completionHandler (success: Bool?) -> Void in do {

}

message.completionHandler = true in {

}

To be quite honest I don't understand the method syntax good enough to use it.

Upvotes: 1

Views: 1656

Answers (2)

andromedainiative
andromedainiative

Reputation: 4972

I got a great informative and quick response from official dev team. Huge kudos to romoore for this help.

ObjC

- (void)showSimpleSnackbar:(id)sender {
  MDCSnackbarMessage *message = [[MDCSnackbarMessage alloc] init];
  message.text = @"Snackbar Message";

  // Added this assignment to demonstrate completion blocks.
  message.completionHandler = ^(BOOL userInitiated) {
  NSLog(@"Hello, world!");
 };

 [MDCSnackbarManager showMessage:message];
}

Swift

MDCSnackbarManager.show(message)
message.completionHandler = {(_ userInitiated: Bool) -> Void in
    print("Hello, world!")
}

Upvotes: 2

user7219266
user7219266

Reputation:

I don't know if it will help you, but I have recently used this library for displaying a SnackBar to the user.

It is very easy to use and implement.

You can install the pod and instantly try it with this sample code :

let snack = LPSnackbar(title: "Hello SnackBar", buttonTitle: "Cancel")

snack.height = 60

// Customize the snack
snack.bottomSpacing = 80
snack.view.titleLabel.font = UIFont.systemFont(ofSize: 20)

// Show a snack to allow user to undo deletion
snack.show(animated: true) { (undone) in
      if undone {
         // Undo deletion, handle action to revert back    
      } else {
         // Follow through with deletion
      }
}

EDIT: You might want to use Utils Class in order to init and display any SnackBar with title/message you want and handle actions in callbacks.

It will be cleaner. Hope it will help you.

EDIT 2: I looked into your library, and I found examples that explain how to implement the SnackBar with different options.

Here is to display a simple message without any user action :

let message = MDCSnackbarMessage()
message.text = "Tesla is going to Mars"
MDCSnackbarManager.show(message)

And here is the message with action (the handler you didn't understand) :

let action = MDCSnackbarMessageAction()
let actionHandler = {() in
  let answerMessage = MDCSnackbarMessage()
  answerMessage.text = "Fascinating"
  MDCSnackbarManager.show(answerMessage)
}
action.handler = actionHandler
action.title = "OK"
message.action = action

Upvotes: -1

Related Questions