Skip to content

Commit

Permalink
EditEventView: Migrate to material time picker
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Apr 4, 2024
1 parent f896410 commit 79145d2
Showing 1 changed file with 66 additions and 93 deletions.
159 changes: 66 additions & 93 deletions app/src/main/java/com/android/calendar/event/EditEventView.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.TimePicker;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -95,6 +94,8 @@

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.timepicker.MaterialTimePicker;
import com.google.android.material.timepicker.TimeFormat;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -409,8 +410,8 @@ private void populateWhen() {
mStartDateButton.setOnClickListener(new DateClickListener(mStartTime));
mEndDateButton.setOnClickListener(new DateClickListener(mEndTime));

mStartTimeButton.setOnClickListener(new TimeClickListener(mStartTime));
mEndTimeButton.setOnClickListener(new TimeClickListener(mEndTime));
mStartTimeButton.setOnClickListener(this::showTimerPickerDialog);
mEndTimeButton.setOnClickListener(this::showTimerPickerDialog);
}

// Implements OnTimeZoneSetListener
Expand Down Expand Up @@ -1658,6 +1659,68 @@ private void updateHomeTime() {
public void onNothingSelected(AdapterView<?> parent) {
}

public void showTimerPickerDialog(View view) {
int timeFormat = DateFormat.is24HourFormat(mActivity) ? TimeFormat.CLOCK_24H : TimeFormat.CLOCK_12H;

MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
.setHour((view == mStartTimeButton) ? mStartTime.getHour() : mEndTime.getHour())
.setMinute((view == mStartTimeButton) ? mStartTime.getMinute() : mEndTime.getMinute())
.setTimeFormat(timeFormat)
.build();

materialTimePicker.addOnPositiveButtonClickListener((dialog) -> {
onTimeSet(view, materialTimePicker.getHour(), materialTimePicker.getMinute());
});
materialTimePicker.show(mActivity.getSupportFragmentManager(), "TimePicker");
}

public void onTimeSet(View view, int hourOfDay, int minute) {
// Cache the member variables locally to avoid inner class overhead.
Time startTime = mStartTime;
Time endTime = mEndTime;

// Cache the start and end millis so that we limit the number
// of calls to normalize() and toMillis(), which are fairly
// expensive.
long startMillis;
long endMillis;
if (view == mStartTimeButton) {
// The start time was changed.
int hourDuration = endTime.getHour() - startTime.getHour();
int minuteDuration = endTime.getMinute() - startTime.getMinute();

startTime.setHour(hourOfDay);
startTime.setMinute(minute);
startMillis = startTime.normalize();

// Also update the end time to keep the duration constant.
endTime.setHour(hourOfDay + hourDuration);
endTime.setMinute(minute + minuteDuration);

// Update tz in case the start time switched from/to DLS
populateTimezone(startMillis);
} else {
// The end time was changed.
startMillis = startTime.toMillis();
endTime.setHour(hourOfDay);
endTime.setMinute(minute);

// Move to the start time if the end time is before the start
// time.
if (endTime.compareTo(startTime) < 0) {
endTime.setDay(startTime.getDay() + 1);
}
// Call populateTimezone if we support end time zone as well
}

endMillis = endTime.normalize();

setDate(mEndDateButton, endMillis);
setTime(mStartTimeButton, startMillis);
setTime(mEndTimeButton, endMillis);
updateHomeTime();
}

public static class CalendarsAdapter extends ResourceCursorAdapter {
public CalendarsAdapter(Context context, int resourceId, Cursor c) {
super(context, resourceId, c);
Expand Down Expand Up @@ -1689,96 +1752,6 @@ public void bindView(View view, Context context, Cursor cursor) {
}
}

/* This class is used to update the time buttons. */
private class TimeListener implements TimePickerDialog.OnTimeSetListener {
private View mView;

public TimeListener(View view) {
mView = view;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Cache the member variables locally to avoid inner class overhead.
Time startTime = mStartTime;
Time endTime = mEndTime;

// Cache the start and end millis so that we limit the number
// of calls to normalize() and toMillis(), which are fairly
// expensive.
long startMillis;
long endMillis;
if (mView == mStartTimeButton) {
// The start time was changed.
int hourDuration = endTime.getHour() - startTime.getHour();
int minuteDuration = endTime.getMinute() - startTime.getMinute();

startTime.setHour(hourOfDay);
startTime.setMinute(minute);
startMillis = startTime.normalize();

// Also update the end time to keep the duration constant.
endTime.setHour(hourOfDay + hourDuration);
endTime.setMinute(minute + minuteDuration);

// Update tz in case the start time switched from/to DLS
populateTimezone(startMillis);
} else {
// The end time was changed.
startMillis = startTime.toMillis();
endTime.setHour(hourOfDay);
endTime.setMinute(minute);

// Move to the start time if the end time is before the start
// time.
if (endTime.compareTo(startTime) < 0) {
endTime.setDay(startTime.getDay() + 1);
}
// Call populateTimezone if we support end time zone as well
}

endMillis = endTime.normalize();

setDate(mEndDateButton, endMillis);
setTime(mStartTimeButton, startMillis);
setTime(mEndTimeButton, endMillis);
updateHomeTime();
}
}

private class TimeClickListener implements View.OnClickListener {
private Time mTime;

public TimeClickListener(Time time) {
mTime = time;
}

@Override
public void onClick(View v) {

TimePickerDialog dialog;
if (v == mStartTimeButton) {
if (mStartTimePickerDialog != null) {
mStartTimePickerDialog.dismiss();
}
mStartTimePickerDialog = new TimePickerDialog(mActivity, new TimeListener(v),
mTime.getHour(), mTime.getMinute(), DateFormat.is24HourFormat(mActivity));
dialog = mStartTimePickerDialog;
} else {
if (mEndTimePickerDialog != null) {
mEndTimePickerDialog.dismiss();
}
mEndTimePickerDialog = new TimePickerDialog(mActivity, new TimeListener(v),
mTime.getHour(), mTime.getMinute(), DateFormat.is24HourFormat(mActivity));
dialog = mEndTimePickerDialog;

}

dialog.show();

}
}

private class DateListener implements DatePickerDialog.OnDateSetListener {
View mView;

Expand Down

0 comments on commit 79145d2

Please sign in to comment.