savepoint to revert back to

This commit is contained in:
Phillip Hsu 2016-07-20 01:37:08 -07:00
parent a8f3fafafa
commit 249a2c76f9
6 changed files with 153 additions and 125 deletions

View File

@ -2,11 +2,11 @@ package com.philliphsu.clock2.editalarm;
import android.content.Context; import android.content.Context;
import android.support.annotation.CallSuper; import android.support.annotation.CallSuper;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
@ -14,6 +14,8 @@ import java.util.Arrays;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
/** /**
* Created by Phillip Hsu on 7/12/2016. * 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, @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 }) R.id.five, R.id.six, R.id.seven, R.id.eight, R.id.nine })
Button[] mButtons; Button[] mButtons;
@Bind(R.id.backspace) ImageButton mBackspace;
/** /**
* Informs clients how to output the digits inputted into this numpad. * 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(); 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) { public final void setOnInputChangeListener(OnInputChangeListener onInputChangeListener) {
mOnInputChangeListener = onInputChangeListener; mOnInputChangeListener = onInputChangeListener;
} }
@ -95,6 +92,10 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
mButtons[i].setEnabled(i >= lowerLimitInclusive && i < upperLimitExclusive); mButtons[i].setEnabled(i >= lowerLimitInclusive && i < upperLimitExclusive);
} }
protected final void setBackspaceEnabled(boolean enabled) {
mBackspace.setEnabled(enabled);
}
protected final int valueAt(int index) { protected final int valueAt(int index) {
return mInput[index]; return mInput[index];
} }
@ -130,6 +131,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
return currentInput; return currentInput;
} }
@OnClick(R.id.backspace)
public void delete() { public void delete() {
/* /*
if (mCount - 1 >= 0) { if (mCount - 1 >= 0) {
@ -150,6 +152,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
} }
} }
@OnLongClick(R.id.backspace)
public boolean clear() { public boolean clear() {
Arrays.fill(mInput, UNMODIFIED); Arrays.fill(mInput, UNMODIFIED);
mCount = 0; mCount = 0;
@ -235,7 +238,7 @@ public abstract class GridLayoutNumpad extends GridLayout implements View.OnClic
private void init() { private void init() {
setAlignmentMode(ALIGN_BOUNDS); setAlignmentMode(ALIGN_BOUNDS);
setColumnCount(COLUMNS); setColumnCount(COLUMNS);
View.inflate(getContext(), contentLayout(), this); View.inflate(getContext(), R.layout.content_grid_layout_numpad, this);
ButterKnife.bind(this); ButterKnife.bind(this);
for (Button b : mButtons) for (Button b : mButtons)
b.setOnClickListener(this); b.setOnClickListener(this);

View File

@ -5,8 +5,8 @@ import android.support.annotation.IntDef;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
@ -15,10 +15,6 @@ import java.lang.annotation.RetentionPolicy;
import java.text.DateFormatSymbols; import java.text.DateFormatSymbols;
import java.util.Calendar; import java.util.Calendar;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnLongClick;
/** /**
* Created by Phillip Hsu on 7/12/2016. * Created by Phillip Hsu on 7/12/2016.
*/ */
@ -49,10 +45,8 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
private int mAmPmState = UNSPECIFIED; private int mAmPmState = UNSPECIFIED;
private final StringBuilder mFormattedInput = new StringBuilder(MAX_CHARS); private final StringBuilder mFormattedInput = new StringBuilder(MAX_CHARS);
@Bind({ R.id.leftAlt, R.id.rightAlt }) private Button[] mAltButtons;
Button[] mAltButtons; private FloatingActionButton mFab;
@Bind(R.id.fab) FloatingActionButton mFab;
@Bind(R.id.backspace) ImageButton mBackspace;
/** /**
* Provides additional APIs to configure clients' display output. * Provides additional APIs to configure clients' display output.
@ -80,11 +74,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
return MAX_DIGITS; return MAX_DIGITS;
} }
@Override
protected int contentLayout() {
return R.layout.content_numpad_time_picker;
}
@Override @Override
protected void enable(int lowerLimitInclusive, int upperLimitExclusive) { protected void enable(int lowerLimitInclusive, int upperLimitExclusive) {
super.enable(lowerLimitInclusive, upperLimitExclusive); super.enable(lowerLimitInclusive, upperLimitExclusive);
@ -121,7 +110,6 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
} }
@Override @Override
@OnClick(R.id.backspace)
public void delete() { public void delete() {
int len = mFormattedInput.length(); int len = mFormattedInput.length();
if (!is24HourFormat() && mAmPmState != UNSPECIFIED) { 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 */ /** Returns the hour of day (0-23) regardless of clock system */
@Override @Override
public int hourOfDay() { public int hourOfDay() {
@ -247,7 +229,8 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
mAmPmState = amPmState; mAmPmState = amPmState;
if (mAmPmState != HRS_24) { 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) { public void setFabClickListener(OnClickListener fabClickListener) {
mFab.setOnClickListener(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() { private void init() {
if (is24HourFormat()) { if (is24HourFormat()) {
@ -270,41 +266,47 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
} }
updateNumpadStates(); 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 }) Button altBtn = (Button) view;
void onAltButtonClick(Button altBtn) {
if (mAltButtons[0] != altBtn && mAltButtons[1] != altBtn)
throw new IllegalArgumentException("Not called with one of the alt buttons");
// Manually insert special characters for 12-hour clock // Manually insert special characters for 12-hour clock
if (!is24HourFormat()) { if (!is24HourFormat()) {
if (count() <= 2) { if (count() <= 2) {
// The colon is inserted for you // The colon is inserted for you
insertDigits(0, 0); 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()); updateNumpadStates();
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();
}
private boolean is24HourFormat() { private boolean is24HourFormat() {
return DateFormat.is24HourFormat(getContext()); return DateFormat.is24HourFormat(getContext());
@ -392,10 +394,12 @@ public class NumpadTimePicker extends GridLayoutNumpad implements TimePicker {
} }
private void updateBackspaceState() { private void updateBackspaceState() {
mBackspace.setEnabled(count() > 0); setBackspaceEnabled(count() > 0);
} }
private void updateAltButtonStates() { private void updateAltButtonStates() {
if (mAltButtons == null || mAltButtons[0] == null || mAltButtons[1] == null)
return;
if (count() == 0) { if (count() == 0) {
// No input, no access! // No input, no access!
mAltButtons[0].setEnabled(false); mAltButtons[0].setEnabled(false);

View File

@ -142,13 +142,6 @@ public class NumpadTimePickerDialog extends BaseTimePickerDialog
return true; return true;
} }
/*
@OnClick(R.id.cancel)
void cancel() {
dismiss();
}
*/
private void updateInputText(String inputText) { private void updateInputText(String inputText) {
TimeTextUtils.setText(inputText, mInputField); TimeTextUtils.setText(inputText, mInputField);
// Move the cursor // Move the cursor

View File

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

View File

@ -4,34 +4,4 @@
<include layout="@layout/content_grid_layout_numpad"/> <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"/>
<!-- Used to properly position the FAB -->
<FrameLayout
android:layout_height="56dp"
app:layout_columnWeight="1"
app:layout_column="1">
<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"/>
</FrameLayout>
<ImageButton
android:id="@+id/backspace"
android:src="@drawable/ic_backspace_24dp"
style="@style/GridLayoutNumpadElement"
app:layout_column="2"/>
</merge> </merge>

View File

@ -1,31 +1,82 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- TOneverDO: Use LinearLayout, because it doesn't obey LWM --> <android.support.design.widget.CoordinatorLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> 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">
<!-- TODO: Consider TextView, because we might not want the cursor? --> <LinearLayout
<EditText
android:id="@+id/input_time"
android:layout_width="match_parent" 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"
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"/>
<com.philliphsu.clock2.editalarm.NumpadTimePicker
android:id="@+id/number_grid"
android:layout_width="match_parent"
android:layout_height="@dimen/numpad_height"/>
<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:layout_height="wrap_content"
android:gravity="center" android:src="@drawable/ic_done_24dp"
android:textSize="@dimen/time_input_text_size" android:layout_gravity="center"
android:background="@android:color/transparent"/> app:layout_anchor="@id/alt_buttons"
app:layout_anchorGravity="center"/>
<View style="@style/FocusGrabber" </android.support.design.widget.CoordinatorLayout>
android:id="@+id/focus_grabber"/>
<View style="@style/Divider"/>
<com.philliphsu.clock2.editalarm.NumpadTimePicker
android:id="@+id/number_grid"
android:layout_width="match_parent"
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>