1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.android.horologist.compose.rotaryinput
18 
19 import androidx.compose.ui.input.pointer.util.VelocityTracker1D
20 
21 /**
22  * A wrapper around VelocityTracker1D to provide support for rotary input.
23  */
24 public class RotaryVelocityTracker {
25     private var velocityTracker: VelocityTracker1D = VelocityTracker1D(true)
26 
27     /**
28      * Retrieve the last computed velocity.
29      */
30     public val velocity: Float
31         get() = velocityTracker.calculateVelocity()
32 
33     /**
34      * Start tracking motion.
35      */
startnull36     public fun start(currentTime: Long) {
37         velocityTracker.resetTracking()
38         velocityTracker.addDataPoint(currentTime, 0f)
39     }
40 
41     /**
42      * Continue tracking motion as the input rotates.
43      */
movenull44     public fun move(currentTime: Long, delta: Float) {
45         velocityTracker.addDataPoint(currentTime, delta)
46     }
47 
48     /**
49      * Stop tracking motion.
50      */
endnull51     public fun end() {
52         velocityTracker.resetTracking()
53     }
54 }
55