Skip to content

Commit

Permalink
Merge pull request #49 from udaysrinath/master
Browse files Browse the repository at this point in the history
Add support for landscape mode, saveInstance(lastSelectedDate) on rotation
  • Loading branch information
Jonatan E. Salas committed Aug 12, 2017
2 parents e2e8e76 + d1e18c6 commit 615f511
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<activity
android:name=".view.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public final class CalendarView extends LinearLayout {

private static final String KEY_STATE = "superState";
private static final String KEY_MONTH_INDEX = "currentMonthIndex";
private static final String KEY_SELECTED_DATE = "selectedDate";


private static final int SUNDAY = 1;
private static final int MONDAY = 2;
Expand Down Expand Up @@ -245,6 +247,7 @@ protected Parcelable onSaveInstanceState() {

stateToSave.putParcelable(KEY_STATE, superState);
stateToSave.putInt(KEY_MONTH_INDEX, currentMonthIndex);
stateToSave.putSerializable(KEY_SELECTED_DATE, lastSelectedDay);

return stateToSave;
}
Expand All @@ -254,15 +257,31 @@ protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
final Bundle savedInstanceState = (Bundle) state;

state = savedInstanceState.getParcelable(KEY_STATE);
currentMonthIndex = savedInstanceState.getInt(KEY_MONTH_INDEX);
if (savedInstanceState != null) {
state = savedInstanceState.getParcelable(KEY_STATE);
currentMonthIndex = savedInstanceState.getInt(KEY_MONTH_INDEX);
if (savedInstanceState.getSerializable(KEY_SELECTED_DATE) != null) {
lastSelectedDay = (Date) savedInstanceState.getSerializable(KEY_SELECTED_DATE);
}
}
Calendar calendar = (lastSelectedDay != null) ? getCalDate(lastSelectedDay) : Calendar.getInstance(Locale.getDefault());
update(calendar);

update(Calendar.getInstance(Locale.getDefault()));
if (lastSelectedDay != null) {
markDateAsSelected(lastSelectedDay);
onDateClickListener.onDateClick(lastSelectedDay);
}
}

super.onRestoreInstanceState(state);
}

private Calendar getCalDate(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}

private void initTouchVariables() {
final ViewConfiguration configuration = ViewConfiguration.get(getContext());
final float density = getContext().getResources().getDisplayMetrics().density;
Expand Down

0 comments on commit 615f511

Please sign in to comment.