Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Parcel;
- import android.os.Parcelable;
- import android.support.annotation.NonNull;
- import android.support.v4.app.DialogFragment;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v7.app.AlertDialog;
- import com.edufii.R;
- import org.androidannotations.annotations.EFragment;
- import org.androidannotations.annotations.FragmentArg;
- import static android.content.DialogInterface.BUTTON_POSITIVE;
- import static android.content.DialogInterface.OnClickListener;
- import static com.google.common.base.Preconditions.checkNotNull;
- @EFragment
- public class ConfirmDialogFragment extends DialogFragment implements OnClickListener {
- @FragmentArg Config config;
- @FragmentArg Bundle cookies;
- @NonNull
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- getOnClickCallback(); // check whether callback exist
- setCancelable(config.cancelable);
- AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), config.style)
- .setTitle(config.title)
- .setMessage(config.message);
- if (config.hasButtons()) {
- builder.setPositiveButton(config.okText, this);
- builder.setNegativeButton(config.noText, this);
- }
- return builder.create();
- }
- @Override
- public void onClick(DialogInterface dialog, int which) {
- getOnClickCallback().onConfirmed(which == BUTTON_POSITIVE, getTag(), cookies);
- }
- @Override
- public void onDismiss(DialogInterface dialog) {
- super.onDismiss(dialog);
- OnDismissCallback onDismissCallback = getOnDismissCallback();
- if (onDismissCallback != null) {
- onDismissCallback.onConfirmDialogDismissed(getTag(), cookies);
- }
- }
- private OnConfirmCallback getOnClickCallback() {
- if (getTargetFragment() instanceof OnConfirmCallback) {
- return (OnConfirmCallback) getTargetFragment();
- } else if (getActivity() instanceof OnConfirmCallback) {
- return (OnConfirmCallback) getActivity();
- } else {
- throw new IllegalStateException("Neither target fragment and activity doesn't implement OnConfirmCallback!");
- }
- }
- private OnDismissCallback getOnDismissCallback() {
- if (getTargetFragment() instanceof OnDismissCallback) {
- return (OnDismissCallback) getTargetFragment();
- } else if (getActivity() instanceof OnDismissCallback) {
- return (OnDismissCallback) getActivity();
- } else {
- return null;
- }
- }
- public static final class Builder {
- private static final String DEFAULT_DIALOG_TAG = "ConfirmDialog";
- private final Fragment targetFragment;
- private final FragmentActivity activity;
- private final Config config = new Config();
- private final Bundle cookies = new Bundle();
- private String dialogTag = DEFAULT_DIALOG_TAG;
- public Builder(Fragment targetFragment) {
- this.targetFragment = checkNotNull(targetFragment);
- this.activity = targetFragment.getActivity();
- okCancelButtons();
- }
- public Builder(FragmentActivity activity) {
- this.targetFragment = null;
- this.activity = checkNotNull(activity);
- okCancelButtons();
- }
- public Builder title(String title) {
- config.title = title;
- return this;
- }
- public Builder title(int titleId) {
- return title(activity.getString(titleId));
- }
- public Builder message(String message) {
- config.message = message;
- return this;
- }
- public Builder message(int messageId) {
- return message(activity.getString(messageId));
- }
- public Builder message(int textId, Object... messageArgs) {
- return message(activity.getString(textId, messageArgs));
- }
- public Builder yesNoButtons() {
- return buttons(R.string.yes, R.string.no);
- }
- public Builder okCancelButtons() {
- return buttons(R.string.ok, R.string.cancel);
- }
- public Builder cookie(String key, String value) {
- cookies.putString(key, value);
- return this;
- }
- public Builder cookie(String key, long value) {
- cookies.putLong(key, value);
- return this;
- }
- public Builder cookie(String key, boolean value) {
- cookies.putBoolean(key, value);
- return this;
- }
- public Builder cookie(String key, Parcelable value) {
- cookies.putParcelable(key, value);
- return this;
- }
- public Builder cookie(String key, Bundle value) {
- cookies.putBundle(key, value);
- return this;
- }
- public Builder dialogTag(String dialogTag) {
- this.dialogTag = checkNotNull(dialogTag);
- return this;
- }
- public Builder buttons(int okText, int noText) {
- config.okText = okText;
- config.noText = noText;
- return this;
- }
- public Builder okButton(int okText) {
- config.okText = okText;
- return this;
- }
- public Builder notCancelable() {
- config.cancelable = false;
- return this;
- }
- public Builder destructive() {
- config.style = R.style.Theme_AppCompat_Light_Dialog_Destructive;
- return this;
- }
- public void show() {
- DialogFragment dialogFragment = ConfirmDialogFragment_
- .builder()
- .config(config)
- .cookies(cookies)
- .build();
- dialogFragment.setTargetFragment(targetFragment, 0);
- dialogFragment.show(activity.getSupportFragmentManager(), dialogTag);
- }
- }
- public interface OnConfirmCallback {
- void onConfirmed(boolean confirmed, String dialogTag, Bundle cookies);
- }
- public interface OnDismissCallback {
- void onConfirmDialogDismissed(String dialogTag, Bundle cookies);
- }
- public static final class Config implements Parcelable {
- private String title;
- private String message;
- private int okText;
- private int noText;
- private boolean cancelable = true;
- private int style = R.style.Theme_AppCompat_Light_Dialog;
- private Config() {
- }
- private Config(Parcel in) {
- this.title = in.readString();
- this.message = in.readString();
- this.okText = in.readInt();
- this.noText = in.readInt();
- this.cancelable = in.readInt() != 0;
- this.style = in.readInt();
- }
- public boolean hasButtons() {
- return okText != 0 && noText != 0;
- }
- @Override
- public int describeContents() {
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(this.title);
- dest.writeString(this.message);
- dest.writeInt(this.okText);
- dest.writeInt(this.noText);
- dest.writeInt(this.cancelable ? 1 : 0);
- dest.writeInt(this.style);
- }
- public static Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {
- public Config createFromParcel(Parcel source) {
- return new Config(source);
- }
- public Config[] newArray(int size) {
- return new Config[size];
- }
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement