@@ -22,61 +22,14 @@ public class SimpleDialogFragment extends DialogFragment implements DialogInterf
22
22
23
23
private static final Integer DEFAULT_REQUEST_CODE = 0 ;
24
24
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 " ;
26
26
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 ;
46
28
47
29
public static SimpleDialogFragment newInstance (SimpleDialogContent dialogContent ) {
48
30
Bundle args = new Bundle ();
49
31
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 );
80
33
81
34
SimpleDialogFragment fragment = new SimpleDialogFragment ();
82
35
fragment .setArguments (args );
@@ -89,15 +42,11 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
89
42
90
43
Bundle arguments = getArguments ();
91
44
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 );
99
46
100
- this .setCancelable (arguments .getBoolean (CANCELABLE_BUNDLE_KEY , true ));
47
+ if (this .dialogContent != null ) {
48
+ this .setCancelable (this .dialogContent .cancelable ());
49
+ }
101
50
}
102
51
}
103
52
@@ -106,21 +55,27 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
106
55
public Dialog onCreateDialog (Bundle savedInstanceState ) {
107
56
AlertDialog .Builder builder = new AlertDialog .Builder (getContext ());
108
57
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 ();
111
65
}
112
66
113
- if (!TextUtils .isEmpty (this . message )) {
114
- builder .setMessage ( this . message );
67
+ if (!TextUtils .isEmpty (title )) {
68
+ builder .setTitle ( title );
115
69
}
116
-
117
- if (TextUtils .isEmpty (this .positiveButtonText )) {
118
- this .positiveButtonText = getString (android .R .string .ok );
70
+ if (!TextUtils .isEmpty (message )) {
71
+ builder .setMessage (message );
119
72
}
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 );
124
79
}
125
80
126
81
return builder .create ();
@@ -155,7 +110,11 @@ private Pair<SimpleDialogFragmentListener, Integer> getListener() {
155
110
156
111
FragmentActivity activity = this .getActivity ();
157
112
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 );
159
118
}
160
119
161
120
return null ;
0 commit comments