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

Group side effecting setters to an edit() call #298

Closed
ekchang opened this issue May 13, 2016 · 1 comment
Closed

Group side effecting setters to an edit() call #298

ekchang opened this issue May 13, 2016 · 1 comment

Comments

@ekchang
Copy link
Contributor

ekchang commented May 13, 2016

Problem: Things like setCalendarMode, setMinDate, setMaxDate, and setFirstDayOfWeek are side effecting, namely they rebuild adapters and recreate views on every call. This is very wasteful especially if you need to set all of them at once, and usually you only need to set them once. A lot of things are GC'd with each call. Worst of all, the order that each of them are called matters and it may not be clear to the user why the order should matter.

We need to separate setters that simply decorate/resize the view (like tileHeight/Width, decorators, select current date) to ones that actually modify adapter state and current page (min/max date, first day of the week).

Propose an immutable MaterialCalendarView.State object which is accessed through mcv.state(). Call edit() which creates a new builder from existing state to modify side effecting parameters all at once, then call commit() to trigger rebuild of all adapters and child views.

Something like this would be the ideal API:

// MCV inflated with default state or state defined in XML
MaterialCalendarView mcv = (MaterialCalendarView) findViewById(R.id.calendar);

mcv.setSelectedDate(CalendarDay.today()); // non side effecting, this is fine. selected days are stored as a list

// Modify existing state
mcv.state().edit()
  .setCalendarMode(CalendarMode.WEEK)
  .setMinDate(CalendarDay.from(2016, 4, 4))
  .setMaxDate(CalendarDay.from(2016, 6, 24))
  .setFirstDayOfWeek(Calendar.TUESDAY)
  .commit(); // triggers rebuild of all adapters and childviews

// Make a new state (with defaults)
mcv.newState().setCalendarMode(CalendarMode.WEEK).commit();

commit() should be the ONLY method that requires creating an adapter and telling all children to rebuild state based on the new state.

@quentin41500 quentin41500 added this to the 1.X milestone May 20, 2016
@ekchang
Copy link
Contributor Author

ekchang commented Jun 1, 2016

Implemented in 1.4.0

@ekchang ekchang closed this as completed Jun 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants