Browse Source

Refactor NavigationAnimator

Guy Carmeli 6 years ago
parent
commit
c34a3483d8

+ 2
- 3
lib/android/app/src/main/java/com/reactnativenavigation/anim/BaseAnimator.java View File

5
 import android.animation.ObjectAnimator;
5
 import android.animation.ObjectAnimator;
6
 import android.content.Context;
6
 import android.content.Context;
7
 import android.support.annotation.NonNull;
7
 import android.support.annotation.NonNull;
8
-import android.support.annotation.RestrictTo;
9
 import android.view.View;
8
 import android.view.View;
10
 import android.view.animation.AccelerateInterpolator;
9
 import android.view.animation.AccelerateInterpolator;
11
 import android.view.animation.DecelerateInterpolator;
10
 import android.view.animation.DecelerateInterpolator;
15
 
14
 
16
 class BaseAnimator {
15
 class BaseAnimator {
17
 
16
 
18
-    protected AnimationsOptions options = new AnimationsOptions();
19
-
20
     private static final int DURATION = 300;
17
     private static final int DURATION = 300;
21
     private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
18
     private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
22
     private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
19
     private static final AccelerateInterpolator ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
23
 
20
 
21
+    protected AnimationsOptions options = new AnimationsOptions();
22
+
24
     private float translationY;
23
     private float translationY;
25
 
24
 
26
     BaseAnimator(Context context) {
25
     BaseAnimator(Context context) {

+ 8
- 23
lib/android/app/src/main/java/com/reactnativenavigation/anim/NavigationAnimator.java View File

4
 import android.animation.AnimatorListenerAdapter;
4
 import android.animation.AnimatorListenerAdapter;
5
 import android.animation.AnimatorSet;
5
 import android.animation.AnimatorSet;
6
 import android.content.Context;
6
 import android.content.Context;
7
-import android.support.annotation.Nullable;
8
 import android.view.View;
7
 import android.view.View;
9
 
8
 
10
 import com.reactnativenavigation.parse.AnimationsOptions;
9
 import com.reactnativenavigation.parse.AnimationsOptions;
21
         this.options = options;
20
         this.options = options;
22
     }
21
     }
23
 
22
 
24
-    public void animatePush(final View view, @Nullable final AnimationListener animationListener) {
23
+    public void push(final View view, AnimationListener animationListener) {
25
         view.setVisibility(View.INVISIBLE);
24
         view.setVisibility(View.INVISIBLE);
26
-        AnimatorSet set;
27
-        if (options.push.content.hasValue()) {
28
-            set = options.push.content.getAnimation(view);
29
-        } else {
30
-            set = getDefaultPushAnimation(view);
31
-        }
25
+        AnimatorSet set = options.push.content.getAnimation(view, getDefaultPushAnimation(view));
32
         set.addListener(new AnimatorListenerAdapter() {
26
         set.addListener(new AnimatorListenerAdapter() {
33
             @Override
27
             @Override
34
             public void onAnimationStart(Animator animation) {
28
             public void onAnimationStart(Animator animation) {
37
 
31
 
38
             @Override
32
             @Override
39
             public void onAnimationEnd(Animator animation) {
33
             public void onAnimationEnd(Animator animation) {
40
-                if (animationListener != null) {
41
-                    animationListener.onAnimationEnd();
42
-                }
34
+                animationListener.onAnimationEnd();
43
             }
35
             }
44
         });
36
         });
45
         set.start();
37
         set.start();
46
     }
38
     }
47
 
39
 
48
-    public void animatePop(View view, @Nullable final AnimationListener animationListener) {
49
-        AnimatorSet set;
50
-        if (options.pop.content.hasValue()) {
51
-            set = options.pop.content.getAnimation(view);
52
-        } else {
53
-            set = getDefaultPopAnimation(view);
54
-        }
40
+    public void pop(View view, AnimationListener animationListener) {
41
+        AnimatorSet set = options.pop.content.getAnimation(view, getDefaultPopAnimation(view));
55
         set.addListener(new AnimatorListenerAdapter() {
42
         set.addListener(new AnimatorListenerAdapter() {
56
             @Override
43
             @Override
57
             public void onAnimationEnd(Animator animation) {
44
             public void onAnimationEnd(Animator animation) {
58
-                if (animationListener != null) {
59
-                    animationListener.onAnimationEnd();
60
-                }
45
+                animationListener.onAnimationEnd();
61
             }
46
             }
62
         });
47
         });
63
         set.start();
48
         set.start();
64
     }
49
     }
65
 
50
 
66
-    public void animateStartApp(View view, @Nullable final AnimationListener animationListener) {
51
+    public void animateStartApp(View view, AnimationListener animationListener) {
67
         view.setVisibility(View.INVISIBLE);
52
         view.setVisibility(View.INVISIBLE);
68
-        AnimatorSet set = options.startApp.getAnimation(view);
53
+        AnimatorSet set = options.startApp.getAnimation(view, null);
69
         set.addListener(new AnimatorListenerAdapter() {
54
         set.addListener(new AnimatorListenerAdapter() {
70
             @Override
55
             @Override
71
             public void onAnimationStart(Animator animation) {
56
             public void onAnimationStart(Animator animation) {

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/parse/AnimationOptions.java View File

3
 
3
 
4
 import android.animation.Animator;
4
 import android.animation.Animator;
5
 import android.animation.AnimatorSet;
5
 import android.animation.AnimatorSet;
6
-import android.util.Log;
7
 import android.util.Property;
6
 import android.util.Property;
8
 import android.view.View;
7
 import android.view.View;
9
 
8
 
62
         return hasValue;
61
         return hasValue;
63
     }
62
     }
64
 
63
 
65
-    public AnimatorSet getAnimation(View view) {
64
+    public AnimatorSet getAnimation(View view, AnimatorSet defaultAnimation) {
65
+        if (!hasValue()) return defaultAnimation;
66
         AnimatorSet animationSet = new AnimatorSet();
66
         AnimatorSet animationSet = new AnimatorSet();
67
         List<Animator> animators = new ArrayList<>();
67
         List<Animator> animators = new ArrayList<>();
68
         for (ValueAnimationOptions options : valueOptions) {
68
         for (ValueAnimationOptions options : valueOptions) {

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/StackController.java View File

97
 
97
 
98
         if (toRemove != null) {
98
         if (toRemove != null) {
99
             if (child.options.animated.isTrueOrUndefined()) {
99
             if (child.options.animated.isTrueOrUndefined()) {
100
-                animator.animatePush(child.getView(), () -> {
100
+                animator.push(child.getView(), () -> {
101
                     getView().removeView(toRemove.getView());
101
                     getView().removeView(toRemove.getView());
102
                     listener.onSuccess(child.getId());
102
                     listener.onSuccess(child.getId());
103
                 });
103
                 });
143
         );
143
         );
144
 
144
 
145
         if (disappearing.options.animated.isTrueOrUndefined()) {
145
         if (disappearing.options.animated.isTrueOrUndefined()) {
146
-            animator.animatePop(
146
+            animator.pop(
147
                     disappearing.getView(),
147
                     disappearing.getView(),
148
                     () -> finishPopping(disappearing, listener)
148
                     () -> finishPopping(disappearing, listener)
149
             );
149
             );

+ 2
- 2
lib/android/app/src/test/java/com/reactnativenavigation/mocks/TestNavigationAnimator.java View File

13
     }
13
     }
14
 
14
 
15
     @Override
15
     @Override
16
-    public void animatePush(final View enteringView, AnimationListener animationListener) {
16
+    public void push(final View enteringView, AnimationListener animationListener) {
17
         if (animationListener != null) animationListener.onAnimationEnd();
17
         if (animationListener != null) animationListener.onAnimationEnd();
18
     }
18
     }
19
 
19
 
20
     @Override
20
     @Override
21
-    public void animatePop(final View enteringView, AnimationListener animationListener) {
21
+    public void pop(final View enteringView, AnimationListener animationListener) {
22
         if (animationListener != null) animationListener.onAnimationEnd();
22
         if (animationListener != null) animationListener.onAnimationEnd();
23
     }
23
     }
24
 }
24
 }

+ 4
- 4
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/StackControllerTest.java View File

328
                 uut.popTo(child2, new CommandListenerAdapter() {
328
                 uut.popTo(child2, new CommandListenerAdapter() {
329
                     @Override
329
                     @Override
330
                     public void onSuccess(String childId) {
330
                     public void onSuccess(String childId) {
331
-                        verify(animator, times(0)).animatePop(eq(child1.getView()), any());
332
-                        verify(animator, times(0)).animatePop(eq(child2.getView()), any());
333
-                        verify(animator, times(1)).animatePop(eq(child4.getView()), any());
331
+                        verify(animator, times(0)).pop(eq(child1.getView()), any());
332
+                        verify(animator, times(0)).pop(eq(child2.getView()), any());
333
+                        verify(animator, times(1)).pop(eq(child4.getView()), any());
334
                     }
334
                     }
335
                 });
335
                 });
336
             }
336
             }
376
                 uut.popToRoot(new CommandListenerAdapter() {
376
                 uut.popToRoot(new CommandListenerAdapter() {
377
                     @Override
377
                     @Override
378
                     public void onSuccess(String childId) {
378
                     public void onSuccess(String childId) {
379
-                        verify(animator, times(1)).animatePop(eq(child3.getView()), any());
379
+                        verify(animator, times(1)).pop(eq(child3.getView()), any());
380
                     }
380
                     }
381
                 });
381
                 });
382
             }
382
             }