com.facebook.litho.StateValue Java Examples

The following examples show how to use com.facebook.litho.StateValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HorizontalScrollSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext c,
    StateValue<ScrollPosition> lastScrollPosition,
    StateValue<ComponentTree> childComponentTree,
    @Prop Component contentProps,
    @Prop(optional = true) int initialScrollPosition) {

  lastScrollPosition.set(new ScrollPosition(initialScrollPosition));
  childComponentTree.set(
      ComponentTree.create(
              new ComponentContext(
                  c.getAndroidContext(), c.getLogTag(), c.getLogger(), c.getTreePropsCopy()),
              contentProps)
          .incrementalMount(false)
          .build());
}
 
Example #2
Source File: ExpandingPickerComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateInitialState
static void createInitialState(
    ComponentContext c,
    StateValue<AtomicReference<Animator>> animatorRef,
    StateValue<DynamicValue<Float>> expandedAmount,
    StateValue<AtomicReference<Boolean>> isExpanded,
    StateValue<DynamicValue<Float>> contractButtonRotation,
    StateValue<DynamicValue<Float>> contractButtonScale,
    StateValue<DynamicValue<Float>> contractButtonAlpha,
    StateValue<DynamicValue<Float>> expandButtonScale,
    StateValue<DynamicValue<Float>> expandButtonAlpha,
    StateValue<DynamicValue<Float>> menuButtonAlpha) {
  animatorRef.set(new AtomicReference<Animator>(null));
  expandedAmount.set(new DynamicValue<>(START_EXPANDED ? 1f : 0f));
  isExpanded.set(new AtomicReference<>(START_EXPANDED));

  contractButtonRotation.set(new DynamicValue<>(START_EXPANDED ? 0f : -90f));
  contractButtonScale.set(new DynamicValue<>(START_EXPANDED ? 1f : 0.5f));
  contractButtonAlpha.set(new DynamicValue<>(START_EXPANDED ? 1f : 0f));

  expandButtonScale.set(new DynamicValue<>(START_EXPANDED ? 0.5f : 1f));
  expandButtonAlpha.set(new DynamicValue<>(START_EXPANDED ? 0f : 1f));

  menuButtonAlpha.set(new DynamicValue<>(START_EXPANDED ? 1f : 0f));
}
 
Example #3
Source File: ExpandableElementRootComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnUpdateState
static void onUpdateList(
    StateValue<List<Message>> messages,
    StateValue<Integer> counter,
    @Param boolean adding,
    @Param int initialMessagesSize) {
  final ArrayList<Message> updatedMessageList = new ArrayList<>(messages.get());

  int counterValue = counter.get();
  if (adding) {
    updatedMessageList.add(
        1, new Message(true, "Just Added #" + counterValue, true, "Recently", true));
    counter.set(counterValue + 1);
  } else if (initialMessagesSize < updatedMessageList.size()) {
    updatedMessageList.remove(1);
  }
  messages.set(updatedMessageList);
}
 
Example #4
Source File: FastScrollHandleComponentSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext c,
    StateValue<RecyclerCollectionEventsController> recyclerEventsController,
    StateValue<DynamicValue<Float>> handleTranslation,
    StateValue<ScrollController> scrollController) {
  final RecyclerCollectionEventsController recyclerEventsControllerValue =
      new RecyclerCollectionEventsController();
  final DynamicValue<Float> handleTranslationValue = new DynamicValue<>(0f);
  final ScrollController scrollControllerValue =
      new ScrollController(recyclerEventsControllerValue, handleTranslationValue);

  recyclerEventsController.set(recyclerEventsControllerValue);
  handleTranslation.set(handleTranslationValue);
  scrollController.set(scrollControllerValue);
}
 
Example #5
Source File: MountSpecLifecycleTesterSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext context,
    StateValue<Object> dummyState,
    @Prop LifecycleTracker lifecycleTracker) {
  dummyState.set(new Object());
  lifecycleTracker.addStep(LifecycleStep.ON_CREATE_INITIAL_STATE);
}
 
Example #6
Source File: FullDiffSection.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void createInitialState(SectionContext c) {
  StateValue<Object> state1 = new StateValue<>();
  FullDiffSectionSpec.onCreateInitialState(
      (SectionContext) c, (Integer) prop1, (StateValue<Object>) state1);
  mStateContainer.state1 = state1.get();
}
 
Example #7
Source File: LearningStateComponentSpecTest.java    From litho with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncrementClickCount() {
  final StateValue<Integer> count = new StateValue<>();
  count.set(0);
  LearningStateComponentSpec.incrementClickCount(count);

  LithoAssertions.assertThat(count).valueEqualTo(1);
}
 
Example #8
Source File: SizeSpecMountWrapperComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext c, StateValue<AtomicReference<ComponentTree>> componentTreeRef) {
  componentTreeRef.set(new AtomicReference<ComponentTree>());
  // This is the component tree to be added to the LithoView.
  getOrCreateComponentTree(c, componentTreeRef.get());
}
 
Example #9
Source File: TestAnimationsComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    final ComponentContext c, StateValue<Boolean> state, @Prop StateCaller stateCaller) {
  stateCaller.setStateUpdateListener(
      new StateCaller.StateUpdateListener() {
        @Override
        public void update() {
          TestAnimationsComponent.updateStateSync(c);
        }
      });
  state.set(false);
}
 
Example #10
Source File: SimpleStateUpdateEmulatorSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUpdateState
static void onIncrementCount(StateValue<Integer> count) {
  Integer counter = count.get();
  if (counter == null) {
    throw new RuntimeException("state value is null.");
  }
  count.set(counter + 1);
}
 
Example #11
Source File: HideableDataDiffSectionSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUpdateState
public static void onBlacklistUpdate(
    StateValue<HashSet> blacklistState,
    @Param Object modelObject,
    @Param EventHandler<GetUniqueIdentifierEvent> getUniqueIdentifierHandlerParam) {
  HashSet<Object> newSet = new HashSet<>(blacklistState.get());
  newSet.add(
      HideableDataDiffSection.dispatchGetUniqueIdentifierEvent(
          getUniqueIdentifierHandlerParam, modelObject));
  blacklistState.set(newSet);
}
 
Example #12
Source File: EditTextSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    final ComponentContext c,
    StateValue<AtomicReference<EditTextWithEventHandlers>> mountedView,
    StateValue<AtomicBoolean> configuredInitialText) {
  mountedView.set(new AtomicReference<EditTextWithEventHandlers>());
  configuredInitialText.set(new AtomicBoolean());
}
 
Example #13
Source File: TestLayout.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public void updateState(StateContainer _stateContainer) {
  TestLayoutStateContainer<S> stateContainer = (TestLayoutStateContainer<S>) _stateContainer;
  StateValue<Long> state1 = new StateValue<Long>();
  state1.set(stateContainer.state1);
  TestLayoutSpec.updateCurrentState(state1, mSomeParam);
  stateContainer.state1 = state1.get();
}
 
Example #14
Source File: LifecycleDelegateComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext c,
    StateValue<Random> random,
    StateValue<Integer> colorIndex,
    @Prop DelegateListener delegateListener,
    @Prop int id) {
  final Random rand = new Random();
  random.set(rand);
  colorIndex.set(rand.nextInt(COLORS.length));
  onDelegateMethodCalled(delegateListener, ON_CREATE_INITIAL_STATE, id);
}
 
Example #15
Source File: AnimationStateExampleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUpdateState
static void updatePosition(StateValue<String> actualPosition, @Param boolean alignStart) {
  if (alignStart) {
    actualPosition.set(STATE_START);
  } else {
    actualPosition.set(STATE_END);
  }
}
 
Example #16
Source File: BlocksSameTransitionKeyComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnUpdateState
static void updateState(
    StateValue<Boolean> state,
    StateValue<Boolean> running,
    StateValue<Set<String>> runningAnimations,
    @Param boolean toggleRunning) {
  if (toggleRunning && !running.get()) {
    running.set(true);
    runningAnimations.get().add(ANIM_ALPHA);
    runningAnimations.get().add(ANIM_X);
    state.set(!state.get());
  } else if (!toggleRunning) {
    running.set(false);
  }
}
 
Example #17
Source File: TestLayout.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void createInitialState(ComponentContext c) {
  StateValue<S> state2 = new StateValue<>();
  TestLayoutSpec.createInitialState((ComponentContext) c, (int) prop1, (StateValue<S>) state2);
  if (state2.get() != null) {
    mStateContainer.state2 = state2.get();
  }
}
 
Example #18
Source File: TestMount.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public void updateState(StateContainer _stateContainer) {
  TestMountStateContainer<S> stateContainer = (TestMountStateContainer<S>) _stateContainer;
  StateValue<Long> state1 = new StateValue<Long>();
  state1.set(stateContainer.state1);
  TestMountSpec.updateCurrentState(state1, mSomeParam);
  stateContainer.state1 = state1.get();
}
 
Example #19
Source File: PathExampleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void createInitialState(
    ComponentContext c,
    StateValue<Animator> animator,
    StateValue<DynamicValue<Float>> translationX,
    StateValue<DynamicValue<Float>> translationY) {
  // Set up the DynamicProps and an Animator to manipulate them. Making them State values for easy
  // sharing between the static functions.
  translationX.set(new DynamicValue<>(0f));
  translationY.set(new DynamicValue<>(0f));
  animator.set(createPathAnimator(c, translationX.get(), translationY.get()));
}
 
Example #20
Source File: BounceExampleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void createInitialState(
    ComponentContext c,
    StateValue<AtomicReference<Animator>> animatorRef,
    StateValue<DynamicValue<Float>> translationY) {
  animatorRef.set(new AtomicReference<Animator>(null));
  translationY.set(new DynamicValue<>(0f));
}
 
Example #21
Source File: CallbackExampleComponentSpec.java    From litho with Apache License 2.0 5 votes vote down vote up
@OnCreateInitialState
static void createInitialState(
    ComponentContext c,
    StateValue<AtomicReference<Animator>> animatorRef,
    StateValue<DynamicValue<Float>> scale) {
  animatorRef.set(new AtomicReference<Animator>(null));
  scale.set(new DynamicValue<>(1f));
}
 
Example #22
Source File: UpDownBlocksComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnUpdateState
static void updateState(StateValue<Boolean> top) {
  top.set(!top.get());
}
 
Example #23
Source File: ContinuousExampleComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateInitialState
static void createInitialState(
    ComponentContext c, StateValue<Animator> animator, StateValue<DynamicValue<Float>> rotation) {
  rotation.set(new DynamicValue<>(0f));
  animator.set(createRotationAnimator(rotation.get()));
}
 
Example #24
Source File: LithoAssertions.java    From litho with Apache License 2.0 4 votes vote down vote up
@CheckReturnValue
public static <T> StateValueAssert<T> assertThat(StateValue<T> stateValue) {
  return StateValueAssert.assertThat(stateValue);
}
 
Example #25
Source File: FavouriteButtonSpec.java    From litho-picasso with MIT License 4 votes vote down vote up
@OnUpdateState
static void toggleFavourited(StateValue<Boolean> favourited) {
  favourited.set(!favourited.get());
}
 
Example #26
Source File: SimpleStateUpdateEmulatorSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(ComponentContext c, StateValue<Integer> count) {
  count.set(1);
}
 
Example #27
Source File: CardActionsComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(ComponentContext c, StateValue<Boolean> isEnabled) {
  isEnabled.set(true);
}
 
Example #28
Source File: StateContainerGeneratorTest.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnUpdateState
static <T, E, D> void updateValues(
    StateValue<Function<E, D>> functions, @Param Function<T, D> foos) {}
 
Example #29
Source File: ShowMessageExampleComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnCreateInitialState
static void onCreateInitialState(
    ComponentContext c, StateValue<Boolean> radiusStart, StateValue<Boolean> isRunning) {
  radiusStart.set(true);
  isRunning.set(false);
}
 
Example #30
Source File: OneByOneLeftRightBlocksComponentSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnUpdateState
static void updateState(StateValue<Integer> state) {
  state.set((state.get() + 1) % 6);
}