-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add linear and circular progress bars (#1567)
Co-authored-by: Artsiom Grintsevich <greenfrvr@gmail.com>
- Loading branch information
Showing
15 changed files
with
1,015 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @license | ||
* Copyright Akveo. All Rights Reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import { | ||
Animated, | ||
Easing, | ||
Platform, | ||
} from 'react-native'; | ||
import { | ||
Animation, | ||
AnimationConfig, | ||
} from '../animation/animation'; | ||
|
||
const DEFAULT_CONFIG: CircularProgressBarAnimationConfig = { | ||
duration: 500, | ||
easing: Easing.linear, | ||
cycles: 1, | ||
useNativeDriver: Platform.OS !== 'web', | ||
}; | ||
|
||
interface AnimationStyle { | ||
rotateFirstHalf: Animated.AnimatedInterpolation<string>; | ||
rotateSecondHalf: Animated.AnimatedInterpolation<string>; | ||
} | ||
|
||
type TimingAnimationConfig = Omit<Animated.TimingAnimationConfig, 'toValue'>; | ||
|
||
export type CircularProgressBarAnimationConfig = AnimationConfig & TimingAnimationConfig; | ||
|
||
export class CircularProgressBarAnimation extends Animation<CircularProgressBarAnimationConfig, AnimationStyle> { | ||
|
||
private toValue: number; | ||
private readonly animationValue: Animated.Value; | ||
|
||
constructor(config?: Partial<CircularProgressBarAnimationConfig>) { | ||
super({ ...DEFAULT_CONFIG, ...config }); | ||
this.animationValue = new Animated.Value(0); | ||
} | ||
|
||
protected get animation(): Animated.CompositeAnimation { | ||
return Animated.timing( | ||
this.animationValue, | ||
{ | ||
...this.config, | ||
toValue: this.toValue, | ||
}, | ||
); | ||
} | ||
|
||
public startDeterminate(toValue: number, callback?: Animated.EndCallback): void { | ||
this.toValue = toValue; | ||
super.start(callback); | ||
} | ||
|
||
public stop(): void { | ||
super.stop(); | ||
} | ||
|
||
public toProps(): AnimationStyle { | ||
return { | ||
rotateFirstHalf: this.createRotateFirstHalfInterpolation(), | ||
rotateSecondHalf: this.createRotateSecondHalfInterpolation(), | ||
}; | ||
} | ||
|
||
private createRotateFirstHalfInterpolation = (): Animated.AnimatedInterpolation<string> => { | ||
return this.animationValue.interpolate({ | ||
inputRange: [0, 0.5], | ||
outputRange: ['180deg', '360deg'], | ||
extrapolate: 'clamp', | ||
}); | ||
}; | ||
|
||
private createRotateSecondHalfInterpolation = (): Animated.AnimatedInterpolation<string> => { | ||
return this.animationValue.interpolate({ | ||
inputRange: [0.5, 1], | ||
outputRange: ['180deg', '360deg'], | ||
extrapolate: 'clamp', | ||
}); | ||
}; | ||
|
||
} |
Oops, something went wrong.