Skip to content

Commit

Permalink
Convert DiffClampAnimatedNode to Kotlin (#45716)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #45716

# Changelog:
[Internal] -

As in the title.

Reviewed By: steelrooter

Differential Revision: D60283709

fbshipit-source-id: 110e9ee9deecd0c39575b94b0604ad3fa9a9b96e
  • Loading branch information
rshest authored and facebook-github-bot committed Jul 27, 2024
1 parent c07ca78 commit 58a4e2e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 66 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.animated

import com.facebook.react.bridge.JSApplicationCausedNativeException
import com.facebook.react.bridge.ReadableMap
import kotlin.math.max
import kotlin.math.min

internal class DiffClampAnimatedNode(
config: ReadableMap,
private val nativeAnimatedNodesManager: NativeAnimatedNodesManager
) : ValueAnimatedNode() {
private val inputNodeTag: Int
private val minValue: Double
private val maxValue: Double
private var lastValue: Double = 0.0

init {
inputNodeTag = config.getInt("input")
minValue = config.getDouble("min")
maxValue = config.getDouble("max")
mValue = lastValue
}

override fun update() {
val value = inputNodeValue
val diff = value - lastValue
lastValue = value
mValue = min(max(mValue + diff, minValue), maxValue)
}

private val inputNodeValue: Double
get() {
val animatedNode = nativeAnimatedNodesManager.getNodeById(inputNodeTag)
if (animatedNode == null || animatedNode !is ValueAnimatedNode) {
throw JSApplicationCausedNativeException(
"Illegal node ID set as an input for Animated.DiffClamp node")
}
return animatedNode.value
}

override fun prettyPrint(): String =
"DiffClampAnimatedNode[$mTag]: InputNodeTag: $inputNodeTag min: $minValue " +
"max: $maxValue lastValue: $lastValue super: ${super.prettyPrint()}"
}

0 comments on commit 58a4e2e

Please sign in to comment.