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

Send also the old state to the onStateChanged() callback #11

Merged
merged 1 commit into from
Aug 28, 2018

Conversation

riclage
Copy link
Contributor

@riclage riclage commented Aug 14, 2018

Problem

Listeners of the onStateChanged() callback cannot know the previous state the behavior was in. Knowing the old state is useful to perform one time operations.

For example, while the new state isSTATE_DRAGGING the onStateChanged() callback will be called several times and there is no way of knowing where it came from before without caching the value externally. But we might be interested in performing an action only when the state is changing from STATE_COLLAPSED to STATE_DRAGGING.

Solution

We add the param oldState to the callback.

Copy link
Member

@justasm justasm left a comment

Choose a reason for hiding this comment

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

Looks good, but it seems to be incorrect to say that onStateChanged may be called with STATE_DRAGGING multiple times, since setStateInternal explicitly checks whether mState == state.

Also, the same could be achieved by caching the latest value of newState externally, e.g.

class Callback : BottomSheetCallback {
  private var oldState: Int? = null
  override fun onStateChanged(v: View, newState: Int) {
    if (oldState == STATE_COLLAPSED && newState == STATE_DRAGGING) { /* .. */ }
    oldState = newState
  }
}

@riclage
Copy link
Contributor Author

riclage commented Aug 14, 2018

explicitly checks whether mState == state.

True. I missed that. Still, the problem applies. I updated the PR description.

Also, the same could be achieved by caching the latest value of newState externally, e.g.

Sure. That's how I'm currently doing. Just felt like it belonged in the lib instead.

@riclage
Copy link
Contributor Author

riclage commented Aug 28, 2018

@justasm Could you merge this?

@justasm justasm merged commit c7c3339 into trafi:master Aug 28, 2018
@riclage riclage deleted the ric/old-state branch August 28, 2018 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants