diff --git a/app/src/main/java/com/philliphsu/clock2/editalarm/GridLayoutNumpad.java b/app/src/main/java/com/philliphsu/clock2/editalarm/GridLayoutNumpad.java index 9c7540b..24fc458 100644 --- a/app/src/main/java/com/philliphsu/clock2/editalarm/GridLayoutNumpad.java +++ b/app/src/main/java/com/philliphsu/clock2/editalarm/GridLayoutNumpad.java @@ -2,11 +2,11 @@ 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.Button; +import android.widget.ImageButton; import com.philliphsu.clock2.R; @@ -14,6 +14,8 @@ import java.util.Arrays; import butterknife.Bind; import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.OnLongClick; /** * Created by Phillip Hsu on 7/12/2016. @@ -35,6 +37,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic @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 }) Button[] mButtons; + @Bind(R.id.backspace) ImageButton mBackspace; /** * Informs clients how to output the digits inputted into this numpad. @@ -68,12 +71,6 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic */ public abstract int capacity(); - /** - * @return the layout resource that defines the children for this numpad - */ - @LayoutRes - protected abstract int contentLayout(); - public final void setOnInputChangeListener(OnInputChangeListener onInputChangeListener) { mOnInputChangeListener = onInputChangeListener; } @@ -95,6 +92,10 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic 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]; } @@ -130,6 +131,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic return currentInput; } + @OnClick(R.id.backspace) public void delete() { /* if (mCount - 1 >= 0) { @@ -150,6 +152,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic } } + @OnLongClick(R.id.backspace) public boolean clear() { Arrays.fill(mInput, UNMODIFIED); mCount = 0; @@ -235,7 +238,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic private void init() { setAlignmentMode(ALIGN_BOUNDS); setColumnCount(COLUMNS); - View.inflate(getContext(), contentLayout(), this); + View.inflate(getContext(), R.layout.content_grid_layout_numpad, this); ButterKnife.bind(this); for (Button b : mButtons) b.setOnClickListener(this); diff --git a/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePicker.java b/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePicker.java index b48b604..0bd1cbd 100644 --- a/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePicker.java +++ b/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePicker.java @@ -5,8 +5,8 @@ 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 com.philliphsu.clock2.R; @@ -15,10 +15,6 @@ import java.lang.annotation.RetentionPolicy; 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. */ @@ -49,10 +45,8 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { private int mAmPmState = UNSPECIFIED; private final StringBuilder mFormattedInput = new StringBuilder(MAX_CHARS); - @Bind({ R.id.leftAlt, R.id.rightAlt }) - Button[] mAltButtons; - @Bind(R.id.fab) FloatingActionButton mFab; - @Bind(R.id.backspace) ImageButton mBackspace; + private Button[] mAltButtons; + private FloatingActionButton mFab; /** * Provides additional APIs to configure clients' display output. @@ -80,11 +74,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { 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); @@ -121,7 +110,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { } @Override - @OnClick(R.id.backspace) public void delete() { int len = mFormattedInput.length(); if (!is24HourFormat() && mAmPmState != UNSPECIFIED) { @@ -138,12 +126,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { } } - @Override - @OnLongClick(R.id.backspace) - public boolean clear() { - return super.clear(); - } - /** Returns the hour of day (0-23) regardless of clock system */ @Override public int hourOfDay() { @@ -247,7 +229,8 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { mAmPmState = amPmState; if (mAmPmState != HRS_24) { - onAltButtonClick(mAmPmState == AM ? mAltButtons[0] : mAltButtons[1]); + // TODO: Verify this fires without performing any physical click. + mAltButtonClickListener.onClick(mAmPmState == AM ? mAltButtons[0] : mAltButtons[1]); } } @@ -258,6 +241,19 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { public void setFabClickListener(OnClickListener fabClickListener) { mFab.setOnClickListener(fabClickListener); } + + public void setAltButtons(Button leftAlt, Button rightAlt) { + // TODO: WeakReference? + mAltButtons[0] = leftAlt; + mAltButtons[1] = rightAlt; + leftAlt.setOnClickListener(mAltButtonClickListener); + rightAlt.setOnClickListener(mAltButtonClickListener); + } + + public void setFab(FloatingActionButton fab) { + // TODO: WeakReference? + mFab = fab; + } private void init() { if (is24HourFormat()) { @@ -270,41 +266,47 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { } updateNumpadStates(); } + + private OnClickListener mAltButtonClickListener = new OnClickListener() { + @Override + public void onClick(View view) { + if (mAltButtons[0] != view && mAltButtons[1] != view) + throw new IllegalArgumentException("Not called with one of the alt buttons"); - @OnClick({ R.id.leftAlt, R.id.rightAlt }) - void onAltButtonClick(Button altBtn) { - if (mAltButtons[0] != altBtn && mAltButtons[1] != altBtn) - throw new IllegalArgumentException("Not called with one of the alt buttons"); + Button altBtn = (Button) view; - // Manually insert special characters for 12-hour clock - if (!is24HourFormat()) { - if (count() <= 2) { - // The colon is inserted for you - insertDigits(0, 0); + // Manually insert special characters for 12-hour clock + if (!is24HourFormat()) { + if (count() <= 2) { + // The colon is inserted for you + insertDigits(0, 0); + } + // text is AM or PM, so include space before + mFormattedInput.append(' ').append(altBtn.getText()); + mAmPmState = mAltButtons[0] == altBtn ? AM : PM; + // Digits will be shown for you on insert, but not AM/PM, so update. + // "Qualified super" syntax, similar to qualified this. + NumpadTimePicker.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; } - // text is AM or PM, so include space before - mFormattedInput.append(' ').append(altBtn.getText()); - mAmPmState = mAltButtons[0] == altBtn ? AM : PM; - // Digits will be shown for you on insert, but not AM/PM -/*TOneverDO: remove super*/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(); } - - updateNumpadStates(); - } + }; private boolean is24HourFormat() { return DateFormat.is24HourFormat(getContext()); @@ -392,10 +394,12 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker { } private void updateBackspaceState() { - mBackspace.setEnabled(count() > 0); + setBackspaceEnabled(count() > 0); } private void updateAltButtonStates() { + if (mAltButtons == null || mAltButtons[0] == null || mAltButtons[1] == null) + return; if (count() == 0) { // No input, no access! mAltButtons[0].setEnabled(false); diff --git a/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePickerDialog.java b/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePickerDialog.java index a142d4b..07056d8 100644 --- a/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePickerDialog.java +++ b/app/src/main/java/com/philliphsu/clock2/editalarm/NumpadTimePickerDialog.java @@ -142,13 +142,6 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog return true; } - /* - @OnClick(R.id.cancel) - void cancel() { - dismiss(); - } - */ - private void updateInputText(String inputText) { TimeTextUtils.setText(inputText, mInputField); // Move the cursor diff --git a/app/src/main/res/layout/content_grid_layout_numpad.xml b/app/src/main/res/layout/content_grid_layout_numpad.xml index 09506f2..1616ab0 100644 --- a/app/src/main/res/layout/content_grid_layout_numpad.xml +++ b/app/src/main/res/layout/content_grid_layout_numpad.xml @@ -51,4 +51,11 @@ style="@style/GridLayoutNumpadButton" grid:layout_column="1" android:text="0"/> + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_numpad_time_picker.xml b/app/src/main/res/layout/content_numpad_time_picker.xml index ba59af2..1bb3c94 100644 --- a/app/src/main/res/layout/content_numpad_time_picker.xml +++ b/app/src/main/res/layout/content_numpad_time_picker.xml @@ -4,34 +4,4 @@ -