Moved alt buttons out of NumpadTimePicker and into dialog layout

This commit is contained in:
Phillip Hsu 2016-07-20 03:27:49 -07:00
parent 249a2c76f9
commit 62524bc0b5
5 changed files with 248 additions and 135 deletions

View File

@ -2,6 +2,7 @@ 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;
@ -71,6 +72,12 @@ 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;
}
@ -238,7 +245,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
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);
for (Button b : mButtons)
b.setOnClickListener(this);

View File

@ -5,7 +5,6 @@ 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 com.philliphsu.clock2.R;
@ -15,6 +14,9 @@ import java.lang.annotation.RetentionPolicy;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import butterknife.Bind;
import butterknife.OnClick;
/**
* Created by Phillip Hsu on 7/12/2016.
*/
@ -45,8 +47,9 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
private int mAmPmState = UNSPECIFIED;
private final StringBuilder mFormattedInput = new StringBuilder(MAX_CHARS);
private Button[] mAltButtons;
private FloatingActionButton mFab;
@Bind({ R.id.leftAlt, R.id.rightAlt })
Button[] mAltButtons;
@Bind(R.id.fab) FloatingActionButton mFab;
/**
* Provides additional APIs to configure clients' display output.
@ -74,6 +77,11 @@ 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);
@ -229,8 +237,7 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
mAmPmState = amPmState;
if (mAmPmState != HRS_24) {
// TODO: Verify this fires without performing any physical click.
mAltButtonClickListener.onClick(mAmPmState == AM ? mAltButtons[0] : mAltButtons[1]);
onAltButtonClick(mAmPmState == AM ? mAltButtons[0] : mAltButtons[1]);
}
}
@ -242,19 +249,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
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()) {
mAltButtons[0].setText(R.string.left_alt_24hr);
@ -267,14 +261,11 @@ 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)
@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) {
@ -284,10 +275,8 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
// 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());
// 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];
@ -306,7 +295,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
updateNumpadStates();
}
};
private boolean is24HourFormat() {
return DateFormat.is24HourFormat(getContext());
@ -398,8 +386,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
}
private void updateAltButtonStates() {
if (mAltButtons == null || mAltButtons[0] == null || mAltButtons[1] == null)
return;
if (count() == 0) {
// No input, no access!
mAltButtons[0].setEnabled(false);

View File

@ -0,0 +1,138 @@
package com.philliphsu.clock2.editalarm;
import android.view.View;
/**
* Created by Phillip Hsu on 7/20/2016.
*
* TODO: This class has NOT been properly written yet. Consider moving all button state code
* from the Numpad classes to here.
* TODO: We might need to write a setAmPmState() method in NumpadTimePicker.
*
* NumpadTimePickerDialog would store a reference to this controller and use this to
* update the states of the various buttons in its layout. Currently, this class has
* ported over updateNumpadStates() and its related methods from NumpadTimePicker.
*/
public class NumpadTimePickerController {
private NumpadTimePicker mPicker;
private View mConfirmSelectionButton;
private View mLeftAltButton;
private View mRightAltButton;
private boolean mIs24HourMode;
/*
public NumpadTimePickerController(NumpadTimePicker picker, View confirmSelectionButton,
View leftAltButton, View rightAltButton, boolean is24HourMode) {
mPicker = picker;
mConfirmSelectionButton = confirmSelectionButton;
mLeftAltButton = leftAltButton;
mRightAltButton = rightAltButton;
mIs24HourMode = is24HourMode;
}
public void updateNumpadStates() {
// TOneverDO: after updateNumberKeysStates(), esp. if clock is 12-hour,
// because it calls mPicker.enable(0, 0), which checks if the alt buttons have been
// disabled as well before firing the onInputDisabled().
updateAltButtonStates();
updateBackspaceState();
updateNumberKeysStates();
updateFabState();
}
public void updateFabState() {
mConfirmSelectionButton.setEnabled(mPicker.checkTimeValid());
}
public void updateBackspaceState() {
mPicker.setBackspaceEnabled(mPicker.count() > 0);
}
public void updateAltButtonStates() {
if (mPicker.count() == 0) {
// No input, no access!
mLeftAltButton.setEnabled(false);
mRightAltButton.setEnabled(false);
} else if (mPicker.count() == 1) {
// Any of 0-9 inputted, always have access in either clock.
mLeftAltButton.setEnabled(true);
mRightAltButton.setEnabled(true);
} else if (mPicker.count() == 2) {
// Any 2 digits that make a valid hour for either clock are eligible for access
int time = mPicker.getInput();
boolean validTwoDigitHour = mIs24HourMode ? time <= 23 : time >= 10 && time <= 12;
mLeftAltButton.setEnabled(validTwoDigitHour);
mRightAltButton.setEnabled(validTwoDigitHour);
} else if (mPicker.count() == 3) {
if (mIs24HourMode) {
// For the 24-hour clock, no access at all because
// two more digits (00 or 30) cannot be added to 3 digits.
mLeftAltButton.setEnabled(false);
mRightAltButton.setEnabled(false);
} else {
// True for any 3 digits, if AM/PM not already entered
boolean enabled = mAmPmState == UNSPECIFIED;
mLeftAltButton.setEnabled(enabled);
mRightAltButton.setEnabled(enabled);
}
} else if (mPicker.count() == mPicker.capacity()) {
// 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 = !mIs24HourMode && mAmPmState == UNSPECIFIED;
mLeftAltButton.setEnabled(enabled);
mRightAltButton.setEnabled(enabled);
}
}
public void updateNumberKeysStates() {
int cap = 10; // number of buttons
boolean is24hours = mIs24HourMode;
if (mPicker.count() == 0) {
mPicker.enable(is24hours ? 0 : 1, cap);
return;
} else if (mPicker.count() == mPicker.capacity()) {
mPicker.enable(0, 0);
return;
}
int time = mPicker.getInput();
if (is24hours) {
if (mPicker.count() == 1) {
mPicker.enable(0, time < 2 ? cap : 6);
} else if (mPicker.count() == 2) {
mPicker.enable(0, time % 10 >= 0 && time % 10 <= 5 ? cap : 6);
} else if (mPicker.count() == 3) {
if (time >= 236) {
mPicker.enable(0, 0);
} else {
mPicker.enable(0, time % 10 >= 0 && time % 10 <= 5 ? cap : 0);
}
}
} else {
if (mPicker.count() == 1) {
if (time == 0) {
throw new IllegalStateException("12-hr format, zeroth digit = 0?");
} else {
mPicker.enable(0, 6);
}
} else if (mPicker.count() == 2 || mPicker.count() == 3) {
if (time >= 126) {
mPicker.enable(0, 0);
} else {
if (time >= 100 && time <= 125 && mAmPmState != UNSPECIFIED) {
// Could legally input fourth digit, if not for the am/pm state already set
mPicker.enable(0, 0);
} else {
mPicker.enable(0, time % 10 >= 0 && time % 10 <= 5 ? cap : 0);
}
}
}
}
}
*/
}

View File

@ -4,4 +4,38 @@
<include layout="@layout/content_grid_layout_numpad"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_columnSpan="3">
<android.support.v7.widget.GridLayout
android:id="@+id/ampm"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="1">
<Button
android:id="@+id/leftAlt"
style="@style/GridLayoutNumpadButton"
app:layout_column="0"/>
<Button
android:id="@+id/rightAlt"
style="@style/GridLayoutNumpadButton"
app:layout_column="1"/>
</android.support.v7.widget.GridLayout>
<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/ampm"
app:layout_anchorGravity="center"/>
</android.support.design.widget.CoordinatorLayout>
</merge>

View File

@ -1,20 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="@dimen/bottom_sheet_vertical_space"
android:layout_marginStart="@dimen/bottom_sheet_edge_margin"
android:layout_marginEnd="@dimen/bottom_sheet_edge_margin"
android:layout_marginBottom="@dimen/bottom_sheet_vertical_space">
<LinearLayout
<!-- TOneverDO: Use LinearLayout, because it doesn't obey LWM -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Consider TextView, because we might not want the cursor? -->
<EditText
android:id="@+id/input_time"
android:layout_width="match_parent"
@ -31,52 +21,10 @@
<com.philliphsu.clock2.editalarm.NumpadTimePicker
android:id="@+id/number_grid"
android:layout_width="match_parent"
android:layout_height="@dimen/numpad_height"/>
android:layout_height="@dimen/numpad_height"
android:layout_marginTop="@dimen/bottom_sheet_vertical_space"
android:layout_marginStart="@dimen/bottom_sheet_edge_margin"
android:layout_marginEnd="@dimen/bottom_sheet_edge_margin"
android:layout_marginBottom="@dimen/bottom_sheet_vertical_space"/>
<LinearLayout
android:id="@+id/alt_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/leftAlt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/GridLayoutNumpadButton"/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/rightAlt"
style="@style/GridLayoutNumpadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<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"
android:layout_gravity="center"
app:layout_anchor="@id/alt_buttons"
app:layout_anchorGravity="center"/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>