Skip to content

Instantly share code, notes, and snippets.

@alorma
Created August 24, 2015 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alorma/a9d270b48560fa05165e to your computer and use it in GitHub Desktop.
Save alorma/a9d270b48560fa05165e to your computer and use it in GitHub Desktop.
public class MaterialDialogUtils {
private MaterialDialog.Builder builder;
private COUNTDOWN_BUTTON countdownButton;
private long timeInMillis;
private boolean positive;
private int positiveText;
private int positiveTextCountdown;
private boolean negative;
private int negativeText;
private int negativeTextCountdown;
private boolean neutral;
private int neutralText;
private int neutralTextCountdown;
private int color;
private int colorDisabled;
private String rememberKey;
private MaterialDialog.ButtonCallback buttonCallback;
private CountDownTimer timer;
public boolean isRememberSelected;
public enum COUNTDOWN_BUTTON {
POSITIVE(0),
NEUTRAL(1),
NEGATIVE(2);
private final int value;
COUNTDOWN_BUTTON(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static COUNTDOWN_BUTTON fromValue(int value) {
switch (value) {
case 0:
return POSITIVE;
case 1:
return NEUTRAL;
case 2:
return NEGATIVE;
default:
return POSITIVE;
}
}
}
public MaterialDialogUtils(MaterialDialog.Builder builder) {
this.builder = builder;
}
public MaterialDialogUtils remember(boolean defaultChecked, COUNTDOWN_BUTTON countdownButtonDefault, String key, @StringRes int rememberText) {
rememberKey = "countdowndialog_" + key;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(builder.getContext());
this.isRememberSelected = preferences.getBoolean(rememberKey, defaultChecked);
countdownButton = countdownButtonDefault;
if (this.isRememberSelected) {
int button = preferences.getInt(rememberKey + "_button", countdownButton.getValue());
countdownButton = COUNTDOWN_BUTTON.fromValue(button);
}
Resources resources = builder.getContext().getResources();
builder.items(new String[]{resources.getString(rememberText)});
return this;
}
public MaterialDialogUtils time(TimeUnit timeUnit, int time) {
this.timeInMillis = timeUnit.toMillis(time);
return this;
}
public MaterialDialogUtils positive(boolean enabled, @StringRes int normalText, @StringRes int countdownText) {
this.positive = enabled;
this.positiveText = normalText;
this.positiveTextCountdown = countdownText;
return this;
}
public MaterialDialogUtils negative(boolean enabled, @StringRes int normalText, @StringRes int countdownText) {
this.negative = enabled;
this.negativeText = normalText;
this.negativeTextCountdown = countdownText;
return this;
}
public MaterialDialogUtils neutral(boolean enabled, @StringRes int normalText, @StringRes int countdownText) {
this.neutral = enabled;
this.neutralText = normalText;
this.neutralTextCountdown = countdownText;
return this;
}
public MaterialDialogUtils setButtonCallback(MaterialDialog.ButtonCallback buttonCallback) {
this.buttonCallback = buttonCallback;
return this;
}
public MaterialDialogUtils colors(@ColorRes int colorEnabled, @ColorRes int colorDisabled) {
this.color = colorEnabled;
this.colorDisabled = colorDisabled;
return this;
}
protected MaterialDialog.Builder build() {
if (countdownButton != null) {
if (positive) {
builder.positiveText(positiveText);
builder.positiveColorRes(colorDisabled);
}
if (neutral) {
builder.neutralText(neutralText);
builder.neutralColorRes(colorDisabled);
}
if (negative) {
builder.negativeText(negativeText);
builder.negativeColorRes(colorDisabled);
}
switch (countdownButton) {
case POSITIVE:
builder.positiveColorRes(color);
break;
case NEUTRAL:
builder.neutralColorRes(color);
break;
case NEGATIVE:
builder.negativeColorRes(color);
break;
}
}
return builder;
}
public MaterialDialog show() {
MaterialDialog.ListCallbackMultiChoice callback = new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog materialDialog, Integer[] integers, CharSequence[] charSequences) {
isRememberSelected = integers != null && integers.length == 1 && integers[0] == 0;
return true;
}
};
Integer[] checkedItems = isRememberSelected ? new Integer[]{0} : new Integer[]{};
final MaterialDialog dialog = build()
.cancelable(false)
.itemsCallbackMultiChoice(checkedItems, callback)
.alwaysCallMultiChoiceCallback()
.callback(new MaterialDialog.ButtonCallback() {
@Override
public void onAny(MaterialDialog dialog) {
super.onAny(dialog);
if (buttonCallback != null) {
buttonCallback.onAny(dialog);
}
if (timer != null) {
timer.cancel();
}
}
@Override
public void onPositive(MaterialDialog dialog) {
super.onPositive(dialog);
if (buttonCallback != null) {
buttonCallback.onAny(dialog);
buttonCallback.onPositive(dialog);
}
saveRemember(dialog.getContext(), isRememberSelected, COUNTDOWN_BUTTON.POSITIVE);
}
@Override
public void onNegative(MaterialDialog dialog) {
super.onNegative(dialog);
if (buttonCallback != null) {
buttonCallback.onAny(dialog);
buttonCallback.onNegative(dialog);
}
saveRemember(dialog.getContext(), isRememberSelected, COUNTDOWN_BUTTON.NEGATIVE);
}
@Override
public void onNeutral(MaterialDialog dialog) {
super.onNeutral(dialog);
if (buttonCallback != null) {
buttonCallback.onAny(dialog);
buttonCallback.onNeutral(dialog);
}
saveRemember(dialog.getContext(), isRememberSelected, COUNTDOWN_BUTTON.NEUTRAL);
}
})
.show();
timer = new CountDownTimer(timeInMillis - TimeUnit.SECONDS.toMillis(1), timeInMillis / TimeUnit.SECONDS.toMillis(1)) {
@Override
public void onTick(long millisUntilFinished) {
Context context = dialog.getContext();
Resources resources = context.getResources();
long time = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished + TimeUnit.SECONDS.toMillis(1));
switch (countdownButton) {
case POSITIVE:
if (positive) {
String newPositiveText = resources.getString(positiveTextCountdown, time);
dialog.setActionButton(DialogAction.POSITIVE, newPositiveText);
}
break;
case NEUTRAL:
if (neutral) {
String newNeutralText = resources.getString(neutralTextCountdown, time);
dialog.setActionButton(DialogAction.NEUTRAL, newNeutralText);
}
break;
case NEGATIVE:
if (negative) {
String newNegativeText = resources.getString(negativeTextCountdown, time);
dialog.setActionButton(DialogAction.NEGATIVE, newNegativeText);
}
break;
}
}
@Override
public void onFinish() {
if (dialog != null && dialog.isShowing()) {
switch (countdownButton) {
case POSITIVE:
if (positive) {
dialog.getActionButton(DialogAction.POSITIVE).performClick();
dialog.dismiss();
}
break;
case NEUTRAL:
if (neutral) {
dialog.getActionButton(DialogAction.NEUTRAL).performClick();
dialog.dismiss();
}
break;
case NEGATIVE:
if (negative) {
dialog.getActionButton(DialogAction.NEGATIVE).performClick();
dialog.dismiss();
}
break;
}
}
}
};
timer.start();
return dialog;
}
private void saveRemember(Context context, boolean checked, COUNTDOWN_BUTTON countdownButton) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean(rememberKey, checked);
if (checked) {
edit.putInt(rememberKey + "_button", countdownButton.ordinal());
} else {
edit.remove(rememberKey + "_button");
}
edit.apply();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment