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
// 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.
@ -29,10 +43,25 @@ public abstract class BaseTimePickerDialog extends DialogFragment {
@LayoutRes
protected abstract int contentLayout();
public final void setOnTimeSetListener(TimePicker.OnTimeSetListener callback) {
public final void setOnTimeSetListener(OnTimeSetListener 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.
// @NonNull
// @Override
@ -92,19 +121,4 @@ public abstract class BaseTimePickerDialog extends DialogFragment {
//
// 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,
SharedPreferencesHelper,
LoaderManager.LoaderCallbacks<Alarm>,
NumpadTimePicker.OnTimeSetListener {
BaseTimePickerDialog.OnTimeSetListener {
private static final String TAG = "EditAlarmActivity";
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?
// If so, try finding the time picker in our backstack.
NumpadTimePickerDialog picker = (NumpadTimePickerDialog)
BaseTimePickerDialog picker = (BaseTimePickerDialog)
getSupportFragmentManager().findFragmentByTag(TAG_TIME_PICKER);
if (picker != null) {
// Restore the callback
@ -316,14 +316,20 @@ public class EditAlarmActivity extends BaseActivity implements
void openTimePicker() {
// Close the keyboard first, or else our dialog will be screwed up.
// 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.
// 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
// last inputted time.
// NumpadTimePickerDialog.newInstance(this).show(getSupportFragmentManager(), TAG_TIME_PICKER);
NumberGridTimePickerDialog.newInstance(NumberGridTimePickerDialog.HOUR_INDEX, NumberGridTimePickerDialog.HALF_DAY_1)
.show(getSupportFragmentManager(), TAG_TIME_PICKER);
// TODO: Read preferences to see what time picker style to show.
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() {

View File

@ -23,7 +23,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayout;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
@ -83,10 +82,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
// Delay before starting the pulse animation, in ms.
private static final int PULSE_ANIMATOR_DELAY = 300;
/**
* TODO: (Me) Use my OnTimeSetListener type.
*/
private OnTimeSetListener mCallback;
// private OnTimeSetListener mCallback;
// 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());
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 time (they clicked on the 'Set' button).
*/
/**
* TODO: (Me) Once we extend from my BaseTimePickerDialog,
* delete this because I defined my own.
*/
public interface OnTimeSetListener {
/**
* @param view The view associated with this listener.
* @param hourOfDay The hour that was set.
* @param minute The minute that was set.
*/
// /**
// * The callback interface used to indicate the user is done filling in
// * the time (they clicked on the 'Set' button).
// */
// public interface OnTimeSetListener {
//
// /**
// * @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(View view, int hourOfDay, int minute);
}
// }
public NumberGridTimePickerDialog() {
// 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
* 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}
* 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
* 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) {
NumberGridTimePickerDialog dialog = new NumberGridTimePickerDialog();
dialog.mCurrentIndex = timeFieldIndex;
@ -397,13 +394,15 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
mCallback = callback;
mCallback = callback; // TODO: Use setOnTimeSetListener() instead?
mInitialHourOfDay = hourOfDay;
mInitialMinute = minute;
mIs24HourMode = is24HourMode;
mInKbMode = false;
mThemeDark = false;
mSelectedHalfDay = hourOfDay < 12 ? HALF_DAY_1 : HALF_DAY_2;
}
/**
@ -417,9 +416,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
return mThemeDark;
}
public void setOnTimeSetListener(OnTimeSetListener callback) {
mCallback = callback;
}
// public void setOnTimeSetListener(OnTimeSetListener callback) {
// mCallback = callback;
// }
public void setStartTime(int hourOfDay, int minute) {
mInitialHourOfDay = hourOfDay;
@ -431,7 +430,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The Activity is created at this point
mIs24HourMode = DateFormat.is24HourFormat(getActivity());
// mIs24HourMode = DateFormat.is24HourFormat(getActivity());
mHandler = new Handler();
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY)
&& savedInstanceState.containsKey(KEY_MINUTE)
@ -543,7 +542,6 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
tryVibrate();
}
Log.i(TAG, String.format("Selected time is %02d:%02d", mSelectedHourOfDay, mSelectedMinute));
// TODO: Use your OnTimeSetListener
if (mCallback != null) {
// mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
// 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.
*/
public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
public class NumpadTimePicker extends GridLayoutNumpad {
// Time can be represented with maximum of 4 digits
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 */
@Override
public int hourOfDay() {
if (!checkTimeValid())
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);
}
@Override
public int minute() {
if (!checkTimeValid())
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: Delete is24HourMode?
@Deprecated
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback,
public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
ret.initialize(callback, hourOfDay, minute, is24HourMode);
return ret;
}
public static NumpadTimePickerDialog newInstance(TimePicker.OnTimeSetListener callback) {
public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback) {
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
ret.setOnTimeSetListener(callback);
return ret;
}
@Deprecated
public void initialize(TimePicker.OnTimeSetListener callback,
public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
mCallback = callback;
mInitialHourOfDay = hourOfDay;