Reverted alt buttons and fab to be part of the NumpadTimePicker

This commit is contained in:
Phillip Hsu 2016-07-24 20:08:38 -07:00
parent 447014338e
commit 3936c4285d
8 changed files with 157 additions and 230 deletions

View File

@ -321,14 +321,14 @@ public class EditAlarmActivity extends BaseActivity implements
// 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);
// 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 = NumberGridTimePickerDialog.newInstance(
// this, // OnTimeSetListener
// 0, // Initial hour of day
// 0, // Initial minute
// DateFormat.is24HourFormat(this));
dialog = NumpadTimePickerDialog.newInstance(this);
dialog.show(getSupportFragmentManager(), TAG_TIME_PICKER);
}

View File

@ -2,10 +2,10 @@ package com.philliphsu.clock2.editalarm;
import android.content.Context;
import android.support.annotation.CallSuper;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.GridLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.philliphsu.clock2.R;
@ -15,7 +15,6 @@ import java.util.Arrays;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
/**
* Created by Phillip Hsu on 7/12/2016.
@ -37,7 +36,6 @@ public abstract class GridLayoutNumpad extends GridLayout {
@Bind({ R.id.zero, R.id.one, R.id.two, R.id.three, R.id.four,
R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
TextView[] mButtons;
@Bind(R.id.backspace) ImageButton mBackspace;
/**
* Informs clients how to output the digits inputted into this numpad.
@ -71,6 +69,9 @@ public abstract class GridLayoutNumpad extends GridLayout {
*/
public abstract int capacity();
@LayoutRes
protected abstract int contentLayout();
public final void setOnInputChangeListener(OnInputChangeListener onInputChangeListener) {
mOnInputChangeListener = onInputChangeListener;
}
@ -92,10 +93,6 @@ public abstract class GridLayoutNumpad extends GridLayout {
mButtons[i].setEnabled(i >= lowerLimitInclusive && i < upperLimitExclusive);
}
protected final void setBackspaceEnabled(boolean enabled) {
mBackspace.setEnabled(enabled);
}
protected final int valueAt(int index) {
return mInput[index];
}
@ -131,7 +128,6 @@ public abstract class GridLayoutNumpad extends GridLayout {
return currentInput;
}
@OnClick(R.id.backspace)
public void delete() {
/*
if (mCount - 1 >= 0) {
@ -152,7 +148,6 @@ public abstract class GridLayoutNumpad extends GridLayout {
}
}
@OnLongClick(R.id.backspace)
public boolean clear() {
Arrays.fill(mInput, UNMODIFIED);
mCount = 0;
@ -238,7 +233,7 @@ public abstract class GridLayoutNumpad extends GridLayout {
private void init() {
setAlignmentMode(ALIGN_BOUNDS);
setColumnCount(COLUMNS);
View.inflate(getContext(), R.layout.content_grid_layout_numpad, this);
View.inflate(getContext(), contentLayout(), this);
ButterKnife.bind(this);
// If capacity() < 0, we let the system throw the exception.
mInput = new int[capacity()];

View File

@ -5,13 +5,20 @@ import android.support.annotation.IntDef;
import android.support.design.widget.FloatingActionButton;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.philliphsu.clock2.R;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnLongClick;
/**
* Created by Phillip Hsu on 7/12/2016.
@ -43,9 +50,10 @@ public class NumpadTimePicker extends GridLayoutNumpad {
private int mAmPmState = UNSPECIFIED;
private final StringBuilder mFormattedInput = new StringBuilder(MAX_CHARS);
private WeakReference<TextView> mLeftAlt;
private WeakReference<TextView> mRightAlt;
private WeakReference<FloatingActionButton> mFab;
@Bind({ R.id.leftAlt, R.id.rightAlt })
Button[] mAltButtons;
@Bind(R.id.fab) FloatingActionButton mFab;
@Bind(R.id.backspace) ImageButton mBackspace;
/**
* Provides additional APIs to configure clients' display output.
@ -73,17 +81,18 @@ public class NumpadTimePicker extends GridLayoutNumpad {
return MAX_DIGITS;
}
@Override
protected int contentLayout() {
return R.layout.content_numpad_time_picker;
}
@Override
protected void enable(int lowerLimitInclusive, int upperLimitExclusive) {
super.enable(lowerLimitInclusive, upperLimitExclusive);
if (lowerLimitInclusive == 0 && upperLimitExclusive == 0) {
// For 12-hour clock, alt buttons need to be disabled as well before firing onInputDisabled()
if (!is24HourFormat()) {
if (mLeftAlt != null && mLeftAlt.get() != null
&& mRightAlt != null && mRightAlt.get() != null
&& (mLeftAlt.get().isEnabled() || mRightAlt.get().isEnabled())) {
return;
}
if (!is24HourFormat() && (mAltButtons[0].isEnabled() || mAltButtons[1].isEnabled())) {
return;
}
((OnInputChangeListener) getOnInputChangeListener()).onInputDisabled();
}
@ -113,6 +122,7 @@ public class NumpadTimePicker extends GridLayoutNumpad {
}
@Override
@OnClick(R.id.backspace)
public void delete() {
int len = mFormattedInput.length();
if (!is24HourFormat() && mAmPmState != UNSPECIFIED) {
@ -121,7 +131,7 @@ public class NumpadTimePicker extends GridLayoutNumpad {
mFormattedInput.delete(mFormattedInput.indexOf(" "), len);
// No digit was actually deleted, but we have to notify the
// listener to update its output.
/*TOneverDO: remove super*/super.onDigitDeleted(mFormattedInput.toString());
super/*TOneverDO: remove super*/.onDigitDeleted(mFormattedInput.toString());
// We also have to manually update the numpad.
updateNumpadStates();
} else {
@ -129,8 +139,14 @@ public class NumpadTimePicker extends GridLayoutNumpad {
}
}
@Override
@OnLongClick(R.id.backspace)
public boolean clear() {
return super.clear();
}
/** Returns the hour of day (0-23) regardless of clock system */
public int hourOfDay() {
public int getHour() {
if (!checkTimeValid())
throw new IllegalStateException("Cannot call hourOfDay() until legal time inputted");
int hours = count() < 4 ? valueAt(0) : valueAt(0) * 10 + valueAt(1);
@ -150,7 +166,7 @@ public class NumpadTimePicker extends GridLayoutNumpad {
return hours + (mAmPmState == PM ? 12 : 0);
}
public int minute() {
public int getMinute() {
if (!checkTimeValid())
throw new IllegalStateException("Cannot call minute() until legal time inputted");
return count() < 4 ? valueAt(1) * 10 + valueAt(2) : valueAt(2) * 10 + valueAt(3);
@ -229,9 +245,8 @@ public class NumpadTimePicker extends GridLayoutNumpad {
}
mAmPmState = amPmState;
if (mAmPmState != HRS_24 && mLeftAlt != null && mRightAlt != null
&& mLeftAlt.get() != null && mRightAlt.get() != null) {
mAltButtonClickListener.onClick(mAmPmState == AM ? mLeftAlt.get() : mRightAlt.get());
if (mAmPmState != HRS_24) {
onAltButtonClick(mAmPmState == AM ? mAltButtons[0] : mAltButtons[1]);
}
}
@ -239,81 +254,50 @@ public class NumpadTimePicker extends GridLayoutNumpad {
return mFormattedInput.toString();
}
/**
* This was only useful when the FAB was part of the numpad's layout, and the dialog
* wanted to listen to clicks on it. Now that the dialog contains the FAB, there is
* no use for this.
* @deprecated Pass in a FAB with {@link #setFab(FloatingActionButton)} instead.
*/
@Deprecated
public void setFabClickListener(OnClickListener fabClickListener) {
// mFab.setOnClickListener(fabClickListener);
}
public void setFab(FloatingActionButton fab) {
mFab = new WeakReference<>(fab);
updateNumpadStates();
}
public void setAltButtons(TextView leftAlt, TextView rightAlt) {
mLeftAlt = new WeakReference<>(leftAlt);
mRightAlt = new WeakReference<>(rightAlt);
mLeftAlt.get().setOnClickListener(mAltButtonClickListener);
mRightAlt.get().setOnClickListener(mAltButtonClickListener);
updateNumpadStates();
}
private void init() {
/* This was where we set the text on the alt buttons, when they were still part of our layout */
if (DateFormat.is24HourFormat(getContext())) {
mAltButtons[0].setText(R.string.left_alt_24hr);
mAltButtons[1].setText(R.string.right_alt_24hr);
} else {
String[] amPmTexts = new DateFormatSymbols().getAmPmStrings();
mAltButtons[0].setText(amPmTexts[Calendar.AM]);
mAltButtons[1].setText(amPmTexts[Calendar.PM]);
}
updateNumpadStates();
}
private final AltButtonClickListener mAltButtonClickListener = new AltButtonClickListener(this);
private static class AltButtonClickListener implements OnClickListener {
private final WeakReference<NumpadTimePicker> mPicker;
public AltButtonClickListener(NumpadTimePicker picker) {
mPicker = new WeakReference<>(picker);
}
@Override
public void onClick(View v) {
TextView altBtn = (TextView) v;
// Manually insert special characters for 12-hour clock
if (!mPicker.get().is24HourFormat()) {
if (mPicker.get().count() <= 2) {
// The colon is inserted for you
mPicker.get().insertDigits(0, 0);
}
// text is AM or PM, so include space before
String ampm = altBtn.getText().toString();
StringBuilder mFormattedInput = mPicker.get().mFormattedInput;
mFormattedInput.append(' ').append(ampm);
String am = new DateFormatSymbols().getAmPmStrings()[0];
mPicker.get().mAmPmState = ampm.equals(am) ? AM : PM;
// Digits will be shown for you on insert, but not AM/PM
((/*NOT REDUNDANT! TOneverDO: Remove cast*/GridLayoutNumpad)
mPicker.get()).onDigitInserted(mFormattedInput.toString());
} else {
CharSequence text = altBtn.getText();
int[] digits = new int[text.length() - 1];
// charAt(0) is the colon, so skip i = 0.
// We are only interested in storing the digits.
for (int i = 1; i < text.length(); i++) {
// The array and the text do not have the same lengths,
// so the iterator value does not correspond to the
// array index directly
digits[i - 1] = Character.digit(text.charAt(i), BASE_10);
}
// Colon is added for you
mPicker.get().insertDigits(digits);
mPicker.get().mAmPmState = HRS_24;
@OnClick({ R.id.leftAlt, R.id.rightAlt })
void onAltButtonClick(TextView altBtn) {
// Manually insert special characters for 12-hour clock
if (!is24HourFormat()) {
if (count() <= 2) {
// The colon is inserted for you
insertDigits(0, 0);
}
mPicker.get().updateNumpadStates();
// text is AM or PM, so include space before
String ampm = altBtn.getText().toString();
mFormattedInput.append(' ').append(ampm);
String am = new DateFormatSymbols().getAmPmStrings()[0];
mAmPmState = ampm.equals(am) ? AM : PM;
// Digits will be shown for you on insert, but not AM/PM
super/*TOneverDO: remove super*/.onDigitInserted(mFormattedInput.toString());
} else {
CharSequence text = altBtn.getText();
int[] digits = new int[text.length() - 1];
// charAt(0) is the colon, so skip i = 0.
// We are only interested in storing the digits.
for (int i = 1; i < text.length(); i++) {
// The array and the text do not have the same lengths,
// so the iterator value does not correspond to the
// array index directly
digits[i - 1] = Character.digit(text.charAt(i), BASE_10);
}
// Colon is added for you
insertDigits(digits);
mAmPmState = HRS_24;
}
updateNumpadStates();
}
private boolean is24HourFormat() {
@ -398,50 +382,47 @@ public class NumpadTimePicker extends GridLayoutNumpad {
}
private void updateFabState() {
if (mFab != null && mFab.get() != null)
mFab.get().setEnabled(checkTimeValid());
mFab.setEnabled(checkTimeValid());
}
private void updateBackspaceState() {
setBackspaceEnabled(count() > 0);
mBackspace.setEnabled(count() > 0);
}
private void updateAltButtonStates() {
if (mLeftAlt == null || mRightAlt == null || mLeftAlt.get() == null || mRightAlt.get() == null)
return;
if (count() == 0) {
// No input, no access!
mLeftAlt.get().setEnabled(false);
mRightAlt.get().setEnabled(false);
mAltButtons[0].setEnabled(false);
mAltButtons[1].setEnabled(false);
} else if (count() == 1) {
// Any of 0-9 inputted, always have access in either clock.
mLeftAlt.get().setEnabled(true);
mRightAlt.get().setEnabled(true);
mAltButtons[0].setEnabled(true);
mAltButtons[1].setEnabled(true);
} else if (count() == 2) {
// Any 2 digits that make a valid hour for either clock are eligible for access
int time = getInput();
boolean validTwoDigitHour = is24HourFormat() ? time <= 23 : time >= 10 && time <= 12;
mLeftAlt.get().setEnabled(validTwoDigitHour);
mRightAlt.get().setEnabled(validTwoDigitHour);
mAltButtons[0].setEnabled(validTwoDigitHour);
mAltButtons[1].setEnabled(validTwoDigitHour);
} else if (count() == 3) {
if (is24HourFormat()) {
// For the 24-hour clock, no access at all because
// two more digits (00 or 30) cannot be added to 3 digits.
mLeftAlt.get().setEnabled(false);
mRightAlt.get().setEnabled(false);
mAltButtons[0].setEnabled(false);
mAltButtons[1].setEnabled(false);
} else {
// True for any 3 digits, if AM/PM not already entered
boolean enabled = mAmPmState == UNSPECIFIED;
mLeftAlt.get().setEnabled(enabled);
mRightAlt.get().setEnabled(enabled);
mAltButtons[0].setEnabled(enabled);
mAltButtons[1].setEnabled(enabled);
}
} else if (count() == MAX_DIGITS) {
// If all 4 digits are filled in, the 24-hour clock has absolutely
// no need for the alt buttons. However, The 12-hour clock has
// complete need of them, if not already used.
boolean enabled = !is24HourFormat() && mAmPmState == UNSPECIFIED;
mLeftAlt.get().setEnabled(enabled);
mRightAlt.get().setEnabled(enabled);
mAltButtons[0].setEnabled(enabled);
mAltButtons[1].setEnabled(enabled);
}
}

View File

@ -1,19 +1,13 @@
package com.philliphsu.clock2.editalarm;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.philliphsu.clock2.R;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTouch;
@ -24,6 +18,7 @@ import butterknife.OnTouch;
*/
public class NumpadTimePickerDialog extends BaseTimePickerDialog
implements NumpadTimePicker.OnInputChangeListener {
private static final String TAG = "NumpadTimePickerDialog";
private static final String KEY_HOUR_OF_DAY = "hour_of_day";
private static final String KEY_MINUTE = "minute";
@ -47,12 +42,8 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
@Bind(R.id.input_time) EditText mInputField;
@Bind(R.id.number_grid) NumpadTimePicker mNumpad;
@Bind(R.id.focus_grabber) View mFocusGrabber;
@Bind({ R.id.leftAlt, R.id.rightAlt })
Button[] mAltButtons;
@Bind(R.id.fab) FloatingActionButton mFab;
// 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(OnTimeSetListener callback,
int hourOfDay, int minute, boolean is24HourMode) {
@ -61,6 +52,7 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
return ret;
}
// TODO: is24HourMode param
public static NumpadTimePickerDialog newInstance(OnTimeSetListener callback) {
NumpadTimePickerDialog ret = new NumpadTimePickerDialog();
ret.setOnTimeSetListener(callback);
@ -89,27 +81,12 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
// Pass in our views so numpad can control their states for us
mNumpad.setFab(mFab);
mNumpad.setAltButtons(mAltButtons[0], mAltButtons[1]);
mNumpad.setOnInputChangeListener(this);
mNumpad.insertDigits(mInputtedDigits); // TOneverDO: before mNumpad.setOnInputChangeListener(this);
// Show the cursor immediately
mInputField.requestFocus();
// TODO: Disabled color
//updateInputText(""); // Primarily to disable 'OK'
if (DateFormat.is24HourFormat(getActivity())) {
mAltButtons[0].setText(R.string.left_alt_24hr);
mAltButtons[1].setText(R.string.right_alt_24hr);
} else {
String[] amPmTexts = new DateFormatSymbols().getAmPmStrings();
mAltButtons[0].setText(amPmTexts[Calendar.AM]);
mAltButtons[1].setText(amPmTexts[Calendar.PM]);
}
return view;
}
@ -154,11 +131,14 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
return true;
}
// The FAB is not defined directly in this dialog's layout, but rather in the layout
// of the NumpadTimePicker. We can always reference a child of a ViewGroup that is
// part of our layout.
@OnClick(R.id.fab)
void confirmSelection() {
if (!mNumpad.checkTimeValid())
return;
mCallback.onTimeSet(mNumpad, mNumpad.hourOfDay(), mNumpad.minute());
mCallback.onTimeSet(mNumpad, mNumpad.getHour(), mNumpad.getMinute());
dismiss();
}

View File

@ -51,10 +51,4 @@
style="@style/GridLayoutNumpadButton"
grid:layout_column="1"
android:text="0"/>
<ImageButton
android:id="@+id/backspace"
android:src="@drawable/ic_backspace_24dp"
style="@style/GridLayoutNumpadElement"
grid:layout_column="2"/>
</merge>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="@layout/content_grid_layout_numpad"/>
<Button
android:id="@+id/leftAlt"
style="@style/GridLayoutNumpadButton"
app:layout_column="0"/>
<Button
android:id="@+id/rightAlt"
style="@style/GridLayoutNumpadButton"
app:layout_column="2"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_gravity="center"
app:layout_column="1"
android:src="@drawable/ic_done_24dp"/>
<ImageButton
android:id="@+id/backspace"
android:src="@drawable/ic_backspace_24dp"
style="@style/GridLayoutNumpadElement"
app:layout_column="2"/>
</merge>

View File

@ -1,80 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
<!-- TOneverDO: Use LinearLayout as root container when the
dialog is a DialogFragment (or a subclass of), or else LWM
doesn't work. -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TOneverDO: Use LinearLayout as root container when the
dialog is a DialogFragment (or a subclass of), or else LWM
doesn't work. -->
<RelativeLayout
<EditText
android:id="@+id/input_time"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/input_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="@dimen/time_input_text_size"
android:background="@android:color/transparent"/>
<View style="@style/FocusGrabber"
android:id="@+id/focus_grabber"/>
<View style="@style/Divider.Horizontal"
android:id="@+id/header_divider"
android:layout_below="@id/input_time"/>
<!-- TODO: Adjust margins -->
<com.philliphsu.clock2.editalarm.NumpadTimePicker
android:id="@+id/number_grid"
android:layout_width="match_parent"
android:layout_height="256dp"
android:layout_below="@id/header_divider"
android:layout_marginTop="@dimen/bottom_sheet_vertical_space"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="28dp"/>
<View style="@style/Divider.Horizontal"
android:id="@+id/footer_divider"
android:layout_below="@id/number_grid"/>
<LinearLayout
android:id="@+id/ampm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/footer_divider">
<Button
android:id="@+id/leftAlt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?selectableItemBackground"
android:textSize="20sp"/>
<Button
android:id="@+id/rightAlt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?selectableItemBackground"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_done_24dp"
app:layout_anchor="@id/footer_divider"
app:layout_anchorGravity="center"/>
android:gravity="center"
android:textSize="@dimen/time_input_text_size"
android:background="@android:color/transparent"/>
</android.support.design.widget.CoordinatorLayout>
<View style="@style/FocusGrabber"
android:id="@+id/focus_grabber"/>
<View style="@style/Divider.Horizontal"
android:id="@+id/header_divider"
android:layout_below="@id/input_time"/>
<!-- TODO: Adjust margins -->
<com.philliphsu.clock2.editalarm.NumpadTimePicker
android:id="@+id/number_grid"
android:layout_width="match_parent"
android:layout_height="@dimen/numpad_height"
android:layout_below="@id/header_divider"
android:layout_marginTop="@dimen/bottom_sheet_vertical_space"/>
</RelativeLayout>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/GridLayoutNumpadButton"
android:text="1"
xmlns:android="http://schemas.android.com/apk/res/android"/>