Selection indicator restored on rotate

This commit is contained in:
Phillip Hsu 2016-08-26 21:14:48 -07:00
parent 591bc6d680
commit ee77105a8e
5 changed files with 51 additions and 51 deletions

View File

@ -112,18 +112,6 @@ public class GridSelectorLayout extends ViewAnimator implements NumbersGrid.OnNu
setValueForItem(HOUR_INDEX, initialHoursOfDay); setValueForItem(HOUR_INDEX, initialHoursOfDay);
setValueForItem(MINUTE_INDEX, initialMinutes); setValueForItem(MINUTE_INDEX, initialMinutes);
// Record the selected values in the number grids.
if (!is24HourMode) {
initialHoursOfDay = initialHoursOfDay % 12;
if (initialHoursOfDay == 0) {
initialHoursOfDay = 12;
}
mHoursGrid.setSelection(initialHoursOfDay);
} else {
m24HoursGrid.setSelection(initialHoursOfDay);
}
mMinutesGrid.setSelection(initialMinutes);
mTimeInitialized = true; mTimeInitialized = true;
} }
@ -131,7 +119,6 @@ public class GridSelectorLayout extends ViewAnimator implements NumbersGrid.OnNu
// TODO: This logic needs to be in the Dialog class, since the am/pm view is contained there. // TODO: This logic needs to be in the Dialog class, since the am/pm view is contained there.
// mAmPmView.setTheme(context, themeDark); // mAmPmView.setTheme(context, themeDark);
// TODO: These aren't doing much currently, if at all.
if (m24HoursGrid != null) { if (m24HoursGrid != null) {
m24HoursGrid.setTheme(context, themeDark); m24HoursGrid.setTheme(context, themeDark);
} else if (mHoursGrid != null) { } else if (mHoursGrid != null) {
@ -141,25 +128,8 @@ public class GridSelectorLayout extends ViewAnimator implements NumbersGrid.OnNu
} }
public void setTime(int hours, int minutes) { public void setTime(int hours, int minutes) {
setItem(HOUR_INDEX, hours); setValueForItem(HOUR_INDEX, hours);
setItem(MINUTE_INDEX, minutes); setValueForItem(MINUTE_INDEX, minutes);
}
/**
* Set either the hour or the minute. Will set the internal value, and set the selection.
*/
private void setItem(int index, int value) {
if (index == HOUR_INDEX) {
setValueForItem(HOUR_INDEX, value);
if (mIs24HourMode) {
m24HoursGrid.setSelection(value);
} else {
mHoursGrid.setSelection(value);
}
} else if (index == MINUTE_INDEX) {
setValueForItem(MINUTE_INDEX, value);
mMinutesGrid.setSelection(value);
}
} }
public void setOnValueSelectedListener(OnValueSelectedListener listener) { public void setOnValueSelectedListener(OnValueSelectedListener listener) {
@ -301,16 +271,32 @@ public class GridSelectorLayout extends ViewAnimator implements NumbersGrid.OnNu
* Set the internal value for the hour, minute, or AM/PM. * Set the internal value for the hour, minute, or AM/PM.
*/ */
private void setValueForItem(int index, int value) { private void setValueForItem(int index, int value) {
Log.d(TAG, String.format("setValueForItem(%d, %d)", index, value));
if (index == HOUR_INDEX) { if (index == HOUR_INDEX) {
mCurrentHoursOfDay = value; mCurrentHoursOfDay = value;
setHourGridSelection(value);
} else if (index == MINUTE_INDEX){ } else if (index == MINUTE_INDEX){
mCurrentMinutes = value; mCurrentMinutes = value;
mMinutesGrid.setSelection(value);
} else if (index == AMPM_INDEX) { } else if (index == AMPM_INDEX) {
if (value == HALF_DAY_1) { if (value == HALF_DAY_1) {
mCurrentHoursOfDay = mCurrentHoursOfDay % 12; mCurrentHoursOfDay = mCurrentHoursOfDay % 12;
} else if (value == HALF_DAY_2) { } else if (value == HALF_DAY_2) {
mCurrentHoursOfDay = (mCurrentHoursOfDay % 12) + 12; mCurrentHoursOfDay = (mCurrentHoursOfDay % 12) + 12;
} }
setHourGridSelection(mCurrentHoursOfDay);
}
}
private void setHourGridSelection(int value) {
if (mIs24HourMode) {
m24HoursGrid.setSelection(value);
} else {
value = value % 12;
if (value == 0) {
value = 12;
}
mHoursGrid.setSelection(value);
} }
} }
} }

View File

@ -13,6 +13,14 @@ public class HoursGrid extends NumbersGrid {
super(context); super(context);
} }
@Override
public void setSelection(int value) {
super.setSelection(value);
// We expect value to be within [1, 12]. The position in the grid where
// value is located is thus (value - 1).
setIndicator(getChildAt(value - 1));
}
@Override @Override
protected int contentLayout() { protected int contentLayout() {
return R.layout.content_hours_grid; return R.layout.content_hours_grid;

View File

@ -28,7 +28,7 @@ public class MinutesGrid extends NumbersGrid {
int value = getSelection() - 1; int value = getSelection() - 1;
if (value < 0) if (value < 0)
value = 59; value = 59;
setIndicator(value); setIndicator(value); // TODO: Remove when you move this logic to setSelection()
setSelection(value); setSelection(value);
mSelectionListener.onNumberSelected(value); mSelectionListener.onNumberSelected(value);
} }
@ -39,7 +39,7 @@ public class MinutesGrid extends NumbersGrid {
int value = getSelection() + 1; int value = getSelection() + 1;
if (value == 60) if (value == 60)
value = 0; value = 0;
setIndicator(value); setIndicator(value); // TODO: Remove when you move this logic to setSelection()
setSelection(value); setSelection(value);
mSelectionListener.onNumberSelected(value); mSelectionListener.onNumberSelected(value);
} }
@ -75,8 +75,9 @@ public class MinutesGrid extends NumbersGrid {
* Helper method for minute tuners to set the indicator. * Helper method for minute tuners to set the indicator.
* @param value the new value set by the minute tuners * @param value the new value set by the minute tuners
*/ */
// TODO: Move this logic to setSelection()
private void setIndicator(int value) { private void setIndicator(int value) {
clearIndicator(); clearIndicator(); // TODO: Remove?
if (value % 5 == 0) { if (value % 5 == 0) {
// The new value is one of the predetermined minute values // The new value is one of the predetermined minute values
int positionOfValue = value / 5; int positionOfValue = value / 5;

View File

@ -46,15 +46,15 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
// our subclasses' buttons, if any. // our subclasses' buttons, if any.
registerClickListeners(); registerClickListeners();
mIsInitialized = false; mIsInitialized = false;
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, android.R.attr.textColorPrimary);
mDefaultTextColor = ContextCompat.getColor(context, R.color.text_color_primary_light); mDefaultTextColor = ContextCompat.getColor(context, R.color.text_color_primary_light);
// The reason we can use the Context passed here and get the correct accent color // The reason we can use the Context passed here and get the correct accent color
// is that this NumbersGrid is programmatically created by the GridSelectorLayout in // is that this NumbersGrid is programmatically created by the GridSelectorLayout in
// its initialize(), and the Context passed in there is from // its initialize(), and the Context passed in there is from
// NumberGridTimePickerDialog.getActivity(). // NumberGridTimePickerDialog.getActivity().
mSelectedTextColor = Utils.getThemeAccentColor(context); mSelectedTextColor = Utils.getThemeAccentColor(context);
// Show the first button as default selected final View defaultSelectedView = getChildAt(indexOfDefaultValue());
setIndicator(getChildAt(indexOfDefaultValue())); mSelection = valueOf(defaultSelectedView);
setIndicator(defaultSelectedView);
} }
public void initialize(OnNumberSelectedListener listener) { public void initialize(OnNumberSelectedListener listener) {
@ -81,10 +81,8 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
*/ */
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TextView tv = (TextView) v;
clearIndicator(); // Does nothing if there was no indicator last selected
setIndicator(v); setIndicator(v);
mSelection = Integer.parseInt(tv.getText().toString()); mSelection = valueOf(v);
mSelectionListener.onNumberSelected(mSelection); mSelectionListener.onNumberSelected(mSelection);
} }
@ -108,6 +106,7 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
* @param view the clicked number button * @param view the clicked number button
*/ */
protected void setIndicator(View view) { protected void setIndicator(View view) {
clearIndicator(); // Does nothing if there was no indicator last selected
TextView tv = (TextView) view; TextView tv = (TextView) view;
tv.setTextColor(mSelectedTextColor); tv.setTextColor(mSelectedTextColor);
mLastSelectedView = view; mLastSelectedView = view;
@ -138,22 +137,21 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
* as determined by {@link #canRegisterClickListener(View)}. * as determined by {@link #canRegisterClickListener(View)}.
*/ */
void setTheme(Context context, boolean themeDark) { void setTheme(Context context, boolean themeDark) {
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, themeDark?
// // You may think the order should be switched, but this is in fact the correct order.
// // I'm guessing this is sensitive to the background color?
// android.R.attr.textColorPrimary : android.R.attr.textColorPrimaryInverse);
mDefaultTextColor = ContextCompat.getColor(context, themeDark? mDefaultTextColor = ContextCompat.getColor(context, themeDark?
R.color.text_color_primary_dark : R.color.text_color_primary_light); R.color.text_color_primary_dark : R.color.text_color_primary_light);
for (int i = 0; i < getChildCount(); i++) { for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i); View v = getChildAt(i);
// TODO: We can move this to the ctor, because this isn't dependent on the theme.
Utils.setColorControlHighlight(v, mSelectedTextColor/*colorAccent*/); Utils.setColorControlHighlight(v, mSelectedTextColor/*colorAccent*/);
// Filter out views that aren't number buttons // Filter out views that aren't number buttons
if (canRegisterClickListener(v)) { if (canRegisterClickListener(v)) {
((TextView) v).setTextColor(mDefaultTextColor); final TextView tv = (TextView) v;
// Filter out the current selection
if (mSelection != valueOf(tv)) {
tv.setTextColor(mDefaultTextColor);
}
} }
} }
// Set the indicator again
setIndicator(getChildAt(indexOfDefaultValue()));
} }
/** /**
@ -169,4 +167,11 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
i++; i++;
} }
} }
/**
* Parses the number held by the button into an integer.
*/
private static int valueOf(View button) {
return Integer.parseInt(((TextView) button).getText().toString());
}
} }

View File

@ -39,7 +39,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
// We already verified that this view can have a click listener registered on. // We already verified that this view can have a click listener registered on.
// See canRegisterClickListener(). // See canRegisterClickListener().
TwentyFourHourGridItem item = (TwentyFourHourGridItem) v; TwentyFourHourGridItem item = (TwentyFourHourGridItem) v;
clearIndicator(); clearIndicator(); // TODO: Remove?
setIndicator(v); setIndicator(v);
int newVal = Integer.parseInt(item.getPrimaryText().toString()); int newVal = Integer.parseInt(item.getPrimaryText().toString());
setSelection(newVal); setSelection(newVal);
@ -52,7 +52,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
int newVal = Integer.parseInt(item.getSecondaryText().toString()); int newVal = Integer.parseInt(item.getSecondaryText().toString());
setSelection(newVal); setSelection(newVal);
mSelectionListener.onNumberSelected(newVal); mSelectionListener.onNumberSelected(newVal);
clearIndicator(); clearIndicator(); // TODO: Remove?
swapTexts(); swapTexts();
// TOneverDO: Call before swapping texts, because setIndicator() uses the primary TextView. // TOneverDO: Call before swapping texts, because setIndicator() uses the primary TextView.
setIndicator(v); setIndicator(v);
@ -90,7 +90,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
item.getSecondaryTextView().setTextColor(mSecondaryTextColor); item.getSecondaryTextView().setTextColor(mSecondaryTextColor);
} }
// Set the indicator again // Set the indicator again
setIndicator(getChildAt(indexOfDefaultValue())); setIndicator(getChildAt(indexOfDefaultValue())); // TODO: We don't need this anymore.
} }
public void swapTexts() { public void swapTexts() {