/**
* Copyright (c) 2015-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.react.views.scroll;
import javax.annotation.Nullable;
import java.util.Map;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.common.MapBuilder;
/**
* Helper for view managers to handle commands like 'scrollTo'.
* Shared by {@link ReactScrollViewManager} and {@link ReactHorizontalScrollViewManager}.
*/
public class ReactScrollViewCommandHelper {
public static final int COMMAND_SCROLL_TO = 1;
public static final int COMMAND_SCROLL_WITHOUT_ANIMATION_TO = 2;
public interface ScrollCommandHandler<T> {
void scrollTo(T scrollView, ScrollToCommandData data);
void scrollWithoutAnimationTo(T scrollView, ScrollToCommandData data);
}
public static class ScrollToCommandData {
public final int mDestX, mDestY;
ScrollToCommandData(int destX, int destY) {
mDestX = destX;
mDestY = destY;
}
}
public static Map<String,Integer> getCommandsMap() {
return MapBuilder.of(
"scrollTo",
COMMAND_SCROLL_TO,
"scrollWithoutAnimationTo",
COMMAND_SCROLL_WITHOUT_ANIMATION_TO);
}
public static <T> void receiveCommand(
ScrollCommandHandler<T> viewManager,
T scrollView,
int commandType,
@Nullable ReadableArray args) {
Assertions.assertNotNull(viewManager);
Assertions.assertNotNull(scrollView);
Assertions.assertNotNull(args);
switch (commandType) {
case COMMAND_SCROLL_TO: {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getInt(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getInt(1)));
viewManager.scrollTo(scrollView, new ScrollToCommandData(destX, destY));
return;
}
case COMMAND_SCROLL_WITHOUT_ANIMATION_TO: {
int destX = Math.round(PixelUtil.toPixelFromDIP(args.getInt(0)));
int destY = Math.round(PixelUtil.toPixelFromDIP(args.getInt(1)));
viewManager.scrollWithoutAnimationTo(scrollView, new ScrollToCommandData(destX, destY));
return;
}
default:
throw new IllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
commandType,
viewManager.getClass().getSimpleName()));
}
}
}