Skip to content

Commit

Permalink
fix: Updated enum names as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
oriventi committed Dec 28, 2024
1 parent d2bad09 commit 1c81535
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
13 changes: 7 additions & 6 deletions lib/src/animated_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class _AnimatedTextKitState extends State<AnimatedTextKit>
if (_animatedTextController.state == AnimatedTextState.playing &&
!_controller.isAnimating) {
_controller.forward();
} else if (_animatedTextController.state == AnimatedTextState.userPaused) {
} else if (_animatedTextController.state ==
AnimatedTextState.pausedByUser) {
_controller.stop();
} else if (_animatedTextController.state == AnimatedTextState.reset) {
_controller.reset();
Expand All @@ -196,7 +197,7 @@ class _AnimatedTextKitState extends State<AnimatedTextKit>
behavior: HitTestBehavior.opaque,
onTap: _onTap,
child: _animatedTextController.state ==
AnimatedTextState.pausingBetweenAnimations ||
AnimatedTextState.pausedBetweenAnimations ||
!_controller.isAnimating
? completeText
: AnimatedBuilder(
Expand Down Expand Up @@ -252,11 +253,11 @@ class _AnimatedTextKitState extends State<AnimatedTextKit>
_controller.addStatusListener(_animationEndCallback);

if (_animatedTextController.state ==
AnimatedTextState.pausingBetweenAnimationsWithUserPauseRequested) {
AnimatedTextState.pausedBetweenAnimationsByUser) {
// This post frame callback is needed to ensure that the state is set and the widget is built
// before we pause the animation. otherwise nothing will be shown during the animation cycle
WidgetsBinding.instance.addPostFrameCallback((_) {
_animatedTextController.state = AnimatedTextState.userPaused;
_animatedTextController.state = AnimatedTextState.pausedByUser;
});
}
_animatedTextController.state = AnimatedTextState.playing;
Expand All @@ -266,7 +267,7 @@ class _AnimatedTextKitState extends State<AnimatedTextKit>
void _setPauseBetweenAnimations() {
final isLast = _isLast;

_animatedTextController.state = AnimatedTextState.pausingBetweenAnimations;
_animatedTextController.state = AnimatedTextState.pausedBetweenAnimations;

if (mounted) setState(() {});

Expand All @@ -285,7 +286,7 @@ class _AnimatedTextKitState extends State<AnimatedTextKit>
void _onTap() {
if (widget.displayFullTextOnTap) {
if (_animatedTextController.state ==
AnimatedTextState.pausingBetweenAnimations) {
AnimatedTextState.pausedBetweenAnimations) {
if (widget.stopPauseOnTap) {
_timer?.cancel();
_nextAnimation();
Expand Down
17 changes: 8 additions & 9 deletions lib/src/animated_text_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import 'package:flutter/material.dart';
/// * [reset]: The animation should reset to its initial state.
enum AnimatedTextState {
playing,
userPaused,
pausingBetweenAnimations,
pausingBetweenAnimationsWithUserPauseRequested,
pausedByUser,
pausedBetweenAnimations,
pausedBetweenAnimationsByUser,
stopped,
reset,
}
Expand Down Expand Up @@ -53,8 +53,8 @@ class AnimatedTextController {
/// Call this to resume the animation if it was previously paused.
void play() {
if (stateNotifier.value ==
AnimatedTextState.pausingBetweenAnimationsWithUserPauseRequested) {
stateNotifier.value = AnimatedTextState.pausingBetweenAnimations;
AnimatedTextState.pausedBetweenAnimationsByUser) {
stateNotifier.value = AnimatedTextState.pausedBetweenAnimations;
} else {
stateNotifier.value = AnimatedTextState.playing;
}
Expand All @@ -67,11 +67,10 @@ class AnimatedTextController {
///
/// Call this to pause the animation due to user interaction.
void pause() {
if (stateNotifier.value == AnimatedTextState.pausingBetweenAnimations) {
stateNotifier.value =
AnimatedTextState.pausingBetweenAnimationsWithUserPauseRequested;
if (stateNotifier.value == AnimatedTextState.pausedBetweenAnimations) {
stateNotifier.value = AnimatedTextState.pausedBetweenAnimationsByUser;
} else {
stateNotifier.value = AnimatedTextState.userPaused;
stateNotifier.value = AnimatedTextState.pausedByUser;
}
}

Expand Down
16 changes: 7 additions & 9 deletions test/controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {

test('Calling pause when playing should set state to userPaused', () {
controller.pause();
expect(controller.state, AnimatedTextState.userPaused);
expect(controller.state, AnimatedTextState.pausedByUser);
});

test('Calling play after paused should set state to playing', () {
Expand All @@ -31,19 +31,17 @@ void main() {
'Pausing during pausingBetweenAnimations should set state to pausingBetweenAnimationsWithUserPauseRequested',
() {
// Directly set state to pausingBetweenAnimations to simulate this scenario.
controller.state = AnimatedTextState.pausingBetweenAnimations;
controller.state = AnimatedTextState.pausedBetweenAnimations;
controller.pause();
expect(controller.state,
AnimatedTextState.pausingBetweenAnimationsWithUserPauseRequested);
expect(controller.state, AnimatedTextState.pausedBetweenAnimationsByUser);
});

test(
'Calling play when in pausingBetweenAnimationsWithUserPauseRequested should revert to pausingBetweenAnimations',
() {
controller.state =
AnimatedTextState.pausingBetweenAnimationsWithUserPauseRequested;
controller.state = AnimatedTextState.pausedBetweenAnimationsByUser;
controller.play();
expect(controller.state, AnimatedTextState.pausingBetweenAnimations);
expect(controller.state, AnimatedTextState.pausedBetweenAnimations);
});

test('Resetting should set state to reset', () {
Expand All @@ -52,7 +50,7 @@ void main() {
});

test('Changing state directly via setter works', () {
controller.state = AnimatedTextState.userPaused;
expect(controller.state, AnimatedTextState.userPaused);
controller.state = AnimatedTextState.pausedByUser;
expect(controller.state, AnimatedTextState.pausedByUser);
});
}

0 comments on commit 1c81535

Please sign in to comment.