@@ -76,7 +76,7 @@ private boolean isSpinner() {
76
76
* @return returns 'real' minutes (0-59)
77
77
*/
78
78
private int getRealMinutes (int minutesOrSpinnerIndex ) {
79
- if (mDisplay == RNTimePickerDisplay . SPINNER ) {
79
+ if (isSpinner () ) {
80
80
return minutesOrSpinnerIndex * mTimePickerInterval ;
81
81
}
82
82
@@ -147,27 +147,42 @@ private void correctEnteredMinutes(final TimePicker view, final int hourOfDay, f
147
147
runnable = new Runnable () {
148
148
@ Override
149
149
public void run () {
150
- // set valid hour & minutes
151
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
152
- view .setHour (hourOfDay );
153
- view .setMinute (correctedMinutes );
154
- } else {
155
- view .setCurrentHour (hourOfDay );
156
- // we need to set minutes to 0 for this to work on older android devices
157
- view .setCurrentMinute (0 );
158
- view .setCurrentMinute (correctedMinutes );
159
- }
160
- if (pickerIsInTextInputMode ()) {
161
- // move caret to the end of input
162
- View maybeTextInput = view .findFocus ();
163
- if (maybeTextInput instanceof EditText ) {
164
- final EditText textInput = (EditText ) maybeTextInput ;
165
- textInput .setSelection (textInput .getText ().length ());
166
- } else {
167
- Log .e ("RN-datetimepicker" , "could not set selection on time picker, this is a known issue on some Huawei devices" );
168
- }
169
- }
150
+ if (pickerIsInTextInputMode ()) {
151
+ // only rewrite input when the value makes sense to be corrected
152
+ // eg. given interval 3, when user wants to enter 53
153
+ // we don't rewrite the first number "5" to 6, because it would be confusing
154
+ // but the value will be corrected in onTimeChanged()
155
+ // however, when they enter 10, we rewrite it to 9
156
+ boolean canRewriteTextInput = correctedMinutes > 5 ;
157
+ if (!canRewriteTextInput ) {
158
+ return ;
159
+ }
160
+ fixTime ();
161
+ moveCursorToEnd ();
162
+ } else {
163
+ fixTime ();
164
+ }
170
165
}
166
+ private void fixTime () {
167
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
168
+ view .setHour (hourOfDay );
169
+ view .setMinute (correctedMinutes );
170
+ } else {
171
+ view .setCurrentHour (hourOfDay );
172
+ // we need to set minutes to 0 first for this to work on older android devices
173
+ view .setCurrentMinute (0 );
174
+ view .setCurrentMinute (correctedMinutes );
175
+ }
176
+ }
177
+ private void moveCursorToEnd () {
178
+ View maybeTextInput = view .findFocus ();
179
+ if (maybeTextInput instanceof EditText ) {
180
+ final EditText textInput = (EditText ) maybeTextInput ;
181
+ textInput .setSelection (textInput .getText ().length ());
182
+ } else {
183
+ Log .e ("RN-datetimepicker" , "could not set selection on time picker, this is a known issue on some Huawei devices" );
184
+ }
185
+ }
171
186
};
172
187
173
188
handler .postDelayed (runnable , 500 );
@@ -191,15 +206,17 @@ public void onTimeChanged(final TimePicker view, final int hourOfDay, final int
191
206
192
207
@ Override
193
208
public void onClick (DialogInterface dialog , int which ) {
194
- if (mTimePicker != null && which == BUTTON_POSITIVE && timePickerHasCustomMinuteInterval ()) {
195
- final int hours = mTimePicker .getCurrentHour ();
196
-
197
- final int realMinutes = getRealMinutes ();
198
- int validMinutes = isSpinner () ? realMinutes : snapRealMinutesToInterval (realMinutes );
199
-
200
- if (mTimeSetListener != null ) {
201
- mTimeSetListener .onTimeSet (mTimePicker , hours , validMinutes );
202
- }
209
+ boolean needsCustomHandling = timePickerHasCustomMinuteInterval () || isSpinner ();
210
+ if (mTimePicker != null && which == BUTTON_POSITIVE && needsCustomHandling ) {
211
+ mTimePicker .clearFocus ();
212
+ final int hours = mTimePicker .getCurrentHour ();
213
+ int realMinutes = getRealMinutes ();
214
+ int reportedMinutes = timePickerHasCustomMinuteInterval ()
215
+ ? snapRealMinutesToInterval (realMinutes )
216
+ : realMinutes ;
217
+ if (mTimeSetListener != null ) {
218
+ mTimeSetListener .onTimeSet (mTimePicker , hours , reportedMinutes );
219
+ }
203
220
} else {
204
221
super .onClick (dialog , which );
205
222
}
@@ -228,15 +245,19 @@ public void updateTime(int hourOfDay, int minuteOfHour) {
228
245
public void onAttachedToWindow () {
229
246
super .onAttachedToWindow ();
230
247
248
+ int timePickerId = mContext .getResources ().getIdentifier ("timePicker" , "id" , "android" );
249
+ mTimePicker = this .findViewById (timePickerId );
250
+
231
251
if (timePickerHasCustomMinuteInterval ()) {
232
252
setupPickerDialog ();
233
253
}
234
254
}
235
255
236
256
private void setupPickerDialog () {
237
- int timePickerId = mContext .getResources ().getIdentifier ("timePicker" , "id" , "android" );
238
- mTimePicker = this .findViewById (timePickerId );
239
-
257
+ if (mTimePicker == null ) {
258
+ Log .e ("RN-datetimepicker" , "time picker was null" );
259
+ return ;
260
+ }
240
261
int realMinuteBackup = mTimePicker .getCurrentMinute ();
241
262
242
263
if (isSpinner ()) {
0 commit comments