Implemented done button and half day toggles in dialog

This commit is contained in:
Phillip Hsu 2016-07-23 19:56:19 -07:00
parent 454851e2bf
commit 1162e9177c
8 changed files with 239 additions and 66 deletions

View File

@ -20,6 +20,8 @@ import android.animation.ObjectAnimator;
import android.app.ActionBar.LayoutParams; import android.app.ActionBar.LayoutParams;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayout; import android.support.v7.widget.GridLayout;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.util.Log; import android.util.Log;
@ -29,6 +31,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
@ -40,6 +43,7 @@ import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import butterknife.Bind; import butterknife.Bind;
import butterknife.OnClick;
//import com.android.datetimepicker.HapticFeedbackController; //import com.android.datetimepicker.HapticFeedbackController;
//import com.android.datetimepicker.R; //import com.android.datetimepicker.R;
@ -125,6 +129,8 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
private static final int[] HOURS_24_HALF_DAY_1 = {0,1,2,3,4,5,6,7,8,9,10,11}; private static final int[] HOURS_24_HALF_DAY_1 = {0,1,2,3,4,5,6,7,8,9,10,11};
private static final int[] HOURS_24_HALF_DAY_2 = {12,13,14,15,16,17,18,19,20,21,22,23}; private static final int[] HOURS_24_HALF_DAY_2 = {12,13,14,15,16,17,18,19,20,21,22,23};
private static final int[] MINUTES = {0,5,10,15,20,25,30,35,40,45,50,55}; private static final int[] MINUTES = {0,5,10,15,20,25,30,35,40,45,50,55};
// The delay before a OnLongClick on a TwentyFourHourGridItem defers to OnClick
private static final int LONG_CLICK_RESULT_LEEWAY = 150;
// TODO: Private? // TODO: Private?
// Describes both AM/PM in the 12-hour clock and the half-days of the 24-hour clock. // Describes both AM/PM in the 12-hour clock and the half-days of the 24-hour clock.
@ -133,8 +139,17 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
private int mCurrentIndex = HOUR_INDEX; private int mCurrentIndex = HOUR_INDEX;
private int mSelectedHalfDay = HALF_DAY_1; private int mSelectedHalfDay = HALF_DAY_1;
private int mSelectedHourOfDay;
private int mSelectedMinute;
private Handler mHandler;
@Bind(R.id.grid_layout) GridLayout mGridLayout; @Bind(R.id.grid_layout) GridLayout mGridLayout;
@Bind(R.id.fab) FloatingActionButton mDoneButton;
// These are currently defined as Buttons in the dialog's layout,
// but we refer to them as TextViews to save an extra refactoring
// step in case we change them.
@Bind(R.id.half_day_toggle_1) FrameLayout mLeftHalfDayToggle;
@Bind(R.id.half_day_toggle_2) FrameLayout mRightHalfDayToggle;
@Override @Override
protected int contentLayout() { protected int contentLayout() {
@ -216,10 +231,55 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
private void setClickListenersOnButtons() { private void setClickListenersOnButtons() {
for (int i = 0; i < mGridLayout.getChildCount(); i++) { for (int i = 0; i < mGridLayout.getChildCount(); i++) {
// TODO: Consider leaving out the minute tuner buttons // TODO: Consider leaving out the minute tuner buttons
mGridLayout.getChildAt(i).setOnClickListener(mOnNumberClickListener); View v = mGridLayout.getChildAt(i);
v.setOnClickListener(mOnNumberClickListener);
if (v instanceof TwentyFourHourGridItem) {
v.setOnLongClickListener(mOn24HourItemLongClickListener);
}
} }
} }
@OnClick({ R.id.half_day_toggle_1, R.id.half_day_toggle_2 })
void onHalfDayToggleClick(View v) {
int halfDay = v == mLeftHalfDayToggle ? HALF_DAY_1 : HALF_DAY_2;
if (halfDay != mSelectedHalfDay) {
toggleHalfDay();
}
}
private void toggleHalfDay() {
// int amOrPm = mTimePicker.getIsCurrentlyAmOrPm();
int amOrPm = mSelectedHalfDay;
// TODO: Use HALF_DAY_1 and 2 instead
if (amOrPm == AM) {
amOrPm = PM;
} else if (amOrPm == PM){
amOrPm = AM;
}
updateAmPmDisplay(amOrPm);
// mTimePicker.setAmOrPm(amOrPm);
mSelectedHalfDay = amOrPm;
if (mIs24HourMode) {
if (mCurrentIndex == HOUR_INDEX) {
for (int i = 0; i < mGridLayout.getChildCount(); i++) {
View v = mGridLayout.getChildAt(i);
((TwentyFourHourGridItem) v).swapTexts();
}
}
// TODO: Verify the corresponding TwentyFourHourGridItem retains its indicator
if (amOrPm == HALF_DAY_1) {
mSelectedHourOfDay -= 12;
} else if (amOrPm == HALF_DAY_2) {
mSelectedHourOfDay += 12;
}
onValueSelected(HOUR_INDEX, mSelectedHourOfDay, false);
}
}
// TODO: Break this into two OnClickListeners instead--one for normal TextViews and
// the other for TwentyFourHourGridItem.
// TODO: Set the indicator
private final OnClickListener mOnNumberClickListener = new OnClickListener() { private final OnClickListener mOnNumberClickListener = new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -235,6 +295,19 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
onValueSelected(mCurrentIndex, Integer.parseInt(number), true); onValueSelected(mCurrentIndex, Integer.parseInt(number), true);
} }
}; };
private final View.OnLongClickListener mOn24HourItemLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View v) {
// TODO: Do we need this if we already check this before setting the listener on the view?
if (!(v instanceof TwentyFourHourGridItem))
return false;
toggleHalfDay();
mOnNumberClickListener.onClick(v);
return true;
}
};
// ============================================================================================= // =============================================================================================
/** /**
@ -324,6 +397,7 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// The Activity is created at this point // The Activity is created at this point
mIs24HourMode = DateFormat.is24HourFormat(getActivity()); mIs24HourMode = DateFormat.is24HourFormat(getActivity());
mHandler = new Handler();
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY) if (savedInstanceState != null && savedInstanceState.containsKey(KEY_HOUR_OF_DAY)
&& savedInstanceState.containsKey(KEY_MINUTE) && savedInstanceState.containsKey(KEY_MINUTE)
&& savedInstanceState.containsKey(KEY_IS_24_HOUR_VIEW)) { && savedInstanceState.containsKey(KEY_IS_24_HOUR_VIEW)) {
@ -345,12 +419,12 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
// view.findViewById(R.id.time_picker_dialog).setOnKeyListener(keyboardListener); // view.findViewById(R.id.time_picker_dialog).setOnKeyListener(keyboardListener);
View view = super.onCreateView(inflater, container, savedInstanceState); View view = super.onCreateView(inflater, container, savedInstanceState);
// Inflate the buttons into the grid // Inflate the buttons into the grid
int layout = mIs24HourMode ? R.layout.content_24h_number_grid : R.layout.content_number_grid; int layout = mIs24HourMode ? R.layout.content_24h_number_grid : R.layout.content_number_grid;
View.inflate(getActivity(), layout, mGridLayout); View.inflate(getActivity(), layout, mGridLayout);
setNumberTexts(); setNumberTexts();
setClickListenersOnButtons(); setClickListenersOnButtons();
if (mCurrentIndex == MINUTE_INDEX) { if (mCurrentIndex == MINUTE_INDEX) {
// Add the minute tuner buttons as well // Add the minute tuner buttons as well
View.inflate(getActivity(), R.layout.content_number_grid_minute_tuners, mGridLayout); View.inflate(getActivity(), R.layout.content_number_grid_minute_tuners, mGridLayout);
@ -377,6 +451,21 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
mAmText = amPmTexts[0]; mAmText = amPmTexts[0];
mPmText = amPmTexts[1]; mPmText = amPmTexts[1];
TextView tv1 = (TextView) mLeftHalfDayToggle.getChildAt(0);
TextView tv2 = (TextView) mRightHalfDayToggle.getChildAt(0);
if (mIs24HourMode) {
tv1.setText("00-11");
// Intrinsic bounds meaning the drawable's own bounds? So 24dp box.
tv1.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_half_day_1_black_24dp, 0, 0, 0);
tv2.setText("12-23");
tv2.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_half_day_2_black_24dp, 0, 0, 0);
} else {
tv1.setText(mAmText);
tv2.setText(mPmText);
}
// mHapticFeedbackController = new HapticFeedbackController(getActivity()); // mHapticFeedbackController = new HapticFeedbackController(getActivity());
// mTimePicker = (RadialPickerLayout) view.findViewById(R.id.time_picker); // mTimePicker = (RadialPickerLayout) view.findViewById(R.id.time_picker);
@ -409,21 +498,25 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
}); });
// mDoneButton = (TextView) view.findViewById(R.id.done_button); // mDoneButton = (TextView) view.findViewById(R.id.done_button);
// mDoneButton.setOnClickListener(new OnClickListener() { mDoneButton.setOnClickListener(new OnClickListener() {
// @Override @Override
// public void onClick(View v) { public void onClick(View v) {
// if (mInKbMode && isTypedTimeFullyLegal()) { if (mInKbMode && isTypedTimeFullyLegal()) { // TODO: Delete
// finishKbMode(false); // finishKbMode(false);
// } else { } else {
// tryVibrate(); tryVibrate();
// } }
// if (mCallback != null) { Log.i(TAG, String.format("Selected time is %02d:%02d", mSelectedHourOfDay, mSelectedMinute));
// mCallback.onTimeSet(mTimePicker, // TODO: Use your OnTimeSetListener
// mTimePicker.getHours(), mTimePicker.getMinutes()); if (mCallback != null) {
// } // mCallback.onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
// dismiss(); // I don't think the listener actually uses the first param passed back,
// } // so passing null is fine.
// }); mCallback.onTimeSet(null, mSelectedHourOfDay, mSelectedMinute);
}
dismiss();
}
});
// mDoneButton.setOnKeyListener(keyboardListener); // mDoneButton.setOnKeyListener(keyboardListener);
// Enable or disable the AM/PM view. // Enable or disable the AM/PM view.
@ -444,15 +537,14 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
public void onClick(View v) { public void onClick(View v) {
tryVibrate(); tryVibrate();
// int amOrPm = mTimePicker.getIsCurrentlyAmOrPm(); // int amOrPm = mTimePicker.getIsCurrentlyAmOrPm();
int amOrPm = mSelectedHalfDay; // if (amOrPm == AM) {
if (amOrPm == AM) { // amOrPm = PM;
amOrPm = PM; // } else if (amOrPm == PM){
} else if (amOrPm == PM){ // amOrPm = AM;
amOrPm = AM; // }
} // updateAmPmDisplay(amOrPm);
updateAmPmDisplay(amOrPm);
// mTimePicker.setAmOrPm(amOrPm); // mTimePicker.setAmOrPm(amOrPm);
mSelectedHalfDay = amOrPm; toggleHalfDay();
} }
}); });
} }
@ -578,6 +670,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
} }
private void setHour(int value, boolean announce) { private void setHour(int value, boolean announce) {
// TOneverDO: Set after if-else block (modulo operation changes value!)
mSelectedHourOfDay = value;
String format; String format;
if (mIs24HourMode) { if (mIs24HourMode) {
format = "%02d"; format = "%02d";
@ -605,6 +700,9 @@ public class NumberGridTimePickerDialog extends BaseTimePickerDialog /*DialogFra
// Utils.tryAccessibilityAnnounce(mTimePicker, text); // Utils.tryAccessibilityAnnounce(mTimePicker, text);
mMinuteView.setText(text); mMinuteView.setText(text);
mMinuteSpaceView.setText(text); mMinuteSpaceView.setText(text);
// Setting this here is fine.
mSelectedMinute = value;
} }
// Show either Hours or Minutes. // Show either Hours or Minutes.

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,15.31L23.31,12 20,8.69V4h-4.69L12,0.69 8.69,4H4v4.69L0.69,12 4,15.31V20h4.69L12,23.31 15.31,20H20v-4.69zM12,18V6c3.31,0 6,2.69 6,6s-2.69,6 -6,6z"/>
</vector>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- http://stackoverflow.com/a/8716798/5055032 -->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="180"
android:toDegrees="180"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/ic_half_day_1_black_24dp">
</rotate>

View File

@ -1,46 +1,93 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"> android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout <!-- TOneverDO: Use vertical LinearLayout as long as
android:id="@+id/time_display_background" GridLayout has no fixed width -->
<RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent">
android:background="@android:color/white" >
<include <FrameLayout
layout="@layout/time_header_label" android:id="@+id/time_display_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/header_height" android:layout_height="wrap_content"
android:layout_gravity="center" /> android:background="@android:color/white" >
</FrameLayout> <include
layout="@layout/time_header_label"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:layout_gravity="center" />
</FrameLayout>
<android.support.v7.widget.GridLayout <android.support.v7.widget.GridLayout
android:id="@+id/grid_layout" android:id="@+id/grid_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/numpad_height" android:layout_height="@dimen/numpad_height"
android:layout_below="@id/time_display_background" android:layout_below="@id/time_display_background"
app:columnCount="3"/> app:columnCount="3"
android:layout_marginBottom="28dp"/>
<LinearLayout <View
android:layout_width="match_parent" android:id="@+id/divider"
style="@style/Divider.Horizontal"
android:layout_below="@id/grid_layout"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@id/divider"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/half_day_toggle_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center"
android:layout_gravity="center"
style="@style/TextAppearance.AppCompat.Button"
android:textSize="@dimen/half_day_text_size"/>
</FrameLayout>
<FrameLayout
android:id="@+id/half_day_toggle_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:gravity="center"
android:layout_gravity="center"
style="@style/TextAppearance.AppCompat.Button"
android:textSize="@dimen/half_day_text_size"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
<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:orientation="horizontal" android:src="@drawable/ic_done_24dp"
android:layout_below="@id/grid_layout"> app:layout_anchor="@id/divider"
app:layout_anchorGravity="center_horizontal"/>
<Button </android.support.design.widget.CoordinatorLayout>
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="AM"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="PM"/>
</LinearLayout>
</RelativeLayout>

View File

@ -23,7 +23,7 @@
<View style="@style/FocusGrabber" <View style="@style/FocusGrabber"
android:id="@+id/focus_grabber"/> android:id="@+id/focus_grabber"/>
<View style="@style/Divider" <View style="@style/Divider.Horizontal"
android:id="@+id/header_divider" android:id="@+id/header_divider"
android:layout_below="@id/input_time"/> android:layout_below="@id/input_time"/>
@ -38,7 +38,7 @@
android:layout_marginEnd="20dp" android:layout_marginEnd="20dp"
android:layout_marginBottom="28dp"/> android:layout_marginBottom="28dp"/>
<View style="@style/Divider" <View style="@style/Divider.Horizontal"
android:id="@+id/footer_divider" android:id="@+id/footer_divider"
android:layout_below="@id/number_grid"/> android:layout_below="@id/number_grid"/>

View File

@ -77,7 +77,7 @@
</RelativeLayout> </RelativeLayout>
<View style="@style/Divider" <View style="@style/Divider.Horizontal"
android:layout_marginTop="@dimen/item_margin_between_elements"/> android:layout_marginTop="@dimen/item_margin_between_elements"/>
</LinearLayout> </LinearLayout>

View File

@ -14,6 +14,7 @@
<dimen name="grid_element_text_size">28sp</dimen> <dimen name="grid_element_text_size">28sp</dimen>
<dimen name="grid_element_touch_target">40dp</dimen> <dimen name="grid_element_touch_target">40dp</dimen>
<dimen name="half_day_text_size">17sp</dimen>
<!-- NumpadTimePickerDialog --> <!-- NumpadTimePickerDialog -->
<dimen name="numpad_height">300dp</dimen> <dimen name="numpad_height">300dp</dimen>

View File

@ -41,6 +41,7 @@
<item name="android:textSize">@dimen/grid_element_text_size</item> <item name="android:textSize">@dimen/grid_element_text_size</item>
</style> </style>
<!-- TODO: Replace and Delete -->
<style name="hybrid_time_picker_grid_element"> <style name="hybrid_time_picker_grid_element">
<item name="android:layout_width">@dimen/grid_element_touch_target</item> <item name="android:layout_width">@dimen/grid_element_touch_target</item>
<item name="android:layout_height">@dimen/grid_element_touch_target</item> <item name="android:layout_height">@dimen/grid_element_touch_target</item>
@ -55,9 +56,17 @@
</style> </style>
<style name="Divider"> <style name="Divider">
<item name="android:background">?android:attr/listDivider</item>
</style>
<style name="Divider.Horizontal">
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item> <item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item> </style>
<style name="Divider.Vertical">
<item name="android:layout_width">1dp</item>
<item name="android:layout_height">match_parent</item>
</style> </style>
<style name="FocusGrabber"> <style name="FocusGrabber">