Skip to content

Commit

Permalink
WIP: update text sync dialog - add support for negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Mar 15, 2020
1 parent 85f25aa commit c443ebe
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class MultiFieldTimePickerDialog
extends AlertDialog implements OnClickListener {

private final NumberPicker mSignSpinner;
private final NumberPicker mHourSpinner;
private final NumberPicker mMinuteSpinner;
private final NumberPicker mSecSpinner;
Expand All @@ -38,12 +39,13 @@ public class MultiFieldTimePickerDialog
private final int mStep;
private final int mBaseMilli;
private final boolean mIs24hourFormat;
private final boolean mIsSigned;

/**
* Adds an onTimeSet() method.
*/
public interface OnMultiFieldTimeSetListener {
void onTimeSet(int hourOfDay, int minute, int second, int milli);
void onTimeSet(boolean isNegative, int hourOfDay, int minute, int second, int milli);
}

private static final int SECOND_IN_MILLIS = 1000;
Expand All @@ -53,13 +55,15 @@ public interface OnMultiFieldTimeSetListener {
public MultiFieldTimePickerDialog(
Context context,
int theme,
boolean isNegative,
int hour, int minute, int second, int milli,
int min, int max, int step, boolean is24hourFormat,
int min, int max, int step, boolean is24hourFormat, boolean isSigned,
OnMultiFieldTimeSetListener listener) {
super(context, theme);
mListener = listener;
mStep = step;
mIs24hourFormat = is24hourFormat;
mIsSigned = isSigned;

if (min >= max) {
min = 0;
Expand All @@ -75,6 +79,7 @@ public MultiFieldTimePickerDialog(
View view = inflater.inflate(R.layout.multi_field_time_picker_dialog, null);
setView(view);

mSignSpinner = (NumberPicker) view.findViewById(R.id.sign);
mHourSpinner = (NumberPicker) view.findViewById(R.id.hour);
mMinuteSpinner = (NumberPicker) view.findViewById(R.id.minute);
mSecSpinner = (NumberPicker) view.findViewById(R.id.second);
Expand All @@ -91,6 +96,18 @@ public MultiFieldTimePickerDialog(
hour = minHour;
}

if (!isSigned) {
mSignSpinner.setVisibility(View.GONE);
} else {
int minSign = 0; // + positive
int maxSign = 1; // - negative
int iniSign = isNegative ? maxSign : minSign;
mSignSpinner.setMinValue(minSign);
mSignSpinner.setMaxValue(maxSign);
mSignSpinner.setDisplayedValues(new String[] { "+", "-" });
mSignSpinner.setValue(iniSign);
}

if (is24hourFormat) {
mAmPmSpinner.setVisibility(View.GONE);
} else {
Expand Down Expand Up @@ -171,7 +188,7 @@ public MultiFieldTimePickerDialog(

if (step >= MINUTE_IN_MILLIS) {
// Remove the ':' in front of the second spinner as well.
view.findViewById(R.id.second_colon).setVisibility(View.GONE);
view.findViewById(R.id.second_sep).setVisibility(View.GONE);
mSecSpinner.setVisibility(View.GONE);
}

Expand Down Expand Up @@ -200,7 +217,7 @@ public MultiFieldTimePickerDialog(

if (step >= SECOND_IN_MILLIS) {
// Remove the '.' in front of the milli spinner as well.
view.findViewById(R.id.second_dot).setVisibility(View.GONE);
view.findViewById(R.id.milli_sep).setVisibility(View.GONE);
mMilliSpinner.setVisibility(View.GONE);
}

Expand Down Expand Up @@ -262,7 +279,13 @@ private void notifyDateSet() {
}
hour += ampm * 12;
}
mListener.onTimeSet(hour, minute, sec, milli);
boolean isNegative = false;
if (mIsSigned) {
int sign = getPickerValue(mSignSpinner);
if (sign == 1)
isNegative = true;
}
mListener.onTimeSet(isNegative, hour, minute, sec, milli);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ private static class TextSynchronizerListener implements MultiFieldTimePickerDia
}

@Override
public void onTimeSet(int hourOfDay, int minute, int second, int milli) {
public void onTimeSet(boolean isNegative, int hourOfDay, int minute, int second, int milli) {
long textOffsetMs = (milli) + (second * 1000) + (minute * 60 * 1000) + (hourOfDay * 60 * 60 * 1000);
long textOffsetUs = (textOffsetMs * 1000);

if (isNegative)
textOffsetUs *= -1;

textSynchronizer.setTextOffset(textOffsetUs);
}

Expand All @@ -39,8 +42,9 @@ public void onTimeSet(long textOffsetUs) {

private static void showPickerDialog(
Context mContext,
boolean isNegative,
int hourOfDay, int minute, int second, int millis,
int min, int max, int step, boolean is24hourFormat,
int min, int max, int step, boolean is24hourFormat, boolean isSigned,
MultiFieldTimePickerDialog.OnMultiFieldTimeSetListener mListener,
DialogInterface.OnDismissListener onDismissListener
) {
Expand All @@ -49,8 +53,9 @@ private static void showPickerDialog(
mDialog = new MultiFieldTimePickerDialog(
mContext,
/* theme= */ 0,
isNegative,
hourOfDay, minute, second, millis,
min, max, step, is24hourFormat,
min, max, step, is24hourFormat, isSigned,
mListener
);

Expand Down Expand Up @@ -96,6 +101,11 @@ public static void show(

long offsetPositionUs = textSynchronizer.getTextOffset();

boolean isNegative = (offsetPositionUs < 0);

if (isNegative)
offsetPositionUs *= -1;

int hourOfDay = (int) TimeUnit.MICROSECONDS.toHours(offsetPositionUs);
int minute = (int) (TimeUnit.MICROSECONDS.toMinutes(offsetPositionUs) % 60);
int second = (int) (TimeUnit.MICROSECONDS.toSeconds(offsetPositionUs) % 60);
Expand All @@ -106,11 +116,13 @@ public static void show(
int step = 100; // 100 ms

boolean is24hourFormat = true;
boolean isSigned = true;

showPickerDialog(
mContext,
isNegative,
hourOfDay, minute, second, millis,
min, max, step, is24hourFormat,
min, max, step, is24hourFormat, isSigned,
mListener,
onDismissListener
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
android:orientation="horizontal"
android:gravity="center">

<NumberPicker
android:id="@+id/sign"
android:layout_width="0dp"
android:layout_weight=".2"
android:textSize="12sp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:contentDescription="@string/accessibility_time_picker_sign"
/>
<NumberPicker
android:id="@+id/hour"
android:layout_width="0dp"
Expand Down Expand Up @@ -70,11 +82,12 @@
android:contentDescription="@string/accessibility_time_picker_minute"
/>
<TextView
android:id="@+id/second_colon"
android:id="@+id/second_sep"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/time_picker_dialog_minute_second_separator" />
android:text="@string/time_picker_dialog_minute_second_separator"
/>
<NumberPicker
android:id="@+id/second"
android:layout_width="0dp"
Expand All @@ -88,11 +101,11 @@
android:contentDescription="@string/accessibility_time_picker_second"
/>
<TextView
android:id="@+id/second_dot"
android:id="@+id/milli_sep"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/time_picker_dialog_second_subsecond_separator"
android:text="@string/time_picker_dialog_second_milli_separator"
/>
<NumberPicker
android:id="@+id/milli"
Expand All @@ -106,6 +119,13 @@
android:focusableInTouchMode="true"
android:contentDescription="@string/accessibility_time_picker_milli"
/>
<TextView
android:id="@+id/ampm_sep"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:text="@string/time_picker_dialog_milli_ampm_separator"
/>
<NumberPicker
android:id="@+id/ampm"
android:layout_width="0dp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
<string name="time_picker_dialog_am">"a.m."</string>
<string name="time_picker_dialog_pm">"p.m."</string>

<string name="accessibility_time_picker_sign">"Positive/Negative Sign"</string>
<string name="accessibility_time_picker_hour">"Hour"</string>
<string name="accessibility_time_picker_minute">"Minute"</string>
<string name="accessibility_time_picker_second">"Second"</string>
<string name="accessibility_time_picker_milli">"Millisecond"</string>
<string name="accessibility_time_picker_ampm">"AM/PM"</string>

<string name="time_picker_dialog_hour_minute_separator">":"</string>
<string name="time_picker_dialog_minute_second_separator">":"</string>
<string name="time_picker_dialog_second_subsecond_separator">"."</string>
<string name="time_picker_dialog_hour_minute_separator">"hr :"</string>
<string name="time_picker_dialog_minute_second_separator">"min :"</string>
<string name="time_picker_dialog_second_milli_separator">"sec ."</string>
<string name="time_picker_dialog_milli_ampm_separator">"ms"</string>
</resources>

0 comments on commit c443ebe

Please sign in to comment.