2
2
3
3
import android .content .Context ;
4
4
import android .os .Bundle ;
5
+ import android .text .Editable ;
6
+ import android .text .TextWatcher ;
7
+ import android .view .KeyEvent ;
5
8
import android .view .LayoutInflater ;
6
9
import android .view .View ;
10
+ import android .widget .AdapterView ;
7
11
import android .widget .ArrayAdapter ;
12
+ import android .widget .EditText ;
8
13
import android .widget .LinearLayout ;
9
14
import android .widget .Spinner ;
15
+ import android .widget .TextView ;
10
16
11
17
import androidx .preference .PreferenceDialogFragmentCompat ;
12
18
13
19
import org .andstatus .todoagenda .R ;
20
+ import org .andstatus .todoagenda .prefs .AllSettings ;
21
+ import org .andstatus .todoagenda .prefs .ApplicationPreferences ;
22
+ import org .andstatus .todoagenda .prefs .InstanceSettings ;
14
23
15
- public class DateFormatDialog extends PreferenceDialogFragmentCompat {
24
+ import java .text .ParseException ;
25
+ import java .text .SimpleDateFormat ;
26
+ import java .util .Date ;
27
+ import java .util .Locale ;
28
+
29
+ /**
30
+
31
+ */
32
+ public class DateFormatDialog extends PreferenceDialogFragmentCompat implements AdapterView .OnItemSelectedListener , View .OnKeyListener , TextWatcher {
16
33
private final DateFormatPreference preference ;
17
- private LinearLayout dialogView ;
18
34
private Spinner typeSpinner ;
35
+ private EditText customPatternText ;
36
+ private EditText sampleDateText ;
37
+ private TextView resultText ;
38
+ private DateFormatValue sampleDateFormatValue = DateFormatValue .of (DateFormatType .CUSTOM , "yyyy-MM-dd" );
19
39
20
40
public DateFormatDialog (DateFormatPreference preference ) {
21
41
this .preference = preference ;
@@ -27,18 +47,67 @@ public DateFormatDialog(DateFormatPreference preference) {
27
47
28
48
@ Override
29
49
protected View onCreateDialogView (Context context ) {
30
- dialogView = (LinearLayout ) LayoutInflater .from (context ).inflate (R .layout .dateformat_preference , null );
50
+ LinearLayout dialogView = (LinearLayout ) LayoutInflater .from (context ).inflate (R .layout .dateformat_preference , null );
31
51
32
52
typeSpinner = dialogView .findViewById (R .id .date_format_type );
33
53
ArrayAdapter <CharSequence > adapter = new ArrayAdapter <>(
34
54
context , android .R .layout .simple_spinner_item , DateFormatType .getSpinnerEntryList (context ));
35
55
adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
36
56
typeSpinner .setAdapter (adapter );
37
57
typeSpinner .setSelection (preference .getValue ().type .getSpinnerPosition ());
58
+ typeSpinner .setOnItemSelectedListener (this );
59
+
60
+ customPatternText = dialogView .findViewById (R .id .custom_pattern );
61
+ customPatternText .setText (preference .getValue ().getPattern ());
62
+ customPatternText .addTextChangedListener (this );
63
+
64
+ sampleDateText = dialogView .findViewById (R .id .sample_date );
65
+ sampleDateText .setText (getSampleDateText ());
66
+ sampleDateText .addTextChangedListener (this );
67
+
68
+ resultText = dialogView .findViewById (R .id .result );
38
69
39
70
return dialogView ;
40
71
}
41
72
73
+ private CharSequence getSampleDateText () {
74
+ return new DateFormatter (getContext (), sampleDateFormatValue , getSettings ().clock ().now ())
75
+ .formatMillis (getSettings ().clock ().now ().getMillis ());
76
+ }
77
+
78
+ @ Override
79
+ public void onResume () {
80
+ super .onResume ();
81
+ calcResult ();
82
+ }
83
+
84
+ // Two methods to listen for the Spinner changes
85
+ @ Override
86
+ public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
87
+ if (getValue ().hasPattern ()) {
88
+ customPatternText .setText (getValue ().getPattern ());
89
+ }
90
+ calcResult ();
91
+ }
92
+ @ Override
93
+ public void onNothingSelected (AdapterView <?> parent ) {
94
+ calcResult ();
95
+ }
96
+
97
+ // Four methods to listen to the Custom pattern text changes
98
+ @ Override
99
+ public void beforeTextChanged (CharSequence s , int start , int count , int after ) { }
100
+ @ Override
101
+ public void onTextChanged (CharSequence s , int start , int before , int count ) { }
102
+ @ Override
103
+ public void afterTextChanged (Editable s ) {
104
+ calcResult ();
105
+ }
106
+ @ Override
107
+ public boolean onKey (View v , int keyCode , KeyEvent event ) {
108
+ return false ;
109
+ }
110
+
42
111
@ Override
43
112
public void onDialogClosed (boolean positiveResult ) {
44
113
if (positiveResult ) {
@@ -51,6 +120,32 @@ public void onDialogClosed(boolean positiveResult) {
51
120
52
121
private DateFormatValue getValue () {
53
122
int position = typeSpinner .getSelectedItemPosition ();
54
- return position >= 0 ? DateFormatType .values ()[position ].defaultValue () : DateFormatType .UNKNOWN .defaultValue ();
123
+ return position >= 0
124
+ ? DateFormatValue .of (DateFormatType .values ()[position ], customPatternText .getText ().toString ())
125
+ : DateFormatType .UNKNOWN .defaultValue ();
126
+ }
127
+
128
+ private void calcResult () {
129
+ SimpleDateFormat sampleFormat = getSampleDateFormat ();
130
+ CharSequence result ;
131
+ try {
132
+ Date sampleDate = sampleFormat .parse (sampleDateText .getText ().toString ());
133
+ result = sampleDate == null
134
+ ? "null"
135
+ : new DateFormatter (this .getContext (), getValue (), getSettings ().clock ().now ())
136
+ .formatMillis (sampleDate .getTime ());
137
+ } catch (ParseException e ) {
138
+ result = e .getLocalizedMessage ();
139
+ }
140
+ resultText .setText (result );
141
+ }
142
+
143
+ private SimpleDateFormat getSampleDateFormat () {
144
+ return new SimpleDateFormat (sampleDateFormatValue .getPattern (), Locale .ENGLISH );
145
+ }
146
+
147
+ private InstanceSettings getSettings () {
148
+ int widgetId = ApplicationPreferences .getWidgetId (getActivity ());
149
+ return AllSettings .instanceFromId (getActivity (), widgetId );
55
150
}
56
151
}
0 commit comments