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(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;
}
@ -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.
// mAmPmView.setTheme(context, themeDark);
// TODO: These aren't doing much currently, if at all.
if (m24HoursGrid != null) {
m24HoursGrid.setTheme(context, themeDark);
} else if (mHoursGrid != null) {
@ -141,25 +128,8 @@ public class GridSelectorLayout extends ViewAnimator implements NumbersGrid.OnNu
}
public void setTime(int hours, int minutes) {
setItem(HOUR_INDEX, hours);
setItem(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);
}
setValueForItem(HOUR_INDEX, hours);
setValueForItem(MINUTE_INDEX, minutes);
}
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.
*/
private void setValueForItem(int index, int value) {
Log.d(TAG, String.format("setValueForItem(%d, %d)", index, value));
if (index == HOUR_INDEX) {
mCurrentHoursOfDay = value;
setHourGridSelection(value);
} else if (index == MINUTE_INDEX){
mCurrentMinutes = value;
mMinutesGrid.setSelection(value);
} else if (index == AMPM_INDEX) {
if (value == HALF_DAY_1) {
mCurrentHoursOfDay = mCurrentHoursOfDay % 12;
} else if (value == HALF_DAY_2) {
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);
}
@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
protected int contentLayout() {
return R.layout.content_hours_grid;

View File

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

View File

@ -46,15 +46,15 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
// our subclasses' buttons, if any.
registerClickListeners();
mIsInitialized = false;
// mDefaultTextColor = Utils.getTextColorFromThemeAttr(context, android.R.attr.textColorPrimary);
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
// is that this NumbersGrid is programmatically created by the GridSelectorLayout in
// its initialize(), and the Context passed in there is from
// NumberGridTimePickerDialog.getActivity().
mSelectedTextColor = Utils.getThemeAccentColor(context);
// Show the first button as default selected
setIndicator(getChildAt(indexOfDefaultValue()));
final View defaultSelectedView = getChildAt(indexOfDefaultValue());
mSelection = valueOf(defaultSelectedView);
setIndicator(defaultSelectedView);
}
public void initialize(OnNumberSelectedListener listener) {
@ -81,10 +81,8 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
*/
@Override
public void onClick(View v) {
TextView tv = (TextView) v;
clearIndicator(); // Does nothing if there was no indicator last selected
setIndicator(v);
mSelection = Integer.parseInt(tv.getText().toString());
mSelection = valueOf(v);
mSelectionListener.onNumberSelected(mSelection);
}
@ -108,6 +106,7 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
* @param view the clicked number button
*/
protected void setIndicator(View view) {
clearIndicator(); // Does nothing if there was no indicator last selected
TextView tv = (TextView) view;
tv.setTextColor(mSelectedTextColor);
mLastSelectedView = view;
@ -138,22 +137,21 @@ public abstract class NumbersGrid extends GridLayout implements View.OnClickList
* as determined by {@link #canRegisterClickListener(View)}.
*/
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?
R.color.text_color_primary_dark : R.color.text_color_primary_light);
for (int i = 0; i < getChildCount(); 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*/);
// Filter out views that aren't number buttons
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++;
}
}
/**
* 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.
// See canRegisterClickListener().
TwentyFourHourGridItem item = (TwentyFourHourGridItem) v;
clearIndicator();
clearIndicator(); // TODO: Remove?
setIndicator(v);
int newVal = Integer.parseInt(item.getPrimaryText().toString());
setSelection(newVal);
@ -52,7 +52,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
int newVal = Integer.parseInt(item.getSecondaryText().toString());
setSelection(newVal);
mSelectionListener.onNumberSelected(newVal);
clearIndicator();
clearIndicator(); // TODO: Remove?
swapTexts();
// TOneverDO: Call before swapping texts, because setIndicator() uses the primary TextView.
setIndicator(v);
@ -90,7 +90,7 @@ public class TwentyFourHoursGrid extends NumbersGrid implements View.OnLongClick
item.getSecondaryTextView().setTextColor(mSecondaryTextColor);
}
// Set the indicator again
setIndicator(getChildAt(indexOfDefaultValue()));
setIndicator(getChildAt(indexOfDefaultValue())); // TODO: We don't need this anymore.
}
public void swapTexts() {