Add TimePicker modes

Summary:
In the spirit of #10932, I added the `mode` option to the `TimePicker` Android API.
There is only one mode available for **Android < 5**, the `spinner` one.
If we are on **Android >= 5** we can choose between `spinner` or `clock`. If we specify `default` it will use the default of the current Android version.

On **Android < 5**, whatever we choose it will be this:
![screen shot 2017-02-14 at 17 05 44](https://cloud.githubusercontent.com/assets/5436545/22937805/024ec67e-f2da-11e6-8b32-a680d9bc2247.png)

On **Android >= 5**, with the `spinner` mode:
![screen shot 2017-02-14 at 16 51 17](https://cloud.githubusercontent.com/assets/5436545/22937803/024e0bbc-f2da-11e6-9f4b-26102ff2eeac.png)

And with the `clock` mode, the default:
![screen shot 2017-02-14 at 16 51 02](https://cloud.githubusercontent.com/assets/5436545/22937804/024e64e0-f2da-11e6-9911-4135049f4726.png)
Closes https://github.com/facebook/react-native/pull/12384

Differential Revision: D6006689

Pulled By: hramos

fbshipit-source-id: fcd37c867c4061b9982b1687f2c10211e54df7cf
This commit is contained in:
Yann Pringault
2017-10-08 12:28:10 -07:00
committed by Facebook Github Bot
parent f9be64aea0
commit 1c24440644
6 changed files with 112 additions and 13 deletions

View File

@@ -9,19 +9,22 @@
package com.facebook.react.modules.timepicker;
import javax.annotation.Nullable;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import javax.annotation.Nullable;
@SuppressWarnings("ValidFragment")
public class TimePickerDialogFragment extends DialogFragment {
@@ -44,6 +47,11 @@ public class TimePickerDialogFragment extends DialogFragment {
int minute = now.get(Calendar.MINUTE);
boolean is24hour = DateFormat.is24HourFormat(activityContext);
TimePickerMode mode = TimePickerMode.DEFAULT;
if (args != null && args.getString(TimePickerDialogModule.ARG_MODE, null) != null) {
mode = TimePickerMode.valueOf(args.getString(TimePickerDialogModule.ARG_MODE).toUpperCase(Locale.US));
}
if (args != null) {
hour = args.getInt(TimePickerDialogModule.ARG_HOUR, now.get(Calendar.HOUR_OF_DAY));
minute = args.getInt(TimePickerDialogModule.ARG_MINUTE, now.get(Calendar.MINUTE));
@@ -52,12 +60,42 @@ public class TimePickerDialogFragment extends DialogFragment {
DateFormat.is24HourFormat(activityContext));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mode == TimePickerMode.CLOCK) {
return new DismissableTimePickerDialog(
activityContext,
activityContext.getResources().getIdentifier(
"ClockTimePickerDialog",
"style",
activityContext.getPackageName()
),
onTimeSetListener,
hour,
minute,
is24hour
);
} else if (mode == TimePickerMode.SPINNER) {
return new DismissableTimePickerDialog(
activityContext,
activityContext.getResources().getIdentifier(
"SpinnerTimePickerDialog",
"style",
activityContext.getPackageName()
),
onTimeSetListener,
hour,
minute,
is24hour
);
}
}
return new DismissableTimePickerDialog(
activityContext,
onTimeSetListener,
hour,
minute,
is24hour);
activityContext,
onTimeSetListener,
hour,
minute,
is24hour
);
}
@Override

View File

@@ -9,8 +9,6 @@
package com.facebook.react.modules.timepicker;
import javax.annotation.Nullable;
import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
@@ -31,6 +29,8 @@ import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.module.annotations.ReactModule;
import javax.annotation.Nullable;
/**
* {@link NativeModule} that allows JS to show a native time picker dialog and get called back when
* the user selects a time.
@@ -46,6 +46,7 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
/* package */ static final String ARG_HOUR = "hour";
/* package */ static final String ARG_MINUTE = "minute";
/* package */ static final String ARG_IS24HOUR = "is24Hour";
/* package */ static final String ARG_MODE = "mode";
/* package */ static final String ACTION_TIME_SET = "timeSetAction";
/* package */ static final String ACTION_DISMISSED = "dismissedAction";
@@ -148,6 +149,9 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
if (options.hasKey(ARG_IS24HOUR) && !options.isNull(ARG_IS24HOUR)) {
args.putBoolean(ARG_IS24HOUR, options.getBoolean(ARG_IS24HOUR));
}
if (options.hasKey(ARG_MODE) && !options.isNull(ARG_MODE)) {
args.putString(ARG_MODE, options.getString(ARG_MODE));
}
return args;
}
}

View File

@@ -0,0 +1,17 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.modules.timepicker;
public enum TimePickerMode {
CLOCK,
SPINNER,
DEFAULT
}