@@ -111,7 +111,9 @@ function NowPlaying() {
111
111
const { index, track } = useCurrentTrack ( ) ;
112
112
const { buffered, position } = useProgress ( ) ;
113
113
const defaultStyles = useDefaultStyles ( ) ;
114
- const previousIndex = usePrevious ( index ) ;
114
+ const previousBuffered = usePrevious ( buffered ) ;
115
+ const previousPosition = usePrevious ( position ) ;
116
+
115
117
const navigation = useNavigation < MusicNavigationProp > ( ) ;
116
118
117
119
const bufferAnimation = useRef ( new Animated . Value ( 0 ) ) ;
@@ -122,21 +124,40 @@ function NowPlaying() {
122
124
} , [ navigation ] ) ;
123
125
124
126
useEffect ( ( ) => {
125
- const hasChangedTrack = previousIndex !== index || track ?. duration === 0 ;
126
127
const duration = ( track ?. duration || 0 ) / 10_000_000 ;
127
128
128
- Animated . timing ( bufferAnimation . current , {
129
- toValue : calculateProgressTranslation ( buffered , duration , NOW_PLAYING_POPOVER_WIDTH ) ,
130
- duration : hasChangedTrack ? 0 : 500 ,
131
- useNativeDriver : true ,
132
- easing : Easing . ease ,
133
- } ) . start ( ) ;
134
- Animated . timing ( progressAnimation . current , {
135
- toValue : calculateProgressTranslation ( position , duration , NOW_PLAYING_POPOVER_WIDTH ) ,
136
- duration : hasChangedTrack ? 0 : 500 ,
137
- useNativeDriver : true ,
138
- } ) . start ( ) ;
139
- } , [ buffered , track ?. duration , position , index , previousIndex ] ) ;
129
+ // GUARD: Don't update when the duration is 0, cause it will put the
130
+ // bars in a weird space.
131
+ if ( duration === 0 ) {
132
+ return ;
133
+ }
134
+
135
+ // First calculate the new value for the buffer animation. Then, check
136
+ // whether the buffered state is smaller than the previous one, in which
137
+ // case we'll just set the value without animation
138
+ const bufferValue = calculateProgressTranslation ( buffered , duration , NOW_PLAYING_POPOVER_WIDTH ) ;
139
+ if ( buffered < ( previousBuffered || 0 ) ) {
140
+ bufferAnimation . current . setValue ( bufferValue ) ;
141
+ } else {
142
+ Animated . timing ( bufferAnimation . current , {
143
+ toValue : bufferValue ,
144
+ duration : 500 ,
145
+ useNativeDriver : true ,
146
+ } ) . start ( ) ;
147
+ }
148
+
149
+ // Then, do the same for the progress animation
150
+ const progressValule = calculateProgressTranslation ( position , duration , NOW_PLAYING_POPOVER_WIDTH ) ;
151
+ if ( position < ( previousPosition || 0 ) ) {
152
+ progressAnimation . current . setValue ( progressValule ) ;
153
+ } else {
154
+ Animated . timing ( progressAnimation . current , {
155
+ toValue : progressValule ,
156
+ duration : 500 ,
157
+ useNativeDriver : true ,
158
+ } ) . start ( ) ;
159
+ }
160
+ } , [ buffered , track ?. duration , position , index , previousBuffered , previousPosition ] ) ;
140
161
141
162
if ( ! track ) {
142
163
return null ;
0 commit comments