/*
 * Copyright (c) 2017-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.litho.widget;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import com.facebook.litho.ThreadUtils;

/**
 * An controller that can be passed as {@link com.facebook.litho.annotations.Prop} to a
 * Recycler component to trigger events from outside the component hierarchy.
 */
public class RecyclerEventsController {

  private RecyclerViewWrapper mRecyclerViewWrapper;

  private final Runnable mClearRefreshRunnable = new Runnable() {
    @Override
    public void run() {
      if (mRecyclerViewWrapper != null && mRecyclerViewWrapper.isRefreshing()) {
        mRecyclerViewWrapper.setRefreshing(false);
      }
    }
  };

  /**
   * Send the Recycler a request to scroll the content to the first item in the binder.
   * @param animated if animated is set to true the scroll will happen with an animation.
   */
  public void requestScrollToTop(boolean animated) {
    requestScrollToPosition(0, animated);
  }

  /**
   * Send the Recycler a request to scroll the content to a specific item in the binder.
   *
   * @param animated if animated is set to true the scroll will happen with an animation.
   */
  public void requestScrollToPosition(final int position, final boolean animated) {
    if (mRecyclerViewWrapper == null) {
      return;
    }

    mRecyclerViewWrapper.post(
        new Runnable() {
          @Override
          public void run() {
            if (mRecyclerViewWrapper == null) {
              return;
            }

            if (animated) {
              mRecyclerViewWrapper.getRecyclerView().smoothScrollToPosition(position);
              return;
            }

            mRecyclerViewWrapper.getRecyclerView().scrollToPosition(position);
          }
        });
  }

  public void clearRefreshing() {
    if (mRecyclerViewWrapper == null || !mRecyclerViewWrapper.isRefreshing()) {
      return;
    }

    if (ThreadUtils.isMainThread()) {
      mRecyclerViewWrapper.setRefreshing(false);
      return;
    }

    mRecyclerViewWrapper.removeCallbacks(mClearRefreshRunnable);
    mRecyclerViewWrapper.post(mClearRefreshRunnable);
  }

  public void showRefreshing() {
    if (mRecyclerViewWrapper == null || mRecyclerViewWrapper.isRefreshing()) {
      return;
    }
    ThreadUtils.assertMainThread();
    mRecyclerViewWrapper.setRefreshing(true);
  }

  void setRecyclerViewWrapper(RecyclerViewWrapper recyclerViewWrapper) {
    mRecyclerViewWrapper = recyclerViewWrapper;
  }

  protected @Nullable RecyclerView getRecyclerView() {
    return mRecyclerViewWrapper == null ? null : mRecyclerViewWrapper.getRecyclerView();
  }
}

@@ -1628,207 +1628,8 @@
  %7D%0A%0A
-    mRecyclerViewWrapper.post(%0A        new Runnable() %7B%0A          @Override%0A          public void run() %7B%0A            if (mRecyclerViewWrapper == null) %7B%0A              return;%0A            %7D%0A%0A        
     
@@ -1644,24 +1644,16 @@
 ated) %7B%0A
-        
       mR
@@ -1733,47 +1733,23 @@
     
-        return;%0A            %7D%0A%0A        
+return;%0A    %7D%0A%0A
     
@@ -1819,32 +1819,8 @@
 n);%0A
-          %7D%0A        %7D);%0A
   %7D%0A

Revert changes made to RecyclerEventsController