2
2
3
3
import android .app .Dialog ;
4
4
import android .os .Bundle ;
5
+ import android .support .annotation .DrawableRes ;
6
+ import android .support .annotation .NonNull ;
7
+ import android .support .annotation .StringRes ;
8
+ import android .support .v7 .app .AppCompatDialog ;
5
9
import android .support .v7 .app .AppCompatDialogFragment ;
6
10
import android .view .LayoutInflater ;
7
11
import android .view .View ;
8
12
import android .view .ViewGroup ;
9
13
import android .view .Window ;
10
14
import android .widget .Button ;
11
15
import android .widget .ImageView ;
16
+ import android .widget .LinearLayout ;
12
17
13
18
import org .wordpress .android .R ;
19
+ import org .wordpress .android .util .DisplayUtils ;
14
20
import org .wordpress .android .widgets .WPTextView ;
15
21
16
22
public class PromoDialog extends AppCompatDialogFragment {
17
- protected int mButtonPositiveId ;
18
- protected int mDescriptionId ;
19
- protected int mDrawableId ;
20
- protected int mTitleId ;
23
+ protected static final String KEY_DRAWABLE_RES_ID = "drawableResId" ;
24
+ protected static final String KEY_TITLE_RES_ID = "titleResId" ;
25
+ protected static final String KEY_DESCRIPTION_RES_ID = "descriptionResId" ;
26
+ protected static final String KEY_BUTTON_POSITIVE_RES_ID = "buttonPositiveResId" ;
21
27
22
- public static PromoDialog newInstance (int drawableId , int titleId , int descriptionId , int buttonPositiveId ) {
23
- PromoDialog fragment = new PromoDialog ();
24
- Bundle args = new Bundle ();
25
- args .putInt ("drawableId" , drawableId );
26
- args .putInt ("titleId" , titleId );
27
- args .putInt ("descriptionId" , descriptionId );
28
- args .putInt ("buttonPositiveId" , buttonPositiveId );
29
- fragment .setArguments (args );
30
- return fragment ;
28
+ public static class Builder {
29
+ @ StringRes int buttonPositiveResId ;
30
+ @ StringRes int descriptionResId ;
31
+ @ DrawableRes int drawableResId ;
32
+ @ StringRes int titleResId ;
33
+
34
+ public Builder (@ DrawableRes int drawableResId , @ StringRes int titleResId , @ StringRes int descriptionResId ,
35
+ @ StringRes int buttonPositiveResId ) {
36
+ this .drawableResId = drawableResId ;
37
+ this .titleResId = titleResId ;
38
+ this .descriptionResId = descriptionResId ;
39
+ this .buttonPositiveResId = buttonPositiveResId ;
40
+ }
41
+
42
+ public PromoDialog build () {
43
+ PromoDialog fragment = new PromoDialog ();
44
+ Bundle args = new Bundle ();
45
+ args .putInt (KEY_DRAWABLE_RES_ID , drawableResId );
46
+ args .putInt (KEY_TITLE_RES_ID , titleResId );
47
+ args .putInt (KEY_DESCRIPTION_RES_ID , descriptionResId );
48
+ args .putInt (KEY_BUTTON_POSITIVE_RES_ID , buttonPositiveResId );
49
+ fragment .setArguments (args );
50
+ return fragment ;
51
+ }
31
52
}
32
53
54
+ @ StringRes protected int mButtonPositiveResId ;
55
+ @ StringRes protected int mDescriptionResId ;
56
+ @ DrawableRes protected int mDrawableResId ;
57
+ @ StringRes protected int mTitleResId ;
58
+
59
+ protected View .OnClickListener mPositiveButtonOnClickListener ;
60
+
33
61
@ Override
34
- public Dialog onCreateDialog (Bundle savedInstanceState ) {
62
+ public @ NonNull Dialog onCreateDialog (Bundle savedInstanceState ) {
35
63
Dialog dialog = super .onCreateDialog (savedInstanceState );
36
- mDrawableId = getArguments ().getInt ("drawableId" );
37
- mTitleId = getArguments ().getInt ("titleId" );
38
- mDescriptionId = getArguments ().getInt ("descriptionId" );
39
- mButtonPositiveId = getArguments ().getInt ("buttonPositiveId" );
40
- // request a window without the title
41
- dialog .getWindow ().requestFeature (Window .FEATURE_NO_TITLE );
64
+ mDrawableResId = getArguments ().getInt (KEY_DRAWABLE_RES_ID );
65
+ mTitleResId = getArguments ().getInt (KEY_TITLE_RES_ID );
66
+ mDescriptionResId = getArguments ().getInt (KEY_DESCRIPTION_RES_ID );
67
+ mButtonPositiveResId = getArguments ().getInt (KEY_BUTTON_POSITIVE_RES_ID );
42
68
dialog .setCanceledOnTouchOutside (false );
43
69
dialog .setCancelable (false );
44
70
return dialog ;
45
71
}
46
72
73
+ @ Override
74
+ public void setupDialog (Dialog dialog , int style ) {
75
+ ((AppCompatDialog ) dialog ).supportRequestWindowFeature (Window .FEATURE_NO_TITLE );
76
+ }
77
+
47
78
@ Override
48
79
public View onCreateView (LayoutInflater inflater , ViewGroup container ,
49
80
Bundle savedInstanceState ) {
@@ -53,19 +84,33 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
53
84
@ Override
54
85
public void onViewCreated (View view , Bundle savedInstanceState ) {
55
86
super .onViewCreated (view , savedInstanceState );
56
- Button btn = (Button ) view .findViewById (R .id .promo_dialog_button_positive );
57
- btn .setText (mButtonPositiveId );
58
87
ImageView image = (ImageView ) view .findViewById (R .id .promo_dialog_image );
59
- image .setImageResource (mDrawableId );
88
+ image .setImageResource (mDrawableResId );
60
89
WPTextView title = (WPTextView ) view .findViewById (R .id .promo_dialog_title );
61
- title .setText (mTitleId );
90
+ title .setText (mTitleResId );
62
91
WPTextView desc = (WPTextView ) view .findViewById (R .id .promo_dialog_description );
63
- desc .setText (mDescriptionId );
64
- btn .setOnClickListener (new View .OnClickListener () {
65
- @ Override
66
- public void onClick (View v ) {
67
- getDialog ().cancel ();
68
- }
69
- });
92
+ desc .setText (mDescriptionResId );
93
+
94
+ Button btn = (Button ) view .findViewById (R .id .promo_dialog_button_positive );
95
+ btn .setText (mButtonPositiveResId );
96
+ if (mPositiveButtonOnClickListener == null ) {
97
+ btn .setOnClickListener (new View .OnClickListener () {
98
+ @ Override
99
+ public void onClick (View v ) {
100
+ getDialog ().cancel ();
101
+ }
102
+ });
103
+ } else {
104
+ btn .setOnClickListener (mPositiveButtonOnClickListener );
105
+ }
106
+ }
107
+
108
+ public void setPositiveButtonOnClickListener (View .OnClickListener listener ) {
109
+ mPositiveButtonOnClickListener = listener ;
110
+ }
111
+
112
+ public void redrawForOrientationChange () {
113
+ LinearLayout imageContainer = (LinearLayout ) getView ().findViewById (R .id .promo_dialog_image_container );
114
+ imageContainer .setVisibility (DisplayUtils .isLandscape (getActivity ()) ? View .GONE : View .VISIBLE );
70
115
}
71
116
}
0 commit comments