Created
May 26, 2016 09:57
-
-
Save NeedPainkiller/296107c2fc8ad54fd43fe049132e61a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RxSnackBar { | |
private static int NONE = 0; | |
private final static String DEF_MESSAGE = "RxSnackBar"; | |
private final static String DEF_ACTION = "ACTION"; | |
private static Snackbar snackbar; | |
private final View rootView; | |
private final int messageID; | |
private final CharSequence messageCharSequence; | |
private final int flag; | |
private final int actionID; | |
private final CharSequence actionCharSequence; | |
private final int messageColor; | |
private final int backGroundColor; | |
public static class Builder { | |
private final View rootView; | |
private final int messageID; | |
private final CharSequence messageCharSequence; | |
private int flag = Snackbar.LENGTH_LONG; | |
private int actionID = NONE; | |
private CharSequence actionCharSequence = DEF_ACTION; | |
private int messageColor = NONE; | |
private int backGroundColor = NONE; | |
public Builder(View rootView, int messageID) { | |
this.rootView = rootView; | |
this.messageID = messageID; | |
this.messageCharSequence = DEF_MESSAGE; | |
} | |
public Builder(View rootView, CharSequence messageCharSequence) { | |
this.rootView = rootView; | |
this.messageID = NONE; | |
this.messageCharSequence = messageCharSequence; | |
} | |
public Builder setFlag(int flag) { | |
this.flag = flag; | |
return this; | |
} | |
public Builder setActionID(int actionID) { | |
this.actionID = actionID; | |
this.actionCharSequence = DEF_ACTION; | |
return this; | |
} | |
public Builder setActionCharSequence(CharSequence actionCharSequence) { | |
this.actionID = NONE; | |
this.actionCharSequence = actionCharSequence; | |
return this; | |
} | |
public Builder setMessageColor(int messageColor) { | |
this.messageColor = messageColor; | |
return this; | |
} | |
public Builder setBackGroundColor(int backGroundColor) { | |
this.backGroundColor = backGroundColor; | |
return this; | |
} | |
public RxSnackBar build() { | |
return new RxSnackBar(this); | |
} | |
} | |
private RxSnackBar(Builder builder) { | |
rootView = builder.rootView; | |
messageID = builder.messageID; | |
messageCharSequence = builder.messageCharSequence; | |
flag = builder.flag; | |
actionID = builder.actionID; | |
actionCharSequence = builder.actionCharSequence; | |
messageColor = builder.messageColor; | |
backGroundColor = builder.backGroundColor; | |
} | |
public Observable<Boolean> showMeTheSnack() { | |
return Observable.create((Subscriber<? super Boolean> subscriber) -> { | |
if (messageID == NONE) { | |
snackbar = Snackbar.make(rootView, messageCharSequence, flag); | |
} else { | |
snackbar = Snackbar.make(rootView, messageID, flag); | |
} | |
if (actionID == NONE) { | |
snackbar.setAction(actionCharSequence, v -> subscriber.onNext(true)); | |
} else { | |
snackbar.setAction(actionID, v -> subscriber.onNext(true)); | |
} | |
if (messageColor != NONE) { | |
((TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text)).setTextColor(messageColor); | |
} | |
if (backGroundColor != NONE) { | |
snackbar.getView().setBackgroundResource(backGroundColor); | |
} | |
snackbar.setCallback(new Snackbar.Callback() { | |
@Override | |
public void onDismissed(Snackbar snackbar, int event) { | |
super.onDismissed(snackbar, event); | |
subscriber.onCompleted(); | |
subscriber.unsubscribe(); | |
} | |
}); | |
subscriber.add(Subscriptions.create(snackbar::dismiss)); | |
snackbar.show(); | |
}).observeOn(AndroidSchedulers.mainThread()); | |
} | |
public static void dismiss() { | |
if (snackbar != null) { | |
snackbar.dismiss(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment