Skip to content

Commit

Permalink
[MaterialDatePicker] Allow client app to access user selected inputMode
Browse files Browse the repository at this point in the history
Resolves #3414

PiperOrigin-RevId: 539713099
  • Loading branch information
paulfthomas committed Jun 14, 2023
1 parent 2336c23 commit 4d80434
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Expand Up @@ -38,7 +38,6 @@
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
Expand Down Expand Up @@ -188,6 +187,7 @@ public final void onSaveInstanceState(@NonNull Bundle bundle) {
bundle.putParcelable(DAY_VIEW_DECORATOR_KEY, dayViewDecorator);
bundle.putInt(TITLE_TEXT_RES_ID_KEY, titleTextResId);
bundle.putCharSequence(TITLE_TEXT_KEY, titleText);
bundle.putInt(INPUT_MODE_KEY, inputMode);
bundle.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, positiveButtonTextResId);
bundle.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, positiveButtonText);
bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, negativeButtonTextResId);
Expand Down Expand Up @@ -384,6 +384,12 @@ public final S getSelection() {
return getDateSelector().getSelection();
}

/** Returns the current {@link InputMode}. */
@InputMode
public int getInputMode() {
return inputMode;
}

private void enableEdgeToEdgeIfNeeded(Window window) {
if (edgeToEdgeEnabled) {
// Avoid enabling edge-to-edge multiple times.
Expand Down Expand Up @@ -415,10 +421,10 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
edgeToEdgeEnabled = true;
}

private void updateTitle(boolean textInputMode) {
private void updateTitle() {
// Set up title text forcing single line for landscape text input mode due to space constraints.
headerTitleTextView.setText(
textInputMode && isLandscape() ? singleLineTitleText : fullTitleText);
inputMode == INPUT_MODE_TEXT && isLandscape() ? singleLineTitleText : fullTitleText);
}

@VisibleForTesting
Expand All @@ -436,13 +442,13 @@ private void startPickerFragment() {
calendar =
MaterialCalendar.newInstance(
getDateSelector(), themeResId, calendarConstraints, dayViewDecorator);
boolean textInputMode = headerToggleButton.isChecked();

pickerFragment =
textInputMode
inputMode == INPUT_MODE_TEXT
? MaterialTextInputPicker.newInstance(
getDateSelector(), themeResId, calendarConstraints)
: calendar;
updateTitle(textInputMode);
updateTitle();
updateHeader(getHeaderText());

FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
Expand Down Expand Up @@ -474,22 +480,20 @@ private void initHeaderToggle(Context context) {
ViewCompat.setAccessibilityDelegate(headerToggleButton, null);
updateToggleContentDescription(headerToggleButton);
headerToggleButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
// Update confirm button in case in progress selection has been reset
confirmButton.setEnabled(getDateSelector().isSelectionComplete());

headerToggleButton.toggle();
updateToggleContentDescription(headerToggleButton);
startPickerFragment();
}
v -> {
// Update confirm button in case in progress selection has been reset
confirmButton.setEnabled(getDateSelector().isSelectionComplete());

headerToggleButton.toggle();
inputMode = (inputMode == INPUT_MODE_TEXT) ? INPUT_MODE_CALENDAR : INPUT_MODE_TEXT;
updateToggleContentDescription(headerToggleButton);
startPickerFragment();
});
}

private void updateToggleContentDescription(@NonNull CheckableImageButton toggle) {
String contentDescription =
headerToggleButton.isChecked()
inputMode == INPUT_MODE_TEXT
? toggle.getContext().getString(R.string.mtrl_picker_toggle_to_calendar_input_mode)
: toggle.getContext().getString(R.string.mtrl_picker_toggle_to_text_input_mode);
headerToggleButton.setContentDescription(contentDescription);
Expand Down
Expand Up @@ -16,6 +16,7 @@

package com.google.android.material.datepicker;

import static com.google.android.material.datepicker.MaterialDatePicker.INPUT_MODE_CALENDAR;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Calendar.APRIL;
import static java.util.Calendar.FEBRUARY;
Expand Down Expand Up @@ -95,7 +96,6 @@ private static void testThisMonthInUtcMillisecondsForLocalTime(

@Test
public void testSelectionAsOpenAt() {

MaterialDatePicker.Builder<Long> datePickerBuilder = MaterialDatePicker.Builder.datePicker();
CalendarConstraints calendarConstraints =
new CalendarConstraints.Builder().setStart(FEB_2016).setEnd(APRIL_2016).build();
Expand Down Expand Up @@ -178,4 +178,11 @@ public void testFirstDayOfWeekAsDefault() {
datePickerBuilder.build();
assertEquals(0, calendarConstraints.getFirstDayOfWeek());
}

@Test
public void testInputModeCalendarAsDefault() {
MaterialDatePicker<Long> materialDatePicker = MaterialDatePicker.Builder.datePicker().build();

assertEquals(INPUT_MODE_CALENDAR, materialDatePicker.getInputMode());
}
}

0 comments on commit 4d80434

Please sign in to comment.