Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. package com.pxl.pxlbeamapp.util;
  2.  
  3. import android.content.Context;
  4. import android.content.res.Configuration;
  5. import android.support.v7.app.AlertDialog;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.view.WindowManager;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13.  
  14. import com.pxl.pxlbeamapp.R;
  15. import com.pxl.pxlbeamapp.util.view.ViewUtils;
  16.  
  17. public class IOSAlertDialog {
  18. private static final int THREE_RATIO = 3;
  19. private static final int TWO_RATIO = 2;
  20.  
  21. private AlertDialog dialog;
  22.  
  23. private String title;
  24. private String message;
  25. private String leftButtonText;
  26. private String rightButtonText;
  27. private Runnable leftButtonListener;
  28. private Runnable rightButtonListener;
  29. private boolean isCancelable;
  30.  
  31.  
  32. public static Builder newBuilder(Context context) {
  33. return new IOSAlertDialog().new Builder(context);
  34. }
  35.  
  36. public void show() {
  37. if (dialog != null && dialog.getWindow() != null) {
  38. int customDialogWidth;
  39. if (dialog.getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  40. customDialogWidth = (ViewUtils.getScreenWidth(dialog.getWindow()) / THREE_RATIO) * TWO_RATIO;
  41. } else {
  42. customDialogWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
  43. }
  44. dialog.show();
  45. dialog.getWindow().setLayout(customDialogWidth, WindowManager.LayoutParams.WRAP_CONTENT);
  46. }
  47. }
  48.  
  49. @SuppressWarnings("unused")
  50. public class Builder {
  51. private Context context;
  52.  
  53. private Builder(Context context) {
  54. //clear fields
  55. IOSAlertDialog.this.isCancelable = true;
  56. IOSAlertDialog.this.title = null;
  57. IOSAlertDialog.this.message = null;
  58. IOSAlertDialog.this.leftButtonText = null;
  59. IOSAlertDialog.this.rightButtonText = null;
  60. IOSAlertDialog.this.leftButtonListener = null;
  61. IOSAlertDialog.this.rightButtonListener = null;
  62. IOSAlertDialog.this.dialog = null;
  63. this.context = context;
  64. }
  65.  
  66. public Builder addTitle(String title) {
  67. IOSAlertDialog.this.title = title;
  68. return this;
  69. }
  70.  
  71. public Builder setCancelable(boolean isCancelable) {
  72. IOSAlertDialog.this.isCancelable = isCancelable;
  73. return this;
  74. }
  75.  
  76. public Builder addMessage(String message) {
  77. IOSAlertDialog.this.message = message;
  78. return this;
  79. }
  80.  
  81. public Builder addLeftButtonText(String leftButtonText) {
  82. IOSAlertDialog.this.leftButtonText = leftButtonText;
  83. return this;
  84. }
  85.  
  86. public Builder addRightButtonText(String rightButtonText) {
  87. IOSAlertDialog.this.rightButtonText = rightButtonText;
  88. return this;
  89. }
  90.  
  91. public Builder addLeftButtonListener(Runnable leftListener) {
  92. IOSAlertDialog.this.leftButtonListener = leftListener;
  93. return this;
  94. }
  95.  
  96. public Builder addRightButtonListener(Runnable rightListener) {
  97. IOSAlertDialog.this.rightButtonListener = rightListener;
  98. return this;
  99. }
  100.  
  101. public IOSAlertDialog build() {
  102. AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.CustomAlertDialog);
  103. //Get alert view
  104. View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog, null);
  105.  
  106. //Get child views
  107. Button leftBtn = view.findViewById(R.id.btn_left);
  108. Button rightBtn = view.findViewById(R.id.btn_right);
  109. View delimiterVertical = view.findViewById(R.id.delimiter_vertical);
  110. TextView tvTitle = view.findViewById(R.id.tv_title);
  111. TextView tvMessage = view.findViewById(R.id.tv_message);
  112.  
  113. //Initialize child views if required
  114. tvTitle.setText(IOSAlertDialog.this.title == null ? "" : IOSAlertDialog.this.title);
  115. tvMessage.setText(IOSAlertDialog.this.message == null ? "" : IOSAlertDialog.this.message);
  116. leftBtn.setText(IOSAlertDialog.this.leftButtonText == null ? "" : IOSAlertDialog.this.leftButtonText);
  117. rightBtn.setText(IOSAlertDialog.this.rightButtonText == null ? "" : IOSAlertDialog.this.rightButtonText);
  118. leftBtn.setOnClickListener(v -> processClick(leftButtonListener));
  119. rightBtn.setOnClickListener(v -> processClick(rightButtonListener));
  120.  
  121. //adopt ui for two buttons style or one button style
  122. boolean isOneButtonStyle = IOSAlertDialog.this.rightButtonText == null && IOSAlertDialog.this.rightButtonListener == null;
  123. int visibility = isOneButtonStyle ? View.GONE : View.VISIBLE;
  124. delimiterVertical.setVisibility(visibility);
  125. rightBtn.setVisibility(visibility);
  126.  
  127. builder.setView(view);
  128. builder.setCancelable(IOSAlertDialog.this.isCancelable);
  129.  
  130. //rebuild
  131. dialog = builder.create();
  132.  
  133. return IOSAlertDialog.this;
  134. }
  135.  
  136. private void processClick(Runnable runnable) {
  137. if (dialog != null && dialog.isShowing()) {
  138. dialog.dismiss();
  139. } else {
  140. Log.e(IOSAlertDialog.class.getSimpleName(), "Dialog is null or was already dismissed)");
  141. }
  142. if (runnable != null) {
  143. runnable.run();
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement