Callback is working between EditAlarmActivity and NumberGridTimePickerDialog

This commit is contained in:
Phillip Hsu 2016-07-24 18:23:28 -07:00
parent e3f02d7aa3
commit 447014338e
5 changed files with 74 additions and 58 deletions

View File

@ -18,7 +18,21 @@ public abstract class BaseTimePickerDialog extends DialogFragment {
// TODO: Consider private access, and then writing package/protected API that subclasses // TODO: Consider private access, and then writing package/protected API that subclasses
// can use to interface with this field. // can use to interface with this field.
/*package*/ TimePicker.OnTimeSetListener mCallback; /*package*/ OnTimeSetListener mCallback;
/**
* The callback interface used to indicate the user is done filling in
* the time (they clicked on the 'Set' button).
*/
interface OnTimeSetListener {
/**
* @param viewGroup The view associated with this listener.
* @param hourOfDay The hour that was set.
* @param minute The minute that was set.
*/
// TODO: Consider removing VG param, since listeners probably won't need to use it....
void onTimeSet(ViewGroup viewGroup, int hourOfDay, int minute);
}
/** /**
* Empty constructor required for dialog fragment. * Empty constructor required for dialog fragment.
@ -29,10 +43,25 @@ public abstract class BaseTimePickerDialog extends DialogFragment {
@LayoutRes @LayoutRes
protected abstract int contentLayout(); protected abstract int contentLayout();
public final void setOnTimeSetListener(TimePicker.OnTimeSetListener callback) { public final void setOnTimeSetListener(OnTimeSetListener callback) {
mCallback = callback; mCallback = callback;
} }
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(contentLayout(), container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
// Code for AlertDialog style only. // Code for AlertDialog style only.
// @NonNull // @NonNull
// @Override // @Override
@ -92,19 +121,4 @@ public abstract class BaseTimePickerDialog extends DialogFragment {
// //
// return dialog; // return dialog;
// } // }
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(contentLayout(), container, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
} }

View File

@ -56,7 +56,7 @@ public class EditAlarmActivity extends BaseActivity implements
AlarmUtilsHelper, AlarmUtilsHelper,
SharedPreferencesHelper, SharedPreferencesHelper,
LoaderManager.LoaderCallbacks<Alarm>, LoaderManager.LoaderCallbacks<Alarm>,
NumpadTimePicker.OnTimeSetListener { BaseTimePickerDialog.OnTimeSetListener {
private static final String TAG = "EditAlarmActivity"; private static final String TAG = "EditAlarmActivity";
private static final String TAG_TIME_PICKER = "time_picker"; private static final String TAG_TIME_PICKER = "time_picker";
@ -108,7 +108,7 @@ public class EditAlarmActivity extends BaseActivity implements
// Are we recreating this Activity because of a rotation? // Are we recreating this Activity because of a rotation?
// If so, try finding the time picker in our backstack. // If so, try finding the time picker in our backstack.
NumpadTimePickerDialog picker = (NumpadTimePickerDialog) BaseTimePickerDialog picker = (BaseTimePickerDialog)
getSupportFragmentManager().findFragmentByTag(TAG_TIME_PICKER); getSupportFragmentManager().findFragmentByTag(TAG_TIME_PICKER);
if (picker != null) { if (picker != null) {
// Restore the callback // Restore the callback
@ -316,14 +316,20 @@ public class EditAlarmActivity extends BaseActivity implements
void openTimePicker() { void openTimePicker() {
// Close the keyboard first, or else our dialog will be screwed up. // Close the keyboard first, or else our dialog will be screwed up.
// If not open, this does nothing. // If not open, this does nothing.
hideKeyboard(this); hideKeyboard(this); // This is only important for BottomSheetDialogs!
// Create a new instance each time we want to show the dialog. // Create a new instance each time we want to show the dialog.
// If we keep a reference to the dialog, we keep its previous state as well. // If we keep a reference to the dialog, we keep its previous state as well.
// So the next time we call show() on it, the input field will show the // So the next time we call show() on it, the input field will show the
// last inputted time. // last inputted time.
// NumpadTimePickerDialog.newInstance(this).show(getSupportFragmentManager(), TAG_TIME_PICKER); // NumpadTimePickerDialog.newInstance(this).show(getSupportFragmentManager(), TAG_TIME_PICKER);
NumberGridTimePickerDialog.newInstance(NumberGridTimePickerDialog.HOUR_INDEX, NumberGridTimePickerDialog.HALF_DAY_1) // TODO: Read preferences to see what time picker style to show.
.show(getSupportFragmentManager(), TAG_TIME_PICKER); BaseTimePickerDialog dialog;
dialog = NumberGridTimePickerDialog.newInstance(
this, // OnTimeSetListener
0, // Initial hour of day
0, // Initial minute
DateFormat.is24HourFormat(this));
dialog.show(getSupportFragmentManager(), TAG_TIME_PICKER);
} }
private void setWeekDaysText() { private void setWeekDaysText() {

View File

@ -23,7 +23,6 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
import android.view.KeyCharacterMap; import android.view.KeyCharacterMap;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -83,10 +82,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
// Delay before starting the pulse animation, in ms. // Delay before starting the pulse animation, in ms.
private static final int PULSE_ANIMATOR_DELAY = 300; private static final int PULSE_ANIMATOR_DELAY = 300;
/** // private OnTimeSetListener mCallback;
* TODO: (Me) Use my OnTimeSetListener type.
*/
private OnTimeSetListener mCallback;
// private HapticFeedbackController mHapticFeedbackController; // private HapticFeedbackController mHapticFeedbackController;
@ -308,7 +304,11 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
Log.e(TAG, "TimePicker does not support button type " + v.getClass().getName()); Log.e(TAG, "TimePicker does not support button type " + v.getClass().getName());
return; return;
} }
onValueSelected(mCurrentIndex, Integer.parseInt(number), true); int value = Integer.parseInt(number);
if (mCurrentIndex == HOUR_INDEX && !mIs24HourMode && mSelectedHalfDay == HALF_DAY_2) {
value = (value % 12) + 12;
}
onValueSelected(mCurrentIndex, value, true);
} }
}; };
@ -345,24 +345,19 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
// ============================================================================================= // =============================================================================================
/** // /**
* The callback interface used to indicate the user is done filling in // * The callback interface used to indicate the user is done filling in
* the time (they clicked on the 'Set' button). // * the time (they clicked on the 'Set' button).
*/ // */
/** // public interface OnTimeSetListener {
* TODO: (Me) Once we extend from my BaseTimePickerDialog, //
* delete this because I defined my own. // /**
*/ // * @param view The view associated with this listener.
public interface OnTimeSetListener { // * @param hourOfDay The hour that was set.
// * @param minute The minute that was set.
/** // */
* @param view The view associated with this listener.
* @param hourOfDay The hour that was set.
* @param minute The minute that was set.
*/
// void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute); // void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute);
void onTimeSet(View view, int hourOfDay, int minute); // }
}
public NumberGridTimePickerDialog() { public NumberGridTimePickerDialog() {
// Empty constructor required for dialog fragment. // Empty constructor required for dialog fragment.
@ -384,10 +379,12 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
* @param timeFieldIndex The index representing the time field whose values, ranging from its natural * @param timeFieldIndex The index representing the time field whose values, ranging from its natural
* lower and upper limits, will be presented as choices in the GridLayout * lower and upper limits, will be presented as choices in the GridLayout
* contained in this dialog's layout. Must be one of {@link #HOUR_INDEX} * contained in this dialog's layout. Must be one of {@link #HOUR_INDEX}
* or {@link #MINUTE_INDEX}. * or {@link #MINUTE_INDEX}. TODO: Why do we need this?
* @param initialHalfDay The half-day, a.k.a. AM/PM for 12-hour time, that this picker should be * @param initialHalfDay The half-day, a.k.a. AM/PM for 12-hour time, that this picker should be
* initialized to. Must be one of {@link #HALF_DAY_1} or {@link #HALF_DAY_2}. * initialized to. Must be one of {@link #HALF_DAY_1} or {@link #HALF_DAY_2}.
* TODO: Why do we need this?
*/ */
@Deprecated
public static NumberGridTimePickerDialog newInstance(int timeFieldIndex, int initialHalfDay) { public static NumberGridTimePickerDialog newInstance(int timeFieldIndex, int initialHalfDay) {
NumberGridTimePickerDialog dialog = new NumberGridTimePickerDialog(); NumberGridTimePickerDialog dialog = new NumberGridTimePickerDialog();
dialog.mCurrentIndex = timeFieldIndex; dialog.mCurrentIndex = timeFieldIndex;
@ -397,13 +394,15 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
public void initialize(OnTimeSetListener callback, public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) { int hourOfDay, int minute, boolean is24HourMode) {
mCallback = callback; mCallback = callback; // TODO: Use setOnTimeSetListener() instead?
mInitialHourOfDay = hourOfDay; mInitialHourOfDay = hourOfDay;
mInitialMinute = minute; mInitialMinute = minute;
mIs24HourMode = is24HourMode; mIs24HourMode = is24HourMode;
mInKbMode = false; mInKbMode = false;
mThemeDark = false; mThemeDark = false;
mSelectedHalfDay = hourOfDay < 12 ? HALF_DAY_1 : HALF_DAY_2;
} }
/** /**
@ -417,9 +416,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
return mThemeDark; return mThemeDark;
} }
public void setOnTimeSetListener(OnTimeSetListener callback) { // public void setOnTimeSetListener(OnTimeSetListener callback) {
mCallback = callback; // mCallback = callback;
} // }
public void setStartTime(int hourOfDay, int minute) { public void setStartTime(int hourOfDay, int minute) {
mInitialHourOfDay = hourOfDay; mInitialHourOfDay = hourOfDay;
@ -431,7 +430,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// The Activity is created at this point // The Activity is created at this point
mIs24HourMode = DateFormat.is24HourFormat(getActivity()); // mIs24HourMode = DateFormat.is24HourFormat(getActivity());
mHandler = new Handler(); mHandler = new Handler();
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY) if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY)
&& savedInstanceState.containsKey(KEY_MINUTE) && savedInstanceState.containsKey(KEY_MINUTE)
@ -543,7 +542,6 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
tryVibrate(); tryVibrate();
} }
Log.i(TAG, String.format("Selected time is %02d:%02d", mSelectedHourOfDay, mSelectedMinute)); Log.i(TAG, String.format("Selected time is %02d:%02d", mSelectedHourOfDay, mSelectedMinute));
// TODO: Use your OnTimeSetListener
if (mCallback != null) { if (mCallback != null) {
// mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes()); // mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
// I don't think the listener actually uses the first param passed back, // I don't think the listener actually uses the first param passed back,

View File

@ -16,7 +16,7 @@ import java.text.DateFormatSymbols;
/** /**
* Created by Phillip Hsu on 7/12/2016. * Created by Phillip Hsu on 7/12/2016.
*/ */
public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { public class NumpadTimePicker extends GridLayoutNumpad {
// Time can be represented with maximum of 4 digits // Time can be represented with maximum of 4 digits
private static final int MAX_DIGITS = 4; private static final int MAX_DIGITS = 4;
@ -130,7 +130,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
} }
/** Returns the hour of day (0-23) regardless of clock system */ /** Returns the hour of day (0-23) regardless of clock system */
@Override
public int hourOfDay() { public int hourOfDay() {
if (!checkTimeValid()) if (!checkTimeValid())
throw new IllegalStateException("Cannot call hourOfDay() until legal time inputted"); throw new IllegalStateException("Cannot call hourOfDay() until legal time inputted");
@ -151,7 +150,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
return hours + (mAmPmState == PM ? 12 : 0); return hours + (mAmPmState == PM ? 12 : 0);
} }
@Override
public int minute() { public int minute() {
if (!checkTimeValid()) if (!checkTimeValid())
throw new IllegalStateException("Cannot call minute() until legal time inputted"); throw new IllegalStateException("Cannot call minute() until legal time inputted");

View File

@ -54,21 +54,21 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
// TODO: We don't need to pass in an initial hour and minute for a new instance. // TODO: We don't need to pass in an initial hour and minute for a new instance.
// TODO: Delete is24HourMode? // TODO: Delete is24HourMode?
@Deprecated @Deprecated
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback, public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) { int hourOfDay, int minute, boolean is24HourMode) {
NumpadTimePickerDialog ret = new NumpadTimePickerDialog(); NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
ret.initialize(callback, hourOfDay, minute, is24HourMode); ret.initialize(callback, hourOfDay, minute, is24HourMode);
return ret; return ret;
} }
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback) { public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback) {
NumpadTimePickerDialog ret = new NumpadTimePickerDialog(); NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
ret.setOnTimeSetListener(callback); ret.setOnTimeSetListener(callback);
return ret; return ret;
} }
@Deprecated @Deprecated
public void initialize(TimePicker.OnTimeSetListener callback, public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) { int hourOfDay, int minute, boolean is24HourMode) {
mCallback = callback; mCallback = callback;
mInitialHourOfDay = hourOfDay; mInitialHourOfDay = hourOfDay;