Created
January 20, 2016 14:39
-
-
Save mkonicek/dbd067490802d8bf44ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.facebook.catalyst.modules.dialog; | |
import javax.annotation.Nullable; | |
import java.util.Map; | |
import android.app.Activity; | |
import android.app.TimePickerDialog.OnTimeSetListener; | |
import android.content.DialogInterface; | |
import android.content.DialogInterface.OnDismissListener; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.app.FragmentManager; | |
import android.widget.TimePicker; | |
import com.facebook.react.bridge.Callback; | |
import com.facebook.react.bridge.NativeModule; | |
import com.facebook.react.bridge.ReactApplicationContext; | |
import com.facebook.react.bridge.ReactContextBaseJavaModule; | |
import com.facebook.react.bridge.ReactMethod; | |
import com.facebook.react.bridge.ReadableMap; | |
import com.facebook.react.common.MapBuilder; | |
import com.facebook.react.common.annotations.VisibleForTesting; | |
/** | |
* {@link NativeModule} that allows JS to show a native time picker dialog and get called back when | |
* the user selects a time. | |
*/ | |
public class TimePickerDialogModule extends ReactContextBaseJavaModule { | |
@VisibleForTesting | |
public static final String FRAGMENT_TAG = "TimePickerDialogAndroid"; | |
/* 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 ACTION_TIME_SET = "timeSetAction"; | |
/* package */ static final String ACTION_DISMISSED = "dismissedAction"; | |
private class TimePickerDialogListener implements OnTimeSetListener, OnDismissListener { | |
private final Callback mCallback; | |
private boolean mCallbackConsumed; | |
public TimePickerDialogListener(Callback callback) { | |
mCallback = callback; | |
} | |
@Override | |
public void onDismiss(DialogInterface dialog) { | |
if (!mCallbackConsumed && getReactApplicationContext().hasActiveCatalystInstance()) { | |
mCallback.invoke(ACTION_DISMISSED); | |
mCallbackConsumed = true; | |
} | |
} | |
@Override | |
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { | |
if (!mCallbackConsumed && getReactApplicationContext().hasActiveCatalystInstance()) { | |
mCallback.invoke(ACTION_TIME_SET, hourOfDay, minute); | |
mCallbackConsumed = true; | |
} | |
} | |
} | |
public TimePickerDialogModule(ReactApplicationContext reactContext) { | |
super(reactContext); | |
} | |
@Override | |
public String getName() { | |
return "TimePickerDialogAndroid"; | |
} | |
@Override | |
public Map<String, Object> getConstants() { | |
final Map<String, Object> constants = MapBuilder.newHashMap(); | |
constants.put(ACTION_TIME_SET, ACTION_TIME_SET); | |
constants.put(ACTION_DISMISSED, ACTION_DISMISSED); | |
return constants; | |
} | |
@ReactMethod | |
public void open( | |
@Nullable final ReadableMap options, | |
@Nullable final Callback error, | |
@Nullable final Callback success) { | |
Activity activity = getCurrentActivity(); | |
if (activity == null || !(activity instanceof FragmentActivity)) { | |
if (error != null) { | |
error.invoke("Tried to open datepicker dialog while not attached to a FragmentActivity"); | |
} | |
return; | |
} | |
FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager(); | |
final TimePickerDialogFragment fragment = new TimePickerDialogFragment(); | |
if (success != null) { | |
final TimePickerDialogListener listener = new TimePickerDialogListener(success); | |
fragment.setOnTimeSetListener(listener); | |
fragment.setOnDismissListener(listener); | |
} | |
if (options != null) { | |
final Bundle args = new Bundle(); | |
if (options.hasKey(ARG_HOUR) && !options.isNull(ARG_HOUR)) { | |
args.putInt(ARG_HOUR, options.getInt(ARG_HOUR)); | |
} | |
if (options.hasKey(ARG_MINUTE) && !options.isNull(ARG_MINUTE)) { | |
args.putInt(ARG_MINUTE, options.getInt(ARG_MINUTE)); | |
} | |
if (options.hasKey(ARG_IS24HOUR) && !options.isNull(ARG_IS24HOUR)) { | |
args.putBoolean(ARG_IS24HOUR, options.getBoolean(ARG_IS24HOUR)); | |
} | |
fragment.setArguments(args); | |
} | |
fragment.show(fragmentManager, FRAGMENT_TAG); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment