Advertisement
danikula

Android confirmation dialog fragment

Jun 9th, 2015
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.91 KB | None | 0 0
  1. import android.app.Dialog;
  2. import android.content.DialogInterface;
  3. import android.os.Bundle;
  4. import android.os.Parcel;
  5. import android.os.Parcelable;
  6. import android.support.annotation.NonNull;
  7. import android.support.v4.app.DialogFragment;
  8. import android.support.v4.app.Fragment;
  9. import android.support.v4.app.FragmentActivity;
  10. import android.support.v7.app.AlertDialog;
  11.  
  12. import com.edufii.R;
  13.  
  14. import org.androidannotations.annotations.EFragment;
  15. import org.androidannotations.annotations.FragmentArg;
  16.  
  17. import static android.content.DialogInterface.BUTTON_POSITIVE;
  18. import static android.content.DialogInterface.OnClickListener;
  19. import static com.google.common.base.Preconditions.checkNotNull;
  20.  
  21. @EFragment
  22. public class ConfirmDialogFragment extends DialogFragment implements OnClickListener {
  23.  
  24.     @FragmentArg Config config;
  25.     @FragmentArg Bundle cookies;
  26.  
  27.     @NonNull
  28.     @Override
  29.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  30.         getOnClickCallback(); // check whether callback exist
  31.         setCancelable(config.cancelable);
  32.         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), config.style)
  33.                 .setTitle(config.title)
  34.                 .setMessage(config.message);
  35.         if (config.hasButtons()) {
  36.             builder.setPositiveButton(config.okText, this);
  37.             builder.setNegativeButton(config.noText, this);
  38.         }
  39.         return builder.create();
  40.     }
  41.  
  42.     @Override
  43.     public void onClick(DialogInterface dialog, int which) {
  44.         getOnClickCallback().onConfirmed(which == BUTTON_POSITIVE, getTag(), cookies);
  45.     }
  46.  
  47.     @Override
  48.     public void onDismiss(DialogInterface dialog) {
  49.         super.onDismiss(dialog);
  50.  
  51.         OnDismissCallback onDismissCallback = getOnDismissCallback();
  52.         if (onDismissCallback != null) {
  53.             onDismissCallback.onConfirmDialogDismissed(getTag(), cookies);
  54.         }
  55.     }
  56.  
  57.     private OnConfirmCallback getOnClickCallback() {
  58.         if (getTargetFragment() instanceof OnConfirmCallback) {
  59.             return (OnConfirmCallback) getTargetFragment();
  60.         } else if (getActivity() instanceof OnConfirmCallback) {
  61.             return (OnConfirmCallback) getActivity();
  62.         } else {
  63.             throw new IllegalStateException("Neither target fragment and activity doesn't implement OnConfirmCallback!");
  64.         }
  65.     }
  66.  
  67.     private OnDismissCallback getOnDismissCallback() {
  68.         if (getTargetFragment() instanceof OnDismissCallback) {
  69.             return (OnDismissCallback) getTargetFragment();
  70.         } else if (getActivity() instanceof OnDismissCallback) {
  71.             return (OnDismissCallback) getActivity();
  72.         } else {
  73.             return null;
  74.         }
  75.     }
  76.  
  77.     public static final class Builder {
  78.  
  79.         private static final String DEFAULT_DIALOG_TAG = "ConfirmDialog";
  80.  
  81.         private final Fragment targetFragment;
  82.         private final FragmentActivity activity;
  83.         private final Config config = new Config();
  84.         private final Bundle cookies = new Bundle();
  85.         private String dialogTag = DEFAULT_DIALOG_TAG;
  86.  
  87.         public Builder(Fragment targetFragment) {
  88.             this.targetFragment = checkNotNull(targetFragment);
  89.             this.activity = targetFragment.getActivity();
  90.             okCancelButtons();
  91.         }
  92.  
  93.         public Builder(FragmentActivity activity) {
  94.             this.targetFragment = null;
  95.             this.activity = checkNotNull(activity);
  96.             okCancelButtons();
  97.         }
  98.  
  99.         public Builder title(String title) {
  100.             config.title = title;
  101.             return this;
  102.         }
  103.  
  104.         public Builder title(int titleId) {
  105.             return title(activity.getString(titleId));
  106.         }
  107.  
  108.         public Builder message(String message) {
  109.             config.message = message;
  110.             return this;
  111.         }
  112.  
  113.         public Builder message(int messageId) {
  114.             return message(activity.getString(messageId));
  115.         }
  116.  
  117.         public Builder message(int textId, Object... messageArgs) {
  118.             return message(activity.getString(textId, messageArgs));
  119.         }
  120.  
  121.         public Builder yesNoButtons() {
  122.             return buttons(R.string.yes, R.string.no);
  123.         }
  124.  
  125.         public Builder okCancelButtons() {
  126.             return buttons(R.string.ok, R.string.cancel);
  127.         }
  128.  
  129.         public Builder cookie(String key, String value) {
  130.             cookies.putString(key, value);
  131.             return this;
  132.         }
  133.  
  134.         public Builder cookie(String key, long value) {
  135.             cookies.putLong(key, value);
  136.             return this;
  137.         }
  138.  
  139.         public Builder cookie(String key, boolean value) {
  140.             cookies.putBoolean(key, value);
  141.             return this;
  142.         }
  143.  
  144.         public Builder cookie(String key, Parcelable value) {
  145.             cookies.putParcelable(key, value);
  146.             return this;
  147.         }
  148.  
  149.         public Builder cookie(String key, Bundle value) {
  150.             cookies.putBundle(key, value);
  151.             return this;
  152.         }
  153.  
  154.         public Builder dialogTag(String dialogTag) {
  155.             this.dialogTag = checkNotNull(dialogTag);
  156.             return this;
  157.         }
  158.  
  159.         public Builder buttons(int okText, int noText) {
  160.             config.okText = okText;
  161.             config.noText = noText;
  162.             return this;
  163.         }
  164.  
  165.         public Builder okButton(int okText) {
  166.             config.okText = okText;
  167.             return this;
  168.         }
  169.  
  170.         public Builder notCancelable() {
  171.             config.cancelable = false;
  172.             return this;
  173.         }
  174.  
  175.         public Builder destructive() {
  176.             config.style = R.style.Theme_AppCompat_Light_Dialog_Destructive;
  177.             return this;
  178.         }
  179.  
  180.         public void show() {
  181.             DialogFragment dialogFragment = ConfirmDialogFragment_
  182.                     .builder()
  183.                     .config(config)
  184.                     .cookies(cookies)
  185.                     .build();
  186.             dialogFragment.setTargetFragment(targetFragment, 0);
  187.             dialogFragment.show(activity.getSupportFragmentManager(), dialogTag);
  188.         }
  189.     }
  190.  
  191.     public interface OnConfirmCallback {
  192.  
  193.         void onConfirmed(boolean confirmed, String dialogTag, Bundle cookies);
  194.  
  195.     }
  196.  
  197.     public interface OnDismissCallback {
  198.  
  199.         void onConfirmDialogDismissed(String dialogTag, Bundle cookies);
  200.  
  201.     }
  202.  
  203.     public static final class Config implements Parcelable {
  204.  
  205.         private String title;
  206.         private String message;
  207.         private int okText;
  208.         private int noText;
  209.         private boolean cancelable = true;
  210.         private int style = R.style.Theme_AppCompat_Light_Dialog;
  211.  
  212.         private Config() {
  213.         }
  214.  
  215.         private Config(Parcel in) {
  216.             this.title = in.readString();
  217.             this.message = in.readString();
  218.             this.okText = in.readInt();
  219.             this.noText = in.readInt();
  220.             this.cancelable = in.readInt() != 0;
  221.             this.style = in.readInt();
  222.         }
  223.  
  224.         public boolean hasButtons() {
  225.             return okText != 0 && noText != 0;
  226.         }
  227.  
  228.         @Override
  229.         public int describeContents() {
  230.             return 0;
  231.         }
  232.  
  233.         @Override
  234.         public void writeToParcel(Parcel dest, int flags) {
  235.             dest.writeString(this.title);
  236.             dest.writeString(this.message);
  237.             dest.writeInt(this.okText);
  238.             dest.writeInt(this.noText);
  239.             dest.writeInt(this.cancelable ? 1 : 0);
  240.             dest.writeInt(this.style);
  241.         }
  242.  
  243.         public static Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {
  244.             public Config createFromParcel(Parcel source) {
  245.                 return new Config(source);
  246.             }
  247.  
  248.             public Config[] newArray(int size) {
  249.                 return new Config[size];
  250.             }
  251.         };
  252.     }
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement