Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to fix/unfix the view's dimens for use in dialogs #93

Merged
merged 1 commit into from
Jan 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 59 additions & 23 deletions library/src/com/squareup/timessquare/CalendarPickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,7 @@ public FluentInitializer withSelectedDates(Collection<Date> selectedDates) {
selectDate(date);
}
}
Integer selectedIndex = null;
Integer todayIndex = null;
Calendar today = Calendar.getInstance(locale);
for (int c = 0; c < months.size(); c++) {
MonthDescriptor month = months.get(c);
if (selectedIndex == null) {
for (Calendar selectedCal : selectedCals) {
if (sameMonth(selectedCal, month)) {
selectedIndex = c;
break;
}
}
if (selectedIndex == null && todayIndex == null && sameMonth(today, month)) {
todayIndex = c;
}
}
}
if (selectedIndex != null) {
scrollToSelectedMonth(selectedIndex);
} else if (todayIndex != null) {
scrollToSelectedMonth(todayIndex);
}
scrollToSelectedDates();

validateAndUpdate();
return this;
Expand All @@ -292,11 +271,68 @@ private void scrollToSelectedMonth(final int selectedIndex) {
post(new Runnable() {
@Override
public void run() {
smoothScrollToPosition(selectedIndex);
Logr.d("Scrolling to position %d", selectedIndex);
setSelection(selectedIndex);
}
});
}

private void scrollToSelectedDates() {
Integer selectedIndex = null;
Integer todayIndex = null;
Calendar today = Calendar.getInstance(locale);
for (int c = 0; c < months.size(); c++) {
MonthDescriptor month = months.get(c);
if (selectedIndex == null) {
for (Calendar selectedCal : selectedCals) {
if (sameMonth(selectedCal, month)) {
selectedIndex = c;
break;
}
}
if (selectedIndex == null && todayIndex == null && sameMonth(today, month)) {
todayIndex = c;
}
}
}
if (selectedIndex != null) {
scrollToSelectedMonth(selectedIndex);
} else if (todayIndex != null) {
scrollToSelectedMonth(todayIndex);
}
}

/**
* This method should only be called if the calendar is contained in a dialog, and it should only
* be called once, right after the dialog is shown (using {@link android.content.DialogInterface.OnShowListener}
* or {@link android.app.DialogFragment#onStart()}).
*/
public void fixDialogDimens() {
Logr.d("Fixing dimensions to h = %d / w = %d", getMeasuredHeight(), getMeasuredWidth());
// Fix the layout height/width after the dialog has been shown.
getLayoutParams().height = getMeasuredHeight();
getLayoutParams().width = getMeasuredWidth();
// Post this runnable so it runs _after_ the dimen changes have been applied/re-measured.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is re-measuring guaranteed here? You aren't doing anything to trigger a layout. Or are you assuming the initial layout pass hasn't happened yet?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nevermind. I get it. You're changing the params to be fixed at the measured size.

post(new Runnable() {
@Override public void run() {
Logr.d("Dimens are fixed: now scroll to the selected date");
scrollToSelectedDates();
}
});
}

/**
* This method should only be called if the calendar is contained in a dialog, and it should only
* be called when the screen has been rotated and the dialog should be re-measured.
*/
public void unfixDialogDimens() {
Logr.d("Reset the fixed dimensions to allow for re-measurement");
// Fix the layout height/width after the dialog has been shown.
getLayoutParams().height = LayoutParams.MATCH_PARENT;
getLayoutParams().width = LayoutParams.MATCH_PARENT;
requestLayout();
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (months.isEmpty()) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
Expand All @@ -20,6 +21,8 @@
public class SampleTimesSquareActivity extends Activity {
private static final String TAG = "SampleTimesSquareActivity";
private CalendarPickerView calendar;
private AlertDialog theDialog;
private CalendarPickerView dialogView;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -94,19 +97,25 @@ public void onClick(View v) {

dialog.setOnClickListener(new OnClickListener() {
@Override public void onClick(View view) {
CalendarPickerView dialogView =
(CalendarPickerView) getLayoutInflater().inflate(R.layout.dialog, null, false);
dialogView.init(new Date(), nextYear.getTime());
new AlertDialog.Builder(SampleTimesSquareActivity.this)
.setTitle("I'm a dialog!")
.setView(dialogView)
.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create()
.show();
dialogView = (CalendarPickerView) getLayoutInflater().inflate(R.layout.dialog, null, false);
dialogView.init(lastYear.getTime(), nextYear.getTime()) //
.withSelectedDate(new Date());
theDialog =
new AlertDialog.Builder(SampleTimesSquareActivity.this).setTitle("I'm a dialog!")
.setView(dialogView)
.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create();
theDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override public void onShow(DialogInterface dialogInterface) {
Log.d(TAG, "onShow: fix the dimens!");
dialogView.fixDialogDimens();
}
});
theDialog.show();
}
});

Expand All @@ -119,4 +128,21 @@ public void onClick(View view) {
}
});
}

@Override public void onConfigurationChanged(Configuration newConfig) {
boolean applyFixes = theDialog != null && theDialog.isShowing();
if (applyFixes) {
Log.d(TAG, "Config change: unfix the dimens so I'll get remeasured!");
dialogView.unfixDialogDimens();
}
super.onConfigurationChanged(newConfig);
if (applyFixes) {
dialogView.post(new Runnable() {
@Override public void run() {
Log.d(TAG, "Config change done: re-fix the dimens!");
dialogView.fixDialogDimens();
}
});
}
}
}