Skip to content

Commit 82d0ff6

Browse files
author
jarzul
committed
Refact:(Simple Dialog) Uses SimpleDialogContent inside the SimpleDialogFragment
1 parent 11baf7e commit 82d0ff6

File tree

1 file changed

+29
-70
lines changed

1 file changed

+29
-70
lines changed

library/src/main/java/com/julienarzul/simpledialogfragment/SimpleDialogFragment.java

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -22,61 +22,14 @@ public class SimpleDialogFragment extends DialogFragment implements DialogInterf
2222

2323
private static final Integer DEFAULT_REQUEST_CODE = 0;
2424

25-
private static final String TITLE_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.title";
25+
private static final String DIALOG_CONTENT_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.dialogContent";
2626

27-
private static final String MESSAGE_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.message";
28-
29-
private static final String POSITIVE_BUTTON_TEXT_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.positiveButtonText";
30-
31-
private static final String NEGATIVE_BUTTON_TEXT_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.negativeButtonText";
32-
33-
private static final String REQUEST_CODE_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.requestCode";
34-
35-
private static final String CANCELABLE_BUNDLE_KEY = "com.julienarzul.simpledialogfragment.SimpleDialogFragment.cancelable";
36-
37-
private String title = null;
38-
39-
private String message = null;
40-
41-
private String positiveButtonText = null;
42-
43-
private String negativeButtonText = null;
44-
45-
private Integer requestCode = null;
27+
private SimpleDialogContent dialogContent = null;
4628

4729
public static SimpleDialogFragment newInstance(SimpleDialogContent dialogContent) {
4830
Bundle args = new Bundle();
4931

50-
if (dialogContent != null) {
51-
String title = dialogContent.title();
52-
if (!TextUtils.isEmpty(title)) {
53-
args.putString(TITLE_BUNDLE_KEY, title);
54-
}
55-
56-
String message = dialogContent.message();
57-
if (!TextUtils.isEmpty(message)) {
58-
args.putString(MESSAGE_BUNDLE_KEY, message);
59-
}
60-
61-
String positiveButtonText = dialogContent.positiveButtonText();
62-
if (!TextUtils.isEmpty(positiveButtonText)) {
63-
args.putString(POSITIVE_BUTTON_TEXT_BUNDLE_KEY, positiveButtonText);
64-
}
65-
66-
String negativeButtonText = dialogContent.negativeButtonText();
67-
if (!TextUtils.isEmpty(negativeButtonText)) {
68-
args.putString(NEGATIVE_BUTTON_TEXT_BUNDLE_KEY, negativeButtonText);
69-
}
70-
71-
Integer requestCode = dialogContent.requestCode();
72-
if (requestCode == null) {
73-
requestCode = DEFAULT_REQUEST_CODE;
74-
}
75-
args.putInt(REQUEST_CODE_BUNDLE_KEY, requestCode);
76-
77-
boolean cancelable = dialogContent.cancelable();
78-
args.putBoolean(CANCELABLE_BUNDLE_KEY, cancelable);
79-
}
32+
args.putParcelable(DIALOG_CONTENT_BUNDLE_KEY, dialogContent);
8033

8134
SimpleDialogFragment fragment = new SimpleDialogFragment();
8235
fragment.setArguments(args);
@@ -89,15 +42,11 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
8942

9043
Bundle arguments = getArguments();
9144
if (arguments != null) {
92-
this.title = arguments.getString(TITLE_BUNDLE_KEY);
93-
this.message = arguments.getString(MESSAGE_BUNDLE_KEY);
94-
this.positiveButtonText = arguments.getString(POSITIVE_BUTTON_TEXT_BUNDLE_KEY);
95-
this.negativeButtonText = arguments.getString(NEGATIVE_BUTTON_TEXT_BUNDLE_KEY, null);
96-
if (arguments.containsKey(REQUEST_CODE_BUNDLE_KEY)) {
97-
this.requestCode = arguments.getInt(REQUEST_CODE_BUNDLE_KEY);
98-
}
45+
this.dialogContent = arguments.getParcelable(DIALOG_CONTENT_BUNDLE_KEY);
9946

100-
this.setCancelable(arguments.getBoolean(CANCELABLE_BUNDLE_KEY, true));
47+
if (this.dialogContent != null) {
48+
this.setCancelable(this.dialogContent.cancelable());
49+
}
10150
}
10251
}
10352

@@ -106,21 +55,27 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
10655
public Dialog onCreateDialog(Bundle savedInstanceState) {
10756
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
10857

109-
if (!TextUtils.isEmpty(this.title)) {
110-
builder.setTitle(this.title);
58+
String title = null, message = null, positiveButtonText = null, negativeButtonText = null;
59+
boolean cancelable = true;
60+
if (this.dialogContent != null) {
61+
title = this.dialogContent.title();
62+
message = this.dialogContent.message();
63+
positiveButtonText = this.dialogContent.positiveButtonText();
64+
negativeButtonText = this.dialogContent.negativeButtonText();
11165
}
11266

113-
if (!TextUtils.isEmpty(this.message)) {
114-
builder.setMessage(this.message);
67+
if (!TextUtils.isEmpty(title)) {
68+
builder.setTitle(title);
11569
}
116-
117-
if (TextUtils.isEmpty(this.positiveButtonText)) {
118-
this.positiveButtonText = getString(android.R.string.ok);
70+
if (!TextUtils.isEmpty(message)) {
71+
builder.setMessage(message);
11972
}
120-
builder.setPositiveButton(this.positiveButtonText, this);
121-
122-
if (!TextUtils.isEmpty(this.negativeButtonText)) {
123-
builder.setNegativeButton(this.negativeButtonText, this);
73+
if (TextUtils.isEmpty(positiveButtonText)) {
74+
positiveButtonText = getString(android.R.string.ok);
75+
}
76+
builder.setPositiveButton(positiveButtonText, this);
77+
if (!TextUtils.isEmpty(negativeButtonText)) {
78+
builder.setNegativeButton(negativeButtonText, this);
12479
}
12580

12681
return builder.create();
@@ -155,7 +110,11 @@ private Pair<SimpleDialogFragmentListener, Integer> getListener() {
155110

156111
FragmentActivity activity = this.getActivity();
157112
if (activity instanceof SimpleDialogFragmentListener) {
158-
return new Pair<>((SimpleDialogFragmentListener) activity, this.requestCode);
113+
Integer requestCode = DEFAULT_REQUEST_CODE;
114+
if (this.dialogContent != null) {
115+
requestCode = this.dialogContent.requestCode();
116+
}
117+
return new Pair<>((SimpleDialogFragmentListener) activity, requestCode);
159118
}
160119

161120
return null;

0 commit comments

Comments
 (0)