SHARE
TWEET

Untitled

a guest Jul 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.graphics.Color
  2. import android.view.View
  3. import androidx.annotation.ColorInt
  4. import androidx.databinding.BindingAdapter
  5. import androidx.databinding.ObservableField
  6. import com.google.android.material.snackbar.Snackbar
  7.  
  8. /**
  9.  * Created by Ricard Aparicio on 04/04/2019.
  10.  * ricard.aparicio@inqbarna.com
  11.  */
  12.  
  13. interface IBindingSnackProvider {
  14.     val snackMessage: ObservableField<ConsumableSnackMessage>
  15.     fun showSnack(snackMessage: SnackMessage)
  16. }
  17.  
  18. class BindingSnackProvider : IBindingSnackProvider {
  19.     override val snackMessage: ObservableField<ConsumableSnackMessage> by lazy { ObservableField<ConsumableSnackMessage>() }
  20.  
  21.     override fun showSnack(snackMessage: SnackMessage) {
  22.         this.snackMessage.set(ConsumableSnackMessage(snackMessage.message, snackMessage.actionData))
  23.     }
  24. }
  25.  
  26. open class SnackMessage(val message: String, val actionData: SnackActionData? = null)
  27.  
  28. data class ConsumableSnackMessage(val msg: String, val actData: SnackActionData? = null) :
  29.     SnackMessage(msg, actData) {
  30.     var consumed = false
  31. }
  32.  
  33. data class SnackActionData(
  34.     val message: String,
  35.     @ColorInt val color: Int = Color.CYAN,
  36.     val clickListener: () -> Unit
  37. )
  38.  
  39. @BindingAdapter("snackMessage")
  40. fun View.showSnackbar(snackMessage: SnackMessage?) {
  41.     snackMessage?.let {
  42.         val showSnackFun: () -> Unit = {
  43.             Snackbar.make(this, snackMessage.message, Snackbar.LENGTH_LONG).apply {
  44.                 snackMessage.actionData?.let { data ->
  45.                     setAction(data.message) { data.clickListener() }
  46.                     setActionTextColor(data.color)
  47.                 }
  48.             }.show()
  49.         }
  50.  
  51.         if (snackMessage is ConsumableSnackMessage) {
  52.             if (snackMessage.consumed.not()) {
  53.                 showSnackFun()
  54.             }
  55.             snackMessage.consumed = true
  56.         } else {
  57.             showSnackFun()
  58.         }
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top